简体   繁体   English

d3过渡单击节点

[英]d3 transition click on node

I have just started learning d3 and I am trying to make a transition of appearing text on a mouse click event on tree nodes. 我刚刚开始学习d3,并且尝试在树节点上的鼠标单击事件上转换显示文本的方式。 The nodeLayout is the production of the d3.layout.tree(). nodeLayout是d3.layout.tree()的产物。

        var node = svg.selectAll("g.classNode")
            .data(nodeLayout.filter(function(d){return d.depth < 2;}));                     


        var nodeEnter = node
            .enter()
            .append("g")
            .attr("transform", function(d) { 
                return "translate(" + d.x  + "," + d.y + ")";
            })
            .on("mouseover",mouseover)
            .on("mouseout",mouseout)
            .on("click",mouseclick);

And the mouseclick function is 而mouseclick功能是

function mouseclick(d) {

    d3.select(this).append("text")
        .transition()
        .duration(2000)
        .attr("x",100)
        .attr("y",30)
        .attr("font-family", "sans-serif")
        .attr("font-size", "16px")
        .text(function(d){if(d.depth==1)return Hello;});}

The .duration is not working but the .delay is. .duration不起作用,但.delay有效。 Anybody knows why? 有人知道为什么吗? Thank you very much. 非常感谢你。

If you want to show the text gradually try to set the text attributes first and change the opacity only in the transition. 如果要逐渐显示文本,请尝试先设置文本属性,然后仅在过渡中更改不透明度。 Something like this: 像这样:

function mouseclick(d) {

d3.select(this).append("text")
    .attr("x",100)
    .attr("y",30)
    .attr("font-family", "sans-serif")
    .attr("font-size", "16px")
    .text(function(d){if(d.depth==1)return Hello;})
    .style("opacity", 1e-6)
    .transition()
       .duration(2000)
       .style("opacity", 1)

;} ;}

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

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