-
Notifications
You must be signed in to change notification settings - Fork 7
/
common.js
174 lines (138 loc) · 3.84 KB
/
common.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
function tooltipsPlugin(opts) {
let cursortt;
function init(u, opts, data) {
let over = u.over;
let tt = cursortt = document.createElement("div");
tt.className = "tooltip";
tt.textContent = "y";
tt.style.pointerEvents = "none";
tt.style.position = "absolute";
tt.style.background = "rgba(0,0,255,0.1)";
tt.style.display = "none";
over.appendChild(tt);
over.addEventListener("mouseleave", () => {
if (!u.cursor._lock)
cursortt.style.display = "none";
});
over.addEventListener("mouseenter", () => {
cursortt.style.display = null;
});
}
function setCursor(u) {
const {left, top, idx} = u.cursor;
cursortt.style.left = left + 10 + "px";
cursortt.style.top = top + 10 + "px";
cursortt.textContent = u.posToVal(top, "y").toFixed(5) + " Bytes/Cycle";
}
return { hooks: { init, setCursor, }, };
}
function wheelZoomPlugin(opts) {
let factor = opts.factor || 0.75;
return { hooks: { ready: u => {
let rect = u.over.getBoundingClientRect();
u.over.addEventListener("wheel", e => {
e.preventDefault();
let {left, top} = u.cursor;
let ys = e.deltaY < 0 ? factor : 2-factor;
let y1 = rect.height - top;
let y2 = top;
let nyMax = u.posToVal(top + y1*ys, "y");
let nyMin = u.posToVal(top - y2*ys, "y");
u.batch(() => { u.setScale("y", { min: nyMin, max: nyMax, }); });
});
} } };
}
// we patch this in later, with out this the zooming somethimes hangs
function myLogAxisSplits(self, axisIdx, scaleMin, scaleMax, foundIncr, foundSpace, forceMin) {
const splits = [];
const logBase = self.scales[self.axes[axisIdx].scale].log;
const logFn = logBase == 10 ? log10 : log2;
const exp = floor(logFn(scaleMin));
foundIncr = pow(logBase, exp);
if (logBase == 10 && exp < 0)
foundIncr = roundDec(foundIncr, -exp);
let split = scaleMin;
do {
splits.push(split);
split = split + foundIncr;
let dec = fixedDec.get(foundIncr);
if (dec == undefined) // this part changed
break;
if (logBase == 10)
split = roundDec(split, dec);
if (split >= foundIncr * logBase)
foundIncr = split;
} while (split <= scaleMax);
return splits;
}
// https://stackoverflow.com/questions/17242144/javascript-convert-hsb-hsv-color-to-rgb-accurately
function hsv2rgb(h,s,v) {
let f= (n,k=(n+h*6)%6) => v - v*s*Math.max( Math.min(k,4-k,1), 0);
return [f(5),f(3),f(1)].reduce((a,v)=>a+(~~(v*256)).toString(16).padStart(2,"0"),"#");
}
let colorMap = {};
function randColor(lbl) {
let c = colorMap[lbl];
if (c == undefined)
c = colorMap[lbl] = hsv2rgb(Math.random(), Math.random()*0.4 + 0.6, Math.random()*0.4 + 0.6);
return c;
}
function makeChart(info) {
let series = [];
for (let i = 1; i < info.data.length; i++) {
series.push({
label: info.labels[i],
stroke: randColor(info.labels[i]),
value: (self, v) => v == null ? null : v.toString(),
});
}
function getSize() {
return {
width: window.innerWidth * 0.99,
height: window.innerHeight * 0.8,
}
}
let opts = {
title: info.title,
...getSize(),
plugins: [
wheelZoomPlugin({factor: 0.75}),
tooltipsPlugin(),
],
scales: {
x: { time: false, distr: 2 },
y: { distr: 3, },
},
axes: [ {
label: "data size",
values: (self, splits) => splits.map(v => {
if (v == null) return null;
if (v < 1024)
return v.toString() + " B";
else if ((v /= 1024) < 1024)
return v.toFixed(1) + " KiB";
else if ((v /= 1024) < 1024)
return v.toFixed(1) + " MiB";
else
return (v/1024).toFixed(1) + " GiB";
})
}, {
label: "Byte/Cycle",
},
],
series: [
{},
...series
]
};
let u = new uPlot(opts, info.data, document.body);
u.axes.forEach( x => {
if (x.splits == logAxisSplits)
x.splits = myLogAxisSplits;
});
window.addEventListener("resize", e => {
u.setSize(getSize());
});
return u;
}
document.getElementById("main").insertAdjacentHTML('beforeend', prefix);