13👍
found the Solution myself.
to change any options of the chart:
myBar.config.options
in my case
myBar.config.options.scales.yAxes[0].ticks.max = newValue
after this you have to call
window.myBar.update();
- [Chartjs]-React-chart-js-2 in combination with TypeScript for LineCharts: Uncaught Error: "point" is not a registered element
- [Chartjs]-Set a limit / max number for legends
5👍
This is my solution for Line Chart.
You can use ‘beforeFit‘ callback function that you can find here in the docs http://www.chartjs.org/docs/latest/axes/
- In scale.chart.config.data.datasets you have the chart dataset
- You must set the scale.options.ticks.max
Example:
scales: {
yAxes: [{
beforeFit: function (scale) {
// See what you can set in scale parameter
console.log(scale)
// Find max value in your dataset
let maxValue = 0
if (scale.chart.config && scale.chart.config.data && scale.chart.config.data.datasets) {
scale.chart.config.data.datasets.forEach(dataset => {
if (dataset && dataset.data) {
dataset.data.forEach(value => {
if (value > maxValue) {
maxValue = value
}
})
}
})
}
// After, set max option !!!
scale.options.ticks.max = maxValue
}
}
I hope I’ve been helpful.
- [Chartjs]-Chart.js v2: How to make tooltips always appear on pie chart?
- [Chartjs]-How to add an on click event to my Line chart using Chart.js
1👍
The link https://www.chartjs.org/docs/latest/developers/updates.html has pretty up to date information.
the keey is to recognise what the chart object is. 🙂
I struggled for half a day reading various pages until i found the right way
- use the baseChartDirective to access the chart directly by doing this -> this.chart
- To update scales do this:
var barChartOptions = { legend: { display: true }, scales: { yAxes: [{ id: 'yaxis', type: 'linear', position: 'left', ticks: { min: 0, max: maxScale } }]} }; this.chart.chart.options = barChartOptions;
- then update the chart:
this.chart.chart.update();
you can pretty much update everything. Just need to realise what the chart object means in this page: https://www.chartjs.org/docs/latest/developers/updates.html
🙂
- [Chartjs]-Charts.js Formatting Y Axis with both Currency and Thousands Separator
- [Chartjs]-Charts JS: How to set units?
Source:stackexchange.com