Readit News logoReadit News
Posted by u/holtzy 3 years ago
Show HN: A gallery of graphs built with React and D3.jsreact-graph-gallery.com...
Many JS libraries exist to build graphs on the web (Vega, chartJS, Plotly...).

They allow to make charts quickly. But you lose flexibility: you're limited by the options they offer.

I just created a gallery with hundreds of graphs made with d3.js and React. - Examples are split by graph types - They all come with explanation and code sandboxes - Gradual complexity to ease the learning curve

It took me ages to create this project! Feedback welcome!

jitl · 3 years ago
Very cool to explore the code here. Have you looked at Visx? It’s a D3+React mashup library, used by Airbnb and Notion for graphs. https://airbnb.io/visx/gallery
patrickthebold · 3 years ago
VisX is mentioned in the about page, so presumably. https://www.react-graph-gallery.com/about, that page also gives some motivation on why this is a separate project.
holtzy · 3 years ago
Yes I know about VisX.

The approach is kind of in-between what I suggest and what a classic JS library like Vega does.

It is definitely a very good abstraction imo!! But I'm always scared to be stuck with a customisation I cannot do because of a layer of abstraction.

internetter · 3 years ago
Wow how have I never heard of this? Looks wonderful
jitl · 3 years ago
Harry (the guy who wrote it) is skilled but not a JS thought-leader Twitter personality. I know about it because I worked at Airbnb back in the day. https://twitter.com/hshoff
littlecranky67 · 3 years ago
This is great! The example page on official d3 is a bit dated and certainly has "grown" over time. I also particular don't like that d3 is too much of a "one tool for everything" approach. I.e. the data calculations and the drawings shouldn't be done in the same framework (thats were React comes into play).

My biggest pet peeve with D3 is the lack of TypeScript friendly-ness. I not only mean the lack of official typescript .d.ts bindings, but the API itself is completely not designed for strict typing. Functions are heavily overloaded/polymorphic, take all sort of different types of parameter shapes, and change their return values on what is passed in etc. My experience was always, once I got my code "properly" typed, the next update of the .d.ts bindings would break again, and this would repeat every couple of weeks.

azangru · 3 years ago
> I.e. the data calculations and the drawings shouldn't be done in the same framework (thats were React comes into play).

I used to think so too; but I don't any longer. Rendering in React is fairly expensive. It may easily turn out that direct DOM manipulation is better suited to visualizations than DOM manipulation through React.

jdonaldson · 3 years ago
Data calculations and rendering should be done in the same framework if you wish to optimize interactivity.
azangru · 3 years ago
Could you tell us what you have learnt about the performance of these tools? E.g. when you write about different approaches to building charts with d3 and react here (https://www.react-graph-gallery.com/about), is approach 2, which directly interacts with the DOM, more performant than approach 3, which has to go through React? Also, what, approximately, is the upper limit of a dataset size after which the React+d3=>svg approach begins to stutter?
Swizec · 3 years ago
In my experience (author of ReactForDataviz) the React renders SVG approach starts struggling around 10,000 to 50,000 nodes depending on hardware and what kind of calculations you’re doing.

Directly manipulating the SVG DOM with D3 (wrapped in a React blackbox) goes to around 100,000 nodes. After that you really have to start using canvas or webgl.

At those numbers you also start getting huge performance gains by mutating data and doing less copying. Everything you learn in “how to do react” tutorials quickly starts being wrong when your arrays have thousands of elements.

Edit: there’s also a lot you can do with how things are nested. The flatter your DOM, the fewer nodes React has to touch for every update

Here’s a fun stress test I built a while back that goes up to a million nodes rendered by React https://swizec.com/blog/a-better-react-18-starttransition-de...

holtzy · 3 years ago
This is a very good question as performance is a key struggle in Data Visualization. Swizec answer below is great.

I will write more about performance soon. But using several layers of canvas is definitely the way to go in most situation to put it in a nutshell

mhh__ · 3 years ago
In my experience of implementing some clever financial plots in D3 I find that most examples on websites such as these are very quickly exhausted - unless you're dealing with a chart that either an undergraduate statistician or a programmer of any level might use you're on your own - I don't mind that, except that I notice that libraries often focus on a few examples rather than really nailing (say) getting zooming right.
aweb · 3 years ago
Wow, I don't know a thing about dataviz but I really liked the website! The real world examples are really helpful to understand why we could use a certain kind of chart. Keep up the good work!
holtzy · 3 years ago
Thanks a lot for the nice feedback!
sputknick · 3 years ago
This is really helpful for me, the right level of example. I find most D3 examples to either be too simple or too complex, this nails the sweetspot of visually impressive, while still being practical.
holtzy · 3 years ago
Nice thanks!
sterlind · 3 years ago
The link for "Connections" (I've been calling them geo-graphs or network maps) seems to be broken, which is a shame. I've had a hard time finding good libraries for them, and they're hard to get right for a few reasons:

- if you zoom out to show the world, map projections wrap around the antimeridian (e.g. you turn right at Kamchatka, loop around and hit Alaska.) if you want your edges to wrap (i.e. follow a great arc) rather than taking the long way around (i.e. drawing a line all through Europe and Asia), you have to do spherical math.

- if you want a multigraph (multiple edges between any pair of nodes), you need to have a way for those edges to "curve" rather than following a straight path (great arc.) this makes the math much more complicated, especially keeping numerical stability and accounting for coordinate singularities.

- now imagine all the fiddly bits of making the arrowheads touch the nodes at the right place, scale in width properly, etc. if you're really committed, you'll use lat/lon for every vertex of every poly you render. if not, there will be distortions. it's a trade-off.

- now imagine updating all of that while panning and zooming, and keeping it efficient and smooth.

I made a component for this, I'll try to open-source it, but I'm curious how other people have tackled the same issue. Did I just over-engineer the hell out of it doing spherical trig, vector math and iterative methods, or is it plain hard?

holtzy · 3 years ago
All the map sections are not ready yet. But they will be soon!
MR4D · 3 years ago
Did anyone else just waste 5 minutes playing with the background image?

I haven't seen that before - very cool.

Juicyy · 3 years ago
it comes from some library called js particles or particles.js i forget which and there are knockoffs... https://particles.js.org/samples/index.html#basic