Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set ChartJS Y axis title?

I am using Chartjs for showing diagrams and I need to set title of y axis, but there are no information about it in documentation.

I need y axis to be set like on picture, or on top of y axis so someone could now what is that parameter

I have looked on official website but there was no information about it

like image 267
emir Avatar asked Aug 10 '15 07:08

emir


People also ask

How do you give a title in ChartJS?

The example below would enable a title of 'Custom Chart Title' on the chart that is created. const chart = new Chart(ctx, { type: 'line', data: data, options: { plugins: { title: { display: true, text: 'Custom Chart Title' } } } }); Copied!

How do you align a title in ChartJS?

title. textStyle. align is used to align the text within the position.

How do you hide Y axis labels in ChartJS?

To also hide the tick marks themselves, add gridLines: { tickMarkLength: 0 } to the y axis definition (tested in version 2.9. 4).

What is the difference between chart title and axis title?

This is Expert Verified Answer After you create a chart, you can add axis titles to the horizontal and vertical axes in charts that have axes. ... Much like a chart title you can add, axis titles help the people who view the chart understand what the data is about..


2 Answers

In Chart.js version 2.0 this is possible:

options = {   scales: {     yAxes: [{       scaleLabel: {         display: true,         labelString: 'probability'       }     }]   } } 

See axes labelling documentation for more details.

like image 65
andyhasit Avatar answered Sep 24 '22 00:09

andyhasit


For Chart.js 2.x refer to andyhasit's answer - https://stackoverflow.com/a/36954319/360067

For Chart.js 1.x, you can tweak the options and extend the chart type to do this, like so

Chart.types.Line.extend({     name: "LineAlt",     draw: function () {         Chart.types.Line.prototype.draw.apply(this, arguments);          var ctx = this.chart.ctx;         ctx.save();         // text alignment and color         ctx.textAlign = "center";         ctx.textBaseline = "bottom";         ctx.fillStyle = this.options.scaleFontColor;         // position         var x = this.scale.xScalePaddingLeft * 0.4;         var y = this.chart.height / 2;         // change origin         ctx.translate(x, y);         // rotate text         ctx.rotate(-90 * Math.PI / 180);         ctx.fillText(this.datasets[0].label, 0, 0);         ctx.restore();     } }); 

calling it like this

var ctx = document.getElementById("myChart").getContext("2d"); var myLineChart = new Chart(ctx).LineAlt(data, {     // make enough space on the right side of the graph     scaleLabel: "          <%=value%>" }); 

Notice the space preceding the label value, this gives us space to write the y axis label without messing around with too much of Chart.js internals


Fiddle - http://jsfiddle.net/wyox23ga/


enter image description here

like image 30
potatopeelings Avatar answered Sep 20 '22 00:09

potatopeelings