How to make labels and nodes in D3 forced layout clickable to navigate to a URL?

£可爱£侵袭症+ 提交于 2019-12-01 06:51:19

Generally speaking, you can add click events to SVG elements in D3.js using

.on('click', function(d, i) {
  window.location.href = d.url;
})

d is the data object and the i is in the index of d within the collection.

Just add that click handler to your text node as well as your node (path) node like in this fiddle http://jsfiddle.net/jNyrf/

Lars Kotthoff

You have two options here.

  • You can use the .on("click", ...) handler to set the current page to the target.
  • You can use an a element with .attr("xlink:href", url) that contains the element acting as a hyperlink to set the link in a more traditional way.

More information in this question/answer, although I believe you don't have to import the xlink namespace explicitly, at least not in the latest version of D3.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!