最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - d3.js, click to link to another URL encoded with variables - Stack Overflow

programmeradmin5浏览0评论

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) { 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 = ".php?id=";
        url += d.link_id;
        //$(location).attr('href', url);
        //window.location = url;    
    });

It works if I just put the pure String link such as window.location = "" However, if I add queries to the end of the URL from a variable, the page does not redirected.

Neither jquery nor javascript worked (as mented.)

I also tried an external js file, still fail.

This is in a PHP file, if this helps.

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) { 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./link.php?id=";
        url += d.link_id;
        //$(location).attr('href', url);
        //window.location = url;    
    });

It works if I just put the pure String link such as window.location = "http://stackoverflow." However, if I add queries to the end of the URL from a variable, the page does not redirected.

Neither jquery nor javascript worked (as mented.)

I also tried an external js file, still fail.

This is in a PHP file, if this helps.

Share Improve this question asked May 13, 2012 at 3:57 clerksxclerksx 6404 gold badges13 silver badges21 bronze badges 1
  • simply solved. change to on("click", function(d).... – clerksx Commented May 13, 2012 at 12:31
Add a ment  | 

1 Answer 1

Reset to default 9

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./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)

  1. 暂无评论