Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plot.rescale #2251

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export function plot(options = {}) {
stateByMark.set(mark, {data, facets, channels});
}

// Initalize the scales and dimensions.
// Initialize the scales and dimensions.
const scaleDescriptors = createScales(addScaleChannels(channelsByScale, stateByMark, options), options);
const dimensions = createDimensions(scaleDescriptors, marks, options);

Expand All @@ -159,6 +159,11 @@ export function plot(options = {}) {
context.className = className;
context.projection = createProjection(options, subdimensions);

// A path generator for marks that want to draw GeoJSON.
context.path = function () {
return geoPath(this.projection ?? xyProjection(scales));
};

mbostock marked this conversation as resolved.
Show resolved Hide resolved
// Allows e.g. the axis mark to determine faceting lazily.
context.filterFacets = (data, channels) => {
return facetFilter(facets, {channels, groups: facetGroups(data, channels)});
Expand Down Expand Up @@ -236,11 +241,6 @@ export function plot(options = {}) {
facetTranslate = facetTranslator(fx, fy, dimensions);
}

// A path generator for marks that want to draw GeoJSON.
context.path = function () {
return geoPath(this.projection ?? xyProjection(scales));
};

// Compute value objects, applying scales and projection as needed.
for (const [mark, state] of stateByMark) {
state.values = mark.scale(state.channels, scales, context);
Expand Down Expand Up @@ -357,6 +357,17 @@ export function plot(options = {}) {
.text(`${w.toLocaleString("en-US")} warning${w === 1 ? "" : "s"}. Please check the console.`);
}

figure.rescale = (rescales) => {
const reoptions = {...options, figure: false};
for (const key in rescales) {
if (!(key in scales.scales)) throw new Error(`missing scale: ${key}`);
reoptions[key] = {...scales.scales[key], ...rescales[key]};
}
const resvg = plot(reoptions);
while (svg.lastChild) svg.removeChild(svg.lastChild);
while (resvg.firstChild) svg.appendChild(resvg.firstChild);
};

return figure;
}

Expand Down
1 change: 1 addition & 0 deletions test/plots/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ export * from "./raster-vapor.js";
export * from "./raster-walmart.js";
export * from "./rect-band.js";
export * from "./reducer-scale-override.js";
export * from "./rescale.js";
export * from "./rounded-rect.js";
export * from "./seattle-precipitation-density.js";
export * from "./seattle-precipitation-rule.js";
Expand Down
18 changes: 18 additions & 0 deletions test/plots/rescale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as Plot from "@observablehq/plot";
import * as d3 from "d3";

export async function rescaleZoom() {
const data = await d3.csv<any>("data/gistemp.csv", d3.autoType);
const plot = Plot.dot(data, {x: "Date", y: "Anomaly", stroke: "Anomaly"}).plot();
requestAnimationFrame(() => {
let frame: number;
(function tick(now) {
if (!plot.isConnected) return cancelAnimationFrame(frame);
const t = (Math.sin(now / 2000) + 1) / 2;
const [x1, x2] = plot.scale("x").domain;
plot.rescale({x: {domain: [+x1 + ((x2 - x1) / 2) * t, +x1 + ((x2 - x1) / 2) * (t + 1)]}});
frame = requestAnimationFrame(tick);
})(performance.now());
});
return plot;
}
Loading