Chartjs-Chart.js: Fixed horizontal location of vertical axis

0👍

Maybe there’s a better way, but this worked for me.

In the chart options I set: options > layout > padding > left to a default pixel amount that works for my situation when the scale label has only two digit number labels.

Then I set an ID for the yAxes so that I can access it later: options > scales > yAxes > id.

Then in my JavaScript, after the code that defines the chart, I added the following code which adjusts the left padding if the scale label has three digits (in my case I set it to 1):

var axis = myLineChart.scales.<id of my y axis>;
var max_tick_label = axis.ticks[0];
if (max_tick_label >= 100) {
    myLineChart.options.layout.padding['left'] = 1;
    myLineChart.update();
}

Leave a comment