4👍
✅
The original opinion on the axis title is to do it outside of the canvas (see here https://github.com/nnnick/Chart.js/issues/114), but given the activity on the linked issue https://github.com/nnnick/Chart.js/issues/52 this could change.
Adding a Y Axis Title
That said, here’s how you can do it on the current version using the canvas. First, extend the chart to draw the axis title (mostly a rehash from How to set ChartJS y axis title with hopefully cleaner code)
Chart.types.Line.extend({
name: "LineAlt",
initialize: function (data) {
// making space for the title by increasing the y axis label width
if (this.options.yAxisLabel)
this.options.scaleLabel = ' ' + this.options.scaleLabel;
Chart.types.Line.prototype.initialize.apply(this, arguments);
if (this.options.yAxisLabel)
this.scale.yAxisLabel = this.options.yAxisLabel;
},
draw: function () {
Chart.types.Line.prototype.draw.apply(this, arguments);
// drawing the title
if (this.scale.yAxisLabel) {
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.2;
var y = this.chart.height / 2;
// change origin
ctx.translate(x, y)
// rotate text
ctx.rotate(-90 * Math.PI / 180);
ctx.fillText(this.scale.yAxisLabel, 0, 0);
ctx.restore();
}
}
});
From your directives, I assume you are using http://jtblin.github.io/angular-chart.js/. If so you register the new chart type like so
angular.module('chart.js')
.directive('chartLineAlt', ['ChartJsFactory', function (ChartJsFactory) { return new ChartJsFactory('LineAlt'); }]);
and you pass in the axis title using options like so
...
$scope.options = {
yAxisLabel: "My Y Axis Label",
}
with markup
<canvas id="line" class="chart chart-line-alt" data="data"
labels="labels" legend="true" series="series" options="options"
click="onClick" colours="['Red','Yellow']" width="402" height="201" style="width: 402px; height: 201px"></canvas>
Note the added options
and the changed class chart-line-alt
Fiddle – http://jsfiddle.net/eeqfvy6f/
Source:stackexchange.com