javascript - d3.js 使用 exit() 和 Enter() 进行子转换

标签 javascript d3.js

简短问题: 如何在 this.selection.exit() 的子转换中访问整个 this.selection

我正在尝试制作 d3.js 转换序列。我知道如何使用 transition.transition() 函数来创建序列,但这是我的问题。

顺序应该是:[删除.exit()节点]→[移动节点]→[添加.enter()节点]

代码是:

        this.selection.exit().remove();
        this.selection
            .style("left", this.styleLeft)
            .style("top", this.styleTop)
            .style("width", this.styleWidth)
            .style("height", this.styleHeight);
        this.selection.enter().append("div")
            .attr("class", "node")
            .style("left", this.styleLeft)
            .style("top", this.styleTop)
            .style("width", this.styleWidth)
            .style("height", this.styleHeight);

如果我将 transition() 添加到上面的每个命令中,所有转换将同时开始。 .delay() 似乎无法同时在多个转换上正常工作。

所以我想将子转换与 .transition() 一起使用,但如果我将第二个命令应用于第一个命令,我只有 .exit()选择。

编辑1:

它不起作用的示例:

        this.selection.exit().transition()
        .duration(1000).style("opacity", 0).remove();

        this.selection.transition().delay(1010)
            .duration(1000)
            .style("left", this.styleLeft)
            .style("top", this.styleTop)
            .style("width", this.styleWidth)
            .style("height", this.styleHeight)

        this.selection.enter().append("div")
            .attr("class", "node")
            .style("opacity", 0)
            .style("left", this.styleLeft)
            .style("top", this.styleTop)
            .style("width", this.styleWidth)
            .style("height", this.styleHeight)
            .transition()
            .delay(2020)
            .duration(1000)
            .style("opacity", 1);

此处错误:最后一个转换未开始,因此所有新节点都是不透明的。但如果我禁用第二个转换,它就会起作用。

编辑2:

http://jsfiddle.net/dq6d117g/6/

如果在转换期间再次调用该函数,就会出现问题。

此外,延迟应该和动画一样长。如果没有动画,延迟应该为0。这就是为什么我想使用结束事件。

最佳答案

您可以按照 Mike Bostock 的 answer here 使用 d3.transition().each() 在预先存在的选择上链接转换。但是,在我的测试中,它没有跳过不需要的转换,因此我添加了条件来跳过空选择的转换:

http://jsfiddle.net/dq6d117g/8/

var transition=d3.transition().duration(2000);

if (!selection.exit().empty()) transition=transition.each(function(){
    selection.exit()
        .transition()
        .style("opacity", 0)
        .remove();
}).transition();

if (!selection.empty()) transition=transition.each(function(){
    selection.transition()
    .style("height", function (d) {
        return (d.value * 10) + "px";
    });
}).transition();

transition.each(function(){
    selection.enter()
        .append("div")
        .attr("class", "bar")
        .attr("id", function (d) {
            return d;
        })
        .style("opacity", 0)
        .style("height", function (d) {
            return (d.value * 9) + "px";
        })
        .text(function (d) {
            return d.id
        })
        .transition()
        .style("opacity", 1)
        .style("height", function (d) {
            return (d * 10) + "px";
        });
});

关于javascript - d3.js 使用 exit() 和 Enter() 进行子转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26421388/

相关文章:

css - 漂亮的悬停效果(D3图)

javascript - dc.js:具有弹性的条形总计

javascript - 在 d3 中创建动态垂直线

javascript - 在 Angular 中拖动时触发 D3 缩放事件

javascript - 从后端验证用户 token

javascript - Angular 2 需要表单组中的两项之一

javascript - JQuery 检查 ID 中的类..如果存在更改 img src 内容

javascript - Angular 通配符智能搜索

javascript - 代码不起作用(JavaScript)

javascript - 使用具有多个 y 轴的 D3 画笔