js树形图:将新旧属性绑定到SVG元素

d3.js dendrogram : binding old and new properties to the svg element

本文关键字:绑定 属性 SVG 元素 新旧 js      更新时间:2023-09-26

我对d3的理解目前相当有限,我目前正在摆弄d3 .js的树形图示例。它可以在几个地方找到:例如:

当我实现它时,一切都很好,直到你尝试更新一个属性,比如节点的圆直径。如果我这样做(使用像AngularJS这样的交互式框架来观察参数的变化):节点的大小会发生变化。所以还没有问题。但是,如果我随后单击其中一个节点,则大小将重置为初始化大小,而不是新大小。

当一个节点被单击时,它遵循click函数:

 var nodeEnter = node.enter().append("g")
                        .attr("class", "dendrogramnode2")
                        .on("click", click);

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 update(source) {
  // Compute the new tree layout.
  var nodes = tree.nodes(root),
      links = tree.links(nodes);
  // Normalize for fixed-depth.
  nodes.forEach(function(d) { d.y = d.depth * 80; });
  // 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 "rotate(" + (d.x - 90) + ")translate(" + d.y + ")"; })
      .on("click", click);
  nodeEnter.append("circle")
      .attr("r", 1e-6)
      .style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
  nodeEnter.append("text")
      .attr("x", 10)
      .attr("dy", ".35em")
      .attr("text-anchor", "start")
      //.attr("transform", function(d) { return d.x < 180 ? "translate(0)" : "rotate(180)translate(-" + (d.name.length * 8.5)  + ")"; })
      .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 "rotate(" + (d.x - 90) + ")translate(" + d.y + ")"; })
  nodeUpdate.select("circle")
      .attr("r", 4.5)
      .style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
  nodeUpdate.select("text")
      .style("fill-opacity", 1)
      .attr("transform", function(d) { return d.x < 180 ? "translate(0)" : "rotate(180)translate(-" + (d.name.length + 50)  + ")"; });
  // TODO: appropriate transform
  var nodeExit = node.exit().transition()
      .duration(duration)
      //.attr("transform", function(d) { return "diagonal(" + 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;
  });
}

这个更新函数因此被称为:

  1. 初始化->没问题(例如:圆r = 5)

  2. 当一个参数得到更新->没有问题,参数更新。(例如:圆r = 10)

  3. 当你点击一个节点->问题->图形接受初始参数。(例如:圆r = 5)

所有这些可能有很大的范围和javascript如何处理数据绑定,但我不知道如何正确地做到这一点。我要么需要能够访问新的属性,而不是旧的。

有没有办法

  1. 调整代码以便采用新参数而不是旧参数?

  2. 将参数绑定到svg组作为对象(可能不那么有效?)所以我可以手动达到它从任何范围使用d3选择器?

我已经为DOM元素设置了一个属性,这些属性在每个数据中不不同。

            function dograph(p){
                //paramater object is passed and it's name is "p"
                d3.select(elementid).selectAll("svg")[0][0]["p"] = p
                //at some point update function gets called as part of an event and p is no longer available. 
                //someSvgElement
                        .on("mouseover"){update())}
                function update(source) {
                    var p = d3.select(elementid).selectAll("svg")[0][0]["p"];
                    //stuff happens
                }
            }

可能不是完美的解决方案,但它适合我。