3.5.5 • Published 9 years ago

d3-jsnext v3.5.5

Weekly downloads
5
License
-
Repository
github
Last release
9 years ago

d3-jsnext

d3, ported to ES6 modules.

Why?

Because most d3 projects only use a tiny fraction of d3's code. It has so much stuff in there - data manipulation, DOM stuff, AJAX, string interpolators, etc, etc... that you just don't need if you only want to (for example) generate a map projection function.

You can build d3 yourself, only including the bits that you need, but... can you really be bothered? And when you decide you want to use another d3 function that isn't in the build, do you waste time rebuilding it, or waste time implementing the function yourself?

ES6 modules allow you to selectively import the bits of a library that you actually need.

// Instead of this...
var d3 = require( 'd3' );
var projection = d3.geo.albers();

// ...write this:
import { albers } from 'd3-jsnext';
var projection = albers();

How does it work?

ES6 modules were designed from the ground for static analysis, which lets us determine which bits of a library you're using. (With CommonJS (i.e. node/browserify) and AMD modules, that just isn't possible.)

But you have to use the right tools. rollup is an ES6 module bundle that's smart enough to only include the right bits of code.

Does it really make that much difference?

Yes! Minified and gzipped, d3 weighs around 53kb. An adaptation of the Voronoi Tesselation example - you can see it here - weighs just 8kb, including the example code itself. Depending on which of d3's functions you're actually using, the savings can be even more dramatic.

How do I use it?

Check out the examples folder to see how to use d3-jsnext. You need to have a build step which takes your source code and bundles it up, but it can be as simple as typing a single command into your terminal. In theory, any ES6-aware module bundler will work, but for best results use rollup.

Note that whereas the examples use a relative path, in your own projects you should import from 'd3-jsnext'.

Table of import names

With regular d3, functions live in namespaces, like d3.geom.voronoi. ES6 modules don't work like that - instead, you import from a single namespace:

import { voronoi } from 'd3';
var layout = voronoi()...

In most cases, the import name is just the last part of the name as it was before. In a few cases (e.g d3.svg.line.radial and d3.svg.diagonal.radial) that introduces conflicts, so refer to the following table:

