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
Advertisement
Answer
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
JavaScript
x
23
23
1
Chart.types.Line.extend({
2
name: "LineAlt",
3
draw: function () {
4
Chart.types.Line.prototype.draw.apply(this, arguments);
5
6
var ctx = this.chart.ctx;
7
ctx.save();
8
// text alignment and color
9
ctx.textAlign = "center";
10
ctx.textBaseline = "bottom";
11
ctx.fillStyle = this.options.scaleFontColor;
12
// position
13
var x = this.scale.xScalePaddingLeft * 0.4;
14
var y = this.chart.height / 2;
15
// change origin
16
ctx.translate(x, y);
17
// rotate text
18
ctx.rotate(-90 * Math.PI / 180);
19
ctx.fillText(this.datasets[0].label, 0, 0);
20
ctx.restore();
21
}
22
});
23
calling it like this
JavaScript
1
6
1
var ctx = document.getElementById("myChart").getContext("2d");
2
var myLineChart = new Chart(ctx).LineAlt(data, {
3
// make enough space on the right side of the graph
4
scaleLabel: " <%=value%>"
5
});
6
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/