• Stars
    star
    107
  • Rank 323,587 (Top 7 %)
  • Language
    JavaScript
  • License
    BSD 3-Clause "New...
  • Created about 9 years ago
  • Updated almost 7 years ago

Reviews

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

Repository Details

A lightweight component abstraction for D3.js.

d3-component

A lightweight component abstraction for D3.js.

Features:

Examples:

Todos
Clock
Airport Clocks
example-viewer (Redux, ES6)
Fractal Pie Chart (ES6)

Using this component abstraction, you can easily encapsulate data-driven user interface components as conceptual "boxes-within-boxes", cleanly isolating concerns for various levels of your DOM tree. This component abstraction is similar in concept and functionality to React Stateless Functional Components. Everything a component needs to render itself and interact with application state gets passed down through the component tree at render time. Components don't store any local state; this is the main difference between d3-component and the Towards Reusable Charts pattern. No special treatment is given to events or event delegation, because the intended use is within a unidirectional data flow architecture like Redux.

Installing

If you use NPM, npm install d3-component. Otherwise, download the latest release. You can also load directly from unpkg.com as a standalone library. AMD, CommonJS, and vanilla environments are supported. In vanilla, a d3 global is exported:

<script src="https://unpkg.com/d3@4"></script>
<script src="https://unpkg.com/d3-component@3"></script>
<script>
  var myComponent = d3.component("div");
</script>

API Reference

Note: There was a recent major version release, and along with it there were substantial API Changes.

In summary, the API looks like this:

var myComponent = d3.component("div", "some-class")
  .create((selection, d, i) => { ... }) // Invoked for entering component instances.
  .render((selection, d, i) => { ... }) // Invoked for entering AND updating component instances.
  .destroy((selection, d, i) => { ... }); // Invoked for exiting instances, may return a transition.

// To invoke the component,
d3.select("body") // create a selection with a single element,
  .call(myComponent, "Hello d3-component!"); // then use selection.call().

To see the full API in action, check out this "Hello d3-component" example.

# component(tagName[, className]))

Creates a new component generator that manages and renders into DOM elements of the specified tagName.

The optional parameter className determines the value of the class attribute on the DOM elements managed.

# component.create(function(selection, d, i))

Sets the create function of this component generator, which will be invoked whenever a new component instance is created, being passed a selection containing the current DOM element, the current datum (d), and the index of the current datum (i).

# component.render(function(selection, d, i))

Sets the render function of this component generator. This function will be invoked for each component instance during rendering, being passed a selection containing the current DOM element, the current datum (d), and the index of the current datum (i).

# component.destroy(function(selection, d, i))

Sets the destroy function of this component generator, which will be invoked whenever a component instance is destroyed, being passed a selection containing the current DOM element, the current datum (d), and the index of the current datum (i).

When a component instance gets destroyed, the destroy function of all its children is also invoked (recursively), so you can be sure that this function will be invoked before the compoent instance is removed from the DOM.

The destroy function may optionally return a transition, which will defer DOM element removal until after the transition is finished (but only if the parent component instance is not destroyed). Deeply nested component instances may have their DOM nodes removed before the transition completes, so it's best not to depend on the DOM node existing after the transition completes.

# component.key(function)

Sets the key function used in the internal data join when managing DOM elements for component instances. Specifying a key function is optional (the array index is used as the key by default), but will make re-rendering more efficient in cases where data arrays get reordered or spliced over time.

# component(selection[,data[,context]])

Renders the component to the given selection, a D3 selection containing a single DOM element. A raw DOM element may also be passed in as the selection argument. Returns a D3 selection containing the merged Enter and Update selections for component instances.

  • If data is specified and is an array, one component instance will be rendered for each element of the array, and the render function will receive a single element of the data array as its d argument.
    • Useful case: If data is specified as an empty array [], all previously rendered component instances will be removed.
  • If data is specified and is not an array, exactly one component instance will be rendered, and the render function will receive the data value as its d argument.
  • If data is not specified, exactly one component instance will be rendered, and the render function will receive undefined as its d argument.

