简体   繁体   中英

D3 - Labeling of x axis are links

I would like to make the labelling of the x axis clickable so that the text includes links.

Here is a fiddle of my code so far: http://jsfiddle.net/3gLfb401/

The x axis labelling consists of the date and a build number. The build number should be a link. In this example it should be enough if it links to google.com or something.

I already searched in the internet and found some things, that might do it but I don't know how to place it.

I thought something like this could work:

x.html(d3.time.format("%Y-%m-%d")(d.date) + '<a href= "http://google.com" target="_blank">' + "#" + d.buildNb + "</a>")   

This is the method which forms at the moment the text of the labelling:

function formatDataToTick(d) {
    return d3.time.format("%Y-%m-%d")(d.date) + " #" + d.buildNb;
}  

Try this.

 var xLabels = svg.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + height + ")")
    .call(xAxis)
    .selectAll("text")
    .style("text-anchor", "end")
    .attr("dx", "-.8em")
    .attr("dy", ".15em")
    .attr("transform", function(d) {
        return "rotate(-65)"
    });

 var insertTspans = function(a) {
    var b = a.split(" ");
    var textNode = d3.select(this); 
    textNode.text("");
    textNode.append("tspan").text(b[0]);
    var link = textNode.append("tspan").text(b[1]);
    link.style("cursor", "pointer")
      .style("fill","blue")
      .style("text-decoration","underline")
      .on("click", function(a) {
        document.location.href = "http://www.example.com/" + a;
      });
};

xLabels.each(insertTspans);

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