Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chart.js Bar graph with percentage values

Tags:

chart.js

I want to make a chart which has percentage values on y-axis but I'm not able to find any options in document. Please suggest some answers

like image 960
Manjunath Gk Avatar asked Dec 06 '16 11:12

Manjunath Gk


2 Answers

You could do something like this:

If you know the absolute value that represents 100% in your dataset, you can pass this to your options object:

    scales: {
  yAxes: [
    {
      ticks: {
        min: 0,
        max: this.max,// Your absolute max value
        callback: function (value) {
          return (value / this.max * 100).toFixed(0) + '%'; // convert it to percentage
        },
      },
      scaleLabel: {
        display: true,
        labelString: 'Percentage',
      },
    },
  ],
},
like image 62
Théo T. Carranza Avatar answered Sep 27 '22 17:09

Théo T. Carranza


If you have a static total...say a max value of 800, you can just show custom ticks like this:

ticks: {
    //.. Other settings
    stepSize: 200, /* total/4 shows 0, 25%, 50%, 75%, 100% */             
    callback: function(value, index, values) {
        return ((value / 800) * 100) + '%';
    }
}
like image 38
Wayne F. Kaskie Avatar answered Sep 27 '22 19:09

Wayne F. Kaskie