In summary, components can be rendered using the following signatures:

  • selection.call(myComponent, dataObject) β†’ One instance, render function d will be dataObject.
  • selection.call(myComponent, dataArray) β†’ dataArray.length instances, render function d will be dataArray[i]
  • selection.call(myComponent) β†’ One instance, render function d will be undefined.

If a context object is specified, each data element in the data array will be shallow merged into a new object whose prototype is the context object, and the resulting array will be used in place of the data array. This is useful for passing down callback functions through your component tree. To clarify, the following two invocations are equivalent:

var context = {
  onClick: function (){ console.log("Clicked!");
};
selection.call(myComponent, dataArray.map(function (d){
  return Object.assign(Object.create(context), d);
}));
var context = {
  onClick: function (){ console.log("Clicked!");
};
selection.call(myComponent, dataArray, context);

More Repositories

1

screencasts

Code that goes along with my screencasts.
JavaScript
2,622
star
2

data

A collection of public data sets
HTML
473
star
3

model

A functional reactive model library for interactive data visualization.
JavaScript
316
star
4

dataviz-course-2018

Online course on data visualization analysis, design & construction with D3.js.
HTML
224
star
5

portfolio

Curran Kelleher's Portfolio
Java
78
star
6

d3-area-label

A library for placing labels in areas.
JavaScript
70
star
7

dataviz-course-archive

Archive of lesson material from an online course at Worcester Polytechnic Institute
HTML
66
star
8

d3-in-motion

Code examples and references for the course "D3.js in Motion"
HTML
38
star
9

HTML5Examples

Examples of HTML5 in action (best results in Chrome)
JavaScript
36
star
10

d3-bundler-ui

A Web application for defining custom d3 builds.
JavaScript
31
star
11

jspm-mocha-example

An example project that uses Mocha and JSPM.
JavaScript
19
star
12

google-diff-match-patch

Automatically exported from code.google.com/p/google-diff-match-patch
Python
17
star
13

data-reduction

A library for reducing the size of data sets for visualization.
JavaScript
13
star
14

phd

Curran Kelleher's Ph. D. work.
TeX
12
star
15

ticks

A utility for choosing nice tick marks or histogram intervals.
JavaScript
11
star
16

model-contrib

Open source visualizations built with ModelJS
JavaScript
10
star
17

nextjs-esm-example

An example that uses ESM with Next.js
JavaScript
10
star
18

dsv-dataset

A metadata specification and parsing library for data sets.
JavaScript
9
star
19

dataviz-project-template

A template project that uses Webpack and D3.
JavaScript
9
star
20

lambda

A Lambda Calculus interpreter
CoffeeScript
7
star
21

visEngine

A server side component for interactive data reduction queries.
JavaScript
7
star
22

mathematorium

A 3D graphing calculator and 3D particle simulator.
Java
6
star
23

d3-format-india

Indian Number System formatter
JavaScript
6
star
24

d3-rosetta

Maximize framework interoperability for interactive graphics
JavaScript
6
star
25

reactivis-old

Reusable Interactive D3 Data Visualizations
JavaScript
6
star
26

canvas-vis

A set of modules useful for information visualization using HTML5 Canvas.
JavaScript
5
star
27

scalatra-spark

A proof of concept integration serving Apache Spark services with Scalatra.
JavaScript
5
star
28

dataviz-project-template-proposal

A template for data visualization project proposals.
5
star
29

udcvis

The Universal Data Cube Visualization System
JavaScript
4
star
30

renderCyliner

A bit of OPENGL C code that renders a cylinder between two points.
C
4
star
31

dashboardScaffold

Plumbing for creating dashboards with D3.js
JavaScript
4
star
32

Fretboard

Fretboard is a Jack midi client which maps keystrokes to midi notes as though the keyboard were a fretted instrument.
4
star
33

old-d3-examples

A large dataset of source files for old D3 examples
JavaScript
3
star
34

stamen-dataviz-workshop

Material for a Data Visualization workshop at Stamen Design.
3
star
35

datacubesinfo

Content posted on datacubes.info. Mostly demos and proofs of concept.
JavaScript
3
star
36

Code-Hub

A database of code snippets with dependency management
JavaScript
3
star
37

LiquidMath

A 3D Graphing Calculator in Clojure using OpenGL
3
star
38

rollup-react-pwa-example

An example project setup with Rollup, server-side React rendering, and code splitting.
JavaScript
3
star
39

backboneComputedProperties

An implementation of computed properties for Backbone.js
JavaScript
3
star
40

unidirectional-data-flow

Unidirectional Data Flow for Vanilla JavaScript
TypeScript
2
star
41

crossCountry

A photo show of my cross country trip.
JavaScript
2
star
42

migrantDeathsVis

JavaScript
2
star
43

reactive-charts

Another charting library
2
star
44

simpleSynth

SimpleSynth is a JACK client which can synthesize and output enveloped sine waves from MIDI input.
C++
2
star
45

PublicDataSets

Scripts for downloading and processing public data sets
Shell
2
star
46

udc

The Universal Data Cube
JavaScript
2
star
47

example-viewer

A presentation tool and programming sandbox for code examples.
JavaScript
2
star
48

jyvis

A Java-based information visualization system with multiple linked views.
Java
2
star
49

graph

A directed graph implementation with depth first search.
JavaScript
2
star
50

udf-scatter-plot

Unidirectional Data Flow Scatter Plot Example
JavaScript
2
star
51

sharedb-snapshot

ShareDB library for fetching historical document snapshots.
JavaScript
2
star
52

streamgraph-explorer

An prototype interactive visualization for exploring flow timeseries data. Try it -->
JavaScript
2
star
53

get-it-right

Materials for the livestream series Get it Right in Black & White
HTML
1
star
54

setupHadoop

Shell scripts and instructions for setting up Hadoop on a cluster.
Shell
1
star
55

quadstream

Quadstream is a Java library for polygon simplification.
JavaScript
1
star
56

curran.github.io

Curran's Web Site
JavaScript
1
star
57

dashboardScaffoldExample

An example application using the dashboardScaffold framework.
JavaScript
1
star
58

world-alliances

A dataset about world alliances
1
star
59

evalIDE

A simple live coding environment for the Web.
CSS
1
star
60

IWSCourseProject

A group project for the course "Internet and Web Systems" at University of Massachusetts Lowell Fall 2011
JavaScript
1
star
61

JSProjectTemplate

Boilerplate structure for starting a JavaScript project.
JavaScript
1
star
62

gash

Graphical ASH - this code is a first attempt to create a graphics library for ASH.
Java
1
star
63

igf

Interactive Graphics Framework (IGF) is a minimal immediate mode graphics API that supports both Java 2D and OpenGL, inspired by Processing (processing.org).
Java
1
star
64

Dynamic-Graphics

A C# framework for interactive dynamic graphics
1
star
65

liquidglobe

A series of small HTML5 prototypes for smooth panning and zooming.
JavaScript
1
star
66

PDJava

A collection of noise-making prototypes linking Java to PureData over a socket, using Processing as a graphics and network library.
Pure Data
1
star
67

TickMarkUtils

A simple algorithm for computing the values to use for tick marks such that nice numbers are displayed (intervals of {1 or 2 or 5}X10^n)
Java
1
star
68

images

A place to store images referenced by other repos.
1
star
69

hot-server-rollup-poc

Proof of concept for hot reloading with ES module support.
HTML
1
star
70

forceDirected

A simple prototype of force directed graph layout using Canvas & CoffeeScript
JavaScript
1
star
71

vis2013Tutorial

Material I presented at IEEE Vis 2013 in a tutorial called "Mobile and cloud Web-based graphics and visualization".
JavaScript
1
star
72

rollup-swc-crash-reproduction

Minimal reproduction for Rollup #5379
JavaScript
1
star