简体   繁体   English

d3js堆积的条形图未更新

[英]d3js stacked bar chart not updating

I am experimenting with a stacked bar chart in d3js and ran into enter exit selection difficulties. 我正在d3js中使用堆积条形图进行实验,并遇到了enter exit选择的困难。 I used the d3.stack to get two arrays organized by keys, then I tried to follow the general update pattern. 我使用d3.stack来获取由键组织的两个数组,然后尝试遵循常规更新模式。 The problem I'm having now is the data is not getting updated when I click a different state in the dropdown menu. 我现在遇到的问题是,当我在下拉菜单中单击其他状态时,数据没有得到更新。 Here's the problem code and a link to the full project: http://plnkr.co/edit/8N8b2yUYRF9zqRkjkIiO?p=preview 这是问题代码和完整项目的链接: http : //plnkr.co/edit/8N8b2yUYRF9zqRkjkIiO?p=preview

var series = g.append("g")

var seriesready = 
    series.selectAll("g")
    .data(stack(data))
    .enter().append("g")
    .attr("fill",function(d){console.log(d); return z(d.key)}) //not logging 
when I update the bar chart

var rectangles = 
    seriesready.selectAll("rect")
    .data(function(d){return d})

rectangles.exit().remove()

rectangles.enter().append("rect")
      .attr("width", x.bandwidth())
      .transition()
        .duration(1500)
        .attr("transform", function(d) {console.log(d); return "translate(" + x(d.data.Date) + ",0)"; })
        .attr("height", function(d) {
        return height - y(d[1]-d[0]);
      })
        .attr("y", function(d) {
        return y(d[1]-d[0]);
      });

I also think I'm getting confused as to what selections should be removed or added. 我还认为我对应该删除或添加哪些选择感到困惑。 Would really appreciate any pointers. 真的很感谢任何指针。 Data viz is fun to work with, but I still haven't fully grasped data binding yet. 数据可视化很有趣,但是我仍然没有完全掌握数据绑定。

I have not made the switch to version 4 yet, but the data binding methodology is the same i think. 我还没有切换到版本4,但是我认为数据绑定方法是相同的。

You need to define a key function as the second parameter to the .data() function. 您需要将键函数定义为.data()函数的第二个参数。

A key function may be specified to control which datum is assigned to which element, replacing the default join-by-index. 可以指定一个键函数来控制将哪个基准分配给哪个元素,从而替换默认的按索引联接。

https://github.com/d3/d3-selection/blob/master/README.md#selection_data https://github.com/d3/d3-selection/blob/master/README.md#selection_data


Your updated code 您更新的代码

http://plnkr.co/edit/wwdjJEflZtyACr6w9LiS?p=preview http://plnkr.co/edit/wwdjJEflZtyACr6w9LiS?p=preview

The changed code: 更改后的代码:

var seriesUpdate = series.selectAll("g")
        .data(stack(data),d=>d)
var seriesready = seriesUpdate.enter().append("g")
        .attr("fill",function(d){return z(d.key)})

seriesUpdate.exit().remove()

When binding data to elements, D3 calculates what data is new/existing/removed in relation to the selection. 将数据绑定到元素时,D3计算与选择有关的新数据/已存在数据/已删除数据。 By default it does this by data index - the size of the input array. 默认情况下,它通过数据索引-输入数组的大小来执行此操作。 Since the computed stack data for michigan and ohio both return 2 sets of data (injured and killed), D3 views this as "same" data, thus it's an update . 由于为密歇根州和俄亥俄州计算出的堆栈数据都返回了2组数据(受伤和死亡),因此D3将此视为“相同”数据,因此是update

If you define a key function, D3 recognizes the computed stack data for michigan and ohio as being "different" data, thus it's an enter . 如果定义键功能,则D3会将密歇根州和俄亥俄州的计算堆栈数据识别为“不同”数据,因此它是一个enter

With a key function, when you select Ohio first, the enter selection is size 2 with Ohio. 使用按键功能时,首先选择俄亥俄时,回车选择的大小为2。 If you then select Michigan, the enter selection is size 2 with Michigan, and the exit selection is size 2 with Ohio. 如果然后选择密歇根州,则密歇根州的输入选择为2号,俄亥俄州的出口选择为2号。

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

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