Bivariate Maps with React and D3
github.com
github.com
React has tools for state management, code reuse, routing and other common front-end needs and leaves the DOM content to be decided by the coder with XML-like markup. D3 abstracts over the DOM content to handle the specifics of rendering while the coder focuses on the data.
Pairing D3 with React or Vue is a very common decision. Because this project seems to make little use of React beyond memoization I would think that doing without React would be much more plausible.
<svg
width={width}
height={height}
>
{geoJson.features.map(feature =>
<path
key={`path-${feature.properties.bfsId}`}
stroke='white'
strokeWidth={0.25}
d={path(feature)}
fill='steelblue'
/>
)}
</svg>
I was thinking maybe React was also used with this visualization in production (e.g. adding interactive elements for a published story), and so maybe the author found it easy to use React for the rendering too?To me this is more readable and easy to understand, but you don't really need react to do any of this. This can also be done with plan js.
Fast and easy to start and expand as needed.
The use of react is hereby not sctrictly necessary and much of the code found in SwissMap.js is also valid for other programming environments.
The usefulness of combing react and d3 is that you can create reusable and composable components, instead of creating something news every time you need a specific chart/map. This makes it easier to create more complex things like dashboards or UIs.Subjectively, I didn’t find the API all that great either.
I've been using D3 for a couple years now, and it's so low-level that I really don't feel like it abstracts very much for me. Using React, I can easily build an abstraction over an SVG path element that receives datapoints and produces a `d` attribute.