问题
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