How can I get the id of a record in a d3 scatter plot?

﹥>﹥吖頭↗ 提交于 2020-01-06 07:42:09

问题


I have already attached my data and assigned the ids as follow:

svg.selectAll("circle").data(csv).enter().append("circle")
                    .attr("id", function(d){return "row"+d["ROW ID"];});

"ROW ID" is the name of the column that contains the ids of my data. Now I want to add an on-click event to the circles and save the ID of the currently point in a new variable to use it to call other function. Can anyone tell me how to get the "ROW ID" of the currently selected point?

thx


回答1:


Do you want something like this or have I misunderstood your question?

svg = d3.select("svg");

csv = [
  {"row ID": "id1",
   "x": 20,
   "y": 30},
  {"row ID": "id2",
   "x": 30,
   "y": 50}

]

svg.selectAll("circle").data(csv).enter().append("circle")
  .attr("id", function(d){return "row"+d["ROW ID"];})
  .attr("cx", function(d){return d.x})
  .attr("cy",function(d){return d.y})
  .attr("r",10)
  .style("fill", "black");

circles = d3.selectAll("circle");

circles.on("click",function(d) {
  alert(d["row ID"]);
})

Interactive version here: http://tributary.io/inlet/5455132



来源:https://stackoverflow.com/questions/16200935/how-can-i-get-the-id-of-a-record-in-a-d3-scatter-plot

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