javascript - display vertical axis label in line chart using chart.js -
how add vertical axis (y-axis) label line graph created using chart.js , angular-chart.js
i need display y-axis label.
html <ion-content ng-controller="agecontroller"> <canvas id="line" class="chart chart-line" data="data" labels="labels" legend="true" series="series" click="onclick" colours="['red','yellow']" width="402" height="201" style="width: 402px; height: 201px"> </canvas> </ion-content> js app.controller('agecontroller', ['$scope','$http',function($scope,$http) { $scope.labels = ["january", "february", "march", "april", "may", "june", "july"]; $scope.series = ['series a', 'series b']; $scope.data = [ [65, 59, 80, 81, 56, 55, 40], [28, 48, 40, 19, 86, 27, 90] ]; }]);
the original opinion on axis title outside of canvas (see here https://github.com/nnnick/chart.js/issues/114), given activity on linked issue https://github.com/nnnick/chart.js/issues/52 change.
adding y axis title
that said, here's how can on current version using canvas. first, extend chart draw axis title (mostly rehash how set chartjs y axis title cleaner code)
chart.types.line.extend({ name: "linealt", initialize: function (data) { // making space title increasing 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 title if (this.scale.yaxislabel) { var ctx = this.chart.ctx; ctx.save(); // text alignment , 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 directives, assume using http://jtblin.github.io/angular-chart.js/. if register new chart type so
angular.module('chart.js') .directive('chartlinealt', ['chartjsfactory', function (chartjsfactory) { return new chartjsfactory('linealt'); }]);
and pass in axis title using options 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 added options
, changed class chart-line-alt
fiddle - http://jsfiddle.net/eeqfvy6f/
Comments
Post a Comment