为什么d3js exit()。remove()不起作用?

时间:2015-06-18 07:49:32

标签: d3.js

根据我的理解,我是d3js的新手。

// links is an array and id is unique key
var pathData = paths.data(links, function(d) {return d.id;});

pathData.enter().append('').attr() ...

// in there will be delete all the duplicate items
pathData.exit().remove();

但是在我的代码中,它重新创建了一个节点。

这是我的完整代码http://jsfiddle.net/yunfanyunfan/URpfB/34/

单击该行,将出现多行,因为重复节点不会删除。

2 个答案:

答案 0 :(得分:4)

  1. 您需要路径选择的实时副本。您的原始代码只有初始快照。
  2. .attr('d', 需要在UPDATE + ENTER选项上,因为输入 由于您要重新使用现有路径,因此选择将为零 每一次。
  3. 
    
    // set up SVG for D3
    var width  = 960,
        height = 800;
    
    var isWantToAddNode = false;
    var svg = d3.select('body').select('svg')
        .attr('width', width)
        .attr('height', height);
    var links = [{
        id:1,
        fromPoint: {x:5,y:5},
        toPoint: {x:50,y:50},
        params: 'android',
        color: '#000'
    }];
    var times = 0;
    function initAllDef() {
        svg.select('#defs').append('svg:marker')
            .attr('id', 'arrow').attr("viewBox", "0 -5 10 10")
            .attr("refX", 9)
            .attr("refY", 0)
            .attr("markerWidth", 6)
            .attr("markerHeight", 6)
            .attr("orient", "auto")
            .append("path")
            .attr("d", "M0,-3L10,0L0,3")
            .attr('fill', '#000');
    }
    
    initAllDef();
    
    // handles to link and node element groups
    var paths = svg.select('#paths').selectAll('path');
    var routeNodes = svg.select('#rects').selectAll('rects');
    
    
    // update graph (called when needed)
    function restart() {
        function rebuildLink() {
            //need a live selection
            var pathData = svg.select('#paths').selectAll('path').data(links, function(d){
                return d.id;
            });
            pathData.enter()
                .append('svg:path')
                .attr('class', 'link')
                .style('marker-end', 'url(#arrow)')
                .on('mousedown', function(d){
                    times += 1;
                    restart();
                });
            
            //path needs to be on update+enter
            pathData.attr('d', function(d) {
                    var f = d.fromPoint, t = d.toPoint;
                    return 'M' + (f.x + times * 10) + ',' + (f.y) + 'L' + t.x + ',' + t.y;
                });
    
            pathData.exit().remove();
        }
    
        rebuildLink();
    }
    
    function getStartPoint(rect) {
        return {
            x: rect.x + (rect.width>>1),
            y: rect.y + rect.height
        }
    }
    
    function getEndPoint(rect) {
        return {
            x: rect.x + (rect.width>>1),
            y: rect.y
        }
    }
    
    
    restart();
    
    svg {
        background-color: #FFF;
        /*cursor: default;*/
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        -o-user-select: none;
        user-select: none;
    }
    
    svg:not(.active):not(.ctrl) {
        cursor: crosshair;
    }
    
    path.link {
        fill: none;
        stroke: black;
        stroke-width: 2px;
        cursor: default;
    }
    
    svg:not(.active):not(.ctrl) path.link {
        cursor: pointer;
    }
    
    path.link.selected {
        stroke-dasharray: 10,2;
        color: blue;
        stroke: blue;
    }
    
    path.link.dragline {
        pointer-events: none;
    }
    
    path.link.hidden {
        stroke-width: 0;
    }
    
    circle.node {
        stroke-width: 1.5px;
        cursor: pointer;
    }
    
    circle.node.reflexive {
        stroke: #000 !important;
        stroke-width: 2.5px;
    }
    
    rect.route-node {
        cursor: pointer;
        stroke-width: 1px;
    }
    
    text {
        font: 12px sans-serif;
        pointer-events: none;
    }
    
    text.id {
        text-anchor: middle;
        font-weight: bold;
    }
    
    input {
        width: 300px;
    }
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
    <body>
        <svg>
            <defs id="defs"></defs>
            <g id="paths"></g>
            <g id="rects"></g>
        </svg>        
    	</body>
    &#13;
    &#13;
    &#13;

答案 1 :(得分:2)

在您的代码中,您尝试删除已创建的行。

//But this is wrong way to delete.
pathData.exit().remove();
//this is the right way
svg.selectAll('path').data(pathData).exit().remove();

检查更新的代码: http://jsfiddle.net/cyril123/ktvkuhta/2/

这应该会让您在进入和退出时获得非常好的帮助:http://bost.ocks.org/mike/circles/