Congrats on getting it working this far!
Maybe it's time to take a step back. The overall desired feature is:
"When I hover over the bar chart, the corresponding data points on my scatter plot will automatically be highlighted too."
Based on this I'm wondering, why is it necessary to assign unique classes for each bin? It sounds like you wanted to query based on the class, then change the style of the resulting elements depending on whether or not they match the highlighted bin. This is, however, more natural to do at the level of the data itself. Particularly, if you have a property called "bin" on each of the data elements for the bar chart and the scatter plot, they can be easily compared.
Maybe it might work better to try assigning a class that fades elements into the background, maybe call it "hidden". With this approach, you can assign this class to all marks that correspond to bins other than the highlighted (hovered) bin. That might look something like this:
// This function gets called on hover, passing the hovered bin value.
function updateHighlightedBin(bin) {
// Update the highlighting on the bars of the bar chart.
svg.selectAll("rect").classed("hidden", function(d) {
return d.bin !== bin;
});
// Update the highlighting on the circles of the scatter plot.
svg.selectAll("circle").classed("hidden", function(d) {
return d.bin !== bin;
});
}
.hidden {
fill: #ccc !important;
}
The above approach has the disadvantage that the highlighted marks may not appear in front of non-highlighted marks that overlap. To solve this case, another approach would be to have two SVG groups (<g> elements) on top of one another, one that renders all the marks with the "hidden" style (or faded to the background with a semi-transparent overlay rectangle), and another (on top) that only renders the highlighted mark(s). An example of this approach can be seen in a Block I made
"Horizontal Stacked Bars" (hover over the color legend to see it in action).
Hope this is helpful. Good luck!
All the best,
Curran