Using d3.v3.min.js and the example D3 Zoomable Plot as reference I built a scatter plot. The most relevant bits are:
var svg = d3.select("#scatter1")
.append("svg")
.attr("width", outerWidth)
.attr("height", outerHeight)
.attr("viewBox", "0 0 " + (width + margin.left + margin.right) + " " + (height + margin.top + margin.bottom))
.attr("preserveAspectRatio", "xMidYMid meet")
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
var objects = svg.append("svg")
.classed("objects", true)
.attr("width", width)
.attr("height", height);
objects.selectAll(".dot")
.data(values)
.enter().append("circle")
.classed("dot", true)
.attr("r", function (d) {
return 6 * Math.sqrt(2.0 / Math.PI);
})
.attr("transform", transform)
.style("fill", colorVal);
and some needed css:
.dot {
fill-opacity: .5;
}
.dotHidden {
fill-opacity: .1;
}
My data values are defined as:
var values = [[x0, y0], [x1, y1], ....];
with a custom property idx
per element i.e.
values[i].idx === i
Now I'd like to highlight only a subset of the dot elements, namely only those that are in an indexes list e.g. selectedIdx = [4, 8, 10, 38, 90]
. How can I do that in the fastest possible way? is there an d3 js idiomatic way to do it? I have attempted to do this below and it does the job to "hide" all the dots but I actually want to hide only the ones not in the selectedIdx
.
d3.select("#scatter1 svg")
.selectAll("circle")
.attr("class", "dotHidden");
UPDATE: This is a follow up of the question How to get the index of the data element in a histogram on mouseover?