• Stars
    star
    818
  • Rank 55,733 (Top 2 %)
  • Language
    JavaScript
  • License
    BSD 3-Clause "New...
  • Created over 8 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Data-Driven Declarative Documents

d4 -- Declarative Data-Driven Documents

What is it?

d4 is an experiment in using React to produce data-driven documents (ala d3) that are performant and understandable. This is not a library, but rather a demonstration that it's possible (and preferable) to use React instead of the core of d3.

Why?

d3 can produce fantastic results. Look no further than Mike Bostock's blocks for examples. Unfortunately, I always find d3 code surprisingly difficult to understand and extend, in the same way I used to find code difficult to approach before React encouraged a declarative style. By using React (which can render SVGs, no problem) for data-driven documents, we can improve comprehension and performance and use tools from the React ecosystem.

How does it work?

We replace the core d3 interaction of Enter, Update, Exit with, well, render. Let's first see an example.

d3

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

svg.selectAll("path")
    .data(voronoi(samples).polygons())
  .enter().append("path")
    .attr("d", d => `M${d.join("L")}Z`)
    .style("fill", d => color(d.point))
    .style("stroke", d => color(d.point));

d4

function Mesh() {
  const paths = voronoi(samples)
    .polygons()
    .map(sample => (
      <path
        d={`M${sample.join('L')}Z`}
        fill={color(sample.data)}
        stroke={color(sample.data)}
      />
    ));

  return (
    <svg width={width} height={height}>
      {paths}
    </svg>
  );
}

We replace the mutating select, selectAll, enter, append, data, attr, and style with familiar React rendering of the points.

Animation is more complicated, but again, React can help. By using keys and the ReactCSSTransitionGroup, it's possible to describe animations in CSS, rather than using d3's interpolation. I haven't verified the performance, but I expect CSS transition group animations to be faster, since they're browser-native and avoid the JS engine. For example:

d3.select("body")
    .style("color", "green") // make the body green
  .transition()
    .style("color", "red"); // then transition to red

Becomes (specifying the duration, which the original left out):

body {
  transition: color 250ms;
}

Why we still need d3

d3 does a lot and we can continue to use most of it. In fact, these demos collectively use a dozen d3 packages. d3 is especially useful for calculating layouts and colors.

Future Work

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 (eg selection.call(d3.zoom().on("zoom", zoomed));).

I'm curious about the performance of this approach. I haven't benchmarked yet, but my intuition is that it should be fast -- as fast as React's reconciliation. However, I don't know how that part of d3 is implemented, so maybe d3 is actually faster.

A small thing -- it's possible to use only parts of d3. For example: import {voronoi as d3Voronoi} from 'd3-voronoi'; instead of d3.voronoi, and import {lab} from 'd3-color'; instead of d3.color.lab), but nobody uses it that way, so examples of the import style are hard to find (and it's often not obvious which name will be exported (d3-geo exports geoArea and geoBounds rather than area and bounds).

Besides the five completed demos, I've also started working on a few others, but I'm deferring them to get this article published.

Demos

In all the demos we continue to use some d3 utilities, but use React to separate the logic from the display declaration. Take a look at the source for a few!

More Repositories

1

react-haskell

React bindings for Haskell
Haskell
351
star
2

react-live-editor

live editing react components
JavaScript
163
star
3

pigment

Programming Language meets UI
JavaScript
51
star
4

slimlock

SLiM + slock = slimlock
C++
39
star
5

evm-rust

Minimal EVM implementation in Rust
Rust
29
star
6

lvca

language verification, construction, and analysis
OCaml
20
star
7

hubble

javascript lenses
JavaScript
15
star
8

haskell-loader

A webpack loader for Haskell (!)
JavaScript
15
star
9

haskell-ipld

An implementation of (some of) IPLD
Haskell
12
star
10

material-ui-hs

Material UI in Haskell!
Haskell
11
star
11

easytest

Simple testing toolkit
Haskell
11
star
12

assimp

Haskell FFI bindings for Assimp
C++
11
star
13

ghcjs-box

Quick start for the vagrant box I use for ghcjs / react-haskell / pigment development
Shell
11
star
14

typed-algebraic-parsing

An implementation of "A Typed, Algebraic Approach to Parsing"
OCaml
10
star
15

daily-typecheckers

I'm writing a typechecker every day
Haskell
9
star
16

mamba-sae

Training and evaluating Sparse Autoencoders for Mamba
Python
9
star
17

simplicity-hs

Implementation of "Simplicity: A New Language for Blockchains"
Haskell
8
star
18

dotfiles

My important config files
Vim Script
6
star
19

Cologne

Good Smelling Ray Tracer
Haskell
6
star
20

jison-loader

jison loader module for webpack
JavaScript
6
star
21

structured-editing

JavaScript
5
star
22

plur

Plurality Monad: Zero, one, or at least two.
Haskell
5
star
23

monaco-ocaml

Ocaml bindings to Microsoft's Monaco editor
JavaScript
4
star
24

lvca-hs

language verification, construction, and automation
Haskell
4
star
25

data-lens-template

Template Haskell utilities for Data.Lens
Haskell
4
star
26

mylens

Haskell
3
star
27

sdl2-image

Haskell bindings to sdl_image for SDL 2
Haskell
3
star
28

gadts

The contents of a talk on GADTs
Haskell
3
star
29

corewarjs

Corewar in Javascript
JavaScript
3
star
30

sdl2-ttf

Haskell bindings for SDL(2)-ttf
Haskell
2
star
31

secret

Haskell
2
star
32

isl-hs

haskell bindings to the integer set library
Haskell
2
star
33

backprop-fun

fun with backprop
Haskell
2
star
34

journal

My public technical journal
Python
2
star
35

berkeley-quorum

Demos / Materials for my Blockchain at Berkeley Developer Course lecture
2
star
36

sdl2-gfx

Haskell bindings to SDL2_gfx
Haskell
2
star
37

turbo-unify

a unification visualization in the browser
Haskell
1
star
38

a-la-carte

Haskell
1
star
39

blog.next

Possibly the technology that will get me blogging again
JavaScript
1
star
40

huttons-raz3r

Haskell
1
star
41

graphreduction

Haskell
1
star
42

tapl

Types and Programming Languages, in Haskell
Haskell
1
star
43

sat

OCaml
1
star
44

hazel

Haskell
1
star
45

formative

My Website
Python
1
star
46

website

The old joelburget.com
JavaScript
1
star
47

diagrams-opengl

An OpenGL Backend for Diagrams
Haskell
1
star
48

joelburget.com

joelburget.com
JavaScript
1
star
49

language-python

Haskell
1
star
50

glowing-ironman

Haskell
1
star
51

benchmarks

OSU RSRG benchmarks website
Python
1
star
52

focusing

Haskell
1
star
53

oplog

Haskell
1
star
54

motivated-list-checking

Haskell
1
star
55

perseus-one-server

An experimental server implementing the Perseus One API
Haskell
1
star
56

symmetric-properties

`Same` / `Unique` monoidal witnesses
Haskell
1
star
57

planetary

playing with programming languages again
Haskell
1
star
58

miftiwac

CSE 731 MIFTIWAC group project
Java
1
star