Skip to content

Commit

Permalink
Add Synchronize graphs on mobile/touch
Browse files Browse the repository at this point in the history
Signed-off-by: VillageR88 <[email protected]>
  • Loading branch information
VillageR88 committed Sep 29, 2023
1 parent 02a9cc9 commit c4d9fbc
Showing 1 changed file with 18 additions and 16 deletions.
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 c4d9fbc

Please sign in to comment.