在D3.js中添加多个svg:行

时间:2013-11-20 11:42:21

标签: svg d3.js

我尝试增强D3.js示例,并在向svg:g容器添加多行时遇到一些问题。一行工作正常,但添加第二行与空网站。

这是我的工作代码:

function update(source) {
    var duration = d3.event && d3.event.altKey ? 5000 : 500;

    // Compute the new tree layout.
    var nodes = tree.nodes(root).reverse();

    // Normalize for fixed-depth.
    nodes.forEach(function(d) { d.y = d.depth * 280; });

    // Update the nodes…
    var node = vis.selectAll("g.node")
        .data(nodes, function(d) { return d.id || (d.id = ++i); });

    // Enter any new nodes at the parents previous position.
    var nodeEnter = node.enter().append("svg:g")
        .attr("class", "node")
        .attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; })
        .on("click", function(d) { toggle(d); update(d); });

    nodeEnter.append("svg:rect")
        .attr("x", -100)
        .attr("y", -40)
        .attr("rx", 6)
        .attr("ry", 6)
        .attr("width", 200)
        .attr("height", 80)
        .style("fill-opacity", 1)
        .style("fill", function(d) { return d._children ? "#FF0033" : "#fff"; });

     nodeEnter.append("svg:line")
        .attr("x1",-100)
        .attr("y1",-25)
        .attr("x2",100)
        .attr("y2",-25)
        .attr("stroke-width","2px")
        .attr("stroke","#f03");

这是我的工作代码:

function update(source) {
    var duration = d3.event && d3.event.altKey ? 5000 : 500;

    // Compute the new tree layout.
    var nodes = tree.nodes(root).reverse();

    // Normalize for fixed-depth.
    nodes.forEach(function(d) { d.y = d.depth * 280; });

    // Update the nodes…
    var node = vis.selectAll("g.node")
        .data(nodes, function(d) { return d.id || (d.id = ++i); });

    // Enter any new nodes at the parents previous position.
    var nodeEnter = node.enter().append("svg:g")
        .attr("class", "node")
        .attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; })
        .on("click", function(d) { toggle(d); update(d); });

    nodeEnter.append("svg:rect")
        .attr("x", -100)
        .attr("y", -40)
        .attr("rx", 6)
        .attr("ry", 6)
        .attr("width", 200)
        .attr("height", 80)
        .style("fill-opacity", 1)
        .style("fill", function(d) { return d._children ? "#FF0033" : "#fff"; });

     nodeEnter.append("svg:line")
        .attr("x1",-100)
        .attr("y1",-25)
        .attr("x2",100)
        .attr("y2",-25)
        .attr("stroke-width","2px")
        .attr("stroke","#f03");

     nodeEnter.append("svg:line")
        .attr("x1",-100)
        .attr("y1",-40)
        .attr("x2",100)
        .attr("y2",-40)
        .attr("stroke-width","2px")
        .attr("stroke","#f03");

如何添加第二个SVG:LINE对象?

以下是我编码的其余部分:

    // Transition nodes to their new position.
    var nodeUpdate = node.transition()
        .duration(duration)
        .attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; });

    nodeUpdate.selectAll("rect")
        .style("fill", function(d) { return d._children ? "#FF0033" : "#fff"; });

    nodeUpdate.selectAll("text")
        .style("fill-opacity", 1);

    // Transition exiting nodes to the parents new position.
    var nodeExit = node.exit().transition()
        .duration(duration)
        .attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; })
        .remove();

    nodeExit.selectAll("text")
        .style("fill-opacity", 1e-6);

    // Update the links…
    var link = vis.selectAll("path.link")
        .data(tree.links(nodes), function(d) { return d.target.id; });

    // Enter any new links at the parents previous position.
    link.enter().insert("svg:path", "g")
        .attr("class", "link")
        .attr("d", function(d) {
          var o = {x: source.x0, y: source.y0};
          return diagonal({source: o, target: o});
        })
        .transition()
        .duration(duration)
        .attr("d", diagonal);

    // Transition links to their new position.
    link.transition()
        .duration(duration)
        .attr("d", diagonal);

    // Transition exiting nodes to the parents new position.
    link.exit().transition()
        .duration(duration)
        .attr("d", function(d) {
          var o = {x: source.x, y: source.y};
          return diagonal({source: o, target: o});
        })
        .remove();

    // Stash the old positions for transition.
    nodes.forEach(function(d) {
      d.x0 = d.x;
      d.y0 = d.y;
    });
  }

  // Toggle children.
  function toggle(d) {
    if (d.children) {
      d._children = d.children;
      d.children = null;
    } else {
      d.children = d._children;
      d._children = null;
    }
  }

此致 安东

0 个答案:

没有答案