【问题标题】:How to add url/link on d3 objects in Cal-heatmap如何在 Cal-heatmap 中的 d3 对象上添加 url/链接
【发布时间】:2015-10-02 10:07:23
【问题描述】:

作为 d3 和 js 的新手,或者只是一般的前端,我花了足够的时间来基本了解 Cal-heatmap 的工作原理。我有一个工作版本,但真的想在日历超链接上制作小框以获得额外的功能,阅读源代码对我来说是一场彻底的灾难。我想我应该按照here 的建议将.attr("xlink:href", url) 添加到某个对象,但我真的不知道在哪里。有没有人做过类似的事情?另外,我也不太明白我在初始化时指定的属性是如何传递给源代码的。任何帮助表示赞赏!

【问题讨论】:

    标签: d3.js cal-heatmap


    【解决方案1】:

    这需要一些技巧。

    绘制热图后,您可以:

    d3.selectAll('.graph-rect')
      .each(function(d){
        var a = d3.select( this.parentNode ) // get the parent of the rect (it's a g)
          .append("a") // append the link
          .attr("xlink:href","http://www.google.com")
          .attr("target", "_blank"); // set its link
        var rect = this;
        rect.remove(); // remove the rect
        a.insert(function(){return rect}); // and re-add it to the link
      });
    

    工作example

    【讨论】:

    • 嗨,我很好奇:你为什么要为insert 使用函数参数?
    • @CoolBlue,来自docsThe name may be specified either as a constant string or as a function that returns the DOM element to append.
    • 好吧,我可以发誓我以前在那个上下文中使用过裸 DOM 引用,但显然不是因为,是的......它失败了。给你。
    • 嘿@Mark,原来提供了ClickOn 功能.. 但这无疑提供了更多洞察力。总之谢谢!
    • @MegaMax,Do'h,假设您知道click 处理程序的易用性;会让这个答案变得更容易:)祝你未来好运d3ing!
    猜你喜欢
    • 2019-01-25
    • 2016-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2016-08-26
    • 2012-10-17
    相关资源
    最近更新 更多