Skip to content

Commit

Permalink
Synchronize graphs on zoom (#1315)
Browse files Browse the repository at this point in the history
  • Loading branch information
VillageR88 authored Sep 29, 2023
1 parent 10b6150 commit f06382b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 30 deletions.
35 changes: 21 additions & 14 deletions src/utils/uPlot/mouseZoomPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ interface WheelZoomPluginOptions {
animationDuration?: number;
}

const chartInstances: uPlot[] = [];

function wheelZoomPlugin(options: WheelZoomPluginOptions = {}): Plugin {
const factor = options.factor || 0.9;

Expand All @@ -15,8 +17,8 @@ function wheelZoomPlugin(options: WheelZoomPluginOptions = {}): Plugin {
let yMax: number;
let xRange: number;
let yRange: number;
let over = null;
let rect: DOMRect;
let over: HTMLElement | null = null;
let rect: DOMRect | null = null;

function isCtrlPressed(e: MouseEvent): boolean {
return e.ctrlKey || e.metaKey;
Expand Down Expand Up @@ -50,6 +52,8 @@ function wheelZoomPlugin(options: WheelZoomPluginOptions = {}): Plugin {
return {
hooks: {
ready(u: uPlot) {
chartInstances.push(u); // Add the current chart instance to the list

xMin = u.scales.x.min ?? 0;
xMax = u.scales.x.max ?? 0;
yMin = u.scales.y.min ?? 0;
Expand All @@ -59,7 +63,7 @@ function wheelZoomPlugin(options: WheelZoomPluginOptions = {}): Plugin {
over = u.over;
rect = over.getBoundingClientRect();

over.addEventListener('mousedown', (e: MouseEvent) => {
over?.addEventListener('mousedown', (e: MouseEvent) => {
if (e.button === 1) {
e.preventDefault();

Expand All @@ -84,7 +88,7 @@ function wheelZoomPlugin(options: WheelZoomPluginOptions = {}): Plugin {
}
});

over.addEventListener('wheel', (e: WheelEvent) => {
over?.addEventListener('wheel', (e: WheelEvent) => {
e.preventDefault();

if (!isCtrlPressed(e)) {
Expand All @@ -94,8 +98,8 @@ function wheelZoomPlugin(options: WheelZoomPluginOptions = {}): Plugin {
const cursor = u.cursor;

const { left, top } = cursor;
const leftPct = (left || 0) / rect.width;
const btmPct = 1 - (top || 0) / rect.height;
const leftPct = (left || 0) / (rect?.width || 1);
const btmPct = 1 - (top || 0) / (rect?.height || 1);
const xVal = u.posToVal(left || 0, 'x');
const yVal = u.posToVal(top || 0, 'y');
const oxRange = (u.scales.x.max || 0) - (u.scales.x.min || 0);
Expand All @@ -111,15 +115,18 @@ function wheelZoomPlugin(options: WheelZoomPluginOptions = {}): Plugin {
let nyMax = nyMin + nyRange;
[nyMin, nyMax] = clamp(nyRange, nyMin, nyMax, yRange, yMin, yMax);

u.batch(() => {
u.setScale('x', {
min: nxMin,
max: nxMax,
});
// Loop through all chart instances and apply the same zoom to each of them
chartInstances.forEach((chartInstance) => {
chartInstance.batch(() => {
chartInstance.setScale('x', {
min: nxMin,
max: nxMax,
});

u.setScale('y', {
min: nyMin,
max: nyMax,
chartInstance.setScale('y', {
min: nyMin,
max: nyMax,
});
});
});
});
Expand Down
34 changes: 18 additions & 16 deletions src/utils/uPlot/touchZoomPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ interface Point {
dy: number;
}

const chartInstances: uPlot[] = [];

const touchZoomPlugin = () => {
const init = (u: uPlot, _opts: any, _data: any) => {
const { over } = u;
Expand Down Expand Up @@ -56,17 +58,12 @@ const touchZoomPlugin = () => {

let rafPending = false;

const zoom = () => {
const zoomCharts = () => {
rafPending = false;

const left = to.x;
const top = to.y;

// non-uniform scaling
// let xFactor = fr.dx / to.dx;
// let yFactor = fr.dy / to.dy;

// uniform x/y scaling
const xFactor = fr.d! / to.d!;
const yFactor = fr.d! / to.d!;

Expand All @@ -81,15 +78,18 @@ const touchZoomPlugin = () => {
const nyMin = yVal - btmPct * nyRange;
const nyMax = nyMin + nyRange;

u.batch(() => {
u.setScale('x', {
min: nxMin,
max: nxMax,
});

u.setScale('y', {
min: nyMin,
max: nyMax,
// Loop through all chart instances and apply the same zoom to each of them
chartInstances.forEach((chartInstance) => {
chartInstance.batch(() => {
chartInstance.setScale('x', {
min: nxMin,
max: nxMax,
});

chartInstance.setScale('y', {
min: nyMin,
max: nyMax,
});
});
});
};
Expand All @@ -99,7 +99,7 @@ const touchZoomPlugin = () => {

if (!rafPending) {
rafPending = true;
requestAnimationFrame(zoom);
requestAnimationFrame(zoomCharts);
}
};

Expand Down Expand Up @@ -128,6 +128,8 @@ const touchZoomPlugin = () => {
over.addEventListener('touchend', (_e: TouchEvent) => {
document.removeEventListener('touchmove', touchmove);
});

chartInstances.push(u); // Add the current chart instance to the list
};

return {
Expand Down

0 comments on commit f06382b

Please sign in to comment.