D3.js - 单击 TreeLayout 中的 link 连接节点时添加按钮

D3.js - add a button on click of a link connecting nodes in a TreeLayout

我正在寻找一种方法来将按钮添加到 D3 中 TreeLayout 中的 link 连接节点。 单击此按钮后,我必须在其下方添加另一个 rhombus/rect 节点。下图只是一个 UX 可视化。该示例可以是 D3 中的简单树布局。

示例 D3 树布局:

var jsonData = getData();

var margin = {
    top: 20,
    right: 120,
    bottom: 20,
    left: 120
  },
  width = 960 - margin.right - margin.left,
  height = 500 - margin.top - margin.bottom;

var i = 0,
  duration = 750,
  root;

var tree = d3.layout.tree()
  .size([height, width]);

var diagonal = d3.svg.diagonal()
  .projection(function(d) {
    return [d.y, d.x];
  });

var svg = d3.select("body").append("svg")
  .attr("width", width + margin.right + margin.left)
  .attr("height", height + margin.top + margin.bottom)
  .append("g")
  .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

root = jsonData;
root.x0 = height / 2;
root.y0 = 0;

update(root);

//svg.style("height", "500px");

function update(source) {

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

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

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

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

  nodeEnter.append("circle")
    .attr("r", 1e-6)
    .attr('stroke', function(d) {
      return d.color ? d.color : 'blue';
    })
    .style("fill", function(d) {
      return d._children ? "#ccc" : "#fff";
    });

  nodeEnter.append("text")
    .attr("x", function(d) {
      return d.children || d._children ? -13 : 13;
    })
    .attr("dy", ".35em")
    .attr("text-anchor", function(d) {
      return d.children || d._children ? "end" : "start";
    })
    .text(function(d) {
      return d.name;
    })
    .style("fill-opacity", 1e-6);

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

  nodeUpdate.select("circle")
    .attr("r", 10)
    .style("fill", function(d) {
      var collapseColor = d.color ? d.color : '#ccc';
      return d._children ? collapseColor : "#fff";
    });

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

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

  nodeExit.select("circle")
    .attr("r", 1e-6);

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

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

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

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

  // Transition exiting nodes to the parent's 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 on click.
function click(d) {
  if (d.children) {
    d._children = d.children;
    d.children = null;
  } else {
    d.children = d._children;
    d._children = null;
  }
  update(d);
}


function getData() {
  return {
    "name": "js",
    "parent": "null",
    "color": "green",
    "children": [{
        "name": "frameworks",
        "parent": "js",
        "color": "red",
        "children": [{
            "name": "Angular",
            "parent": "frameworks",
            "color": "red",
            "size": 75
          },
          {
            "name": "Backbone",
            "parent": "frameworks",
            "color": "red",
            "size": 15
          },
          {
            "name": "Ember",
            "parent": "frameworks",
            "color": "red",
            "size": 5
          },
          {
            "name": "Aurelia",
            "parent": "frameworks",
            "color": "red",
            "size": 5
          }
        ]
      },
      {
        "name": "libraries",
        "parent": "js",
        "color": "blue",
        "children": [{
            "name": "jQuery",
            "parent": "libraries",
            "size": 70
          },
          {
            "name": "YUI",
            "parent": "libraries",
            "size": 30
          },
          {
            "name": "Dojo",
            "parent": "libraries",
            "size": 10
          },
          {
            "name": "Prototype",
            "parent": "libraries",
            "size": 15
          },
          {
            "name": "MooTools",
            "parent": "libraries",
            "size": 5
          },
          {
            "name": "ExtJS",
            "parent": "libraries",
            "size": 5
          }
        ]
      }
    ]
  };
};
.node {
  cursor: pointer;
}

.node circle {
  fill: #fff;
  stroke-width: 3px;
}

.node text {
  font: 12px sans-serif;
}

.link {
  fill: none;
  stroke: #ccc;
  stroke-width: 2px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"></script>
<div class="tree-diagram"></div>

当用户点击(或悬停?)任何 link 时,应显示“+”按钮。我想出了画一个 + 的 SVG 路径。

<svg height="400" width="450">
  <path d= "M0 6 H12 M6 0 V12" stroke="blue" stroke-width="1.5" />
</svg>

但我仍然坚持要将其转换为按钮并将其附加到 link 并使其可点击。

我将按钮添加到您的示例中,但还没有让它执行任何操作。只是,只要您将鼠标悬停在 link 上,按钮就会显示在 link 的中间,当您单击它时,它会记录到控制台。

现在,您将来可能需要一些东西:

    link的
  1. Increase the hitbox使用起来更方便;
  2. 知道点击了哪个 link,可以通过写入全局变量 clickedLink,或者通过 re-assigning on('click', ...) inside link的on('mouseenter');
  3. 通过添加新节点并在整个数据集(简单但计算量大)或源节点及其子节点上调用 update 来更改数据对象。

var jsonData = getData();

var margin = {
    top: 20,
    right: 120,
    bottom: 20,
    left: 120
  },
  width = 960 - margin.right - margin.left,
  height = 500 - margin.top - margin.bottom;

var i = 0,
  duration = 750,
  root;

var tree = d3.layout.tree()
  .size([height, width]);

var diagonal = d3.svg.diagonal()
  .projection(function(d) {
    return [d.y, d.x];
  });

var svg = d3.select("body").append("svg")
  .attr("width", width + margin.right + margin.left)
  .attr("height", height + margin.top + margin.bottom)
  .append("g")
  .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

// New part
var plusButton = svg
  .append('g')
  .classed('button', true)
  .classed('hide', true)
  .on('click', function() {
    console.log("CLICKED");
  });

plusButton
  .append('rect')
  .attr('transform', 'translate(-8, -8)') // center the button inside the `g`
  .attr('width', 16)
  .attr('height', 16)
  .attr('rx', 2);

plusButton
  .append('path')
  .attr('d', 'M-6 0 H6 M0 -6 V6');

root = jsonData;
root.x0 = height / 2;
root.y0 = 0;

update(root);

//svg.style("height", "500px");

function update(source) {

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

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

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

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

  nodeEnter.append("circle")
    .attr("r", 1e-6)
    .attr('stroke', function(d) {
      return d.color ? d.color : 'blue';
    })
    .style("fill", function(d) {
      return d._children ? "#ccc" : "#fff";
    });

  nodeEnter.append("text")
    .attr("x", function(d) {
      return d.children || d._children ? -13 : 13;
    })
    .attr("dy", ".35em")
    .attr("text-anchor", function(d) {
      return d.children || d._children ? "end" : "start";
    })
    .text(function(d) {
      return d.name;
    })
    .style("fill-opacity", 1e-6);

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

  nodeUpdate.select("circle")
    .attr("r", 10)
    .style("fill", function(d) {
      var collapseColor = d.color ? d.color : '#ccc';
      return d._children ? collapseColor : "#fff";
    });

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

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

  nodeExit.select("circle")
    .attr("r", 1e-6);

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

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

  // Enter any new links at the parent's previous position.
  link.enter().insert("path", "g")
    .attr("class", "link")
    .attr("d", function(d) {
      var o = {
        x: source.x0,
        y: source.y0
      };
      return diagonal({
        source: o,
        target: o
      });
    })
    // New part
    .on('mouseenter', function(d, i) {
      // Use the native SVG interface to get the bounding box to
      // calculate the center of the path
      var bbox = this.getBBox();
      var x = bbox.x + bbox.width/2,
        y = bbox.y + bbox.height/2;
      plusButton
        .attr('transform', 'translate(' + x + ', ' + y + ')')
        .classed('hide', false);
    })
    .on('mouseleave', function(d, i) {
      plusButton
        .classed('hide', true);
    });

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

  // Transition exiting nodes to the parent's 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 on click.
function click(d) {
  if (d.children) {
    d._children = d.children;
    d.children = null;
  } else {
    d.children = d._children;
    d._children = null;
  }
  update(d);
}


function getData() {
  return {
    "name": "js",
    "parent": "null",
    "color": "green",
    "children": [{
        "name": "frameworks",
        "parent": "js",
        "color": "red",
        "children": [{
            "name": "Angular",
            "parent": "frameworks",
            "color": "red",
            "size": 75
          },
          {
            "name": "Backbone",
            "parent": "frameworks",
            "color": "red",
            "size": 15
          },
          {
            "name": "Ember",
            "parent": "frameworks",
            "color": "red",
            "size": 5
          },
          {
            "name": "Aurelia",
            "parent": "frameworks",
            "color": "red",
            "size": 5
          }
        ]
      },
      {
        "name": "libraries",
        "parent": "js",
        "color": "blue",
        "children": [{
            "name": "jQuery",
            "parent": "libraries",
            "size": 70
          },
          {
            "name": "YUI",
            "parent": "libraries",
            "size": 30
          },
          {
            "name": "Dojo",
            "parent": "libraries",
            "size": 10
          },
          {
            "name": "Prototype",
            "parent": "libraries",
            "size": 15
          },
          {
            "name": "MooTools",
            "parent": "libraries",
            "size": 5
          },
          {
            "name": "ExtJS",
            "parent": "libraries",
            "size": 5
          }
        ]
      }
    ]
  };
};
.node {
  cursor: pointer;
}

.node circle {
  fill: #fff;
  stroke-width: 3px;
}

.node text {
  font: 12px sans-serif;
}

.link {
  fill: none;
  stroke: #ccc;
  stroke-width: 2px;
}


/* New part */
.button > path {
  stroke: blue;
  stroke-width: 1.5;
}

.button > rect {
  fill: #ddd;
  stroke: grey;
  stroke-width: 1px;
}


.hide {
  opacity: 0 !important;
  pointer-events: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"></script>
<div class="tree-diagram"></div>