gpt4 book ai didi

d3.js - 为什么 d3js exit().remove() 不起作用?

转载 作者:行者123 更新时间:2023-12-04 22:59:16 25 4
gpt4 key购买 nike

据我所知,我是 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/

单击该行,会出现很多行,因为重复节点没有删除。

最佳答案

  • 您需要路径选择的实时副本。您的原始代码只有初始快照。
  • .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>

    关于d3.js - 为什么 d3js exit().remove() 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30909472/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com