npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

d3kit

v3.2.0

Published

A kit of tools to speed D3 related project development.

Downloads

2,526

Readme

d3Kit

NPM version Build Status Dependency Status

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

  • takes a target container (usually a <div>) and helps you build a chart inside.
  • encapsulates D3's margin convention. The dimension of each chart is defined by width, height and margin.
    • chart.width() get/set the total width (including margin)
    • chart.height() get/set the total height (including margin)
    • chart.margin() get/set the margin
    • chart.getInnerWidth() returns width excluding margin. This is usually used as the boundary of the x-axis.
    • chart.getInnerHeight() returns height excluding margin. This is usually used as the boundary of the y-axis.
  • can resize the chart to be percentage of a container and/or maintain aspect ratio
    • chart.fit(fitOptions:Object) Calling this function with single argument will resize the chart to fit into the container once. Please refer to slimfit documentation for fitOptions.
  • can listen to resize (either window or element) and update the chart size to fit container.
    • chart.fit(fitOptions:Object, watchOptions:Boolean/Object) Calling with two arguments, such as chart.fit({...}, true) or chart.fit({...}, {...}), will enable watching. Please refer to slimfit documentation for fitOptions and watchOptions
    • chart.stopFitWatcher() will disable the watcher.
  • dispatches event resize when the chart is resized.
    • chart.on('resize', listener) is then use to register what to do after the chart is resized.
  • defines two main input channels .data(...) and .options(...) and dispatches event data and options when they are changed, respectively.
    • chart.data(data) get/set data.
    • chart.options(options) get/merge options
    • chart.on('data', listener)
    • chart.on('options', listener)
  • assumes little about how you implement a chart. You can extends the class and implements it the way you want.

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.

  • An SvgChart is an AbstractChart that has an SvgPlate in it.
  • A CanvasChart is an AbstractChart that has a CanvasPlate in it.
  • A HybridChart, as you may guess, is an AbstractChart that has two plates (CanvasPlate and SvgPlate) in it.

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

Authors

License

© 2015-2017 Twitter, Inc. MIT License