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

yunfan

以我的理解,我是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/

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

酷蓝
  1. 您需要路径选择的实时副本。您的原始代码仅具有初始快照。
  2. .attr('d',需求是在UPDATE + Enter选择,因为进入的选择将是零大小,因为你是重新使用现有的路径各一次。

// 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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么我的 css 在 d3js 上不起作用

exit()。remove()在我的D3.js强制布局图中不起作用

d3.js-.exit()。remove()组元素不起作用

为什么remove(x)不起作用?

简单的D3js代码不起作用

为什么我的 exit() 命令在 python 中不起作用?

JavaScript:为什么 classList.remove 不起作用?

使用 JQuery clone() 时,为什么 remove() 不起作用

为什么 onclick javascript remove 不起作用?

为什么我的d3.tip不起作用?

为什么.each()在D3中不起作用?

为什么D3 Drag不起作用?

d3.js自定义布局exit()不起作用

D3js更新直方图元素不起作用(常规更新模式)

为什么monitor.exit在其他工作中不起作用?

为什么不起作用?

JS30 DRUMKIT classList.remove 不起作用

为什么重置过滤器在 d3 js 强制有向图中不起作用?

为什么在ebean(ORM)模型对象的HashSet上的iterator.remove()不起作用?

为什么range(0,10).remove(1)不起作用?

为什么我的ArrayList.remove(id)调用不起作用?

为什么remove_reference在函数上不起作用?

为什么 remove_meta_box() 函数在我的插件中不起作用?

为什么 list.remove() 在这个 for 循环中不起作用?

React.js:为什么删除按钮不起作用?

为什么“打印”在node.js中不起作用?

为什么这个选择器不起作用?JS

ScrollIntoView()不起作用-香草JS,为什么?

Phaser.js为什么重叠不起作用?