4

我制作了序列旭日形可视化,并希望为每条路径添加一个链接。

我阅读了类似的问题d3.js,单击链接到另一个用变量编码的 URL,并且可以根据特定路径的变量建立链接。(参见下面的代码)此代码可能会生成像“ http://somelink.com/link.php?id1=CurrentNode ”这样的 url 。

但是,我想使用层次结构信息生成诸如“ http://somelink.com/link.php?id1=CurrentNode&id2=ParentNode ”之类的url。

我对javascript不太了解,所以我需要帮助。

// Main function to draw and set up the visualization, once we have the data.
function createVisualization(json) {

  // Basic setup of page elements.
  initializeBreadcrumbTrail();
  drawLegend();
  d3.select("#togglelegend");

  // Bounding circle underneath the sunburst, to make it easier to detect
  // when the mouse leaves the parent g.
  vis.append("svg:circle")
      .attr("r", radius)
      .style("opacity", 0);

  // For efficiency, filter nodes to keep only those large enough to see.
  var nodes = partition.nodes(json)
      .filter(function(d) {
      return (d.dx > 0.005); // 0.005 radians = 0.29 degrees
      });

  var path = vis.data([json]).selectAll("path")
      .data(nodes)
      .enter().append("svg:path")
      .attr("display", function(d) { return d.depth ? null : "none"; })
      .attr("d", arc)
      .attr("fill-rule", "evenodd")
      .style("fill", function(d) { return colors[d.name]; })
      .style("opacity", 1)
      .on("mouseover", mouseover)
      .on("click", function(d) {
              var url = "http://somelink.com/link.php?id1=";
              url += d.name;
              $(location).attr('href', url);
              window.location = url;
            });

  // Add the mouseleave handler to the bounding circle.
  d3.select("#container").on("mouseleave", mouseleave);

  // Get total size of the tree = value of root node from partition.
  totalSize = path.node().__data__.value;
 };

我想出了如何为三个层次的阳光制作网址。为了获得基于朝阳水平的价值,我对 Lars 的建议进行了少许修改。

     .on("click", function(d) {
        var url = "http://somelink.com/link.php";
        if (d.depth== 1) {
         url += "?id1=" + d.name;
        }
        if (d.depth== 2) {
         url += "?id1=" + (d.parent ? d.parent.name : "");
         url += "&id2=" + d.name;
        }
        if (d.depth== 3) {
         url += "?id1=" + (d.parent.parent ? d.parent.parent.name : "");
         url += "&id2=" + (d.parent ? d.parent.name : "");
         url += "&id3=" + d.name;
        }
        $(location).attr('href', url);
        window.location = url;
    });
4

1 回答 1

5

旭日形布局的节点有一个.parent包含父节点的属性(或者对于根节点为 null)。您可以像这样在代码中直接使用它:

.on("click", function(d) {
          var url = "http://somelink.com/link.php?id1=" + d.name +
                      "?id2=" + (d.parent ? d.parent.name : "");
          $(location).attr('href', url);
          window.location = url;
 });
于 2014-04-30T10:20:15.463 回答