【问题标题】:D3 - Labeling of x axis are linksD3 - x 轴的标签是链接
【发布时间】:2014-11-06 20:33:01
【问题描述】:

我想让 x 轴的标签可点击,以便文本包含链接。

到目前为止,这是我的代码:http://jsfiddle.net/3gLfb401/

x 轴标签由日期和版本号组成。内部版本号应该是一个链接。 在这个例子中,如果它链接到 google.com 或其他东西就足够了。

我已经在互联网上搜索并找到了一些东西,可能会这样做,但我不知道如何放置它。

我认为这样的事情可以工作:

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

这是目前形成标签文本的方法:

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

【问题讨论】:

标签: javascript d3.js hyperlink


【解决方案1】:

试试这个。

 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    • 2013-08-20
    • 1970-01-01
    • 1970-01-01
    • 2021-05-01
    • 2021-10-11
    • 1970-01-01
    相关资源
    最近更新 更多