Published
Edited
Nov 28, 2020
5 forks
Importers
20 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// TODO: Should we have a default for the filter on the zoom? What about key controls for the actions? Need to make zoom events not clash
// TODO: Currently all underlying zoom methods available on zoomable, is this the best option? Should exclude any?
// TODO: This does not currently add zoom transform data to the state, do not want the transform to be mutable and cascade (although declarative updating would be nice). Can maybe do this if necessary as an enumerable readonly getter?
// Mix zoom behavior into a usmap object. Returns a configurable function that takes a second 'name' argument. Use the name argument to provide a name to the zoom transition, if not provided subsequent calls will replace the zoom event on any previously run selections.
function zoomable(mapContainer) {
let sync = true;
let ease = zoomableDefaults.ease;
const zoomSymbol = Symbol('zoomable');
const zoom = d3.zoom()
.duration(zoomableDefaults.duration)
.scaleExtent(zoomableDefaults.scaleExtent)
.extent(zoomableDefaults.extent)
.translateExtent(zoomableDefaults.extent);
function zoomable(selection, name) {
// First call the container to ensure the elements are set up properly
const mapGroup = mapContainer(selection);
// Then bind the zoom transform and input event emitter
zoom.on(`zoom.usmap-base${name && `-${name}`}`, function () {
const { transform } = d3.event;
const el = this;
// When in sync mode apply same transform to all elements in the selection
// Need to use the this.__zoom escape hatch to avoid triggering an infinite loop of events
// When not synced must only transform the target element, not the entire selection
if (sync) {
mapGroup.attr('transform', transform);
selection.each(function () { if (this !== el) this.__zoom = transform });
} else {
d3.select(el).select('g.usMap').attr('transform', transform);
}
});
// Add the zoom handlers to each container svg if not previously done
// This will only add the behavior once, letting the user unset the animations as per d3-zoom docs
// Use zoomable.clear(selection) to scrub the zoom behavior from the selection
selection.each(function () {
if (!this[zoomSymbol]) {
d3.select(this)
.call(zoom)
.append('rect')
.attr('width', usMapViewBox[0])
.attr('height', usMapViewBox[1])
.attr('fill', 'none')
.attr('pointer-events', 'visible')
.lower();
this[zoomSymbol] = true;
}
});
return mapGroup;
}
Object.assign(assignAllMethods(zoom, zoomable), mapContainer);
/**
* Clears all zoom behavior on the passed selection and resets the default ease, duration, extents
* Calling the zoomable on the selection again will re-add all the handlers
*/
zoomable.clear = function (selection) {
zoomable
.ease(zoomableDefaults.ease)
.duration(zoomableDefaults.duration)
.scaleExtent(zoomableDefaults.scaleExtent)
.extent(zoomableDefaults.extent)
.translateExtent(zoomableDefaults.translateExtent);
selection.on('.zoom', null);
selection.on('click.zoomReset', null);
selection.each(function () { this[zoomSymbol] = undefined });
return this;
};
// Sets whether the zooms applied to the same selection should by synced or run separately
zoomable.sync = function (_) { return _ === undefined ? sync : (sync = _, this); }
zoomable.ease = function (_) { return _ === undefined ? ease : (ease = _, this); }
return zoomable;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
viewof zcExample = zoomControls({ vertical: false })
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more