oldnew
d3.ascendingascending
d3.bisectorbisector
d3.bisectbisect
d3.bisectRightbisectRight
d3.bisectLeftbisectLeft
d3.descendingdescending
d3.deviationdeviation
d3.entriesentries
d3.extentextent
d3.keyskeys
d3.mapmap
d3.maxmax
d3.meanmean
d3.medianmedian
d3.mergemerge
d3.minmin
d3.nestnest
d3.pairspairs
d3.permutepermute
d3.quantilequantile
d3.rangerange
d3.setset
d3.shuffleshuffle
d3.sumsum
d3.transposetranspose
d3.valuesvalues
d3.variancevariance
d3.zipzip
d3.behaviorbehavior
d3.behavior.dragdrag
d3.behavior.zoomzoom
d3.colorcolor
d3.hclhcl
d3.hslhsl
d3.lablab
d3.rgbrgb
d3.functorfunctor
d3.nsns
d3.rebindrebind
d3.csvcsv
d3.dsvdsv
d3.tsvtsv
d3.dispatchdispatch
d3.mousemouse
d3.timer.flushflush
d3.timertimer
d3.touchtouch
d3.touchestouches
d3.formatformat
d3.formatPrefixformatPrefix
d3.requoterequote
d3.roundround
d3.geo.albersUsaalbersUsa
d3.geo.albersalbers
d3.geo.areageo_area
d3.geo.azimuthalEqualAreaazimuthalEqualArea
d3.geo.azimuthalEquidistantazimuthalEquidistant
d3.geo.boundsbounds
d3.geo.centroidcentroid
d3.geo.circlecircle
d3.geo.clipExtentclipExtent
d3.geo.conicConformalconicConformal
d3.geo.conicEqualAreaconicEqualArea
d3.geo.conicEquidistantconicEquidistant
d3.geo.distancedistance
d3.geo.equirectangularequirectangular
d3.geogeo
d3.geo.gnomonicgnomonic
d3.geo.graticulegraticule
d3.geo.greatArcgreatArc
d3.geo.interpolategeo_interpolate
d3.geo.lengthlength
d3.geo.mercatormercator
d3.geo.orthographicorthographic
d3.geo.pathpath
d3.geo.projectionMutatorprojectionMutator
d3.geo.projectionprojection
d3.geo.rotationrotation
d3.geo.stereographicstereographic
d3.geo.streamstream
d3.geo.transformgeo_transform
d3.geo.transverseMercatortransverseMercator
d3.geom.delaunaydelaunay
d3.geomgeom
d3.geom.hullhull
d3.geom.polygonpolygon
d3.geom.quadtreequadtree
d3.geom.voronoivoronoi
d3.interpolateArrayinterpolateArray
d3.easeease
d3.interpolateHclinterpolateHcl
d3.interpolateHslinterpolateHsl
d3.interpolatorsinterpolators
d3.interpolateinterpolate
d3.interpolateLabinterpolateLab
d3.interpolateNumberinterpolateNumber
d3.interpolateObjectinterpolateObject
d3.interpolateRgbinterpolateRgb
d3.interpolateRoundinterpolateRound
d3.interpolateStringinterpolateString
d3.interpolateTransforminterpolateTransform
d3.interpolateZoominterpolateZoom
d3.layout.bundlebundle
d3.layout.chordlayout_chord
d3.layout.clustercluster
d3.layout.forceforce
d3.layout.hierarchyhierarchy
d3.layout.histogramhistogram
d3.layoutlayout
d3.layout.packpack
d3.layout.partitionpartition
d3.layout.piepie
d3.layout.stackstack
d3.layout.treetree
d3.layout.treemaptreemap
d3.locale.ca_ESca_ES
d3.locale.de_DEde_DE
d3.locale.en_GBen_GB
d3.locale.es_ESes_ES
d3.locale.fi_FIfi_FI
d3.locale.fr_FRfr_FR
d3.locale.he_ILhe_IL
d3.localelocale
d3.locale.mk_MKmk_MK
d3.locale.nl_NLnl_NL
d3.locale.pt_BRpt_BR
d3.locale.ru_RUru_RU
d3.locale.zh_CNzh_CN
d3.randomrandom
d3.transformtransform
d3.scale.category20ccategory20c
d3.scale.category20bcategory20b
d3.scale.category20category20
d3.scale.category10category10
d3.scale.identityidentity
d3.scale.linearlinear
d3.scale.loglog
d3.scale.ordinalordinal
d3.scale.powpow
d3.scale.quantilescale_quantile
d3.scale.quantizequantize
d3.scalescale
d3.scale.sqrtsqrt
d3.scale.thresholdthreshold
d3.selectAllselectAll
d3.selectselect
d3.selection.enterenter
d3.selectionselection
d3.svg.arcarc
d3.svg.area.radialarea_radial
d3.svg.areasvg_area
d3.svg.axisaxis
d3.svg.brushbrush
d3.svg.chordsvg_chord
d3.svg.diagonal.radialdiagonal_radial
d3.svg.diagonaldiagonal
d3.svg.line.radialline_radial
d3.svg.lineline
d3.svgsvg
d3.svg.symbolTypessymbolTypes
d3.svg.symbolsymbol
d3.timetime
d3.transitiontransition
d3.htmlhtml
d3.jsonjson
d3.texttext
d3.xhrxhr
d3.xmlxml

Building and testing

First, you need a local copy of d3's source code, plus its devDependencies:

npm run update-d3

Generating the ES6 modules and tests

npm run build

This will generate transformed tests and source code in the test and src folders respectively.

Running the tests

Err... the tests don't quite work yet. In progress! (It's tricky, because the test suite makes a number of perfectly justifiable assumptions that no longer hold.)

License etc

d3 is copyright Mike Bostock - license here.

d3-jsnext copyright Rich Harris and released under the MIT license.

Feedback

I'm @Rich_Harris on Twitter.