0
我有一个使用Charts.js api构建的图表。 x轴被配置为通过moment.js在小时显示刻度。我想给于X轴的小时刻度改为日的一周中的选项:我知道要做到这一点,我只需要在图表的代码从:Charts.js:使用函数更改xAxis时间属性
xAxes:
[{
type: 'time',
distribution: 'linear',
time:
{
unit: 'hour'
}
}],
为此:
xAxes:
[{
type: 'time',
distribution: 'linear',
time:
{
unit: 'week'
}
}],
但我不能让它发生在JavaScript函数中。我希望能够根据偏好更改此设置,所以我想要构建一个函数,在调用时将“单元”属性更改为不同的内容。谁能帮忙?以下是我迄今为止:
function setXAxis()
{
chart.config.options.scales.xAxes.time.unit.push('week');
chart.update();
};
完全chart.js之代码:
// Setting up progress chart via Charts.js
var ctx = document.getElementById("myChart").getContext("2d");
Chart.defaults.global.defaultFontColor = "#58a7dd";
// Configuration
var chart = new Chart(ctx,
{
type: 'line',
data:
[{
x: new Date(),
y: 1
},
{
t: new Date(),
y: 10
}],
options:
{
scales:
{
xAxes:
[{
type: 'time',
distribution: 'linear',
time:
{
unit: 'hour' //THIS IS WHAT I WANT TO CHANGE WITH THE FUNCTION
}
}],
yAxes:
[{
type: 'category',
labels: ['one', 'two', 'three', 'four', 'five']
}]
}
}
});
谢谢,解决了它。愚蠢的我忘记了xAxes阵列的方括号。 – Brian