This is a good start but it glosses over the hardest part of making React work with D3, which is animations. Most animations you want to do with D3 don't fall neatly into categories you can use with CSS animation (namely updating paths and entering / updating / exiting animations).
React as an ecosystem is pretty fantastic but the React animation story is still pretty terrible unfortunately. "Portals" / "reparenting" is a majorly hacky way of getting an element from one part of the DOM to another, and even React Motion, which solves a few of the animation gripes, is hard to use, slow, and brittle in my experience. There just isn't a good substitute for the enter-update-exit selections that make up the core of D3.
My usual workflow is to get as much DOM building done in React as I can, with D3 filling out the tricky bits. D3's lifecycles and direct DOM manipulation are much more complicated to reason about than React, on the whole.
I've tried both, but between D3's odd imho data-binding and React animation limitations, I gave up and found using RactiveJS with SVG directly was much easier: http://examples.ractivejs.org
RactiveJS uses virtual dom as well, so was quite performant and very easy animations of properties.
I don't agree that D3's data binding is especially odd. It isn't as straightforward as React's vdom descriptor, but it's not especially complicated: https://bost.ocks.org/mike/circles/
It's definitely a steeper learning curve than simply creating a node and appending to the DOM (the d3-specific idioms are particularly obtuse) but it's not insurmountable.
To each their own - my issue is if you are starting with some JS data (via API, etc) - trying to bind to D3 is rather complex compared to using other view tech.
That's fair. Keep in mind that d3's major use case has historically been for building interactive data visualizations with dynamic data. Viewed through that lens, its data binding is a pretty integral part of the design.
Has anyone worked on these issues in Om or Om Next? I feel I'm coming up against exactly this wall myself, and I don't think my worldview is sufficiently Ommy to know which way to go.
I'm afraid I fail to see how Liquid Fire competes with D3 in any way. It looks like a library for UI state transitions, but not for animating graphs, or all the crazy things one can do with D3 (some of these examples are even given in the D4 article).
Sorry, I don't get it. The code in any of the given example comparisons has no less obvious complexity and just removes the abstraction that writes the SVG nodes for you with having to explicitly write them in your JS which I similarly have never understood why we're intent on going back to.
Different for the sake of different is fine, but "preferable" is a bold statement that appears to have no evidence.
I suppose that everything is a matter of opinion, but d3's syntax is the worst combination of obtuse (".enter"???), cute (the incredibly obnoxious chained api), and cumbersome (all the mutation, and the fact that d3 code always ends up being a long string of procedural spaghetti).
Of course, it does have a lot of useful code around math, projections, etc.
This project looks like a very welcome step in the right direction, showing that d3's great math can be used with obvious, declarative syntax.
Can you explain why do you feel 'enter' is obtuse? I found enter/update/exit pretty logical. I do agree that long-term value of Bostock's work is in other parts than data-binding, but I didn't find enter/update/exit particularly hard
I agree with you as well. The enter/update/exit cycle is actually very intuitive although the documentation could do a better job of explaining what the functions are doing to the DOM.
In this case, I think a little more verbosity in the names of these methods would assist in understanding significantly. For example, enterAnimation/enterTransition would increase the clarity for someone new to d3, or who has been away from it for a while.
enter is used to insert elements into the DOM; if there is data in the data-set which isn't represented in the DOM, then new elements will be inserted which have that data. It doesn't have to do with animations really, though you can animate those newly inserted elements if you wish.
I understand how enter works, but my point is that the naming is pretty poor. If I haven't been around d3 for a while, it becomes very easy to forget how it works because of exactly this. Its api isn't intuitive, which often indicates either bad naming or design - in this case, I would pin it largely on the naming, and the api design not facilitating understanding as easily.
For me, the biggest problem is that d3 is a declarative library which you write code procedurally for. If you look at d3 code, it has lots of imperative-style verbs (enter) which are not readily clear.
I agree with you that is his personal opinion about "preferable" way to use D3.
> "Unfortunately, I always find D3 code surprisingly difficult to understand and extend"
I would say something different - it is very easy to extend and create various visualization. It is easy to scaffold new chart (line, pie, box-plot, heatmap etc.) and customize it to specified needs. I'd rather say that it is difficult to create a brand new type of visualization which wasn't been already developed [1] and it is easy to understand by random folk.
Of course it is! to start with, it looks like SVG, but also you only have to provide the state that you want and React adds and removes what is necessary, while in D3 you need to use `enter` and `exit` imperatively.
I'm just concerned about speed. Diffing works ok for forms but charts with 1000s of elements... let's see.
As someone who has worked with D3 and React a lot, D4 makes a lot of sense.
enter() and exit() are declarative. You're not causing anything at all to happen, but rather declaring what should happen, in the form of a function, when new data is found or data becomes stale.
The problem is that calling a function called "enter" does not feel like writing declarative code.
It's an active verb and seems to imply an imperative style. Except it's actually declarative. The problem with d3 is that it takes a declarative system and gives it an imperative-looking API.
I think you're reading it wrong. `enter` isn't imperative. You're asking for the `enter` selection, which is the list of entering nodes. Same thing with `exit`. Calling either of those methods doesn't actually alter any data
And SQL `SELECT` statements looks like I'm actively selecting something, when in reality I'm declaring what bits of the table I want returned... All syntax looks opaque until you understand the underlying concepts.
Uh, a SQL SELECT statement is actively selecting something. You're selecting what you want to return. Terrible example.
The problem with "enter" is that nothing will happen if you simply use enter by itself. Yet it's an active verb which seems to imply that something will be made to enter.
OK, if a SELECT statement is sufficient by itself, surely you can tell me the results of this:
SELECT * FROM stuff WHERE thing=2
Of course you can't. Because every declarative process has two main steps: declare and execute. SELECT and enter() both only declare; they must then be executed against data in an engine to produce a result.
What we mean when we say "SQL is declarative" is that it doesn't provide instructions on how to carry out a task, and instead only provides a description of the outcome of the task, once it's been carried out. SELECT is declarative because it doesn't tell the computer how to do a query. SELECT tells the interpreter what you want not how to get it.
It might be proven to be more performant, as it's relying on React's virtual DOM rather than d3's presumably made-from-scratch data binding. Would love to see some benchmarks though.
Have been doing data visualizations like this ever since, it works. Yes, the hardest part is animation, which we had to address, as well as the axis components and other higher order functionality that builds on d3 itself:
My team and I have been using and finding success with such a pattern for a couple of years now.
As others are alluding to, transitions are not currently so easy to express with React alone. We wrote and recently open-sourced a React component that aims to encapsulate the power and simplicity of d3 transitions (feedback & contributions welcome): https://github.com/dechov/join-transition/
>>There are some pieces of d3 that I would love to use but aren't easily portable. For example, d3-drag and d3-zoom smooth over a lot of the quirks you'd have to deal with when implementing dragging and zooming, but they're only designed to work with d3 selections [...] Ideally we could decouple these libraries from selections, but it might also be possible to mock the selection interface they expect while still using React.
According to the changelog for the recently released version 4.0.0 of D3.js by Mike Bostock[1], one of the big shifts is for D3 v4.0.0 to be "composed of many small libraries that"[2] can be used independently. So the author of D4.js can now more easily examine Bostock's refactored code for Zoom and Drag as they please.
If you look at the example code, the author is aware of the new modules pulled out of d3. He's actively using them. It doesn't change his comments re: those two modules specifically.
I did that! One function that receives actions for update, create, delete, etc. Now my code is easier to read. Check the function gupChildren (AKA: General update pattern for child nodes of a selection) in my code, and feel free to use it: https://bitbucket.org/aurelito/sandro-lib/src/83b81c4b556848...
At last, unless you understand what you are doing, use selectAll and no select.
Me too, so last week I created gulp-pug-hyperscript to compile Pug/Jade templates to Hyperscript [1].
In order to completely avoid JS in the markup, event binding must be done outside of the markup. I do this by using event delegation rather than binding directly to elements.
Shameless plug: I have been proposing this approach for long. I found that the missing piece is how to generate the actual geometric information starting from data, hence I wrote a library to this effect https://github.com/andreaferretti/paths-js
I've done the opposite: use d3 to provide React-like semantics.
- Basically the data fetches get pushed to a lightweight queue/topic system in the browser (using publish.js).
- Each part of the page listens for messages it cares about and updates the page accordingly.
There is still some of the d3 plumbing (I must specify how updates differ from mere element creations, and handle the exits). But I still get a nice decoupling between the different parts of my app. Coupled with browserify, this ends up with semantics not unlike Erlang (I even named the listener method "receive" and it acts on patterns of messages).
Intelligently-optimized JS animations are generally more performant than CSS ones by an order of magnitude. See Greensock for relevant examples. Side note, your examples don't assign keys to your array-mapped paths, which will emit warnings.
Very cool.. My only question is.. Is this "D4.js" a full feature replacement for D3.js? Could be a bit misleading. Might want to have a new name for it? Thoughts?
It gives you a continuously changing variable that you can use for whatever you want. The name style is confusing, because it isn't actually a style. In the very first example it's just displaying the changing value.
React as an ecosystem is pretty fantastic but the React animation story is still pretty terrible unfortunately. "Portals" / "reparenting" is a majorly hacky way of getting an element from one part of the DOM to another, and even React Motion, which solves a few of the animation gripes, is hard to use, slow, and brittle in my experience. There just isn't a good substitute for the enter-update-exit selections that make up the core of D3.
My usual workflow is to get as much DOM building done in React as I can, with D3 filling out the tricky bits. D3's lifecycles and direct DOM manipulation are much more complicated to reason about than React, on the whole.