Skip to content Skip to sidebar Skip to footer

How To Use Same Data / Labels On Two Y Axes In Chart.js

I have one set of data I'm using in angular-chart.js, but I want both y-axis to use the same data set (for the labels, I don't wish to chart the same data twice). If I just duplica

Solution 1:

You can use the following chart plugin to show/use the same labels/ticks on two different y-axis :

Chart.plugins.register({
   beforeInit: function(chart) {
      chart.options.scales.yAxes[1].ticks.suggestedMin = Math.min.apply(this, chart.data.datasets[0].data);
      chart.options.scales.yAxes[1].ticks.suggestedMax = Math.max.apply(this, chart.data.datasets[0].data);
   }
});

ᴡᴏʀᴋɪɴɢ ᴇxᴀᴍᴘʟᴇ

Chart.plugins.register({
   beforeInit: function(chart) {
      chart.options.scales.yAxes[1].ticks.suggestedMin = Math.min.apply(this, chart.data.datasets[0].data);
      chart.options.scales.yAxes[1].ticks.suggestedMax = Math.max.apply(this, chart.data.datasets[0].data);
   }
});

var chart = newChart(ctx, {
   type: 'line',
   data: {
      labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
      datasets: [{
         label: 'LINE',
         data: [3, 1, 4, 2, 5],
         backgroundColor: 'rgba(0, 119, 290, 0.2)',
         borderColor: 'rgba(0, 119, 290, 0.6)',
         fill: false
      }]
   },
   options: {
      scales: {
         yAxes: [{
            id: 'y-axis-0',
            position: 'left',
            ticks: {
               stepSize: 1
            }
         }, {
            id: 'y-axis-1',
            position: 'right',
            ticks: {
               stepSize: 1
            }
         }]
      }
   }
});
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script><canvasid="ctx"></canvas>

Post a Comment for "How To Use Same Data / Labels On Two Y Axes In Chart.js"