Chartjs-Chartjs Line chart options display increase and decrease percentage between each datapoint

0๐Ÿ‘

โœ…

I think that you need something like this to match the previous record.

const options = {
  tooltips: {
    callbacks: {
      label: function(tooltipItem, data) {
        var dataset = data.datasets[tooltipItem.datasetIndex];
        var currentValue = dataset.data[tooltipItem.index];
        var precentage, previousValue;
        if(tooltipItem.index - 1 >= 0) {
            previousValue = dataset.data[tooltipItem.index - 1];
            precentage = parseFloat((currentValue - previousValue) * 100 / previousValue).toFixed(1);
        } else {
            precentage = 0;
        }         
        return currentValue + ' (' + precentage + "%)";
      },
      title: function(tooltipItem, data) {
        return data.labels[tooltipItem[0].index];
      }
    }
  },
}

Leave a comment