Sitemap

React + d3 = ?

5 min readFeb 27, 2020

--

Both React and d3 are big names in web development and have proven to be among the best frontend libraries to use when creating a web app. React is one of the most popular javascript libraries for building user interfaces, while d3 is one of the most appreciated front-end libraries for creating stunning data driven visualisations. However, working with both of them at the same time isn’t straight forward and can easily get confusing.

I recently had to build a bubble chart and integrate it into an existing React application. As I had some prior experience with d3, I went on using d3 and React for the job.

The problem

One of my first questions was: Who will manipulate the DOM? Who will manage the app state?

https://macwright.org/2016/10/11/d3-and-react.html
https://macwright.org/2016/10/11/d3-and-react.html

Since both libraries do a great job at manipulating the DOM, when building a React+d3 component, there needs to be a tradeoff:

You can use React as a simple wrapper that instantiates some standalone d3 visualisations, while d3 does most of the work:

Another approach is to give React the ownership of the DOM and rely on d3 for the heavy math stuff:

There are plenty of articles on this topic (some older, some newer) and, while the opinions are divided, I got the impression that letting React handle rendering would be a better approach.

There are even great collections of components written this way, and my favourite is by far https://vx-demo.now.sh.

Letting React handle the DOM

So I started to build my bubble chart component as a react component that renders some SVG:

That is pretty straight forward: there’s a React component that’s rendering an SVG with multiple circles, and with React, you can easily manage the style, position and hover/clicked state of each circle.

The thing with this bubble chart was that I had no information on how to plot the bubbles. There were no specs on how to map each circle on a fixed cartesian point (x, y).
So I needed an algorithm to place the bubbles on the screen in a meaningful way in order to make some sense out of the visualisation.

This is where d3’s force layout kicked in:

What I did was to setup a d3 force simulation that would give each circle a unique position.

If you’re not familiar with it, the simulation is ultimately an event emitter that emits a tick event with the updated positions of the nodes. After it’s setup, you can just listen to that event and update all the rendered bubbles.

I won’t go into depth on this functionality, but will just note that for me, this was one of the selling points of using d3 in the first place, since you can do awesome animations using this module, such as centring items, adding gravity, managing collision and many others.

Get gradinariu bogdan’s stories in your inbox

Join Medium for free to get updates from this writer.

This solution worked as a charm on a small dataset. But it didn’t scale. When rendering more items, the result was a staggering animation of circles that are constantly updating their position:

You can see it in action here:

I then decided to only use the last position of the force simulation (and not having to constantly update the intermediary steps) and add a simple tween to each circle using a react-spring — a lightweight react animation library.

Setting up react-spring to handle animations was the easy part. Forcing the simulation to end in order to get the last positions proved to be a bit tricky.

D3’s simulation isn’t a timeline based animation (as greensock is), so there’s no way to force it to complete unless all the intermediary steps are completed.
So I ended up manually iterating through each tick of the simulation in order to force it to finish (a technique described by Mike Bostock — the creator of d3 — as “static force layout” in https://bl.ocks.org/mbostock/1667139). After that I would update all the circle positions in one single smooth animation:

This solution works great and there’s no staggering animation 🎉.

The only downside is that with larger datasets, the simulation can take too much time to update, leaving you with several “frozen screen” situations:

Press enter or click to view image in full size

That’s when I thought I would try the other approach and let d3 handle both math, rendering and animation.

Giving d3 the ownership of the DOM

By letting d3 handle most of the logic, this means that React would only serve as a wrapper component that initiates a d3 function with its props and a DOM element.

Once that is setup, we need to dive a bit more in d3. Since d3 handles all the rendering, you should get familiar with the way d3 handles updates. Here’s one article that I think does a good job at explaining d3’s enter-update-exit pattern: http://bl.ocks.org/alansmithy/e984477a741bc56db5a5

Here’s my take on a simple d3 bubble chart implementation:

What’s left now is to hook React and d3 together and make sure that react informs d3 of any updates.

You can notice that we fixed the frozen screen issue from the initial approach so that the chart reacts and renders the new data as soon as the props change.

Press enter or click to view image in full size

Conclusion

Integrating React with d3 can have a lot of advantages, but it’s important to clearly separate who is doing what, according to what each does best.

With most DOM manipulations, you should let React handle it, like storing data or passing it to components. But when it comes to highly complex data visualisation calculations that need to scale, leave that it d3.

I can’t say which of the implementations is better, because you have to take into consideration many things, like the use case that you’re building the component for, which approach is easier to understand, maintain and extend and, of course, which one performs better.

I’ve put both implementations side by side in the following playground so you can see, tweak and decide for yourself which one is for you:

probably viewed better on a larger screen

After this experience, for me React+d3 = ❤️.

--

--

gradinariu bogdan
gradinariu bogdan

Written by gradinariu bogdan

javascript developer, piano player