简体   繁体   中英

d3js: `xlink:href` is not working

I'm trying to implement href to d3js chart as explained in this post and here . I added one simple href to this d3js bar chart but it's not working. Here is my code:
I imported xlink in html :

<myatr xmlns:xlink="http://www.w3.org/1999/xlink">

</myatr>

and this is my part of d3js code,

svg.selectAll(".bar")
      .data(data)
      .enter().append("rect")
      .attr("class", "bar")
      .attr("x", function(d) { return x(d.letter); })
      .attr("width", x.rangeBand())
      .attr("y", function(d) { return y(d.frequency); })
      .attr("height", function(d) { return height - y(d.frequency); })
      .attr("xlink:href",function(d){return "www.google.com";});

It looks like you need to append the a tag too. Try this:

svg.selectAll(".bar")
      .data(data)
    .enter().append("a")
      .attr("xlink:href", "www.google.com")
    .append("rect")
      .attr("class", "bar")
      .attr("x", function(d) { return x(d.letter); })
      .attr("width", x.rangeBand())
      .attr("y", function(d) { return y(d.frequency); })
      .attr("height", function(d) { return height - y(d.frequency); })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM