Skip to content

Instantly share code, notes, and snippets.

View mbostock's full-sized avatar
📊
Plottin’

Mike Bostock mbostock

📊
Plottin’
View GitHub Profile
@mbostock
mbostock / .block
Last active August 9, 2019 05:54
Mona Lisa Histogram
license: gpl-3.0
height: 1452
redirect: https://observablehq.com/@d3/mona-lisa-histogram
@mbostock
mbostock / .block
Last active August 8, 2019 16:01
Drag & Zoom II
license: gpl-3.0
redirect: https://observablehq.com/@d3/drag-zoom
@mbostock
mbostock / .block
Last active December 10, 2019 17:13
Drag & Zoom
license: gpl-3.0
@mbostock
mbostock / README.md
Last active December 21, 2023 19:06
Local Variables

It’s often desirable when using D3 to define local behavior, that is, behavior that is specific to an individual element, rather than the same for all elements in a selection. The simplest example of this is passing a function to selection.attr to compute an attribute value for each element.

But what happens if your local behavior is more complicated? What if you want multiple operations (multiple attributes, or elements) to have local behavior, but still share local state between them? For instance, when rendering small multiples of time-series data, you might want the same x-scale for all charts but distinct y-scales to compare the relative (not absolute) performance of each metric.

There are several ways to do this in D3:

  1. Make the y-scale global, but set the domain on the y-scale before use. (Example.)

  2. Use selection.each to create a local con

@mbostock
mbostock / .block
Last active July 4, 2016 13:37
Pan & Zoom V
license: gpl-3.0
@mbostock
mbostock / .block
Last active January 12, 2023 06:16
Pan & Zoom IV
license: gpl-3.0
redirect: https://observablehq.com/@d3/zoom-svg-rescaled
@mbostock
mbostock / .block
Last active October 7, 2023 20:01
Pan & Zoom III
license: gpl-3.0
redirect: https://observablehq.com/@d3/zoom
@mbostock
mbostock / .block
Last active January 12, 2023 06:16
Pan & Zoom II
license: gpl-3.0
redirect: https://observablehq.com/@d3/zoom-canvas-rescaled
@mbostock
mbostock / .block
Last active August 12, 2019 04:07
Pan & Zoom I
license: gpl-3.0
redirect: https://observablehq.com/@d3/zoom-canvas
@mbostock
mbostock / .block
Last active April 3, 2020 23:49
Pan & Zoom Axes
license: gpl-3.0
redirect: https://observablehq.com/@d3/zoomable-scatterplot