Skip to content

Commit e579947

Browse files
committed
timeseries-discrete demo. close #939.
1 parent 7c1be93 commit e579947

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

demos/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ <h1>μPlot Demos</h1>
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>
8282
<a href="y-scale-drag.html">Draggable y scales via axes</a>
83+
<a href="timeseries-discrete.html">Discrete &amp; shifted series w/sync</a>
8384
<a href="update-cursor-select-resize.html">Maintains location of cursor/select/hoverPts during resize (test)</a>
8485

8586
<a href="months-ru.html">Russian month names on date/time axis</a>

demos/timeseries-discrete.html

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>TimeSeries + Discrete</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+
let fromTs = 1704067200;
15+
let stepTs = 15;
16+
let numSteps = 50;
17+
18+
let xVals = Array.from({length: numSteps}, (v, i) => fromTs + stepTs * i);
19+
20+
// floats
21+
let data1 = [
22+
xVals,
23+
Array.from({length: numSteps}, (v, i) => Math.random() * 100),
24+
];
25+
26+
const opts1 = {
27+
width: 1920,
28+
height: 600,
29+
padding: [5,10,0,0],
30+
cursor: {
31+
y: false,
32+
sync: {
33+
key: '_'
34+
}
35+
},
36+
axes: [
37+
{
38+
size: 10,
39+
gap: 0,
40+
values: (u, splits) => splits.map(v => null),
41+
}
42+
],
43+
series: [
44+
{},
45+
{
46+
stroke: "red",
47+
},
48+
],
49+
};
50+
51+
let u = new uPlot(opts1, data1, document.body);
52+
53+
54+
// discretes
55+
let data2 = [
56+
xVals,
57+
Array.from({length: numSteps}, (v, i) => Math.random() > 0.5 ? 0 : 1),
58+
Array.from({length: numSteps}, (v, i) => Math.random() > 0.2 ? 0 : 1),
59+
Array.from({length: numSteps}, (v, i) => Math.random() > 0.1 ? 0 : 1),
60+
];
61+
62+
let fmtVal = (u, v, seriesIdx) => v == null ? null : v - (seriesIdx - 1) * 2;
63+
64+
const opts2 = {
65+
width: 1920,
66+
height: 200,
67+
padding: [5,10,0,0],
68+
cursor: {
69+
y: false,
70+
sync: {
71+
key: '_'
72+
}
73+
},
74+
scales: {
75+
y: {
76+
range: [0, 5],
77+
}
78+
},
79+
axes: [
80+
{},
81+
{
82+
splits: (u) => [0,2,4],
83+
values: (u, splits) => splits.map((v, i) => `DEV${i + 1}`),
84+
},
85+
],
86+
series: [
87+
{},
88+
{
89+
stroke: "green",
90+
paths: uPlot.paths.stepped({align: 1}),
91+
value: fmtVal,
92+
},
93+
{
94+
stroke: "blue",
95+
paths: uPlot.paths.stepped({align: 1}),
96+
value: fmtVal,
97+
},
98+
{
99+
stroke: "magenta",
100+
paths: uPlot.paths.stepped({align: 1}),
101+
value: fmtVal,
102+
},
103+
],
104+
};
105+
106+
let u2 = new uPlot(opts2, data2.map((vals, seriesIdx) => {
107+
if (seriesIdx == 0)
108+
return vals;
109+
110+
return vals.map(v => v + (seriesIdx - 1) * 2);
111+
}), document.body);
112+
113+
// combine legends
114+
setTimeout(() => {
115+
let tr = u.root.querySelector('.u-series:nth-child(2)');
116+
u2.root.querySelector('.u-legend tbody').insertBefore(tr, u2.root.querySelector('.u-series:nth-child(2)'));
117+
u.root.querySelector('.u-legend').style.display = 'none';
118+
});
119+
</script>
120+
</body>
121+
</html>

0 commit comments

Comments
 (0)