Skip to content Skip to sidebar Skip to footer

D3.js, Click To Link To Another Url Encoded With Variables

I made a scatter plot, and want to add a link to each dot. chart.selectAll('scatter-dots') .data(data) .enter().append('circle') .attr('cx', function (d) {

Solution 1:

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) { returnx(d.position[0]); } )
      .attr("cy", function (d) { returny(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.)

Post a Comment for "D3.js, Click To Link To Another Url Encoded With Variables"