javascript - How to find a d3.js plot element by its connected data? -
i have d3.js plot made array of objects, such as:
svg.selectall(".circ").data(dataarr) // dataarr array of objects .enter().append("circle") .attr("cy", function (d) { return d.y }) .attr("cx", function (d) { return d.x }) .attr("r", elemsize / 2) .on("dblclick", function (d, i) { itemwasclicked(d,i); // function handle double click }
in mentioned itemwasclicked(d,i)
routine (where d
data of clicked element , i
index of in array dataarr
) need find actual svg element clicked , change color.
how find d3.js element data attached it? not want, unless necessary, use attaching/searching id element. if faster or there no way otherwise.
inside of event handler this
direct reference actioned element.
see selection.on
documentation:
the specified listener invoked in same manner other operator functions, being passed current datum
d
, indexi
,this
context current dom element.
so can simply:
.on("dblclick", function (d, i) { changecolorof(this); }
Comments
Post a Comment