简体   繁体   English

D3可重复使用的圆形热图无法成功更新

[英]D3 reusable circular heat chart cannot successfully update

I am using the D3 reusable chart framework to create a circular heat chart to visualize acoustic data. 我正在使用D3可重复使用的图表框架来创建一个圆形热图来可视化声学数据。 I am not able to successfully update the data. 我无法成功更新数据。 When I try to update the data instead of updating the paths in the existing g element it draws new g elements. 当我尝试更新数据而不是更新现有g元素中的路径时,它会绘制新的g元素。 I created two charts just to see if my reusable framework works, and am testing data update only on the first chart. 我创建了两个图表,以查看我的可重用框架是否有效,并且仅在第一个图表上测试数据更新。 For some reason when I try to update the first chart, the colors also change to that of the second chart. 出于某种原因,当我尝试更新第一个图表时,颜色也会变为第二个图表的颜色。 Here is an image of the problem: 这是问题的图像: 在此输入图像描述

I think the problem may related to how I am creating the svg and g elements, but I can't figure out what the specific problem is. 我认为问题可能与我如何创建svg和g元素有关,但我无法弄清楚具体问题是什么。 In my reusable module this is how I am creating the svg and elements and the segments: 在我的可重用模块中,这就是我创建svg和元素以及段的方式:

svg.enter().append("svg")
            .classed("chart", true)
            .attr("width", width + margin.left + margin.right)
            .attr("height", height + margin.top + margin.bottom);


        var g = svg.append("g")
            .classed("circular-heat"+_index, true)
            .attr("transform", "translate(" + parseInt(margin.left + offset) + "," + parseInt(margin.top + offset) + ")");

        var segments = g.selectAll("path").data(data);

And here is a JSFiddle with my code. 这是一个带有我的代码的JSFiddle。

https://jsfiddle.net/jhjanicki/s3gsae5p/1/ https://jsfiddle.net/jhjanicki/s3gsae5p/1/

I was able to get some advice from a friend, Shirley Wu (thank you!), and she pointed out that the problem is that every time the viz updates, it's calling the chart function, which is ok but I have this part in my chart function: svg.append("g"). 我能够从朋友Shirley Wu那里得到一些建议(谢谢!),她指出问题是,每次viz更新时,它都会调用图表功能,这是好的,但我有这个部分在我的图表功能:svg.append(“g”)。

Which means that every time that chart is called, a new g element is appended instead of using the one that already exists. 这意味着每次调用该图表时,都会附加一个新的g元素,而不是使用已存在的元素。 That's why I was getting a new set of paths every time, since I was creating a new g and then filling that with paths every time. 这就是我每次都获得一组新路径的原因,因为我每次创建一个新g然后用路径填充它。

The solution is to either: (1) Create ag element only the first time around, and remember it for any subsequent calls to the chart function, OR (2) Create the g element in the chart function, and then have a separate update function that takes that g element and updates the paths within it 解决方案是:(1)仅在第一次创建ag元素,并记住它对图表函数的任何后续调用,或者(2)在图表函数中创建g元素,然后具有单独的更新函数获取该g元素并更新其中的路径

I used option 1 and edited the part within the chart function as the following in order not to create a new g every time: 我使用选项1并在图表函数中编辑了部件,如下所示:为了不每次都创建一个新g:

if(svg.select('g.circular-heat')[0][0] == null){
                var g = svg.append("g")
                    .classed("circular-heat", true)
                    .attr("transform", "translate(" + parseInt(margin.left + offset) + "," + parseInt(margin.top + offset) + ")");
            }

Here is the updated fiddle https://jsfiddle.net/jhjanicki/h93ka17y/ 这是更新的小提琴https://jsfiddle.net/jhjanicki/h93ka17y/

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

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