d3.js, click to link to another URL encoded with variables

前端 未结 1 1027
栀梦
栀梦 2021-01-01 06:50

I made a scatter plot, and want to add a link to each dot.

    chart.selectAll(\"scatter-dots\")
      .data(data)
      .enter().append(\"circle\")
                 


        
相关标签:
1条回答
  • 2021-01-01 07:38

    If it works with a static string then something is wrong with d.link_id. Try seeing what's inside either by doing alert(d.link_id) or if you use a firebug or similars do console.log(d.link_id).

    Alternatively, you should use real anchors for linking your nodes instead of setting click events. This would go something like...

    chart.selectAll("scatter-dots")
      .data(data)
      .enter()
      .append("a")
        .attr("xlink:href", function(d) {return "http://somelink.com/link.php?id=" + d.link_id})
        .append("circle")
          .attr("cx", function (d) { return x(d.position[0]); } )
          .attr("cy", function (d) { return y(d.position[1]); } )
          .attr("r", 4)
          .style("fill", "#666")
          .style("opacity", 0.5)
    

    (I can't seem to recall if this is the exact way of doing it, you might need to save the anchors in a variable and then append the circles to them.)

    0 讨论(0)
提交回复
热议问题