[Chartjs]-Chart.js Show Dollar Amount On Left Y And Percent Amount On Right Y

3👍

ꜰɪʀꜱᴛ

set yAxisID: 'y-axis-1' for your first dataset, like so :

...
datasets: [{
   type: 'line',
   fill: false,
   label: 'Line Data',
   backgroundColor: 'rgba(255,0,0,1)',
   borderColor: 'rgba(255,0,0,1)',
   data: [3, 4, 10, 5, 8, 7],
   yAxisID: 'y-axis-1' //<- set this
}, {
   ...
}]
...

this is to map the dataset to right y-axis

ꜱᴇᴄᴏɴᴅ

add another y-axis (for percent amount) to the yAxes array, as such :

...
scales: {
   yAxes: [{
      ...
   }, {
      id: 'y-axis-1',
      position: 'right',
      ticks: {
         beginAtZero: true,
         callback: function(value, index, values) {
            return value + '%';
         }
      }
   }]
}
...

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

var labelsarr = ["George 11", "George 12", "Fred 11", "Fred 12", "Sage 11", "Sage 12"];
var ctx = document.getElementById('canvas').getContext('2d');
var chart = new Chart(ctx, {
   type: 'bar',
   data: {
      labels: labelsarr,
      datasets: [{
         type: 'line',
         fill: false,
         label: 'Line Data',
         backgroundColor: 'rgba(255,0,0,1)',
         borderColor: 'rgba(255,0,0,1)',
         data: [3, 4, 10, 5, 8, 7],
         yAxisID: 'y-axis-1'

      }, {
         label: 'Bar Data',
         backgroundColor: 'rgba(0, 129, 214, 0.8)',
         data: [12, 19, 3, 5, 2, 3]
      }]
   },
   options: {
      tooltips: {
         callbacks: {
            label: function(t, d) {
               if (t.datasetIndex === 0) {
                  var xLabel = d.datasets[t.datasetIndex].label;
                  var yLabel = t.yLabel + '%';
                  return xLabel + ': ' + yLabel;
               } else if (t.datasetIndex === 1) {
                  var xLabel = d.datasets[t.datasetIndex].label;
                  var yLabel = t.yLabel >= 1000 ? '$' + t.yLabel.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") : '$' + t.yLabel;
                  return xLabel + ': ' + yLabel;
               }
            }
         }
      },
      legend: {
         display: false,
         position: 'top',
      },
      scales: {
         yAxes: [{
            ticks: {
               beginAtZero: true,
               callback: function(value, index, values) {
                  if (parseInt(value) >= 1000) {
                     return '$' + value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
                  } else {
                     return '$' + value;
                  }
               }
            }
         }, {
            id: 'y-axis-1',
            position: 'right',
            ticks: {
               beginAtZero: true,
               callback: function(value, index, values) {
                  return value + '%';
               }
            }
         }]
      }
   },
   plugins: [{
      beforeDraw: function(chart) {
         var labels = chart.data.labels;
         labels.forEach(function(e, i) {
            var bar = chart.data.datasets[1]._meta[0].data[i]._model;
            var dataPoint = e.split(/\s/)[1];
            if (dataPoint === '16')
               bar.backgroundColor = 'blue';
            else if (dataPoint === '17')
               bar.backgroundColor = 'green';
         });
      }
   }]
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="canvas"></canvas>

Leave a comment