Javascript 动态更改ChartJs轴标题
我创建了一个这样的图表Javascript 动态更改ChartJs轴标题,javascript,chart.js,axis-labels,Javascript,Chart.js,Axis Labels,我创建了一个这样的图表 if (userLanguageCode === "es") { customTooltipFormat = 'DD/MM/YYYY, HH:mm:ss'; customDisplayFormats = { 'millisecond': 'SSS [ms]', 'second': 'HH:mm:ss', // 11:20:01 AM 'minute': 'D/MM/YY HH:mm', // 11:20:0
if (userLanguageCode === "es") {
customTooltipFormat = 'DD/MM/YYYY, HH:mm:ss';
customDisplayFormats = {
'millisecond': 'SSS [ms]',
'second': 'HH:mm:ss', // 11:20:01 AM
'minute': 'D/MM/YY HH:mm', // 11:20:01 AM
'hour': 'D/MM/YY HH[h]', // Sept 4, 5PM
'day': 'D/MM/YYYY', // Sep 4 2015
'week': 'll', // Week 46, or maybe "[W]WW - YYYY" ?
'month': 'MMM YYYY', // Sept 2015
'quarter': '[Q]Q - YYYY', // Q3
'year': 'YYYY', // 2015
};
}
Chart.defaults.global.responsive = true;
Chart.defaults.global.animation = false;
datosChartHistoricos = {
labels: [],
datasets: [{
label: textoValor,
backgroundColor: "rgba(0,181,255,0.5)",
fill: chartWithIncrementValues? true: false,
borderColor: "rgba(0,192,192,1)",
pointBorderColor: "rgba(0,181,255,1)",
pointBackgroundColor: "rgba(255,255,255,1)",
pointBorderWidth: 1,
data: []
}]
};
var ctx = document.getElementById("grafica").getContext("2d");
chartHistoricos = new Chart(ctx, {
type: chartWithIncrementValues? "bar" : "line",
data: datosChartHistoricos,
options: {
responsive: true,
elements: {
rectangle: {
borderWidth: 1,
borderColor: 'rgb(0, 0, 0)',
borderSkipped: 'bottom'
}
},
scales: {
xAxes: [{
type: "time",
time: {
tooltipFormat: customTooltipFormat,
displayFormats: customDisplayFormats,
}
}, ],
yAxes: [{
scaleLabel: {
display: true,
labelString: textoValor + " (" + unidadesValor + ")"
}
}]
},
legend: {
display: false,
}
}
});
这是图表的默认配置,数据是动态添加的,但用户可以选择不同的数据值来显示,如温度、距离。。。要做到这一点,我只需更改数据集和标签的数据值,但我不知道在更改数据集值时如何使用javascript更改yAxis标签。初始化标题是确定的
有什么建议吗
谢谢 只需更新图表对象的
options
属性中的labelString
值并调用prototype方法,即可更改刻度标题
假设我有一个名为myBar
的图表实例(该实例是从chart.js构造函数返回的),那么我可以使用下面的示例来更改y轴标题
myBar.options.scales.yAxes[0].scaleLabel.labelString = "My New Title";
myBar.update();
下面是一个示例,演示了这方面的工作示例。只需单击“更改标题”按钮即可查看是否有效。使用firebug调试网页。使用console.log(chartHistoricos)我可以看到我可以访问的所有对象,但是Axis对象中没有“labelString”……我知道这个问题已经很老了,但我完全忽略了[0]部分。谢谢!