d3.js,单击动作到另一个带有sunburst变量数组的URL编码

时间:2014-04-30 10:12:09

标签: javascript php jquery d3.js sunburst-diagram

我发了sequences sunburst visualization,想要添加每个路径的链接。

我读了类似的问题d3.js, click to link to another URL encoded with variables,并且可以根据特定路径的变量建立链接。 (见下面的代码) 此代码可能会生成网址,如“http://somelink.com/link.php?id1=CurrentNode”。

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

我对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;
    });

1 个答案:

答案 0 :(得分:5)

sunburst布局的节点具有包含父节点的属性.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;
 });