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

Omni x/y zoom #221

Closed
wants to merge 1 commit into from
Closed
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
61 changes: 61 additions & 0 deletions demos/zoom-variations.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<!doctype html>
<html>

<head>
<meta charset="utf-8">
<title>Zoom Variations</title>
<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="../dist/uPlot.min.css">
<style>
.uplot {
display: inline-block;
width: 400px;
}
</style>
</head>

<body>
<script src="../dist/uPlot.iife.js"></script>
<script>
let xs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30];
let vals = [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

let data = [
xs,
xs.map((t, i) => vals[Math.floor(Math.random() * vals.length)])
];

const opts = (title, dragOpts) => ({
title: title,
width: 400,
height: 200,
scales: {
x: {
time: false,
},
},
cursor: {
drag: dragOpts
},
series: [
{},
{
stroke: "purple"
}
],
});

let variations = [
['X only', { x: true, y: false }],
['Y only', { x: false, y: true }],
['X or Y (adaptive)', { x: true, y: true, uni: Infinity }],
['X or Y (omni)', { x: true, y: true }],
['X or Y (adaptive + omni)', { x: true, y: true, uni: 20 }],
];

variations.forEach((variation) => new uPlot(opts(...variation), data, document.body));
</script>
</body>

</html>
1 change: 1 addition & 0 deletions dist/uPlot.esm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ declare namespace uPlot {
setScale?: boolean; // true
x?: boolean; // true
y?: boolean; // false
uni?: number; // null
};

/** sync cursor between multiple charts */
Expand Down
1 change: 1 addition & 0 deletions src/opts.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ export const cursorOpts = {
setScale: true,
x: true,
y: false,
uni: null
},

focus: {
Expand Down
37 changes: 35 additions & 2 deletions src/uPlot.js
Original file line number Diff line number Diff line change
Expand Up @@ -1611,18 +1611,51 @@ export default function uPlot(opts, data, then) {
// nit: cursor.drag.setSelect is assumed always true
if (mouseLeft1 >= 0 && select.show && dragging) {
// setSelect should not be triggered on move events
if (drag.x) {

let dragX = drag.x;
let dragY = drag.y;
let uni = drag.uni;

if (uni != null) {
let xDistance = abs(mouseLeft0 - mouseLeft1);
let yDistance = abs(mouseTop0 - mouseTop1);

if (xDistance < uni)
dragX = false;
if (yDistance < uni)
dragY = false;

// neither x or y passed the threshold, we are in the magenta box (#219). In this
// scenario, we choose a unidirectional zoom based on which point is furthest from
// the origin M0
if (!dragX && !dragY) {
if (xDistance > yDistance)
dragX = true;
else
dragY = true;
}
}

if (dragX) {
let minX = min(mouseLeft0, mouseLeft1);
let maxX = max(mouseLeft0, mouseLeft1);
setStylePx(selectDiv, LEFT, select[LEFT] = minX);
setStylePx(selectDiv, WIDTH, select[WIDTH] = maxX - minX);
if (uni != null && !dragY) {
setStylePx(selectDiv, TOP, select[TOP] = 0);
setStylePx(selectDiv, HEIGHT, select[HEIGHT] = plotHgtCss);
}
}

if (drag.y) {
if (dragY) {
let minY = min(mouseTop0, mouseTop1);
let maxY = max(mouseTop0, mouseTop1);
setStylePx(selectDiv, TOP, select[TOP] = minY);
setStylePx(selectDiv, HEIGHT, select[HEIGHT] = maxY - minY);
if (uni != null && !dragX) {
setStylePx(selectDiv, LEFT, select[LEFT] = 0);
setStylePx(selectDiv, WIDTH, select[WIDTH] = plotWidCss);
}
}
}

Expand Down