Skip to content

pointer dataflow #1630

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

Merged
merged 4 commits into from
May 24, 2023
Merged
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
6 changes: 4 additions & 2 deletions src/interactions/pointer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function pointerK(kx, ky, {x, y, px, py, maxRadius = 40, channels, render, ...op
// response to pointer events.
render: composeRender(function (index, scales, values, dimensions, context, next) {
const svg = context.ownerSVGElement;
const {data} = context.getMarkState(this);

// Isolate state per-pointer, per-plot; if the pointer is reused by
// multiple marks, they will share the same state (e.g., sticky modality).
Expand Down Expand Up @@ -118,8 +119,9 @@ function pointerK(kx, ky, {x, y, px, py, maxRadius = 40, channels, render, ...op
}
g.replaceWith(r);
}
state.roots[renderIndex] = r;
return (g = r);
state.roots[renderIndex] = g = r;
context.dispatchValue(i == null ? null : data[i]);
return r;
}

function pointermove(event) {
Expand Down
3 changes: 3 additions & 0 deletions src/plot.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,9 @@ export interface Plot {
* scales.
*/
legend(name: ScaleName, options?: LegendOptions): SVGSVGElement | HTMLElement | undefined;

/** For interactive plots, the current value. */
value?: any;
}

/**
Expand Down
9 changes: 8 additions & 1 deletion src/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export function plot(options = {}) {
const context = createContext(options);
const document = context.document;
const svg = creator("svg").call(document.documentElement);
let figure = svg; // replaced with the figure element, if any
context.ownerSVGElement = svg;
context.className = className;
context.projection = createProjection(options, subdimensions);
Expand All @@ -169,6 +170,13 @@ export function plot(options = {}) {
return {...state, channels: {...state.channels, ...facetState?.channels}};
};

// Allows e.g. the pointer transform to support viewof.
context.dispatchValue = (value) => {
if (figure.value === value) return;
figure.value = value;
figure.dispatchEvent(new Event("input", {bubbles: true}));
};

// Reinitialize; for deriving channels dependent on other channels.
const newByScale = new Set();
for (const [mark, state] of stateByMark) {
Expand Down Expand Up @@ -311,7 +319,6 @@ export function plot(options = {}) {
}

// Wrap the plot in a figure with a caption, if desired.
let figure = svg;
const legends = createLegends(scaleDescriptors, context, options);
if (caption != null || legends.length > 0) {
figure = document.createElement("figure");
Expand Down
Loading