简体   繁体   English

Chart.JS:如何将数据添加到特定数据集

[英]Chart.JS: How to add Data to a specific dataset

Currently I use this to add data to my charts: 目前我使用它来向我的图表添加数据:

 targetChart.addData(value,targetDataSetIndex,"");

because I thought that this would add data to a specific set, adding an empty label. 因为我认为这会将数据添加到特定集合,添加空标签。

But this doesnt work. 但这不起作用。

I then tried doing it myself by introducing a new method in the Chart.js script, like this: 然后我尝试通过在Chart.js脚本中引入一个新方法来完成它,如下所示:

    addDataValueToSpecificDataSet :function(value,datasetIndex)
    {
        this.datasets[datasetIndex].points.push(new this.PointClass({
            value: value,
            label: label,
            x: this.scale.calculateX(this.scale.valuesCount + 1),
            y: this.scale.endPoint,
            strokeColor: this.datasets[datasetIndex].pointStrokeColor,
            fillColor: this.datasets[datasetIndex].pointColor
        }));
    }

But when executing it isnt found. 但是在执行它时没有找到。 Where in Chart.JS do I need to put this so that it is accessible from the outside? 在Chart.JS中我需要把它放在哪里才能从外面访问它? Or can someone please tell me how im supposed to add data to a chart with multiple datasets one by one. 或者有人可以告诉我如何将数据添加到具有多个数据集的图表中。 I cant find it in the documentation which covers everything BUT adding and removing data. 我无法在涵盖所有内容的文档中找到它但是添加和删除数据。

This works for me (you probably just need to add update): 这对我有用(你可能只需要添加更新):

function addData() {
  myBarChart.data.labels[12] ="2017";
  myBarChart.data.labels[13] ="2018";
  myBarChart.data.datasets[0].data[12] = 500;
  myBarChart.data.datasets[0].data[13] = 600;
  myBarChart.update();
}

CodePen: Add/Remove adjust data Chart.js CodePen: 添加/删除调整数据Chart.js

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

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