如何获取d3散点图中的记录ID? [英] How can I get the id of a record in a d3 scatter plot?

查看:121
本文介绍了如何获取d3散点图中的记录ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经附加了数据并分配了ID,如下所示:

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"是包含我的数据ID的列的名称. 现在,我想向圆上添加一个单击事件,并将当前点的ID保存在新变量中,以使用它来调用其他函数. 谁能告诉我如何获取当前选定点的行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

推荐答案

您想要这样的东西还是我误解了您的问题?

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"]);
})

此处为互动版本: http://tributary.io/inlet/5455132

这篇关于如何获取d3散点图中的记录ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