Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I display the percentage symbol inside the chartjs?

I can already retrieve the correct values for the percentage. How can I display the percentage symbol for each of the values? the codes for the graph:

like image 780
JS3 Avatar asked Oct 29 '25 06:10

JS3


2 Answers

Tooltips can be modified thus

    <Line
      data={{
<snip>
      options={{
        maintainAspectRatio: false,
        title: {
          display: true,
          text: "Hello",
          fontSize: 20
        },
        plugins: {
          tooltip: {
              callbacks: {
                  label: function(context) {
                      var label = context.dataset.label || '';
                      if (context.parsed.y !== null) {
                          label += ' ' +context.parsed.y + '%';
                      }
                      return label;
                  }
              }
          }
      },
        scales: {
          y: {
            min: 0,
            max: 100,
            ticks: {
              stepSize: 20,
              callback: function (value, index, values) {
                return value + " %";
              }
            }
          }
        },
        legend: {
          labels: {
            fontSize: 25
          }
        }
      }}
    />
like image 164
neophytte Avatar answered Oct 31 '25 13:10

neophytte


You can edit the ticks property like this:

scales: {
    y: {
        min: 0,
        max: 100,
        ticks: {
            stepSize: 20,
            callback: function(value, index, values) {
                return value + " %";
            }            
        }
    }
},

Found that here: https://www.chartjs.org/docs/latest/axes/labelling.html#creating-custom-tick-formats

Seems to do the trick:

enter image description here

like image 38
djs Avatar answered Oct 31 '25 12:10

djs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!