[Chartjs]-How to position yAxes labels in chartJS

4๐Ÿ‘

โœ…

You can do this by adding an animation to the options config:

options: {
    animation: {
        duration: 1,
        onComplete: function() {
            var controller = this.chart.controller;
            var chart = controller.chart;
            var yAxis = controller.scales['y-axis-0'];

            yAxis.ticks.forEach(function(value, index) {
                var xOffset = chart.width - 40;
                var yOffset = ((chart.height - 60) / 2 * index) + 15;
                ctx.fillText(value + 'M', xOffset, yOffset);
            });   
        }
    }
}

JSFiddle Demo: https://jsfiddle.net/m5tnkr4n/1/

You may need to adjust the hardcoded numbers in the xOffset and yOffset. These numbers will depend on the height and width of your chart, as well as what features you are displaying the in the canvas area. For example, if you remove the xAxes tick labels, you many need to decrease the -60 since that will make the chart slightly shorter. / 2 works since you set the maxTicksLimit to 3.

0๐Ÿ‘

You can adjust the tick position with mirror, labelOffset and padding

scales: {
   yAxesA: {
      id: 'yAxisA',
      type: 'linear',
      position: 'left',      
      ticks: {
         display: true,
         mirror: true,
         labelOffset: -5,
         padding: 5
      }
   }
}

Leave a comment