简体   繁体   English

使用chart.js在折线图中显示垂直轴标签

[英]display vertical axis label in line chart using chart.js

How to add a vertical axis (Y-axis) label for a line graph that was created using chart.js and angular-chart.js 如何为使用chart.js和angular-chart.js创建的折线图添加垂直轴(Y轴)标签

I need to display y-axis label. 我需要显示y轴标签。

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 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. 有关轴标题的原始意见是在画布之外进行操作(请参阅此处https://github.com/nnnick/Chart.js/issues/114 ),但是鉴于链接问题的活动https:// github .com / nnnick / Chart.js / issues / 52,这可能会更改。


Adding a Y Axis Title 添加Y轴标题

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) 首先,扩展图表以绘制轴标题(主要是“ 如何使用希望更干净的代码设置ChartJS y轴标题”中的重述

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/ . 根据您的指令,我假设您正在使用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 注意添加的options和更改的类chart-line-alt


Fiddle - http://jsfiddle.net/eeqfvy6f/ 小提琴-http: //jsfiddle.net/eeqfvy6f/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM