D3.js, click on the link to another URL encoded with variables

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

    chart.selectAll("scatter-dots")
      .data(data)
      .enter().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)
    .on("click", function(){
        var url = "http://somelink.com/link.php?id=";
        url += d.link_id;
        //$(location).attr('href', url);
        //window.location = url;    
    });

This works if I just put a clean string link, for example window.location = "http://stackoverflow.com" However, if I add requests to the end of the URL from a variable, the page is not redirected.

Neither jquery nor javascript worked (as commented out.)

I also tried an external js file, still failed.

This is in the PHP file if that helps.

+2
source share
1 answer

, - d.link_id. , , alert(d.link_id), firebug similars do console.log(d.link_id).

click. ...

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)

( , , , .)

+5

Source: https://habr.com/ru/post/1538709/


All Articles