简体   繁体   English

Chart JS 替换所有数据集数据

[英]Chart JS Replace All Dataset Data

I want to replace my chart data entirely with a new array of objects.我想用一个新的对象数组完全替换我的图表数据。 The chart data was replaced by new data but the chart is not updating after I called mychart.update().图表数据已被新数据替换,但在我调用 mychart.update() 后图表并未更新。 Is there any way to replace the entire data without destroying the chart?有什么方法可以在不破坏图表的情况下替换整个数据?

Here's my code:这是我的代码:

  Livewire.on('changeData', () => {
    myChart.data.datasets.data = @this.data;
    myChart.update();

    console.log(myChart.data.datasets.data);
  })

Old Data旧数据

旧数据

New Data新数据

新数据

This is because you are doing it wrong, the datasets field is an array containing all datasets so it doesn't contain a data field.这是因为您做错了, datasets字段是一个包含所有数据集的数组,因此它不包含data字段。 You need to target the specific dataset from which you want to replace the data like so:您需要针对要从中替换数据的特定数据集,如下所示:

myChart.data.datasets[datasetIndex].data = @this.data; // in case you only have 1 dataset you can just hardcode a 0
myChart.update();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM