Skip to content

Commit 631b4bb

Browse files
committed
add draggable y scales demo (#902)
1 parent 0c97825 commit 631b4bb

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

Diff for: demos/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ <h1>μPlot Demos</h1>
7979
<a href="line-paths.html">Bars, stepped &amp; smooth lines</a>
8080
<a href="gradients.html">Gradient fills &amp; strokes (vt &amp; hz, scale-affixed &amp; data-relative)</a>
8181
<a href="scales-dir-ori.html">Scale direction &amp; orientation (e.g. rotation, inversion)</a>
82+
<a href="y-scale-drag.html">Draggable y scales via axes</a>
8283

8384
<a href="months-ru.html">Russian month names on date/time axis</a>
8485
<a href="add-del-series.html">Dynamically add or delete series</a>

Diff for: demos/y-scale-drag.html

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Draggable y scales</title>
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
8+
<link rel="stylesheet" href="../dist/uPlot.min.css">
9+
</head>
10+
<body>
11+
<script type="module">
12+
import uPlot from "../dist/uPlot.esm.js";
13+
14+
const opts = {
15+
width: 600,
16+
height: 350,
17+
hooks: {
18+
init: [
19+
u => {
20+
let axisEls = u.root.querySelectorAll('.u-axis');
21+
22+
for (let i = 0; i < axisEls.length; i++) {
23+
if (i > 0) {
24+
let el = axisEls[i];
25+
26+
el.addEventListener('mousedown', e => {
27+
let y0 = e.clientY;
28+
let scaleKey = u.axes[i].scale;
29+
let scale = u.scales[scaleKey];
30+
let { min, max } = scale;
31+
let unitsPerPx = (max - min) / (u.bbox.height / uPlot.pxRatio);
32+
33+
let mousemove = e => {
34+
let dy = e.clientY - y0;
35+
let shiftyBy = dy * unitsPerPx;
36+
37+
u.setScale(scaleKey, {
38+
min: min + shiftyBy,
39+
max: max + shiftyBy,
40+
});
41+
};
42+
43+
let mouseup = e => {
44+
document.removeEventListener('mousemove', mousemove);
45+
document.removeEventListener('mousemove', mouseup);
46+
};
47+
48+
document.addEventListener('mousemove', mousemove);
49+
document.addEventListener('mouseup', mouseup);
50+
});
51+
52+
}
53+
}
54+
},
55+
]
56+
},
57+
scales: {
58+
x: {
59+
time: false,
60+
},
61+
/*
62+
'meter': {
63+
auto: false,
64+
min: 0,
65+
max: 5,
66+
},
67+
'km/h': {
68+
auto: false,
69+
min: 0,
70+
max: 10,
71+
}
72+
*/
73+
},
74+
series: [
75+
{},
76+
{
77+
label: 'Price',
78+
stroke: 'red',
79+
scale: 'meter',
80+
},
81+
{
82+
label: 'Amount',
83+
stroke: 'blue',
84+
scale: 'km/h',
85+
}
86+
],
87+
axes: [
88+
{},
89+
{
90+
scale: 'meter',
91+
label: 'km/h'
92+
},
93+
{
94+
scale: 'km/h',
95+
label: 'meter'
96+
}
97+
],
98+
};
99+
100+
const data = [
101+
[0, 1, 2, 3, 4],
102+
[1, 3, 2, 4, 3],
103+
[6, 8, 3, 7, 9],
104+
];
105+
106+
let u = new uPlot(opts, data, document.body)
107+
</script>
108+
</body>
109+
</html>

0 commit comments

Comments
 (0)