在Chart.JS(v3)中-如何以编程方式更改Y轴的标签?
文字=yaxislabel;
和
[0].标题.文本=yaxislabel;
......不会更改它-并且我会收到如下错误:
Uncaught TypeError: myChart.options.scales[0] is undefined
...尽管所有其余的数据集和标签都可以使用下面的代码进行更新:
My chart is setup with:
// define chart options
const options = {
plugins: { // 'legend' now within object 'plugins {}'
legend: {
labels: {
color: "white",
// fontSize: 18 // not 'fontSize:' anymore
}
}
},
maintainAspectRatio: false,
scales: {
x: {
title: {
display: true,
text: 'Days',
color: 'white'
},
grid: {
color: 'rgb(239,96,36,0.5)',
borderColor: '#ef6024'
},
ticks: {
color: '#ffffff',
stepSize: 50
}
},
y: {
title: {
display: true,
text: 'Weight (kg)',
color: '#ffffff'
},
grid: {
color: 'rgb(239,96,36,0.5)',
borderColor: '#ef6024'
},
ticks: {
color: '#ffffff',
//stepSize: 50,
beginAtZero: true
}
}
}
};
const config = {
type: 'line',
data: data,
options: options,
};
const myChart = new Chart(
document.getElementById('myChart'),
config
);
$yaxislabel = 'kg';
myChart.data.datasets[0].data = Object.values(varsdata);
myChart.data.labels= Object.values(varslabels);
myChart.data.datasets[0].label = chartdatalabel;
myChart.scales.y.text=yaxislabel; <-- doesn't work
myChart.options.scales[0].title.text=yaxislabel <-- doesn't work
myChart.update();
Thanks, Mark
1条答案
按热度按时间2nc8po8w1#
这是因为你需要把它放在
title
命名空间中,所以你的语句必须是:myChart.options.scales.y.title.text=yaxislabel;
。要显示它,您还需要通过调用以下命令来确保它已启用:myChart.options.scales.y.title.display = true;