javascript – 如何设置ChartJS Y轴标题?

我使用Chartjs显示图表,我需要设置y轴的标题,但在文档中没有关于它的信息.

我需要像在图片上一样设置y轴,或者在y轴的顶部设置,所以有人可以现在是什么参数

我在官方网站上看过,但没有关于它的信息

解决方法:

对于Chart.js 2.x,请参阅andyhasit的回答 – https://stackoverflow.com/a/36954319/360067

对于Chart.js 1.x,您可以调整选项并扩展图表类型以执行此操作,就像这样

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();
    }
});

这样称呼它

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%>"
});

注意标签值之前的空格,这为我们提供了写入y轴标签的空间,而没有弄乱太多的Chart.js内部

小提琴 – http://jsfiddle.net/wyox23ga/

enter image description here

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: &lt;span id=&quot...
jQuery 添加水印 &lt;script src=&quot;../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...