【发布时间】:2021-09-08 08:07:55
【问题描述】:
已经有几篇类似的帖子,但他们的解决方案要么不完整,要么不适用于现代 chart.js。
问题:如何将此代码更新为具有点之间真实距离的时间散点图?目前,它是一个具有点之间单位距离的线性图。如您所见,1 月 11 日和 1 月 8 日(3 天)之间的距离与 e.g. 1 月 8 日和 1 月 7 日(1 天)。
let testPoints = [
{ x: '2017-01-06 00:00:00', y: '20' },
{ x: '2017-01-07 00:00:00', y: '17' },
{ x: '2017-01-08 00:00:00', y: '14' },
{ x: '2017-01-11 00:00:00', y: '7' },
]
var s1 = {
label:'Overall activity',
borderColor: '#33b5e5',
data: testPoints
};
config = {
type: 'line',
data: { datasets: [s1] },
options: {
plugins: {
title: {
display: true,
text: 'Overall activity plot',
font: {
size: 20
}
}
},
legend: {
display: false
},
scales: {
xAxes: [{
type: 'time',
weight : 0,
time: {
unit:'day'
}
}],
}
}
};
new Chart(ctx, config);
注意:如果我包含 moment.js v2.29.1 并使用 x: moment('2017-01-06 00:00:00'),则绘图根本不会绘图,并出现以下错误:
Uncaught SyntaxError: Unexpected token export moment.js:5662
Uncaught ReferenceError: moment is not defined
注意:如果我只设置config.type: 'scatter',情节就会变空。
堆栈:
chart.js v3.5.1
【问题讨论】:
标签: javascript chart.js momentjs