Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format Bar Chart's yAxis labels in Chart.js

I have looked the documentation and similar questions here but I don't seem to find a working solution to my problem.

I'm using Chart.js v.2.1.6, and I have a Bar Chart with percentage values stored as numbers (already multiplied by 100). I need both y-axis labels and tooltips to display the % sign after the values.

Someone can shed some light on that matter?

Here you have my code:

var data = {
  "labels": ["Label1", "Label2", "Label3", "Label4", "Label5"],
  "datasets": [{
    "label": "Variation",
    "data": ["56", "-82.3", "25.7", "32.2", "49.99"],
    "borderWidth": 1,
    "backgroundColor": "rgba(231, 76, 60, 0.2)",
    "borderColor": "rgba(231, 76, 60, 1)"
  }]
};

var myBarChart = new Chart($("#myCanvas"), {
  type: 'bar',
  data: data,
  maintainAspectRatio: false
});

And a fiddle: https://jsfiddle.net/tdjk3ncs/

EDIT: SOLVED

I found the solution thanks to miquelarranz, find the updated fiddle:

https://jsfiddle.net/tdjk3ncs/7/

like image 231
Mumpo Avatar asked Jul 04 '16 08:07

Mumpo


People also ask

How do you label the Y axis on a graph?

Click the chart, and then click the Chart Layout tab. Under Labels, click Axis Titles, point to the axis that you want to add titles to, and then click the option that you want. Select the text in the Axis Title box, and then type an axis title.

How do you change the position of the label of Y axis in Chartjs?

And for that, you can use labelOffset property to achieve it, and then you can also put some padding to move the label a little a bit from the y-line. labelOffset - Distance in pixels to offset the label from the centre point of the tick (in the x direction for the x axis, and the y direction for the y axis).

What is scales in Chart JS?

The linear scale is used to chart numerical data. It can be placed on either the x or y-axis. The scatter chart type automatically configures a line chart to use one of these scales for the x-axis. As the name suggests, linear interpolation is used to determine where a value lies on the axis.


1 Answers

If you want to add %after the values of the Y-Axis you can do it using scales in your chart configuration. Your code will look like this:

var myBarChart = new Chart($("#myCanvas"), {
    type: 'bar',
    data: data,
    maintainAspectRatio: false,
    options: {
        scales: {
            yAxes: [{
                ticks: {
                    // Create scientific notation labels
                    callback: function(value, index, values) {
                        return value + ' %';
                    }
                }
            }]
        }
    }
});

Documentation about scales

Fiddle updated with the %: Fiddle

And if you want to modify the text displayed in the tooltips you can easily change it using callback. You can find more information here Tooltip Callbacks

like image 136
miquelarranz Avatar answered Sep 22 '22 02:09

miquelarranz