I have a bar chart on a page that has data displayed that is dynamically updated. The problem I have is that I need to change the type of the y-axis based on the data that is displayed. I need it to be a linear scale when the data has a range of less than 100 (e.g. 0-99) and logarithmic if it is greater (e.g. 0-1000).
I can calculate whether I need a logarithmic scale or not fine, but when it comes to changing the scale type I am at a loss. I have tried changing the options object of the chart and calling update():
myChart.options/scales.yAxes.map(function (axis) { axis.type = 'logarithmic' });
myChart.update();
and also tried changing the scale itself directly:
Object.keys(mychart.scales).forEach((function (axisName) {
var axis = myChart.scales[axisName];
if (axis && axis.id.startsWith('y')) {
axis.options.type = 'logarithmic'
}
}));
myChart.update();
then I tried using the beforeBuildTicks callback, thinking that changing the type while updating the data and redrawing the graph maybe too late or just at the wrong time:
beforeBuildTicks: function (scale) {
if (scale.chart) {
console.log('logarithmic');
scale.options.type = 'logarithmic';
}
}
all of these approaches change the chart object, but the display is not affected.
Is there a way to dynamically change the scale type of a chartjs bar chart without destroying and recreating the entire chart?


