TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

D4 – Declarative Data-Driven Documents

263 pointsby joelburgetalmost 9 years ago

19 comments

krebbyalmost 9 years ago
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&#x27;t fall neatly into categories you can use with CSS animation (namely updating paths and entering &#x2F; updating &#x2F; exiting animations).<p>React as an ecosystem is pretty fantastic but the React animation story is still pretty terrible unfortunately. &quot;Portals&quot; &#x2F; &quot;reparenting&quot; 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&#x27;t a good substitute for the enter-update-exit selections that make up the core of D3.<p>My usual workflow is to get as much DOM building done in React as I can, with D3 filling out the tricky bits. D3&#x27;s lifecycles and direct DOM manipulation are much more complicated to reason about than React, on the whole.
评论 #12117114 未加载
评论 #12116221 未加载
评论 #12116010 未加载
评论 #12116576 未加载
评论 #12118105 未加载
weegoalmost 9 years ago
Sorry, I don&#x27;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&#x27;re intent on going back to.<p>Different for the sake of different is fine, but &quot;preferable&quot; is a bold statement that appears to have no evidence.
评论 #12115477 未加载
评论 #12118230 未加载
评论 #12115648 未加载
评论 #12115238 未加载
评论 #12115432 未加载
denoalmost 9 years ago
Hybrid approach:<p>&gt; let React do all the migraine-inducing heavy lifting to figure out what to enter and exit, and have D3 take care of the updates.<p><a href="https:&#x2F;&#x2F;medium.com&#x2F;@sxywu&#x2F;on-d3-react-and-a-little-bit-of-flux-88a226f328f3#.h143joihp" rel="nofollow">https:&#x2F;&#x2F;medium.com&#x2F;@sxywu&#x2F;on-d3-react-and-a-little-bit-of-fl...</a>
colinmegillalmost 9 years ago
Love this - awesome work.<p>Wrote an article on this concept last year: <a href="http:&#x2F;&#x2F;formidable.com&#x2F;blog&#x2F;2015&#x2F;05&#x2F;21&#x2F;react-d3-layouts&#x2F;" rel="nofollow">http:&#x2F;&#x2F;formidable.com&#x2F;blog&#x2F;2015&#x2F;05&#x2F;21&#x2F;react-d3-layouts&#x2F;</a><p>and spoke about it at Reactive2015: <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=n8TwLWsR40Y" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=n8TwLWsR40Y</a><p>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:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;FormidableLabs&#x2F;victory-animation" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;FormidableLabs&#x2F;victory-animation</a><p><a href="http:&#x2F;&#x2F;formidable.com&#x2F;open-source&#x2F;victory&#x2F;docs&#x2F;victory-axis&#x2F;" rel="nofollow">http:&#x2F;&#x2F;formidable.com&#x2F;open-source&#x2F;victory&#x2F;docs&#x2F;victory-axis&#x2F;</a>
dechovalmost 9 years ago
My team and I have been using and finding success with such a pattern for a couple of years now.<p>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 &amp; contributions welcome): <a href="https:&#x2F;&#x2F;github.com&#x2F;dechov&#x2F;join-transition&#x2F;" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;dechov&#x2F;join-transition&#x2F;</a>
评论 #12117691 未加载
otoburbalmost 9 years ago
&gt;&gt;<i>There are some pieces of d3 that I would love to use but aren&#x27;t easily portable. For example, d3-drag and d3-zoom smooth over a lot of the quirks you&#x27;d have to deal with when implementing dragging and zooming, but they&#x27;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.</i><p>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 &quot;composed of many small libraries that&quot;[2] can be used independently. So the author of D4.js can now more easily examine Bostock&#x27;s refactored code for Zoom and Drag as they please.<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;d3&#x2F;d3&#x2F;blob&#x2F;master&#x2F;CHANGES.md" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;d3&#x2F;d3&#x2F;blob&#x2F;master&#x2F;CHANGES.md</a><p>[2] <a href="https:&#x2F;&#x2F;github.com&#x2F;d3&#x2F;d3&#x2F;releases&#x2F;tag&#x2F;v4.0.0" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;d3&#x2F;d3&#x2F;releases&#x2F;tag&#x2F;v4.0.0</a>
评论 #12115474 未加载
aurelianitoalmost 9 years ago
I find that D3 is great, but it needs an abstraction for the general update pattern (<a href="https:&#x2F;&#x2F;bl.ocks.org&#x2F;mbostock&#x2F;3808218" rel="nofollow">https:&#x2F;&#x2F;bl.ocks.org&#x2F;mbostock&#x2F;3808218</a>).<p>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: <a href="https:&#x2F;&#x2F;bitbucket.org&#x2F;aurelito&#x2F;sandro-lib&#x2F;src&#x2F;83b81c4b55684866fbab50ff98088822b1d42410&#x2F;src&#x2F;sandro&#x2F;nadaMas&#x2F;d3.js?at=default&amp;fileviewer=file-view-default" rel="nofollow">https:&#x2F;&#x2F;bitbucket.org&#x2F;aurelito&#x2F;sandro-lib&#x2F;src&#x2F;83b81c4b556848...</a><p>At last, unless you understand what you are doing, use selectAll and no select.<p>Cheers!
jordachealmost 9 years ago
argh.. I hate the style of mixing JS with markup language....
评论 #12115700 未加载
评论 #12118434 未加载
评论 #12115667 未加载
jtwalesonalmost 9 years ago
I&#x27;m not sure if this is it, but the world needs a simple way to integrate D3 and React. Looks good at first sight though.
评论 #12115337 未加载
评论 #12115618 未加载
评论 #12115188 未加载
pathsjsalmost 9 years ago
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 <a href="https:&#x2F;&#x2F;github.com&#x2F;andreaferretti&#x2F;paths-js" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;andreaferretti&#x2F;paths-js</a><p>For the animations, it is often just a matter of continuosly updating the state of some component, something that I did in this mixin <a href="https:&#x2F;&#x2F;github.com&#x2F;andreaferretti&#x2F;paths-js-react-demo&#x2F;blob&#x2F;master&#x2F;js&#x2F;animate.jsx" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;andreaferretti&#x2F;paths-js-react-demo&#x2F;blob&#x2F;m...</a>
athenotalmost 9 years ago
I&#x27;ve done the opposite: use d3 to provide React-like semantics.<p>- Basically the data fetches get pushed to a lightweight queue&#x2F;topic system in the browser (using publish.js).<p>- Each part of the page listens for messages it cares about and updates the page accordingly.<p>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 &quot;receive&quot; and it acts on patterns of messages).
koopatrolalmost 9 years ago
I&#x27;m excited to play with this. I wonder what kind of overhead it has. I imagine it would be fairly low.
评论 #12115867 未加载
b34ralmost 9 years ago
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&#x27;t assign keys to your array-mapped paths, which will emit warnings.
FoeNyxalmost 9 years ago
@joelburget, btw the &quot;demos&quot; link leads to a 404.<p>( <a href="https:&#x2F;&#x2F;github.com&#x2F;joelburget&#x2F;d4&#x2F;demo" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;joelburget&#x2F;d4&#x2F;demo</a> )
guylepage3almost 9 years ago
Very cool.. My only question is.. Is this &quot;D4.js&quot; a full feature replacement for D3.js? Could be a bit misleading. Might want to have a new name for it? Thoughts?
评论 #12115312 未加载
评论 #12115395 未加载
adamwong246almost 9 years ago
I&#x27;ve been working with a similar approach for a while and I give it my vote of confidence. Much more elegant than d3&#x27;s awkward data-binding.
nthitzalmost 9 years ago
d3.transition works on element attributes not just CSS. Wish React had something like that!
评论 #12115324 未加载
chowardalmost 9 years ago
What is this? I can&#x27;t even scroll.
colemannerdalmost 9 years ago
I need something exactly like this.
评论 #12117693 未加载