我正在尝试使用图表 JS 绘制一个包含大约 300,000 个数据点的图表。它运行速度非常慢,因此我尝试通过使用数据抽取插件来提高其性能。然而,数据根本没有被破坏。
我的代码示例:
const dataset = {
datasets: {[,
data : [{
x : timetag,
y : data,
}],
id : 'id',
label: 'label',
indexAxis: 'x'
,]}
const config = {
type: 'line',
data: dataset,
options: {
// parsing: false,
animation: false,
scales : {
x: {
type: 'time',
title: {
display: true,
text: 'Time Of Day'},
},
y:{
min : 0,
},
}}};
const myChart = new Chart(
document.getElementById("Chart"),
config
);
function decimateData(myChart) {
myChart.options.plugins.decimation.algorithm = 'lttb';
myChart.options.plugins.decimation.enabled = true;
myChart.options.plugins.decimation.samples = 50;
myChart.update()
}
sample of the data structure
[
{
"x": "2022-02-24 00:00:26",
"y": "11"
},
{
"x": "2022-02-24 00:00:27",
"y": "7"
},
{
"x": "2022-02-24 00:00:28",
"y": "8"
}
]
- 数据集的 indexAxis 必须为“x”
- 数据集必须是一行
- 数据集的 X 轴必须是“线性”或“时间”类型轴
- 数据必须不需要解析,即解析必须为 false
- 数据集对象必须是可变的。该插件将原始数据存储为 dataset._data 和 然后在数据集上定义一个新的数据属性。
我相信 1、2、3 都已实现。 4,在配置中打开 parsing: false 会使我的情节完全停止工作。
我的数据结构有什么不正确之处,不允许图表 js 读取它。
非常感谢您的指导,谢谢
更新
这是我用来在配置 block 中创建数据选项的函数:
function genDatasets() {
base = {
datasets: [],
parsing: false,,
fill: false};
for (var i = 0; i < sources.length; i++){
set =[{
data : [{
x : timetags,
y : data,
}],
id : sources[i].slice(0,-4),
label: sources[i].slice(0,-4),
borderColor: colours[i],
indexAxis: 'x'
,}]
base['datasets'].push(set[0]) ;
}
return base;
};
最佳答案
查看有关解析
的文档:
https://www.chartjs.org/docs/latest/api/interfaces/ParsingOptions.html#parsing
How to parse the dataset. The parsing can be disabled by specifying parsing: false at chart options or dataset. If parsing is disabled, data must be sorted and in the formats the associated chart type and scales use internally.
解决方案: 你的x轴数据(时间标签)需要是“从纪元开始的毫秒”,这是chartjs用于时间值的内部表示。目前您的 x 轴数据是日期字符串,这就是它不起作用的原因
关于javascript - 图表js数据抽取不起作用,解析问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71254033/