-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataViewer.js
More file actions
53 lines (52 loc) · 1.45 KB
/
DataViewer.js
File metadata and controls
53 lines (52 loc) · 1.45 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
export class DataViewer extends HTMLElement {
constructor(data) {
super();
this.data = data;
this.canvas = document.createElement("canvas");
this.canvas.style.width = "100%";
this.canvas.style.height = "100%";
this.appendChild(this.canvas);
this.g = this.canvas.getContext("2d");
const show = () => {
const wc = this.canvas.clientWidth;
const hc = this.canvas.clientHeight;
const dpr = devicePixelRatio;
const w = wc * dpr;
const h = hc * dpr;
this.canvas.width = w;
this.canvas.height = h;
let miny = Number.MAX_VALUE;
let maxy = Number.MIN_VALUE;
for (let i = 0; i < this.data.length; i++) {
const n = this.data[i];
if (n < miny) {
miny = n;
}
if (n > maxy) {
maxy = n;
}
}
const g = this.g;
g.fillStyle = "white";
g.fillRect(0, 0, w, h);
//g.fillStyle = "black";
g.strokeStyle = "black";
for (let i = 0; i < w; i++) {
const i2 = Math.floor(i / w * this.data.length);
const n = this.data[i2];
const m = (h - 1) - (n - miny) / (maxy - miny) * h;
//g.fillRect(i, m, 1, 1);
if (i == 0) {
g.moveTo(i, m);
} else {
g.lineTo(i, m);
}
}
g.stroke();
};
//show();
//this.addEventListener("resize", () => show());
setTimeout(show, 1);
}
};
customElements.define("data-viewer", DataViewer);