简体   繁体   English

D3 v4 中的方形网格退出选择

[英]Square grid exit selection in D3 v4

I have been working on changing this block to v4 with my limited exposure to d3js.我一直致力于将此块更改为 v4,而我对 d3js 的接触有限。

The changes are made are;所做的更改是;

  • "https://d3js.org/d3.v3.js" to "https://d3js.org/d3.v4.js" "https://d3js.org/d3.v3.js""https://d3js.org/d3.v4.js"

  • .ease('linear') to .ease(d3.easeLinear) .ease('linear').ease(d3.easeLinear)

which i understood further from the change readme here .我从这里的更改自述文件中进一步了解了这一点

The changes results in the chart not being able to reach the cell.exit.transition() block when I tried to console.log the exit function, this is also evident as the grids do not exit and just append from new randomized data:当我尝试console.log退出函数时,图表中的更改导致无法到达cell.exit.transition()块,这也很明显,因为网格不退出,只是从新的随机数据追加:

cell.exit().transition()
    .delay(function(d, i) { return (n0 - i) * updateDelay; console.log(n0, n1);})
    .duration(updateDuration)
    .attr("width", 0)
    .remove();

From the readme, there is no change in the transition methods but I am thinking this is due to the change in the select function.从自述文件中,转换方法没有变化,但我认为这是由于 select 函数的变化。 I am having trouble seeing what when wrong as there seems to be no errors within the console when I run this.我在看到错误时遇到了麻烦,因为当我运行它时控制台中似乎没有错误。

The problem here seems to be the infamous magic behaviour that was introduced by Mike Bostock (D3 creator) in D3 v2, and later removed in D3 v4.这里的问题似乎是由 Mike Bostock(D3 创建者)在 D3 v2 中引入的臭名昭著的魔法行为,后来在 D3 v4 中被删除。

According to Bostock :根据博斯托克的说法:

D3 2.0 introduced a change: appending to the enter selection would now copy entering elements into the update selection [...] D3 4.0 removes the magic of enter.append. D3 2.0 引入了一项更改:添加到输入选择现在会将输入元素复制到更新选择中 [...] D3 4.0 删除了 enter.append 的魔力。 (In fact, D3 4.0 removes the distinction between enter and normal selections entirely: there is now only one class of selection.) (实际上,D3 4.0 完全消除了输入和普通选择之间的区别:现在只有一类选择。)

You can read more about this issue in these answers: https://stackoverflow.com/a/47032222/5768908 and https://stackoverflow.com/a/45093007/5768908您可以在以下答案中阅读有关此问题的更多信息: https : //stackoverflow.com/a/47032222/5768908https://stackoverflow.com/a/45093007/5768908

In your case, the solution is changing the update selection to this:在您的情况下,解决方案将更新选择更改为:

var cellUpdate = cell.selectAll("rect")
    .data(d3.range(n1));

And then:进而:

cellUpdate.exit()
    //etc...

cellUpdate.enter()
    .append("rect")
    //etc...

Here is the updated bl.ocks: https://bl.ocks.org/GerardoFurtado/b0d66087d9888a2cac3a42b114e5e8c4/72a0e54de5ce8cba2c398b282d953dd5c2bcc66e这是更新的 bl.ocks: https ://bl.ocks.org/GerardoFurtado/b0d66087d9888a2cac3a42b114e5e8c4/72a0e54de5ce8cba2c398b282d953dd5c2bcc66e

PS: for this to work in v4/5 (but not from v5.8 onwards) you have to change the text tween as well: PS:要使其在 v4/5(但不是从 v5.8 开始)中工作,您还必须更改文本补间:

.tween("text", function() {
    var self = this;
    var i = d3.interpolateNumber(n0, n1);
    return function(t) {
        self.textContent = formatNumber(Math.round(i(t)));
    };
});

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

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