Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在D3.js中添加多个svg:line_Svg_D3.js - Fatal编程技术网

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

在D3.js中添加多个svg:line,svg,d3.js,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();

我试图增强一个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");
这是我的非工作代码:

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;
    }
  }
问候,,
安东

看起来您是从树布局开始的,所以我不确定第二组线条是否适合。原则上,您可以用与第一行完全相同的方式附加第二组行--您可以向我们展示不起作用的代码吗?嗨,Lars,我在我的帖子中添加了不起作用的代码。应该可以。运行此操作时会出现什么错误?获取一个空页面。没有错误。只有在调试器模式下,我才能得到javascript没有成功加载的信息。为什么它没有成功加载?