Strategy 1: CSS pseudo element


The simplest strategy.

Let's explore how to use a CSS pseudo-class to modify only the graph item that is being hovered over.

Free
4 minutes read

What is a pseudo class

A CSS pseudo-class is a keyword added to a CSS selector that specifies a special state of the selected element(s). You can learn more about pseudo-classes in the MDN doc.

Essentially, this means you can assign a class to each shape in a graph and change its appearance when the user hovers over it.

Here is an example:

.scatterplotCircle {
  cursor: pointer;
  fill-opacity: .3;
  stroke-width: 2px;
}

.scatterplotCircle:hover {
  fill-opacity: 1;
  stroke-width: 1px;
}

Application on a scatterplot

Consider a scatterplot with multiple SVG circle elements, each assigned a .scatterplotCircle class. In the CSS file, you can set the fill-opacity to 0.3 using this class.

To change the appearance on hover, use the .scatterplotCircle:hover selector to increase the opacity to 1.

3540455055606570758085

Strategy 1: use a pseudo-class to change the appearance of the hovered marker

Pros & Cons

Pros

  • Easy to implement
  • Excellent performance (no JS computation, minimal redrawing)

Cons

  • Poor design: non-hovered circles remain prominent, so the highlight effect is weak
  • If the highlight information comes as a prop, another solution is needed

More examples

The examples below all use this strategy to implement their hover effect.

Picture of a heatmap showing the effect of vaccination, built with react and d3

Vaccination heatmap

Reproduction of a famous vaccination heatmap using d3 and react

Picture of a simple treemap

Basic treemap

Most simple treemap, with 1 level of hierarchy only

Exercices