Home

Awesome

d3Kit

status: retired

NPM version Build Status Dependency Status CDNJS version

d3Kit provides thin scaffold for creating reusable and responsive charts with D3. It aims to relieve you from the same groundwork tasks you found yourself doing again and again.

Introduction slides | Getting started guide | API Reference | All Documentation

For developers who have tried d3Kit v1-2, d3Kit v3 was rewritten to support D3 v4, consider several new use cases (<canvas>, for example) and use ES6 class for the implementation, making every chart can be extended easily. Documentation of version 1-2 can be found here

Install

npm install d3kit --save

See getting start guide for more details.

Examples

Here are a few examples of d3Kit in action:

Why should you use d3Kit?

Avoid coding basic building blocks again and again.

😫 You are tired of copying the boilerplate d3.select('body').append('svg')... from D3 examples.

Solution: There is SvgChart for that.

😫 You want to create a chart on <canvas> but never remember how to handle different screen resolution (retina display).

Solution: There is CanvasChart for that.

🤔 You want to use <svg> and <canvas> together.

Solution: There is HybridChart for that.

😫 You use D3's margin convention and are tired of copy pasting from Mike's block.

Solution: All SvgChart, CanvasChart and HybridChart extends AbstractChart, which was built based on the margin convention.

Reusable charts

🤔 You want to create a reusable chart in D3.

😫 You want to create a responsive chart, but are tired of listening to window resize or manually polling for element size.

🤔 You want to make a responsive chart that maintains aspect ratio.

Solution: Create a chart extends from SvgChart, CanvasChart, HybridChart or AbstractChart then you get all of the above handled.

🤔 You are familiar with creating charts in D3 and want to adapt them easily into React or angular components.

Solution: Currently there are react-d3kit and angular-d3kit-adapter that can convert charts written in d3Kit into React and Angular 1 components, respectively, in a few lines of code.

What is d3Kit?

The core of d3Kit are base classes for creating a chart. Currently there are SvgChart, CanvasChart and HybridChart. All are extended from AbstractChart.

AbstractChart

Most of the time you will not need to access AbstractChart directly, but you will use one of its children: SvgChart, CanvasChart or HybridChart.

SvgChart

This class creates <svg> boilerplate inside the container.

CanvasChart

While SvgChart creates necessary element for building chart with <svg>. This class creates <canvas> inside the container. It also handles different screen resolution for you (retina display vs. standard display).

HybridChart

Thought about using <svg> and <canvas> in combination? Here it is. A HybridChart creates both <svg> and <canvas> inside the container.

Build your own chart with plates

If SvgChart, CanvasChart or HybridChart does not fit your need yet, you can create your own.

Under the hood, d3Kit use its "plating" system to wrap different type of components (<svg>, <canvas>, etc.). The current implementation includes three types of plates: SvgPlate, CanvasPlate and DivPlate.

Think of AbstractChart as a container. Any resizing done to the chart will be applied to the plates in it by d3Kit. This abstraction helps you think of a chart as one piece and not to worry about how to keep track of each children size. Then you can just focus on what to drawn on svg or canvas based on the current dimension of the chart.

Now if you want to create a chart with multiple canvases and svg, just create a new subclass.

import { AbstractChart, CanvasPlate, SvgPlate } from 'd3kit';

class CustomChart extends AbstractChart {
  constructor(selector, ...options) {
    super(selector, ...options);

    this.addPlate('canvas1', new CanvasPlate());
    // now access D3 selection of this <canvas> element
    // via this.plates.canvas1.getSelection()

    this.addPlate('canvas2', new CanvasPlate());
    // now access D3 selection of this <canvas> element
    // via this.plates.canvas2.getSelection()

    this.addPlate('svg', new SvgPlate());
    // now access D3 selection of this <svg> element
    // via this.plates.svg.getSelection()

    this.updateDimensionNow();
  }
}

Once you call

new CustomChart('#my-chart');

This will be created.

<div id="my-chart">
  <div class="d3kit-chart-root">
  	 <canvas />
  	 <canvas />
  	 <svg></svg>
  </div>
</div>

Other features

LayerOrganizer

This was created from a habit of creating many <g>s inside the root <g>.

Input

<svg>
  <g class="container"></g>
</svg>
const layers = new LayerOrganizer(d3.selection('.container'));
layers.create(['content', 'x-axis', 'y-axis']);

Output

<svg>
  <g class="container">
    <!-- layers.get('content') is a D3 selection of this g -->
    <g class="content-layer"></g>
    <!-- layers.get('x-axis') is a D3 selection of this g -->
    <g class="x-axis-layer"></g>
    <!-- layers.get('y-axis') is a D3 selection of this g -->
    <g class="y-axis-layer"></g>
  </g>
</svg>

All SvgChart includes chart.layers by default, which is new LayerOrganizer(chart.container).

There are more features. Read more here.

Chartlet

d3Kit v1-2 also helps you create reusable subcomponent (a.k.a. Chartlet). We have not ported it to v3 yet.

Documentation

Want to learn more? Follow these links.

Appendix

A diagram explaining D3's margin convention

<p align="center"> <a href="https://bl.ocks.org/trebor/554ffd04106ab92cb8014cd9500f428c" style="width:100%;"> <img src="resources/skeleton.png" height="400"> </a> </p>

Authors

License

© 2015-2017 Twitter, Inc. MIT License