简体   繁体   English

从 Chart.js 动态添加和删除数据集

[英]Dynamically Add and Remove Datasets from Chart.js

I have been tasked with making a web-based data visualizer kind of like tableau using the asp.net framework.我的任务是使用 asp.net 框架制作一个类似于 tableau 的基于 Web 的数据可视化工具。 The user can drag and drop blocks which represents tables to a dropzone, which then triggers the javascript function for adding data.用户可以将代表表格的块拖放到放置区,然后触发用于添加数据的 javascript 函数。 This is the code:这是代码:

function addData(chart, Datalabel, Tabledata, Xlabel, Ylabel, labelsSet) {
        datasintable.push(Tabledata);
        var curColor = randomColor();
        chart.data.labels = labelsSet;
        chart.data.datasets.push({
            label: Datalabel,
            fill: false,
            backgroundColor: curColor,
            borderColor:  curColor,
            data: Tabledata
        });
        chart.options = {
            legend: {
                display:false
            },
            scales: {
                xAxes: [{
                    scaleLabel: {
                        labelString: Xlabel
                    }
                }],
                yAxes: [{
                    scaleLabel: {
                        labelString: Ylabel
                    }
                }]
            }
        }
        chart.update();
    }

This part of the code is working fine, the problem is in removing the data, due to index issues.这部分代码工作正常,问题在于由于索引问题而删除数据。 Right now each draggable component has it's own id which is then read and interpreted as a certain number I determined via an if else.现在每个可拖动组件都有自己的 id,然后读取并解释为我通过 if else 确定的某个数字。 Of course, due to the unpredictable nature of the user interaction the project, this approach does not work because the index pointer needs to be dynamic whereas in my code it is not.当然,由于项目用户交互的不可预测性,这种方法不起作用,因为索引指针需要是动态的,而在我的代码中不是。 Right now the code for removing data is like this:现在删除数据的代码是这样的:

function hide(chart, index) {
        datasintable.splice(index, 1);
        chart.data.datasets[index].data = 0;
        chart.update();
    }

and the index variable is determined by if else conditions like these:并且索引变量由 if else 条件确定,如下所示:

var data = ev.target.id

 if (data == "pop") {
            popdth = "0";
            addData(lineChart, 'Population', @Html.Raw(Json.Serialize(ViewBag.population)), 'Year', 'Population', @Html.Raw(Json.Serialize(ViewBag.year)));
        }
        else if (data = "dth") {
            popdth = "1";
            addData(lineChart, 'Death Rate', @Html.Raw(Json.Serialize(ViewBag.death)), 'Year', 'Population', @Html.Raw(Json.Serialize(ViewBag.yeardeath)));
        }

('pop' and 'dth' is the id I set for the draggables) ('pop' 和 'dth' 是我为可拖动对象设置的 id)

How should I approach this problem so that both adding and removing data to the chart is dynamic?我应该如何解决这个问题,以便在图表中添加和删除数据都是动态的?

没关系,最后我做了一些计算来计算每个可拖动的位置,然后通过变量将可拖动的索引分配给所述计算的值

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

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