Unable to get click event in D3 JavaScript library

倾然丶 夕夏残阳落幕 提交于 2019-11-30 18:58:13

Try this:

var circle = svg.append("svg:g").selectAll("circle")
  .data(force.nodes())
  .enter().append("svg:circle")
  .attr("r", 6)
  .on("click", function(d,i) { alert("Hello world"); })
  .call(force.drag);
Matthew Daiter

Try out this, if you want the node contained within the circle (let's say that your nodes are mapping an object with a key called anger and a value 34:

var circle = svg.append("svg:g").selectAll("circle")
.data(force.nodes())
.enter().append("svg:circle")
.attr("r", 6)
.on("click", function(d,i) { alert(d.anger); }) // this will alert 34
.call(force.drag);

Or try this for the attributes of the svg (getting the radius of the svg, for example):

var circle = svg.append("svg:g").selectAll("circle")
.data(force.nodes())
.enter().append("svg:circle")
.attr("r", 6)
.on("click", function(d,i) { alert(d3.select(this).r; }) // this will print out the radius })
.call(force.drag);

Sorry if my post is like the one above, but I thought the clarification could be useful.

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