Chartjs-Chart.js: How to calculate the labelOffset based on the distance between ticks?

1👍

This question was helpfully answered on Github by jcopperfield, here is the solution;

// centre x-scale label
var xScale = myBubbleChart.scales['x-axis-0'];
var xLabelOffset = (xScale.getPixelForTick(1) - xScale.getPixelForTick(0)) / 2;
myBubbleChart.options.scales.xAxes[0].ticks.minor.labelOffset = xLabelOffset;

// centre y-scale label
var yScale = myBubbleChart.scales['y-axis-0'];
var yLabelOffset = (yScale.getPixelForTick(0) - yScale.getPixelForTick(1)) / 2;
myBubbleChart.options.scales.yAxes[0].ticks.minor.labelOffset = yLabelOffset;

// update chart
myBubbleChart.update();

Leave a comment