如何设置 ChartJS Y 轴标题?
How to set ChartJS Y axis title?
我正在使用 Chartjs 显示图表,我需要设置 y 轴的标题,但文档中没有相关信息。
我需要像图片一样设置 y 轴,或者在 y 轴顶部这样有人现在可以知道那个参数是什么
我在官方网站上看过,但没有相关信息
对于Chart.js 2.x参考andyhasit的回答-
对于 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%>"
});
注意标签值前面的 space,这使我们可以 space 编写 y 轴标签而不会弄乱太多 Chart.js 内部结构
Fiddle - http://jsfiddle.net/wyox23ga/
考虑在元素上使用 transform: rotate(-90deg) 样式。
参见 http://www.w3schools.com/cssref/css3_pr_transform.asp
例如,
在你的 css
.verticaltext_content {
position: relative;
transform: rotate(-90deg);
right:90px; //These three positions need adjusting
bottom:150px; //based on your actual chart size
width:200px;
}
在 Y 轴刻度上添加一个 space 模糊因子,这样文本就有空间在您的 javascript 中呈现。
scaleLabel: " <%=value%>"
然后在你的 html 图表之后 canvas 放一些像...
<div class="text-center verticaltext_content">Y Axis Label</div>
这不是最优雅的解决方案,但当我在 html 和图表代码之间有几层时效果很好(使用 angular-chart 并且不想更改任何源代码).
在 Chart.js 版本 2.0 中,这是可能的:
options = {
scales: {
yAxes: [{
scaleLabel: {
display: true,
labelString: 'probability'
}
}]
}
}
有关详细信息,请参阅 axes labelling documentation。
chart.js支持这个默认勾选link。
chartjs
您可以在选项属性中设置标签。
选项对象如下所示。
options = {
scales: {
yAxes: [
{
id: 'y-axis-1',
display: true,
position: 'left',
ticks: {
callback: function(value, index, values) {
return value + "%";
}
},
scaleLabel:{
display: true,
labelString: 'Average Personal Income',
fontColor: "#546372"
}
}
]
}
};
对我来说,它是这样工作的:
options : {
scales: {
yAxes: [{
scaleLabel: {
display: true,
labelString: 'probability'
}
}]
}
}
对于 x 轴和 y 轴:
options : {
scales: {
yAxes: [{
scaleLabel: {
display: true,
labelString: 'probability'
}
}],
xAxes: [{
scaleLabel: {
display: true,
labelString: 'hola'
}
}],
}
}
为Chart.js3.x
options: {
scales: {
y: {
title: {
display: true,
text: 'Y axis title'
}
}
}
}
我正在使用 Chartjs 显示图表,我需要设置 y 轴的标题,但文档中没有相关信息。
我需要像图片一样设置 y 轴,或者在 y 轴顶部这样有人现在可以知道那个参数是什么
我在官方网站上看过,但没有相关信息
对于Chart.js 2.x参考andyhasit的回答-
对于 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%>"
});
注意标签值前面的 space,这使我们可以 space 编写 y 轴标签而不会弄乱太多 Chart.js 内部结构
Fiddle - http://jsfiddle.net/wyox23ga/
考虑在元素上使用 transform: rotate(-90deg) 样式。 参见 http://www.w3schools.com/cssref/css3_pr_transform.asp
例如, 在你的 css
.verticaltext_content {
position: relative;
transform: rotate(-90deg);
right:90px; //These three positions need adjusting
bottom:150px; //based on your actual chart size
width:200px;
}
在 Y 轴刻度上添加一个 space 模糊因子,这样文本就有空间在您的 javascript 中呈现。
scaleLabel: " <%=value%>"
然后在你的 html 图表之后 canvas 放一些像...
<div class="text-center verticaltext_content">Y Axis Label</div>
这不是最优雅的解决方案,但当我在 html 和图表代码之间有几层时效果很好(使用 angular-chart 并且不想更改任何源代码).
在 Chart.js 版本 2.0 中,这是可能的:
options = {
scales: {
yAxes: [{
scaleLabel: {
display: true,
labelString: 'probability'
}
}]
}
}
有关详细信息,请参阅 axes labelling documentation。
chart.js支持这个默认勾选link。 chartjs
您可以在选项属性中设置标签。
选项对象如下所示。
options = {
scales: {
yAxes: [
{
id: 'y-axis-1',
display: true,
position: 'left',
ticks: {
callback: function(value, index, values) {
return value + "%";
}
},
scaleLabel:{
display: true,
labelString: 'Average Personal Income',
fontColor: "#546372"
}
}
]
}
};
对我来说,它是这样工作的:
options : {
scales: {
yAxes: [{
scaleLabel: {
display: true,
labelString: 'probability'
}
}]
}
}
对于 x 轴和 y 轴:
options : {
scales: {
yAxes: [{
scaleLabel: {
display: true,
labelString: 'probability'
}
}],
xAxes: [{
scaleLabel: {
display: true,
labelString: 'hola'
}
}],
}
}
为Chart.js3.x
options: {
scales: {
y: {
title: {
display: true,
text: 'Y axis title'
}
}
}
}