如何设置 ChartJS Y 轴标题? [英] How to set ChartJS Y axis title?
问题描述
我使用
对于 Chart.js 1.x,您可以调整选项并扩展图表类型来执行此操作,就像这样
Chart.types.Line.extend({name: "LineAlt",绘制:函数(){Chart.types.Line.prototype.draw.apply(this, arguments);var ctx = this.chart.ctx;ctx.save();//文本对齐和颜色ctx.textAlign = "居中";ctx.textBaseline = "底部";ctx.fillStyle = this.options.scaleFontColor;//位置var x = this.scale.xScalePaddingLeft * 0.4;var y = this.chart.height/2;//改变原点ctx.translate(x, y);//旋转文本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, {//在图的右侧留出足够的空间scaleLabel: " <%=value%>"});
注意标签值前面的空格,这为我们提供了编写 y 轴标签的空间,而不会弄乱 Chart.js 的内部结构
<小时>小提琴 -
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
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
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();
}
});
calling it like this
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%>"
});
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/
这篇关于如何设置 ChartJS Y 轴标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!