bamtigraph 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +28 -0
- package/README.md +97 -0
- package/dist/bamtigraph.global.js +3112 -0
- package/dist/index.cjs +3161 -0
- package/dist/index.cjs.map +7 -0
- package/dist/index.d.cts +486 -0
- package/dist/index.d.ts +486 -0
- package/dist/index.js +3141 -0
- package/dist/index.js.map +7 -0
- package/package.json +76 -0
- package/src/index.ts +4069 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,3141 @@
|
|
|
1
|
+
/*! BamtiGraph 0.1.0 | BSD-3-Clause | See LICENSE */
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
var VERSION = "0.1.0";
|
|
5
|
+
var LIMITS = Object.freeze({
|
|
6
|
+
pixels: 16e6,
|
|
7
|
+
layerPixels: 4e7,
|
|
8
|
+
ticks: 5e3,
|
|
9
|
+
series: 128,
|
|
10
|
+
samples: 2e6,
|
|
11
|
+
text: 4096
|
|
12
|
+
});
|
|
13
|
+
var finite = (v) => Number.isFinite(v);
|
|
14
|
+
var round = (v) => Math.floor(v + 0.5);
|
|
15
|
+
var clamp = (v, a, b) => Math.max(a, Math.min(b, v));
|
|
16
|
+
var own = (o, k) => Object.prototype.hasOwnProperty.call(o, k);
|
|
17
|
+
var missing = (v) => v === null || v === void 0 || typeof v === "number" && Number.isNaN(v);
|
|
18
|
+
function check(ok, message) {
|
|
19
|
+
if (!ok) throw new RangeError(message);
|
|
20
|
+
}
|
|
21
|
+
function text(s, name) {
|
|
22
|
+
check(
|
|
23
|
+
typeof s === "string" && s.length <= LIMITS.text && !/[\r\n\u0000]/.test(s),
|
|
24
|
+
name + " must be a single-line string."
|
|
25
|
+
);
|
|
26
|
+
return s;
|
|
27
|
+
}
|
|
28
|
+
function number(v, name, lo = -Infinity, hi = Infinity) {
|
|
29
|
+
check(
|
|
30
|
+
finite(v) && v >= lo && v <= hi,
|
|
31
|
+
name + " is outside its finite range."
|
|
32
|
+
);
|
|
33
|
+
return v;
|
|
34
|
+
}
|
|
35
|
+
function integer(v, name, lo, hi) {
|
|
36
|
+
check(Number.isInteger(v), name + " must be an integer.");
|
|
37
|
+
return number(v, name, lo, hi);
|
|
38
|
+
}
|
|
39
|
+
function record(o) {
|
|
40
|
+
return o !== null && typeof o === "object" && (Object.getPrototypeOf(o) === Object.prototype || Object.getPrototypeOf(o) === null);
|
|
41
|
+
}
|
|
42
|
+
function clone(v) {
|
|
43
|
+
if (Array.isArray(v) || ArrayBuffer.isView(v))
|
|
44
|
+
return Array.from(v, (value) => clone(value));
|
|
45
|
+
if (v instanceof Date) return new Date(v.getTime());
|
|
46
|
+
if (record(v)) {
|
|
47
|
+
const r = {};
|
|
48
|
+
for (const k of Object.keys(v)) {
|
|
49
|
+
check(
|
|
50
|
+
!["__proto__", "prototype", "constructor"].includes(k),
|
|
51
|
+
"Unsafe property name."
|
|
52
|
+
);
|
|
53
|
+
r[k] = clone(v[k]);
|
|
54
|
+
}
|
|
55
|
+
return r;
|
|
56
|
+
}
|
|
57
|
+
return v;
|
|
58
|
+
}
|
|
59
|
+
function merge(a, b) {
|
|
60
|
+
check(record(b), "Options must be a plain object.");
|
|
61
|
+
const r = clone(a);
|
|
62
|
+
for (const k of Object.keys(b)) {
|
|
63
|
+
check(
|
|
64
|
+
!["__proto__", "prototype", "constructor"].includes(k),
|
|
65
|
+
"Unsafe property name."
|
|
66
|
+
);
|
|
67
|
+
const previous = r[k], next = b[k];
|
|
68
|
+
r[k] = record(previous) && record(next) ? merge(previous, next) : clone(next);
|
|
69
|
+
}
|
|
70
|
+
return r;
|
|
71
|
+
}
|
|
72
|
+
function freeze(o) {
|
|
73
|
+
if (o && typeof o === "object") {
|
|
74
|
+
for (const v of Object.values(o)) freeze(v);
|
|
75
|
+
Object.freeze(o);
|
|
76
|
+
}
|
|
77
|
+
return o;
|
|
78
|
+
}
|
|
79
|
+
function color(value) {
|
|
80
|
+
if (Array.isArray(value) || ArrayBuffer.isView(value)) {
|
|
81
|
+
check(
|
|
82
|
+
value.length === 3 || value.length === 4,
|
|
83
|
+
"Color needs 3 or 4 channels."
|
|
84
|
+
);
|
|
85
|
+
const c = Array.from(value);
|
|
86
|
+
c.forEach((v) => integer(v, "Color channel", 0, 255));
|
|
87
|
+
if (c.length === 3) c.push(255);
|
|
88
|
+
return c;
|
|
89
|
+
}
|
|
90
|
+
const named = {
|
|
91
|
+
black: "#000000",
|
|
92
|
+
white: "#ffffff",
|
|
93
|
+
red: "#ff0000",
|
|
94
|
+
green: "#008000",
|
|
95
|
+
blue: "#0000ff",
|
|
96
|
+
transparent: "#00000000"
|
|
97
|
+
};
|
|
98
|
+
check(
|
|
99
|
+
typeof value === "string",
|
|
100
|
+
"Color must be hexadecimal or an RGB(A) array."
|
|
101
|
+
);
|
|
102
|
+
let h = named[value.toLowerCase()] || value;
|
|
103
|
+
check(
|
|
104
|
+
/^#(?:[0-9a-f]{3}|[0-9a-f]{4}|[0-9a-f]{6}|[0-9a-f]{8})$/i.test(h),
|
|
105
|
+
"Invalid color: " + value
|
|
106
|
+
);
|
|
107
|
+
h = h.slice(1);
|
|
108
|
+
if (h.length <= 4) h = [...h].map((c) => c + c).join("");
|
|
109
|
+
if (h.length === 6) h += "ff";
|
|
110
|
+
return [0, 2, 4, 6].map((i) => parseInt(h.slice(i, i + 2), 16));
|
|
111
|
+
}
|
|
112
|
+
function epoch(value) {
|
|
113
|
+
if (value instanceof Date) value = value.getTime() / 1e3;
|
|
114
|
+
if (typeof value === "string") {
|
|
115
|
+
const v = value.trim();
|
|
116
|
+
if (/^[+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?$/i.test(v)) value = Number(v);
|
|
117
|
+
else {
|
|
118
|
+
const m = /^(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d)(?::(\d\d)(\.\d{1,3})?)?(Z|[+-]\d\d:\d\d)$/i.exec(
|
|
119
|
+
v
|
|
120
|
+
);
|
|
121
|
+
check(
|
|
122
|
+
m,
|
|
123
|
+
"Datetime strings require ISO 8601 with an explicit offset; numeric timestamps are seconds."
|
|
124
|
+
);
|
|
125
|
+
const [y, mo, d, h, mi, s] = [
|
|
126
|
+
m[1],
|
|
127
|
+
m[2],
|
|
128
|
+
m[3],
|
|
129
|
+
m[4],
|
|
130
|
+
m[5],
|
|
131
|
+
m[6] || "0"
|
|
132
|
+
].map(Number);
|
|
133
|
+
check(
|
|
134
|
+
y >= 1 && mo >= 1 && mo <= 12 && d >= 1 && h < 24 && mi < 60 && s < 60,
|
|
135
|
+
"Invalid calendar date."
|
|
136
|
+
);
|
|
137
|
+
const test = /* @__PURE__ */ new Date(0);
|
|
138
|
+
test.setUTCFullYear(y, mo - 1, d);
|
|
139
|
+
test.setUTCHours(h, mi, s, 0);
|
|
140
|
+
check(
|
|
141
|
+
test.getUTCMonth() === mo - 1 && test.getUTCDate() === d,
|
|
142
|
+
"Invalid calendar date."
|
|
143
|
+
);
|
|
144
|
+
if (m[8].toUpperCase() !== "Z")
|
|
145
|
+
check(
|
|
146
|
+
Number(m[8].slice(1, 3)) <= 23 && Number(m[8].slice(4)) <= 59,
|
|
147
|
+
"Invalid timezone offset."
|
|
148
|
+
);
|
|
149
|
+
value = Date.parse(v) / 1e3;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return number(value, "Timestamp", -62135596800, 253402300799999e-3);
|
|
153
|
+
}
|
|
154
|
+
function samples(timestamps, values) {
|
|
155
|
+
check(
|
|
156
|
+
(Array.isArray(timestamps) || ArrayBuffer.isView(timestamps)) && (Array.isArray(values) || ArrayBuffer.isView(values)),
|
|
157
|
+
"Timestamps and values must be arrays."
|
|
158
|
+
);
|
|
159
|
+
check(
|
|
160
|
+
timestamps.length === values.length && timestamps.length <= LIMITS.samples,
|
|
161
|
+
"Sample lengths must match and stay within the sample limit."
|
|
162
|
+
);
|
|
163
|
+
const ts = Array.from(timestamps, epoch), vs = Array.from(
|
|
164
|
+
values,
|
|
165
|
+
(v, i) => missing(v) ? NaN : number(v, "Value at " + i)
|
|
166
|
+
);
|
|
167
|
+
for (let i = 1; i < ts.length; i++)
|
|
168
|
+
check(
|
|
169
|
+
ts[i] > ts[i - 1],
|
|
170
|
+
"Timestamps must be strictly increasing (index " + i + ")."
|
|
171
|
+
);
|
|
172
|
+
return { timestamps: ts, values: vs };
|
|
173
|
+
}
|
|
174
|
+
var DEFAULTS = freeze({
|
|
175
|
+
title: "",
|
|
176
|
+
verticalLabel: "",
|
|
177
|
+
watermark: "",
|
|
178
|
+
series: [],
|
|
179
|
+
timeAxis: {
|
|
180
|
+
start: null,
|
|
181
|
+
end: null,
|
|
182
|
+
mode: "auto",
|
|
183
|
+
timezone: "UTC",
|
|
184
|
+
minorSeconds: null,
|
|
185
|
+
majorSeconds: null,
|
|
186
|
+
labelSeconds: null,
|
|
187
|
+
labelFormat: null,
|
|
188
|
+
ticks: null,
|
|
189
|
+
majorTicks: null,
|
|
190
|
+
minorTicks: null,
|
|
191
|
+
labelOffsetSeconds: 0
|
|
192
|
+
},
|
|
193
|
+
yAxis: {
|
|
194
|
+
minimum: 0,
|
|
195
|
+
maximum: null,
|
|
196
|
+
majorStep: null,
|
|
197
|
+
minorDivisions: 5,
|
|
198
|
+
base: 1e3,
|
|
199
|
+
scaleFactor: null,
|
|
200
|
+
suffix: null,
|
|
201
|
+
decimals: null,
|
|
202
|
+
legendDecimals: 2,
|
|
203
|
+
showZeroSuffix: false
|
|
204
|
+
},
|
|
205
|
+
layout: {
|
|
206
|
+
width: 595,
|
|
207
|
+
plotHeight: 122,
|
|
208
|
+
left: 64,
|
|
209
|
+
right: 31,
|
|
210
|
+
top: 34,
|
|
211
|
+
titleY: 8,
|
|
212
|
+
titleOffsetX: 27,
|
|
213
|
+
unitX: 5,
|
|
214
|
+
xLabelGap: 5,
|
|
215
|
+
yLabelGap: 6,
|
|
216
|
+
legendGap: 20,
|
|
217
|
+
legendRowHeight: 14,
|
|
218
|
+
legendBottom: 7,
|
|
219
|
+
legend: "reference",
|
|
220
|
+
antialias: 4,
|
|
221
|
+
pixelScale: 1,
|
|
222
|
+
legendLayout: {
|
|
223
|
+
nameX: 30,
|
|
224
|
+
swatchX: 15,
|
|
225
|
+
swatchWidth: 9,
|
|
226
|
+
swatchHeight: 10,
|
|
227
|
+
referenceWidth: 595,
|
|
228
|
+
autoScaleColumns: true,
|
|
229
|
+
compact: [
|
|
230
|
+
[102, 228],
|
|
231
|
+
[244, 370],
|
|
232
|
+
[386, 512]
|
|
233
|
+
],
|
|
234
|
+
expanded: [
|
|
235
|
+
[124, 250],
|
|
236
|
+
[289, 415],
|
|
237
|
+
[454, 580]
|
|
238
|
+
],
|
|
239
|
+
aligned: [
|
|
240
|
+
[102, 250],
|
|
241
|
+
[267, 415],
|
|
242
|
+
[432, 580]
|
|
243
|
+
]
|
|
244
|
+
}
|
|
245
|
+
},
|
|
246
|
+
theme: {
|
|
247
|
+
background: "#f3f3f3",
|
|
248
|
+
canvas: "#ffffff",
|
|
249
|
+
shadeLight: "#cfcfcf",
|
|
250
|
+
shadeDark: "#9e9e9e",
|
|
251
|
+
text: "#000000",
|
|
252
|
+
minorGrid: "#8f8f8f3c",
|
|
253
|
+
majorGrid: "#df4f4f3c",
|
|
254
|
+
axis: "#777777",
|
|
255
|
+
arrow: "#7f1f1f",
|
|
256
|
+
watermark: "#aaaaaa",
|
|
257
|
+
frame: "#000000",
|
|
258
|
+
gridFront: true,
|
|
259
|
+
gridDash: [1, 1],
|
|
260
|
+
titleSize: 14,
|
|
261
|
+
axisSize: 11,
|
|
262
|
+
unitSize: 10,
|
|
263
|
+
legendSize: 11,
|
|
264
|
+
watermarkSize: 8,
|
|
265
|
+
captionSize: 11,
|
|
266
|
+
titleAdvance: 8,
|
|
267
|
+
axisAdvance: 6,
|
|
268
|
+
legendAdvance: 7
|
|
269
|
+
},
|
|
270
|
+
fonts: {
|
|
271
|
+
mode: "system",
|
|
272
|
+
family: '"DejaVu Sans Mono", "Liberation Mono", Consolas, monospace',
|
|
273
|
+
titleFamily: null,
|
|
274
|
+
unitFamily: null,
|
|
275
|
+
captionFamily: "Arial, sans-serif",
|
|
276
|
+
strictGlyphs: true
|
|
277
|
+
},
|
|
278
|
+
legendLabels: ["Current:", "Average:", "Maximum:"],
|
|
279
|
+
missingLabel: "NaN",
|
|
280
|
+
hRules: [],
|
|
281
|
+
vRules: []
|
|
282
|
+
});
|
|
283
|
+
function series(name, timestamps, values, options = {}) {
|
|
284
|
+
const s = merge(
|
|
285
|
+
{
|
|
286
|
+
name,
|
|
287
|
+
timestamps,
|
|
288
|
+
values,
|
|
289
|
+
kind: "line",
|
|
290
|
+
color: "#0000cc",
|
|
291
|
+
lineWidth: 0.7,
|
|
292
|
+
outline: null,
|
|
293
|
+
baseline: 0,
|
|
294
|
+
interpolation: "linear",
|
|
295
|
+
gapAfter: 0,
|
|
296
|
+
legendValues: null
|
|
297
|
+
},
|
|
298
|
+
options
|
|
299
|
+
);
|
|
300
|
+
text(s.name, "Series name");
|
|
301
|
+
const data = samples(s.timestamps, s.values);
|
|
302
|
+
s.timestamps = data.timestamps;
|
|
303
|
+
s.values = data.values;
|
|
304
|
+
check(["line", "area"].includes(s.kind), "Series kind must be line or area.");
|
|
305
|
+
check(
|
|
306
|
+
["linear", "step-post"].includes(s.interpolation),
|
|
307
|
+
"Interpolation must be linear or step-post."
|
|
308
|
+
);
|
|
309
|
+
number(s.lineWidth, "Line width", 0.01, 128);
|
|
310
|
+
number(s.baseline, "Baseline");
|
|
311
|
+
number(s.gapAfter, "Gap threshold", 0);
|
|
312
|
+
s.color = color(s.color);
|
|
313
|
+
if (s.outline !== null) s.outline = color(s.outline);
|
|
314
|
+
if (s.legendValues !== null) {
|
|
315
|
+
check(record(s.legendValues), "Legend overrides must be an object.");
|
|
316
|
+
for (const k of ["current", "average", "maximum"])
|
|
317
|
+
s.legendValues[k] = missing(s.legendValues[k]) ? null : number(s.legendValues[k], "Legend override");
|
|
318
|
+
}
|
|
319
|
+
return freeze(s);
|
|
320
|
+
}
|
|
321
|
+
function regularSeries(name, values, start, step = 300, options = {}) {
|
|
322
|
+
start = epoch(start);
|
|
323
|
+
number(step, "Step", Number.MIN_VALUE);
|
|
324
|
+
const first = start;
|
|
325
|
+
return series(
|
|
326
|
+
name,
|
|
327
|
+
Array.from(values, (_, i) => first + i * step),
|
|
328
|
+
values,
|
|
329
|
+
options
|
|
330
|
+
);
|
|
331
|
+
}
|
|
332
|
+
function modeAxis(mode, options = {}) {
|
|
333
|
+
return merge(
|
|
334
|
+
DEFAULTS.timeAxis,
|
|
335
|
+
merge(
|
|
336
|
+
{ mode },
|
|
337
|
+
typeof options === "string" ? { timezone: options } : options
|
|
338
|
+
)
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
function daily(options = {}) {
|
|
342
|
+
return modeAxis("daily", options);
|
|
343
|
+
}
|
|
344
|
+
function weekly(options = {}) {
|
|
345
|
+
return modeAxis("weekly", options);
|
|
346
|
+
}
|
|
347
|
+
function monthly(options = {}) {
|
|
348
|
+
return modeAxis("monthly", options);
|
|
349
|
+
}
|
|
350
|
+
function yearly(options = {}) {
|
|
351
|
+
return modeAxis("yearly", options);
|
|
352
|
+
}
|
|
353
|
+
function dimensions(c) {
|
|
354
|
+
const l = c.layout;
|
|
355
|
+
return [
|
|
356
|
+
l.width,
|
|
357
|
+
l.top + l.plotHeight + (l.legend === "none" ? 18 : l.legendGap + Math.max(1, c.series.length) * l.legendRowHeight + l.legendBottom)
|
|
358
|
+
];
|
|
359
|
+
}
|
|
360
|
+
function dash(d) {
|
|
361
|
+
if (d === null) return;
|
|
362
|
+
check(Array.isArray(d) && d.length === 2, "Dash must be null or [on, off].");
|
|
363
|
+
d.forEach((v) => integer(v, "Dash interval", 1, 16384));
|
|
364
|
+
}
|
|
365
|
+
function normalize(options) {
|
|
366
|
+
const c = merge(DEFAULTS, options);
|
|
367
|
+
check(
|
|
368
|
+
Array.isArray(c.series) && c.series.length <= LIMITS.series,
|
|
369
|
+
"Invalid series count."
|
|
370
|
+
);
|
|
371
|
+
c.series = c.series.map((s) => series(s.name, s.timestamps, s.values, s));
|
|
372
|
+
for (const k of [
|
|
373
|
+
"title",
|
|
374
|
+
"verticalLabel",
|
|
375
|
+
"watermark",
|
|
376
|
+
"missingLabel"
|
|
377
|
+
])
|
|
378
|
+
text(c[k], k);
|
|
379
|
+
check(
|
|
380
|
+
Array.isArray(c.legendLabels) && c.legendLabels.length === 3,
|
|
381
|
+
"Three statistic labels are required."
|
|
382
|
+
);
|
|
383
|
+
c.legendLabels.forEach((v) => text(v, "Statistic label"));
|
|
384
|
+
const a = c.timeAxis, l = c.layout, y = c.yAxis, t = c.theme, f = c.fonts;
|
|
385
|
+
check(
|
|
386
|
+
["auto", "daily", "weekly", "monthly", "yearly", "custom"].includes(a.mode),
|
|
387
|
+
"Unknown time axis mode."
|
|
388
|
+
);
|
|
389
|
+
text(a.timezone, "Timezone");
|
|
390
|
+
zoneFormatter(a.timezone);
|
|
391
|
+
for (const k of ["start", "end"])
|
|
392
|
+
if (a[k] !== null) a[k] = epoch(a[k]);
|
|
393
|
+
if (a.start !== null && a.end !== null)
|
|
394
|
+
check(
|
|
395
|
+
a.end > a.start,
|
|
396
|
+
"Time end must exceed start."
|
|
397
|
+
);
|
|
398
|
+
for (const k of ["minorSeconds", "majorSeconds", "labelSeconds"])
|
|
399
|
+
if (a[k] !== null) number(a[k], k, 1e-3);
|
|
400
|
+
number(a.labelOffsetSeconds, "Label offset", -31622400, 31622400);
|
|
401
|
+
if (a.labelFormat !== null) {
|
|
402
|
+
text(a.labelFormat, "Label format");
|
|
403
|
+
formatTime(0, "UTC", a.labelFormat);
|
|
404
|
+
}
|
|
405
|
+
if (a.ticks !== null) {
|
|
406
|
+
check(
|
|
407
|
+
Array.isArray(a.ticks) && a.ticks.length <= LIMITS.ticks,
|
|
408
|
+
"Too many or invalid ticks."
|
|
409
|
+
);
|
|
410
|
+
const ticks = a.ticks.map((v) => ({
|
|
411
|
+
time: epoch(v.time),
|
|
412
|
+
label: text(v.label, "Tick label")
|
|
413
|
+
}));
|
|
414
|
+
for (let i = 1; i < ticks.length; i++)
|
|
415
|
+
check(
|
|
416
|
+
ticks[i].time > ticks[i - 1].time,
|
|
417
|
+
"Explicit ticks must be strictly increasing."
|
|
418
|
+
);
|
|
419
|
+
a.ticks = ticks;
|
|
420
|
+
}
|
|
421
|
+
for (const k of ["minorTicks", "majorTicks"])
|
|
422
|
+
if (a[k] !== null) {
|
|
423
|
+
const input = a[k];
|
|
424
|
+
check(
|
|
425
|
+
Array.isArray(input) && input.length <= LIMITS.ticks,
|
|
426
|
+
"Too many or invalid ticks."
|
|
427
|
+
);
|
|
428
|
+
const ticks = input.map(epoch);
|
|
429
|
+
for (let i = 1; i < ticks.length; i++)
|
|
430
|
+
check(
|
|
431
|
+
ticks[i] > ticks[i - 1],
|
|
432
|
+
"Explicit ticks must be strictly increasing."
|
|
433
|
+
);
|
|
434
|
+
a[k] = ticks;
|
|
435
|
+
}
|
|
436
|
+
for (const k of ["minimum", "maximum", "majorStep", "scaleFactor"])
|
|
437
|
+
if (y[k] !== null) number(y[k], k);
|
|
438
|
+
for (const k of ["majorStep", "scaleFactor"])
|
|
439
|
+
if (y[k] !== null) check(y[k] > 0, k + " must be positive.");
|
|
440
|
+
if (y.minimum !== null && y.maximum !== null)
|
|
441
|
+
check(y.maximum > y.minimum, "Y maximum must exceed minimum.");
|
|
442
|
+
check(y.base === 1e3 || y.base === 1024, "Unit base must be 1000 or 1024.");
|
|
443
|
+
integer(y.minorDivisions, "Minor divisions", 1, 100);
|
|
444
|
+
integer(y.legendDecimals, "Legend decimals", 0, 12);
|
|
445
|
+
if (y.decimals !== null) integer(y.decimals, "Decimals", 0, 12);
|
|
446
|
+
if (y.suffix !== null) text(y.suffix, "Suffix");
|
|
447
|
+
integer(l.width, "Width", 400, 8192);
|
|
448
|
+
integer(l.plotHeight, "Plot height", 30, 4096);
|
|
449
|
+
for (const k of [
|
|
450
|
+
"left",
|
|
451
|
+
"right",
|
|
452
|
+
"top",
|
|
453
|
+
"titleY",
|
|
454
|
+
"unitX",
|
|
455
|
+
"xLabelGap",
|
|
456
|
+
"yLabelGap",
|
|
457
|
+
"legendGap",
|
|
458
|
+
"legendRowHeight",
|
|
459
|
+
"legendBottom"
|
|
460
|
+
])
|
|
461
|
+
integer(l[k], k, 0, 16384);
|
|
462
|
+
check(
|
|
463
|
+
l.left >= 20 && l.right >= 12 && l.top >= 12 && l.width - l.left - l.right >= 100,
|
|
464
|
+
"Insufficient plot margins."
|
|
465
|
+
);
|
|
466
|
+
number(l.titleOffsetX, "Title offset", -8192, 8192);
|
|
467
|
+
check(
|
|
468
|
+
["reference", "aligned", "none"].includes(l.legend),
|
|
469
|
+
"Invalid legend mode."
|
|
470
|
+
);
|
|
471
|
+
if (l.legend !== "none")
|
|
472
|
+
check(
|
|
473
|
+
l.legendGap >= 14 && l.legendRowHeight >= 10,
|
|
474
|
+
"Legend spacing is insufficient."
|
|
475
|
+
);
|
|
476
|
+
integer(l.antialias, "Antialias", 1, 8);
|
|
477
|
+
integer(l.pixelScale, "Pixel scale", 1, 8);
|
|
478
|
+
const ll = l.legendLayout;
|
|
479
|
+
for (const k of [
|
|
480
|
+
"nameX",
|
|
481
|
+
"swatchX",
|
|
482
|
+
"swatchWidth",
|
|
483
|
+
"swatchHeight",
|
|
484
|
+
"referenceWidth"
|
|
485
|
+
])
|
|
486
|
+
integer(ll[k], k, 0, 16384);
|
|
487
|
+
check(
|
|
488
|
+
ll.swatchWidth >= 3 && ll.swatchHeight >= 3 && ll.referenceWidth > ll.nameX,
|
|
489
|
+
"Invalid legend geometry."
|
|
490
|
+
);
|
|
491
|
+
for (const key of ["compact", "expanded", "aligned"]) {
|
|
492
|
+
const cols = ll[key];
|
|
493
|
+
check(Array.isArray(cols) && cols.length === 3, "Invalid legend columns.");
|
|
494
|
+
let last = ll.nameX;
|
|
495
|
+
for (const p of cols) {
|
|
496
|
+
check(
|
|
497
|
+
Array.isArray(p) && p.length === 2 && finite(p[0]) && finite(p[1]) && last < p[0] && p[0] < p[1] && p[1] < ll.referenceWidth,
|
|
498
|
+
"Legend columns must not overlap."
|
|
499
|
+
);
|
|
500
|
+
last = p[1];
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
for (const k of [
|
|
504
|
+
"background",
|
|
505
|
+
"canvas",
|
|
506
|
+
"shadeLight",
|
|
507
|
+
"shadeDark",
|
|
508
|
+
"text",
|
|
509
|
+
"minorGrid",
|
|
510
|
+
"majorGrid",
|
|
511
|
+
"axis",
|
|
512
|
+
"arrow",
|
|
513
|
+
"watermark",
|
|
514
|
+
"frame"
|
|
515
|
+
])
|
|
516
|
+
t[k] = color(t[k]);
|
|
517
|
+
check(
|
|
518
|
+
t.background[3] === 255 && t.canvas[3] === 255,
|
|
519
|
+
"Background and canvas must be opaque."
|
|
520
|
+
);
|
|
521
|
+
dash(t.gridDash);
|
|
522
|
+
check(t.gridDash !== null, "Grid dash is required.");
|
|
523
|
+
for (const k of [
|
|
524
|
+
"titleSize",
|
|
525
|
+
"axisSize",
|
|
526
|
+
"unitSize",
|
|
527
|
+
"legendSize",
|
|
528
|
+
"watermarkSize",
|
|
529
|
+
"captionSize",
|
|
530
|
+
"titleAdvance",
|
|
531
|
+
"axisAdvance",
|
|
532
|
+
"legendAdvance"
|
|
533
|
+
])
|
|
534
|
+
number(t[k], k, 1, 128);
|
|
535
|
+
check(
|
|
536
|
+
["system", "bitmap"].includes(f.mode),
|
|
537
|
+
"Font mode must be system or bitmap."
|
|
538
|
+
);
|
|
539
|
+
text(f.family, "Font family");
|
|
540
|
+
for (const k of ["titleFamily", "unitFamily", "captionFamily"])
|
|
541
|
+
if (f[k] !== null) text(f[k], k);
|
|
542
|
+
function rule(input, field) {
|
|
543
|
+
const r = merge(
|
|
544
|
+
{ color: "#990000", width: 1, dash: [3, 2] },
|
|
545
|
+
input
|
|
546
|
+
);
|
|
547
|
+
if (field === "time") r.time = epoch(r.time);
|
|
548
|
+
else r.value = number(r.value, "Rule value");
|
|
549
|
+
number(r.width, "Rule width", 0.1, 128);
|
|
550
|
+
dash(r.dash);
|
|
551
|
+
r.color = color(r.color);
|
|
552
|
+
return r;
|
|
553
|
+
}
|
|
554
|
+
check(
|
|
555
|
+
Array.isArray(c.hRules) && c.hRules.length <= 1e3,
|
|
556
|
+
"Invalid rule count."
|
|
557
|
+
);
|
|
558
|
+
c.hRules = c.hRules.map((r) => rule(r, "value"));
|
|
559
|
+
check(
|
|
560
|
+
Array.isArray(c.vRules) && c.vRules.length <= 1e3,
|
|
561
|
+
"Invalid rule count."
|
|
562
|
+
);
|
|
563
|
+
c.vRules = c.vRules.map((r) => rule(r, "time"));
|
|
564
|
+
const [w, h] = dimensions(c);
|
|
565
|
+
check(
|
|
566
|
+
w * h * l.pixelScale ** 2 <= LIMITS.pixels,
|
|
567
|
+
"Output allocation exceeds the pixel limit."
|
|
568
|
+
);
|
|
569
|
+
check(
|
|
570
|
+
(l.width - l.left - l.right + 1) * (l.plotHeight + 1) * l.antialias ** 2 <= LIMITS.layerPixels,
|
|
571
|
+
"Supersampled layer exceeds the pixel limit."
|
|
572
|
+
);
|
|
573
|
+
return freeze(c);
|
|
574
|
+
}
|
|
575
|
+
var Surface = class _Surface {
|
|
576
|
+
width;
|
|
577
|
+
height;
|
|
578
|
+
data;
|
|
579
|
+
constructor(width, height, fill = null) {
|
|
580
|
+
integer(width, "Image width", 1, 65536);
|
|
581
|
+
integer(height, "Image height", 1, 65536);
|
|
582
|
+
check(width * height <= LIMITS.layerPixels, "Image is too large.");
|
|
583
|
+
this.width = width;
|
|
584
|
+
this.height = height;
|
|
585
|
+
this.data = new Uint8ClampedArray(width * height * 4);
|
|
586
|
+
if (fill) this.rect(0, 0, width, height, fill);
|
|
587
|
+
}
|
|
588
|
+
pixel(x, y, c, blend = false) {
|
|
589
|
+
if (x < 0 || y < 0 || x >= this.width || y >= this.height) return;
|
|
590
|
+
const i = (y * this.width + x) * 4, d = this.data;
|
|
591
|
+
if (!blend || c[3] === 255) {
|
|
592
|
+
d[i] = c[0];
|
|
593
|
+
d[i + 1] = c[1];
|
|
594
|
+
d[i + 2] = c[2];
|
|
595
|
+
d[i + 3] = c[3];
|
|
596
|
+
return;
|
|
597
|
+
}
|
|
598
|
+
if (c[3] === 0) return;
|
|
599
|
+
const sa = c[3], da = d[i + 3], alpha = sa * 255 + da * (255 - sa);
|
|
600
|
+
for (let k = 0; k < 3; k++)
|
|
601
|
+
d[i + k] = Math.floor(
|
|
602
|
+
(c[k] * sa * 255 + d[i + k] * da * (255 - sa) + alpha / 2) / alpha
|
|
603
|
+
);
|
|
604
|
+
d[i + 3] = Math.floor((alpha + 127) / 255);
|
|
605
|
+
}
|
|
606
|
+
rect(x0, y0, x1, y1, c) {
|
|
607
|
+
x0 = Math.max(0, Math.ceil(x0));
|
|
608
|
+
y0 = Math.max(0, Math.ceil(y0));
|
|
609
|
+
x1 = Math.min(this.width, Math.ceil(x1));
|
|
610
|
+
y1 = Math.min(this.height, Math.ceil(y1));
|
|
611
|
+
for (let y = y0; y < y1; y++)
|
|
612
|
+
for (let x = x0, i = (y * this.width + x0) * 4; x < x1; x++, i += 4) {
|
|
613
|
+
this.data[i] = c[0];
|
|
614
|
+
this.data[i + 1] = c[1];
|
|
615
|
+
this.data[i + 2] = c[2];
|
|
616
|
+
this.data[i + 3] = c[3];
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
over(src, dx = 0, dy = 0) {
|
|
620
|
+
const p = [0, 0, 0, 0];
|
|
621
|
+
for (let y = Math.max(0, -dy); y < Math.min(src.height, this.height - dy); y++)
|
|
622
|
+
for (let x = Math.max(0, -dx); x < Math.min(src.width, this.width - dx); x++) {
|
|
623
|
+
const i = (y * src.width + x) * 4;
|
|
624
|
+
if (src.data[i + 3]) {
|
|
625
|
+
p[0] = src.data[i];
|
|
626
|
+
p[1] = src.data[i + 1];
|
|
627
|
+
p[2] = src.data[i + 2];
|
|
628
|
+
p[3] = src.data[i + 3];
|
|
629
|
+
this.pixel(x + dx, y + dy, p, true);
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
polygon(points, c) {
|
|
634
|
+
if (points.length < 3) return;
|
|
635
|
+
let lo = Infinity, hi = -Infinity;
|
|
636
|
+
for (const p of points) {
|
|
637
|
+
lo = Math.min(lo, p[1]);
|
|
638
|
+
hi = Math.max(hi, p[1]);
|
|
639
|
+
}
|
|
640
|
+
for (let y = Math.max(0, Math.ceil(lo)); y <= Math.min(this.height - 1, Math.floor(hi)); y++) {
|
|
641
|
+
const xs = [];
|
|
642
|
+
let a = points[points.length - 1];
|
|
643
|
+
for (const b of points) {
|
|
644
|
+
if (a[1] === b[1]) {
|
|
645
|
+
if (y === a[1])
|
|
646
|
+
this.rect(
|
|
647
|
+
Math.ceil(Math.min(a[0], b[0])),
|
|
648
|
+
y,
|
|
649
|
+
Math.floor(Math.max(a[0], b[0])) + 1,
|
|
650
|
+
y + 1,
|
|
651
|
+
c
|
|
652
|
+
);
|
|
653
|
+
} else if (a[1] <= y && y < b[1] || b[1] <= y && y < a[1])
|
|
654
|
+
xs.push(a[0] + (y - a[1]) / (b[1] - a[1]) * (b[0] - a[0]));
|
|
655
|
+
a = b;
|
|
656
|
+
}
|
|
657
|
+
xs.sort((a2, b) => a2 - b);
|
|
658
|
+
for (let i = 0; i + 1 < xs.length; i += 2)
|
|
659
|
+
this.rect(
|
|
660
|
+
Math.ceil(xs[i] - 1e-9),
|
|
661
|
+
y,
|
|
662
|
+
Math.floor(xs[i + 1] + 1e-9) + 1,
|
|
663
|
+
y + 1,
|
|
664
|
+
c
|
|
665
|
+
);
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
line(a, b, c, width = 1) {
|
|
669
|
+
let x = round(a[0]), y = round(a[1]), x1 = round(b[0]), y1 = round(b[1]);
|
|
670
|
+
width = Math.max(1, round(width));
|
|
671
|
+
if (width > 1) {
|
|
672
|
+
const r = (width - 1) / 2;
|
|
673
|
+
if (x === x1) {
|
|
674
|
+
this.rect(
|
|
675
|
+
x - Math.floor(width / 2),
|
|
676
|
+
Math.min(y, y1),
|
|
677
|
+
x + Math.floor((width - 1) / 2) + 1,
|
|
678
|
+
Math.max(y, y1) + 1,
|
|
679
|
+
c
|
|
680
|
+
);
|
|
681
|
+
return;
|
|
682
|
+
}
|
|
683
|
+
if (y === y1) {
|
|
684
|
+
this.rect(
|
|
685
|
+
Math.min(x, x1),
|
|
686
|
+
y - Math.floor(width / 2),
|
|
687
|
+
Math.max(x, x1) + 1,
|
|
688
|
+
y + Math.floor((width - 1) / 2) + 1,
|
|
689
|
+
c
|
|
690
|
+
);
|
|
691
|
+
return;
|
|
692
|
+
}
|
|
693
|
+
const len = Math.hypot(x1 - x, y1 - y), ox = -(y1 - y) / len * r, oy = (x1 - x) / len * r;
|
|
694
|
+
this.polygon(
|
|
695
|
+
[
|
|
696
|
+
[x + ox, y + oy],
|
|
697
|
+
[x1 + ox, y1 + oy],
|
|
698
|
+
[x1 - ox, y1 - oy],
|
|
699
|
+
[x - ox, y - oy]
|
|
700
|
+
].map((p) => [round(p[0]), round(p[1])]),
|
|
701
|
+
c
|
|
702
|
+
);
|
|
703
|
+
return;
|
|
704
|
+
}
|
|
705
|
+
const dx = Math.abs(x1 - x), dy = -Math.abs(y1 - y), sx = x < x1 ? 1 : -1, sy = y < y1 ? 1 : -1;
|
|
706
|
+
let err = dx + dy;
|
|
707
|
+
for (; ; ) {
|
|
708
|
+
this.pixel(x, y, c);
|
|
709
|
+
if (x === x1 && y === y1) break;
|
|
710
|
+
const e = 2 * err;
|
|
711
|
+
if (e >= dy) {
|
|
712
|
+
err += dy;
|
|
713
|
+
x += sx;
|
|
714
|
+
}
|
|
715
|
+
if (e <= dx) {
|
|
716
|
+
err += dx;
|
|
717
|
+
y += sy;
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
dashed(a, b, c, pattern = [1, 1], width = 1) {
|
|
722
|
+
if (pattern === null) {
|
|
723
|
+
this.line(a, b, c, width);
|
|
724
|
+
return;
|
|
725
|
+
}
|
|
726
|
+
const len = Math.hypot(b[0] - a[0], b[1] - a[1]);
|
|
727
|
+
if (!len) {
|
|
728
|
+
this.pixel(round(a[0]), round(a[1]), c);
|
|
729
|
+
return;
|
|
730
|
+
}
|
|
731
|
+
const dx = (b[0] - a[0]) / len, dy = (b[1] - a[1]) / len;
|
|
732
|
+
for (let s = 0; s <= Math.ceil(len); s += pattern[0] + pattern[1]) {
|
|
733
|
+
const e = Math.min(len, s + pattern[0] - 1);
|
|
734
|
+
this.line(
|
|
735
|
+
[a[0] + dx * s, a[1] + dy * s],
|
|
736
|
+
[a[0] + dx * e, a[1] + dy * e],
|
|
737
|
+
c,
|
|
738
|
+
width
|
|
739
|
+
);
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
circle(x, y, r, c) {
|
|
743
|
+
for (let yy = Math.max(0, Math.floor(y - r)); yy <= Math.min(this.height - 1, Math.ceil(y + r)); yy++)
|
|
744
|
+
for (let xx = Math.max(0, Math.floor(x - r)); xx <= Math.min(this.width - 1, Math.ceil(x + r)); xx++)
|
|
745
|
+
if ((xx - x) ** 2 + (yy - y) ** 2 <= r * r) this.pixel(xx, yy, c);
|
|
746
|
+
}
|
|
747
|
+
down(scale) {
|
|
748
|
+
if (scale === 1) return this;
|
|
749
|
+
const out = new _Surface(this.width / scale, this.height / scale), p = [0, 0, 0, 0], n = scale * scale;
|
|
750
|
+
for (let y = 0; y < out.height; y++)
|
|
751
|
+
for (let x = 0; x < out.width; x++) {
|
|
752
|
+
let a = 0, r = 0, g = 0, b = 0;
|
|
753
|
+
for (let sy = 0; sy < scale; sy++)
|
|
754
|
+
for (let sx = 0; sx < scale; sx++) {
|
|
755
|
+
const i = ((y * scale + sy) * this.width + x * scale + sx) * 4, ca = this.data[i + 3];
|
|
756
|
+
a += ca;
|
|
757
|
+
r += this.data[i] * ca;
|
|
758
|
+
g += this.data[i + 1] * ca;
|
|
759
|
+
b += this.data[i + 2] * ca;
|
|
760
|
+
}
|
|
761
|
+
if (a) {
|
|
762
|
+
p[0] = Math.floor(r / a + 0.5);
|
|
763
|
+
p[1] = Math.floor(g / a + 0.5);
|
|
764
|
+
p[2] = Math.floor(b / a + 0.5);
|
|
765
|
+
p[3] = Math.floor(a / n + 0.5);
|
|
766
|
+
out.pixel(x, y, p);
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
return out;
|
|
770
|
+
}
|
|
771
|
+
scale(n) {
|
|
772
|
+
if (n === 1) return this;
|
|
773
|
+
const out = new _Surface(this.width * n, this.height * n);
|
|
774
|
+
for (let y = 0; y < out.height; y++)
|
|
775
|
+
for (let x = 0; x < out.width; x++) {
|
|
776
|
+
const s = (Math.floor(y / n) * this.width + Math.floor(x / n)) * 4, i = (y * out.width + x) * 4;
|
|
777
|
+
out.data[i] = this.data[s];
|
|
778
|
+
out.data[i + 1] = this.data[s + 1];
|
|
779
|
+
out.data[i + 2] = this.data[s + 2];
|
|
780
|
+
out.data[i + 3] = this.data[s + 3];
|
|
781
|
+
}
|
|
782
|
+
return out;
|
|
783
|
+
}
|
|
784
|
+
};
|
|
785
|
+
function clipLine(a, b, w, h) {
|
|
786
|
+
let lo = 0, hi = 1;
|
|
787
|
+
const dx = b[0] - a[0], dy = b[1] - a[1];
|
|
788
|
+
for (const [p, q] of [
|
|
789
|
+
[-dx, a[0]],
|
|
790
|
+
[dx, w - a[0]],
|
|
791
|
+
[-dy, a[1]],
|
|
792
|
+
[dy, h - a[1]]
|
|
793
|
+
]) {
|
|
794
|
+
if (p === 0) {
|
|
795
|
+
if (q < 0) return null;
|
|
796
|
+
} else {
|
|
797
|
+
const u = q / p;
|
|
798
|
+
if (p < 0) lo = Math.max(lo, u);
|
|
799
|
+
else hi = Math.min(hi, u);
|
|
800
|
+
if (lo > hi) return null;
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
return [
|
|
804
|
+
[a[0] + lo * dx, a[1] + lo * dy],
|
|
805
|
+
[a[0] + hi * dx, a[1] + hi * dy]
|
|
806
|
+
];
|
|
807
|
+
}
|
|
808
|
+
function clipPolygon(points, w, h) {
|
|
809
|
+
let ps = points;
|
|
810
|
+
for (const [axis, bound, greater] of [
|
|
811
|
+
[0, 0, true],
|
|
812
|
+
[0, w, false],
|
|
813
|
+
[1, 0, true],
|
|
814
|
+
[1, h, false]
|
|
815
|
+
]) {
|
|
816
|
+
if (!ps.length) break;
|
|
817
|
+
const out = [], inside = (p) => greater ? p[axis] >= bound : p[axis] <= bound;
|
|
818
|
+
let a = ps[ps.length - 1], ai = inside(a);
|
|
819
|
+
for (const b of ps) {
|
|
820
|
+
const bi = inside(b);
|
|
821
|
+
if (ai !== bi) {
|
|
822
|
+
const q = (bound - a[axis]) / (b[axis] - a[axis]);
|
|
823
|
+
const p = [a[0] + q * (b[0] - a[0]), a[1] + q * (b[1] - a[1])];
|
|
824
|
+
p[axis] = bound;
|
|
825
|
+
out.push(p);
|
|
826
|
+
}
|
|
827
|
+
if (bi) out.push(b);
|
|
828
|
+
a = b;
|
|
829
|
+
ai = bi;
|
|
830
|
+
}
|
|
831
|
+
ps = out;
|
|
832
|
+
}
|
|
833
|
+
return ps;
|
|
834
|
+
}
|
|
835
|
+
function lowerBound(a, x) {
|
|
836
|
+
let l = 0, r = a.length;
|
|
837
|
+
while (l < r) {
|
|
838
|
+
const m = l + r >>> 1;
|
|
839
|
+
if (a[m] < x) l = m + 1;
|
|
840
|
+
else r = m;
|
|
841
|
+
}
|
|
842
|
+
return l;
|
|
843
|
+
}
|
|
844
|
+
function upperBound(a, x) {
|
|
845
|
+
let l = 0, r = a.length;
|
|
846
|
+
while (l < r) {
|
|
847
|
+
const m = l + r >>> 1;
|
|
848
|
+
if (a[m] <= x) l = m + 1;
|
|
849
|
+
else r = m;
|
|
850
|
+
}
|
|
851
|
+
return l;
|
|
852
|
+
}
|
|
853
|
+
function visibleRuns(s, start, end) {
|
|
854
|
+
const ts = s.timestamps, vs = s.values, result = [];
|
|
855
|
+
let run = [];
|
|
856
|
+
const push = () => {
|
|
857
|
+
if (run.length) {
|
|
858
|
+
result.push(run);
|
|
859
|
+
run = [];
|
|
860
|
+
}
|
|
861
|
+
};
|
|
862
|
+
const left = Math.max(0, lowerBound(ts, start) - 1), right = Math.min(ts.length, upperBound(ts, end) + 1);
|
|
863
|
+
for (let i = left; i < right; i++) {
|
|
864
|
+
if (!finite(vs[i])) {
|
|
865
|
+
push();
|
|
866
|
+
continue;
|
|
867
|
+
}
|
|
868
|
+
if (i > left && s.gapAfter > 0 && ts[i] - ts[i - 1] > s.gapAfter) push();
|
|
869
|
+
if (run.length && s.interpolation === "step-post")
|
|
870
|
+
run.push([ts[i], run[run.length - 1][1]]);
|
|
871
|
+
run.push([ts[i], vs[i]]);
|
|
872
|
+
}
|
|
873
|
+
push();
|
|
874
|
+
const clipped = [];
|
|
875
|
+
for (const r of result) {
|
|
876
|
+
const out = [];
|
|
877
|
+
if (r.length === 1) {
|
|
878
|
+
if (r[0][0] >= start && r[0][0] <= end) out.push(r[0]);
|
|
879
|
+
}
|
|
880
|
+
for (let i = 1; i < r.length; i++) {
|
|
881
|
+
const a = r[i - 1], b = r[i];
|
|
882
|
+
if (b[0] < start || a[0] > end) continue;
|
|
883
|
+
const interp = (x) => a[1] * (1 - (x - a[0]) / (b[0] - a[0])) + b[1] * ((x - a[0]) / (b[0] - a[0]));
|
|
884
|
+
const p = a[0] < start ? [start, interp(start)] : a, q = b[0] > end ? [end, interp(end)] : b;
|
|
885
|
+
if (!out.length || out[out.length - 1][0] !== p[0] || out[out.length - 1][1] !== p[1])
|
|
886
|
+
out.push(p);
|
|
887
|
+
out.push(q);
|
|
888
|
+
}
|
|
889
|
+
if (out.length) clipped.push(out);
|
|
890
|
+
}
|
|
891
|
+
return clipped;
|
|
892
|
+
}
|
|
893
|
+
function decimate(ps, start, end, width) {
|
|
894
|
+
if (ps.length <= width * 4) return ps;
|
|
895
|
+
const out = [];
|
|
896
|
+
let pos = 0;
|
|
897
|
+
while (pos < ps.length) {
|
|
898
|
+
const col = Math.floor((ps[pos][0] - start) / (end - start) * width), first = pos;
|
|
899
|
+
let mn = pos, mx = pos;
|
|
900
|
+
while (pos + 1 < ps.length && Math.floor((ps[pos + 1][0] - start) / (end - start) * width) === col) {
|
|
901
|
+
pos++;
|
|
902
|
+
if (ps[pos][1] < ps[mn][1]) mn = pos;
|
|
903
|
+
if (ps[pos][1] > ps[mx][1]) mx = pos;
|
|
904
|
+
}
|
|
905
|
+
for (const i of [.../* @__PURE__ */ new Set([first, mn, mx, pos])].sort((a, b) => a - b))
|
|
906
|
+
out.push(ps[i]);
|
|
907
|
+
pos++;
|
|
908
|
+
}
|
|
909
|
+
return out;
|
|
910
|
+
}
|
|
911
|
+
function stableMean(values) {
|
|
912
|
+
if (!values.length) return null;
|
|
913
|
+
let max = 0;
|
|
914
|
+
for (const v of values) max = Math.max(max, Math.abs(v));
|
|
915
|
+
if (max === 0) return 0;
|
|
916
|
+
let sum = 0, c = 0;
|
|
917
|
+
for (const v of values) {
|
|
918
|
+
const a = v / max - c, t = sum + a;
|
|
919
|
+
c = t - sum - a;
|
|
920
|
+
sum = t;
|
|
921
|
+
}
|
|
922
|
+
return clamp(sum / values.length, -1, 1) * max;
|
|
923
|
+
}
|
|
924
|
+
function statistics(s, start, end) {
|
|
925
|
+
const a = lowerBound(s.timestamps, start), b = upperBound(s.timestamps, end), values = [];
|
|
926
|
+
let mn = Infinity, mx = -Infinity, miss = 0;
|
|
927
|
+
for (let i = a; i < b; i++) {
|
|
928
|
+
const v = s.values[i];
|
|
929
|
+
if (!finite(v)) miss++;
|
|
930
|
+
else {
|
|
931
|
+
values.push(v);
|
|
932
|
+
mn = Math.min(mn, v);
|
|
933
|
+
mx = Math.max(mx, v);
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
return {
|
|
937
|
+
name: s.name,
|
|
938
|
+
current: b > a && finite(s.values[b - 1]) ? s.values[b - 1] : null,
|
|
939
|
+
average: stableMean(values),
|
|
940
|
+
maximum: values.length ? mx : null,
|
|
941
|
+
minimum: values.length ? mn : null,
|
|
942
|
+
count: values.length,
|
|
943
|
+
missing: miss,
|
|
944
|
+
displayOverride: s.legendValues
|
|
945
|
+
};
|
|
946
|
+
}
|
|
947
|
+
function multiples(lo, hi, step) {
|
|
948
|
+
check(
|
|
949
|
+
finite(step) && step > 0 && finite((hi - lo) / step) && (hi - lo) / step <= LIMITS.ticks,
|
|
950
|
+
"Too many ticks or invalid step."
|
|
951
|
+
);
|
|
952
|
+
const a = Math.ceil(lo / step - 1e-10), b = Math.floor(hi / step + 1e-10);
|
|
953
|
+
check(
|
|
954
|
+
Math.abs(a) < 9e15 && Math.abs(b) < 9e15 && b - a <= LIMITS.ticks,
|
|
955
|
+
"Tick precision limit exceeded."
|
|
956
|
+
);
|
|
957
|
+
return Array.from(
|
|
958
|
+
{ length: Math.max(0, b - a + 1) },
|
|
959
|
+
(_, i) => (a + i) * step || 0
|
|
960
|
+
);
|
|
961
|
+
}
|
|
962
|
+
function nice(v) {
|
|
963
|
+
check(finite(v) && v >= 1e-300, "Unsupported numeric axis span.");
|
|
964
|
+
const p = 10 ** Math.floor(Math.log10(v));
|
|
965
|
+
for (const m of [1, 2, 5, 10]) if (v <= m * p * (1 + 1e-12)) return m * p;
|
|
966
|
+
return 10 * p;
|
|
967
|
+
}
|
|
968
|
+
function unitFor(v, base) {
|
|
969
|
+
if (!v) return { factor: 1, suffix: "" };
|
|
970
|
+
let i = Math.floor(Math.log(Math.abs(v)) / Math.log(base) + 1e-12);
|
|
971
|
+
i = clamp(i, base === 1024 ? 0 : -8, 8);
|
|
972
|
+
return {
|
|
973
|
+
factor: base ** i,
|
|
974
|
+
suffix: (base === 1024 ? ["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"] : [
|
|
975
|
+
"y",
|
|
976
|
+
"z",
|
|
977
|
+
"a",
|
|
978
|
+
"f",
|
|
979
|
+
"p",
|
|
980
|
+
"n",
|
|
981
|
+
"u",
|
|
982
|
+
"m",
|
|
983
|
+
"",
|
|
984
|
+
"k",
|
|
985
|
+
"M",
|
|
986
|
+
"G",
|
|
987
|
+
"T",
|
|
988
|
+
"P",
|
|
989
|
+
"E",
|
|
990
|
+
"Z",
|
|
991
|
+
"Y"
|
|
992
|
+
])[base === 1024 ? i : i + 8]
|
|
993
|
+
};
|
|
994
|
+
}
|
|
995
|
+
function resolveY(a, loData, hiData) {
|
|
996
|
+
let lo = a.minimum === null ? Math.min(0, loData) : a.minimum, hi = a.maximum === null ? Math.max(0, hiData) : a.maximum;
|
|
997
|
+
if (a.maximum === null && hi <= lo)
|
|
998
|
+
hi = lo + Math.max(Math.abs(lo) * 0.05, 1);
|
|
999
|
+
if (a.minimum === null && lo >= hi)
|
|
1000
|
+
lo = hi - Math.max(Math.abs(hi) * 0.05, 1);
|
|
1001
|
+
const span = hi - lo;
|
|
1002
|
+
check(finite(span) && span > 0, "Invalid Y span.");
|
|
1003
|
+
const af = a.scaleFactor === null ? unitFor(Math.max(Math.abs(lo), Math.abs(hi)), a.base).factor : a.scaleFactor;
|
|
1004
|
+
const step = a.majorStep === null ? nice(span / af / 5) * af : a.majorStep, q = step / a.minorDivisions;
|
|
1005
|
+
check(finite(q) && q > 0, "Invalid Y quantum.");
|
|
1006
|
+
if (a.minimum === null && lo < 0) lo = Math.floor((lo - span * 0.02) / q) * q;
|
|
1007
|
+
if (a.maximum === null) hi = Math.ceil((hi + span * 0.02) / q) * q;
|
|
1008
|
+
check(finite(hi - lo) && hi > lo, "Degenerate Y range.");
|
|
1009
|
+
const major = multiples(lo, hi, step), minor = multiples(lo, hi, q).filter(
|
|
1010
|
+
(v) => Math.abs(v / step - round(v / step)) > 1e-8
|
|
1011
|
+
);
|
|
1012
|
+
const unit = unitFor(Math.max(Math.abs(lo), Math.abs(hi)), a.base);
|
|
1013
|
+
if (a.scaleFactor !== null) {
|
|
1014
|
+
unit.factor = a.scaleFactor;
|
|
1015
|
+
if (a.suffix === null) unit.suffix = "";
|
|
1016
|
+
}
|
|
1017
|
+
if (a.suffix !== null) unit.suffix = a.suffix;
|
|
1018
|
+
let decimals = a.decimals;
|
|
1019
|
+
if (decimals === null) {
|
|
1020
|
+
const s = step / unit.factor;
|
|
1021
|
+
decimals = 9;
|
|
1022
|
+
for (let d = 0; d < 10; d++)
|
|
1023
|
+
if (Math.abs(s - round(s * 10 ** d) / 10 ** d) <= Math.max(1e-10, Math.abs(s) * 1e-9)) {
|
|
1024
|
+
decimals = d;
|
|
1025
|
+
break;
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
return { minimum: lo, maximum: hi, step, major, minor, ...unit, decimals };
|
|
1029
|
+
}
|
|
1030
|
+
function formatValue(value, unit, decimals = 2, missingText = "NaN") {
|
|
1031
|
+
if (!finite(value)) return missingText;
|
|
1032
|
+
let v = value / unit.factor;
|
|
1033
|
+
if (Math.abs(v) < 0.5 * 10 ** -decimals) v = 0;
|
|
1034
|
+
return v.toFixed(decimals) + (unit.suffix ? " " + unit.suffix : "");
|
|
1035
|
+
}
|
|
1036
|
+
var formatterCache = /* @__PURE__ */ new Map();
|
|
1037
|
+
function zoneFormatter(zone) {
|
|
1038
|
+
if (formatterCache.has(zone)) return formatterCache.get(zone);
|
|
1039
|
+
let f;
|
|
1040
|
+
try {
|
|
1041
|
+
f = new Intl.DateTimeFormat("en-GB-u-ca-gregory-nu-latn", {
|
|
1042
|
+
timeZone: zone,
|
|
1043
|
+
year: "numeric",
|
|
1044
|
+
month: "2-digit",
|
|
1045
|
+
day: "2-digit",
|
|
1046
|
+
hour: "2-digit",
|
|
1047
|
+
minute: "2-digit",
|
|
1048
|
+
second: "2-digit",
|
|
1049
|
+
hourCycle: "h23"
|
|
1050
|
+
});
|
|
1051
|
+
} catch (e) {
|
|
1052
|
+
throw new RangeError("Unsupported time zone: " + zone);
|
|
1053
|
+
}
|
|
1054
|
+
if (formatterCache.size >= 32)
|
|
1055
|
+
formatterCache.delete(formatterCache.keys().next().value);
|
|
1056
|
+
formatterCache.set(zone, f);
|
|
1057
|
+
return f;
|
|
1058
|
+
}
|
|
1059
|
+
function utcFromParts(y, m, d, h = 0, mi = 0, s = 0) {
|
|
1060
|
+
const t = /* @__PURE__ */ new Date(0);
|
|
1061
|
+
t.setUTCFullYear(y, m - 1, d);
|
|
1062
|
+
t.setUTCHours(h, mi, s, 0);
|
|
1063
|
+
return t.getTime() / 1e3;
|
|
1064
|
+
}
|
|
1065
|
+
function wallParts(t, zone) {
|
|
1066
|
+
if (zone === "UTC") {
|
|
1067
|
+
const d = new Date(t * 1e3);
|
|
1068
|
+
return {
|
|
1069
|
+
year: d.getUTCFullYear(),
|
|
1070
|
+
month: d.getUTCMonth() + 1,
|
|
1071
|
+
day: d.getUTCDate(),
|
|
1072
|
+
hour: d.getUTCHours(),
|
|
1073
|
+
minute: d.getUTCMinutes(),
|
|
1074
|
+
second: d.getUTCSeconds()
|
|
1075
|
+
};
|
|
1076
|
+
}
|
|
1077
|
+
const r = {};
|
|
1078
|
+
for (const p of zoneFormatter(zone).formatToParts(new Date(t * 1e3)))
|
|
1079
|
+
if (p.type !== "literal") r[p.type] = Number(p.value);
|
|
1080
|
+
if (r.hour === 24) r.hour = 0;
|
|
1081
|
+
return r;
|
|
1082
|
+
}
|
|
1083
|
+
function wallEpoch(t, zone) {
|
|
1084
|
+
const p = wallParts(t, zone);
|
|
1085
|
+
return utcFromParts(p.year, p.month, p.day, p.hour, p.minute, p.second) + (t - Math.floor(t));
|
|
1086
|
+
}
|
|
1087
|
+
function offsetAt(t, zone) {
|
|
1088
|
+
return Math.round(wallEpoch(t, zone) - t);
|
|
1089
|
+
}
|
|
1090
|
+
function localCandidates(w, zone) {
|
|
1091
|
+
if (zone === "UTC") return [w];
|
|
1092
|
+
const offsets = new Set(
|
|
1093
|
+
[-172800, -86400, 0, 86400, 172800].map((d) => offsetAt(w + d, zone))
|
|
1094
|
+
), out = [];
|
|
1095
|
+
for (const off of offsets) {
|
|
1096
|
+
const t = w - off;
|
|
1097
|
+
if (Math.abs(wallEpoch(t, zone) - w) < 1e-3) out.push(t);
|
|
1098
|
+
}
|
|
1099
|
+
return out.sort((a, b) => a - b);
|
|
1100
|
+
}
|
|
1101
|
+
function wallTicks(start, end, step, zone) {
|
|
1102
|
+
if (zone === "UTC") return multiples(start, end, step);
|
|
1103
|
+
const sa = wallEpoch(start, zone), sb = wallEpoch(end, zone), a = Math.floor(Math.min(sa, sb) / step) - 2, b = Math.ceil(Math.max(sa, sb) / step) + 2;
|
|
1104
|
+
check(
|
|
1105
|
+
finite(b - a) && b - a <= LIMITS.ticks && Math.abs(a) < 9e15 && Math.abs(b) < 9e15,
|
|
1106
|
+
"Too many time ticks; increase the interval."
|
|
1107
|
+
);
|
|
1108
|
+
const offsets = /* @__PURE__ */ new Set();
|
|
1109
|
+
const probe = Math.max(43200, (end - start) / 4096);
|
|
1110
|
+
for (let t = start - 172800; t <= end + 172800; t += probe)
|
|
1111
|
+
offsets.add(offsetAt(t, zone));
|
|
1112
|
+
offsets.add(offsetAt(end, zone));
|
|
1113
|
+
const out = /* @__PURE__ */ new Set();
|
|
1114
|
+
for (let i = 0; i <= b - a; i++) {
|
|
1115
|
+
const w = (a + i) * step;
|
|
1116
|
+
for (const off of offsets) {
|
|
1117
|
+
const t = w - off;
|
|
1118
|
+
if (t >= start && t <= end && Math.abs(wallEpoch(t, zone) - w) < 1e-3)
|
|
1119
|
+
out.add(t);
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
return [...out].sort((a2, b2) => a2 - b2);
|
|
1123
|
+
}
|
|
1124
|
+
function monthTicks(start, end, zone, stride = 1) {
|
|
1125
|
+
const a = wallParts(start, zone), b = wallParts(end, zone), first = Math.floor((a.year * 12 + a.month - 1) / stride) * stride, last = b.year * 12 + b.month - 1 + stride;
|
|
1126
|
+
check((last - first) / stride <= LIMITS.ticks, "Too many calendar ticks.");
|
|
1127
|
+
const out = [];
|
|
1128
|
+
for (let i = first; i <= last; i += stride) {
|
|
1129
|
+
const y = Math.floor(i / 12), m = i % 12 + 1;
|
|
1130
|
+
if (y < 1 || y > 9999) continue;
|
|
1131
|
+
for (const t of localCandidates(utcFromParts(y, m, 1), zone))
|
|
1132
|
+
if (t >= start && t <= end) out.push(t);
|
|
1133
|
+
}
|
|
1134
|
+
return out.sort((a2, b2) => a2 - b2);
|
|
1135
|
+
}
|
|
1136
|
+
var MONTHS = [
|
|
1137
|
+
"Jan",
|
|
1138
|
+
"Feb",
|
|
1139
|
+
"Mar",
|
|
1140
|
+
"Apr",
|
|
1141
|
+
"May",
|
|
1142
|
+
"Jun",
|
|
1143
|
+
"Jul",
|
|
1144
|
+
"Aug",
|
|
1145
|
+
"Sep",
|
|
1146
|
+
"Oct",
|
|
1147
|
+
"Nov",
|
|
1148
|
+
"Dec"
|
|
1149
|
+
];
|
|
1150
|
+
var DAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
|
1151
|
+
var pad = (v, n = 2) => String(v).padStart(n, "0");
|
|
1152
|
+
function formatTime(t, zone = "UTC", format = "%H:%M") {
|
|
1153
|
+
t = epoch(t);
|
|
1154
|
+
const p = wallParts(t, zone), d = new Date(utcFromParts(p.year, p.month, p.day) * 1e3), off = offsetAt(t, zone), day = d.getUTCDay();
|
|
1155
|
+
const fields = {
|
|
1156
|
+
H: pad(p.hour),
|
|
1157
|
+
I: pad(p.hour % 12 || 12),
|
|
1158
|
+
M: pad(p.minute),
|
|
1159
|
+
S: pad(p.second),
|
|
1160
|
+
d: pad(p.day),
|
|
1161
|
+
e: String(p.day).padStart(2, " "),
|
|
1162
|
+
m: pad(p.month),
|
|
1163
|
+
Y: pad(p.year, 4),
|
|
1164
|
+
y: pad(p.year % 100),
|
|
1165
|
+
a: DAYS[day],
|
|
1166
|
+
A: [
|
|
1167
|
+
"Sunday",
|
|
1168
|
+
"Monday",
|
|
1169
|
+
"Tuesday",
|
|
1170
|
+
"Wednesday",
|
|
1171
|
+
"Thursday",
|
|
1172
|
+
"Friday",
|
|
1173
|
+
"Saturday"
|
|
1174
|
+
][day],
|
|
1175
|
+
b: MONTHS[p.month - 1],
|
|
1176
|
+
h: MONTHS[p.month - 1],
|
|
1177
|
+
B: [
|
|
1178
|
+
"January",
|
|
1179
|
+
"February",
|
|
1180
|
+
"March",
|
|
1181
|
+
"April",
|
|
1182
|
+
"May",
|
|
1183
|
+
"June",
|
|
1184
|
+
"July",
|
|
1185
|
+
"August",
|
|
1186
|
+
"September",
|
|
1187
|
+
"October",
|
|
1188
|
+
"November",
|
|
1189
|
+
"December"
|
|
1190
|
+
][p.month - 1],
|
|
1191
|
+
p: p.hour < 12 ? "AM" : "PM",
|
|
1192
|
+
w: String(day),
|
|
1193
|
+
u: String(day || 7),
|
|
1194
|
+
j: pad(
|
|
1195
|
+
Math.floor((d.getTime() / 1e3 - utcFromParts(p.year, 1, 1)) / 86400) + 1,
|
|
1196
|
+
3
|
|
1197
|
+
),
|
|
1198
|
+
z: (off < 0 ? "-" : "+") + pad(Math.floor(Math.abs(off) / 3600)) + pad(Math.floor(Math.abs(off) % 3600 / 60)),
|
|
1199
|
+
Z: zone,
|
|
1200
|
+
"%": "%"
|
|
1201
|
+
};
|
|
1202
|
+
fields.F = fields.Y + "-" + fields.m + "-" + fields.d;
|
|
1203
|
+
fields.T = fields.H + ":" + fields.M + ":" + fields.S;
|
|
1204
|
+
fields.R = fields.H + ":" + fields.M;
|
|
1205
|
+
let out = "";
|
|
1206
|
+
for (let i = 0; i < format.length; i++) {
|
|
1207
|
+
if (format[i] !== "%") {
|
|
1208
|
+
out += format[i];
|
|
1209
|
+
continue;
|
|
1210
|
+
}
|
|
1211
|
+
const k = format[++i];
|
|
1212
|
+
check(own(fields, k), "Unsupported date directive: %" + (k || ""));
|
|
1213
|
+
out += fields[k];
|
|
1214
|
+
}
|
|
1215
|
+
return out;
|
|
1216
|
+
}
|
|
1217
|
+
function resolveX(a, start, end, width) {
|
|
1218
|
+
const span = end - start, zone = a.timezone, mode = a.mode === "auto" ? span <= 172800 ? "daily" : span <= 864e3 ? "weekly" : span <= 5356800 ? "monthly" : "yearly" : a.mode;
|
|
1219
|
+
let minor = [], major = [], labelTimes = [], format = "%H:%M";
|
|
1220
|
+
if (mode === "yearly") {
|
|
1221
|
+
const stride = span > 550 * 86400 ? Math.max(1, Math.ceil(span / (365.25 * 86400))) : 1;
|
|
1222
|
+
format = stride > 1 ? "%b %Y" : "%b";
|
|
1223
|
+
if (a.majorTicks === null || a.ticks === null) {
|
|
1224
|
+
major = monthTicks(start, end, zone, stride);
|
|
1225
|
+
labelTimes = major;
|
|
1226
|
+
}
|
|
1227
|
+
if (a.minorTicks === null) minor = monthTicks(start, end, zone, 1);
|
|
1228
|
+
if (a.majorTicks === null && a.majorSeconds !== null)
|
|
1229
|
+
major = wallTicks(start, end, a.majorSeconds, zone);
|
|
1230
|
+
if (a.minorTicks === null && a.minorSeconds !== null)
|
|
1231
|
+
minor = wallTicks(start, end, a.minorSeconds, zone);
|
|
1232
|
+
if (a.ticks === null && a.labelSeconds !== null)
|
|
1233
|
+
labelTimes = wallTicks(start, end, a.labelSeconds, zone);
|
|
1234
|
+
} else {
|
|
1235
|
+
let mi = 1800, ma = 7200, ls = 7200;
|
|
1236
|
+
if (mode === "weekly") {
|
|
1237
|
+
mi = 21600;
|
|
1238
|
+
ma = ls = 86400;
|
|
1239
|
+
format = "%d";
|
|
1240
|
+
}
|
|
1241
|
+
if (mode === "monthly") {
|
|
1242
|
+
mi = 86400;
|
|
1243
|
+
ma = ls = 604800;
|
|
1244
|
+
format = "%d %b";
|
|
1245
|
+
}
|
|
1246
|
+
if (a.mode === "auto" && span < 43200) {
|
|
1247
|
+
const target = span / Math.max(2, Math.floor(width / 48));
|
|
1248
|
+
ls = [1, 5, 10, 15, 30, 60, 120, 300, 600, 900, 1800, 3600, 7200].find(
|
|
1249
|
+
(v) => v >= target
|
|
1250
|
+
) || 7200;
|
|
1251
|
+
mi = Math.max(1, ls / 4);
|
|
1252
|
+
ma = ls;
|
|
1253
|
+
if (ls < 60) format = "%H:%M:%S";
|
|
1254
|
+
}
|
|
1255
|
+
mi = Math.max(mi, span / 2e3);
|
|
1256
|
+
if (a.minorSeconds !== null) mi = a.minorSeconds;
|
|
1257
|
+
if (a.majorSeconds !== null) ma = a.majorSeconds;
|
|
1258
|
+
if (a.labelSeconds !== null) ls = a.labelSeconds;
|
|
1259
|
+
if (a.minorTicks === null) minor = wallTicks(start, end, mi, zone);
|
|
1260
|
+
if (a.majorTicks === null) major = wallTicks(start, end, ma, zone);
|
|
1261
|
+
if (a.ticks === null) labelTimes = wallTicks(start, end, ls, zone);
|
|
1262
|
+
}
|
|
1263
|
+
if (a.minorTicks !== null)
|
|
1264
|
+
minor = a.minorTicks.filter((v) => v >= start && v <= end);
|
|
1265
|
+
if (a.majorTicks !== null)
|
|
1266
|
+
major = a.majorTicks.filter((v) => v >= start && v <= end);
|
|
1267
|
+
if (a.labelFormat !== null) format = a.labelFormat;
|
|
1268
|
+
let labels = [];
|
|
1269
|
+
if (a.ticks !== null)
|
|
1270
|
+
labels = a.ticks.filter((v) => v.time >= start && v.time <= end);
|
|
1271
|
+
else if (mode === "weekly" && a.labelSeconds === null && a.labelOffsetSeconds === 0) {
|
|
1272
|
+
for (const t of wallTicks(start - 172800, end, 86400, zone)) {
|
|
1273
|
+
const w = wallEpoch(t, zone), next = localCandidates(w + 86400, zone);
|
|
1274
|
+
if (t < start || !next.length || next[next.length - 1] > end) continue;
|
|
1275
|
+
for (const noon of localCandidates(w + 43200, zone))
|
|
1276
|
+
if (noon >= start && noon <= end)
|
|
1277
|
+
labels.push({ time: noon, label: formatTime(t, zone, format) });
|
|
1278
|
+
}
|
|
1279
|
+
} else
|
|
1280
|
+
for (const t of labelTimes) {
|
|
1281
|
+
const pos = t + a.labelOffsetSeconds;
|
|
1282
|
+
if (pos >= start && pos <= end)
|
|
1283
|
+
labels.push({ time: pos, label: formatTime(t, zone, format) });
|
|
1284
|
+
}
|
|
1285
|
+
labels.sort((a2, b) => a2.time - b.time);
|
|
1286
|
+
if (a.ticks === null && mode === "daily" && span <= 95040) {
|
|
1287
|
+
const seen = /* @__PURE__ */ new Map();
|
|
1288
|
+
for (const t of labels) {
|
|
1289
|
+
if (!seen.has(t.label)) seen.set(t.label, /* @__PURE__ */ new Set());
|
|
1290
|
+
seen.get(t.label).add(offsetAt(t.time, zone));
|
|
1291
|
+
}
|
|
1292
|
+
labels = labels.map(
|
|
1293
|
+
(t) => seen.get(t.label).size > 1 ? {
|
|
1294
|
+
time: t.time,
|
|
1295
|
+
label: t.label + " " + formatTime(t.time, zone, "%z")
|
|
1296
|
+
} : t
|
|
1297
|
+
);
|
|
1298
|
+
}
|
|
1299
|
+
const majorSet = new Set(major);
|
|
1300
|
+
minor = minor.filter((v) => !majorSet.has(v));
|
|
1301
|
+
return { minor, major, labels, mode };
|
|
1302
|
+
}
|
|
1303
|
+
function timeRange(c) {
|
|
1304
|
+
let start = Infinity, end = -Infinity;
|
|
1305
|
+
for (const s of c.series)
|
|
1306
|
+
if (s.timestamps.length) {
|
|
1307
|
+
start = Math.min(start, s.timestamps[0]);
|
|
1308
|
+
end = Math.max(end, s.timestamps[s.timestamps.length - 1]);
|
|
1309
|
+
}
|
|
1310
|
+
if (c.timeAxis.start !== null) start = c.timeAxis.start;
|
|
1311
|
+
if (c.timeAxis.end !== null) end = c.timeAxis.end;
|
|
1312
|
+
if (start === end && c.timeAxis.start === null && c.timeAxis.end === null) {
|
|
1313
|
+
start -= 150;
|
|
1314
|
+
end += 150;
|
|
1315
|
+
}
|
|
1316
|
+
epoch(start);
|
|
1317
|
+
epoch(end);
|
|
1318
|
+
check(
|
|
1319
|
+
end > start,
|
|
1320
|
+
"Empty data needs explicit start and end; range must be nonzero."
|
|
1321
|
+
);
|
|
1322
|
+
return [start, end];
|
|
1323
|
+
}
|
|
1324
|
+
var PIXEL_GLYPHS = {
|
|
1325
|
+
" ": ["00000", "00000", "00000", "00000", "00000", "00000", "00000"],
|
|
1326
|
+
"0": ["01110", "10001", "10011", "10101", "11001", "10001", "01110"],
|
|
1327
|
+
"1": ["00100", "01100", "00100", "00100", "00100", "00100", "01110"],
|
|
1328
|
+
"2": ["01110", "10001", "00001", "00010", "00100", "01000", "11111"],
|
|
1329
|
+
"3": ["11110", "00001", "00001", "01110", "00001", "00001", "11110"],
|
|
1330
|
+
"4": ["00010", "00110", "01010", "10010", "11111", "00010", "00010"],
|
|
1331
|
+
"5": ["11111", "10000", "10000", "11110", "00001", "00001", "11110"],
|
|
1332
|
+
"6": ["01110", "10000", "10000", "11110", "10001", "10001", "01110"],
|
|
1333
|
+
"7": ["11111", "00001", "00010", "00100", "01000", "01000", "01000"],
|
|
1334
|
+
"8": ["01110", "10001", "10001", "01110", "10001", "10001", "01110"],
|
|
1335
|
+
"9": ["01110", "10001", "10001", "01111", "00001", "00001", "01110"],
|
|
1336
|
+
A: ["01110", "10001", "10001", "11111", "10001", "10001", "10001"],
|
|
1337
|
+
B: ["11110", "10001", "10001", "11110", "10001", "10001", "11110"],
|
|
1338
|
+
C: ["01111", "10000", "10000", "10000", "10000", "10000", "01111"],
|
|
1339
|
+
D: ["11110", "10001", "10001", "10001", "10001", "10001", "11110"],
|
|
1340
|
+
E: ["11111", "10000", "10000", "11110", "10000", "10000", "11111"],
|
|
1341
|
+
F: ["11111", "10000", "10000", "11110", "10000", "10000", "10000"],
|
|
1342
|
+
G: ["01110", "10001", "10000", "10111", "10001", "10001", "01111"],
|
|
1343
|
+
H: ["10001", "10001", "10001", "11111", "10001", "10001", "10001"],
|
|
1344
|
+
I: ["01110", "00100", "00100", "00100", "00100", "00100", "01110"],
|
|
1345
|
+
J: ["00111", "00010", "00010", "00010", "00010", "10010", "01100"],
|
|
1346
|
+
K: ["10001", "10010", "10100", "11000", "10100", "10010", "10001"],
|
|
1347
|
+
L: ["10000", "10000", "10000", "10000", "10000", "10000", "11111"],
|
|
1348
|
+
M: ["10001", "11011", "10101", "10101", "10001", "10001", "10001"],
|
|
1349
|
+
N: ["10001", "11001", "10101", "10011", "10001", "10001", "10001"],
|
|
1350
|
+
O: ["01110", "10001", "10001", "10001", "10001", "10001", "01110"],
|
|
1351
|
+
P: ["11110", "10001", "10001", "11110", "10000", "10000", "10000"],
|
|
1352
|
+
Q: ["01110", "10001", "10001", "10001", "10101", "10010", "01101"],
|
|
1353
|
+
R: ["11110", "10001", "10001", "11110", "10100", "10010", "10001"],
|
|
1354
|
+
S: ["01111", "10000", "10000", "01110", "00001", "00001", "11110"],
|
|
1355
|
+
T: ["11111", "00100", "00100", "00100", "00100", "00100", "00100"],
|
|
1356
|
+
U: ["10001", "10001", "10001", "10001", "10001", "10001", "01110"],
|
|
1357
|
+
V: ["10001", "10001", "10001", "10001", "10001", "01010", "00100"],
|
|
1358
|
+
W: ["10001", "10001", "10001", "10101", "10101", "11011", "10001"],
|
|
1359
|
+
X: ["10001", "10001", "01010", "00100", "01010", "10001", "10001"],
|
|
1360
|
+
Y: ["10001", "10001", "01010", "00100", "00100", "00100", "00100"],
|
|
1361
|
+
Z: ["11111", "00001", "00010", "00100", "01000", "10000", "11111"],
|
|
1362
|
+
a: ["00000", "00000", "01110", "00001", "01111", "10001", "01111"],
|
|
1363
|
+
b: ["10000", "10000", "10110", "11001", "10001", "10001", "11110"],
|
|
1364
|
+
c: ["00000", "00000", "01111", "10000", "10000", "10000", "01111"],
|
|
1365
|
+
d: ["00001", "00001", "01101", "10011", "10001", "10001", "01111"],
|
|
1366
|
+
e: ["00000", "00000", "01110", "10001", "11111", "10000", "01110"],
|
|
1367
|
+
f: ["00110", "01001", "01000", "11100", "01000", "01000", "01000"],
|
|
1368
|
+
g: ["00000", "01111", "10001", "10001", "01111", "00001", "01110"],
|
|
1369
|
+
h: ["10000", "10000", "10110", "11001", "10001", "10001", "10001"],
|
|
1370
|
+
i: ["00100", "00000", "01100", "00100", "00100", "00100", "01110"],
|
|
1371
|
+
j: ["00010", "00000", "00110", "00010", "00010", "10010", "01100"],
|
|
1372
|
+
k: ["10000", "10000", "10010", "10100", "11000", "10100", "10010"],
|
|
1373
|
+
l: ["01100", "00100", "00100", "00100", "00100", "00100", "01110"],
|
|
1374
|
+
m: ["00000", "00000", "11010", "10101", "10101", "10101", "10101"],
|
|
1375
|
+
n: ["00000", "00000", "10110", "11001", "10001", "10001", "10001"],
|
|
1376
|
+
o: ["00000", "00000", "01110", "10001", "10001", "10001", "01110"],
|
|
1377
|
+
p: ["00000", "00000", "11110", "10001", "11110", "10000", "10000"],
|
|
1378
|
+
q: ["00000", "00000", "01111", "10001", "01111", "00001", "00001"],
|
|
1379
|
+
r: ["00000", "00000", "10111", "11000", "10000", "10000", "10000"],
|
|
1380
|
+
s: ["00000", "00000", "01111", "10000", "01110", "00001", "11110"],
|
|
1381
|
+
t: ["01000", "01000", "11100", "01000", "01000", "01001", "00110"],
|
|
1382
|
+
u: ["00000", "00000", "10001", "10001", "10001", "10011", "01101"],
|
|
1383
|
+
v: ["00000", "00000", "10001", "10001", "10001", "01010", "00100"],
|
|
1384
|
+
w: ["00000", "00000", "10001", "10001", "10101", "10101", "01010"],
|
|
1385
|
+
x: ["00000", "00000", "10001", "01010", "00100", "01010", "10001"],
|
|
1386
|
+
y: ["00000", "00000", "10001", "10001", "01111", "00001", "01110"],
|
|
1387
|
+
z: ["00000", "00000", "11111", "00010", "00100", "01000", "11111"],
|
|
1388
|
+
"-": ["00000", "00000", "00000", "11111", "00000", "00000", "00000"],
|
|
1389
|
+
_: ["00000", "00000", "00000", "00000", "00000", "00000", "11111"],
|
|
1390
|
+
":": ["00000", "00100", "00100", "00000", "00100", "00100", "00000"],
|
|
1391
|
+
".": ["00000", "00000", "00000", "00000", "00000", "00100", "00100"],
|
|
1392
|
+
",": ["00000", "00000", "00000", "00000", "00100", "00100", "01000"],
|
|
1393
|
+
"/": ["00001", "00010", "00010", "00100", "01000", "01000", "10000"],
|
|
1394
|
+
"\\": ["10000", "01000", "01000", "00100", "00010", "00010", "00001"],
|
|
1395
|
+
"(": ["00010", "00100", "01000", "01000", "01000", "00100", "00010"],
|
|
1396
|
+
")": ["01000", "00100", "00010", "00010", "00010", "00100", "01000"],
|
|
1397
|
+
"[": ["01110", "01000", "01000", "01000", "01000", "01000", "01110"],
|
|
1398
|
+
"]": ["01110", "00010", "00010", "00010", "00010", "00010", "01110"],
|
|
1399
|
+
"+": ["00000", "00100", "00100", "11111", "00100", "00100", "00000"],
|
|
1400
|
+
"=": ["00000", "00000", "11111", "00000", "11111", "00000", "00000"],
|
|
1401
|
+
"%": ["11001", "11010", "00010", "00100", "01000", "01011", "10011"],
|
|
1402
|
+
"?": ["01110", "10001", "00001", "00010", "00100", "00000", "00100"],
|
|
1403
|
+
"!": ["00100", "00100", "00100", "00100", "00100", "00000", "00100"],
|
|
1404
|
+
"#": ["01010", "01010", "11111", "01010", "11111", "01010", "01010"],
|
|
1405
|
+
"*": ["00000", "10101", "01110", "11111", "01110", "10101", "00000"],
|
|
1406
|
+
"<": ["00010", "00100", "01000", "10000", "01000", "00100", "00010"],
|
|
1407
|
+
">": ["01000", "00100", "00010", "00001", "00010", "00100", "01000"],
|
|
1408
|
+
"|": ["00100", "00100", "00100", "00100", "00100", "00100", "00100"],
|
|
1409
|
+
'"': ["01010", "01010", "00000", "00000", "00000", "00000", "00000"],
|
|
1410
|
+
"'": ["00100", "00100", "00000", "00000", "00000", "00000", "00000"],
|
|
1411
|
+
";": ["00000", "00100", "00100", "00000", "00100", "00100", "01000"],
|
|
1412
|
+
"@": ["01110", "10001", "10111", "10101", "10111", "10000", "01111"],
|
|
1413
|
+
$: ["00100", "01111", "10100", "01110", "00101", "11110", "00100"],
|
|
1414
|
+
"&": ["01100", "10010", "10100", "01000", "10101", "10010", "01101"],
|
|
1415
|
+
"^": ["00100", "01010", "10001", "00000", "00000", "00000", "00000"],
|
|
1416
|
+
"`": ["01000", "00100", "00000", "00000", "00000", "00000", "00000"],
|
|
1417
|
+
"~": ["00000", "00000", "01001", "10110", "00000", "00000", "00000"],
|
|
1418
|
+
"{": ["00011", "00100", "00100", "01000", "00100", "00100", "00011"],
|
|
1419
|
+
"}": ["11000", "00100", "00100", "00010", "00100", "00100", "11000"]
|
|
1420
|
+
};
|
|
1421
|
+
function createCanvas(w, h) {
|
|
1422
|
+
let canvas;
|
|
1423
|
+
if (typeof document !== "undefined" && document.createElement)
|
|
1424
|
+
canvas = document.createElement("canvas");
|
|
1425
|
+
else if (typeof OffscreenCanvas !== "undefined")
|
|
1426
|
+
canvas = new OffscreenCanvas(w, h);
|
|
1427
|
+
else
|
|
1428
|
+
throw new Error(
|
|
1429
|
+
'System fonts need Canvas 2D. Use fonts: { mode: "bitmap" } in a DOM-free runtime.'
|
|
1430
|
+
);
|
|
1431
|
+
canvas.width = w;
|
|
1432
|
+
canvas.height = h;
|
|
1433
|
+
return canvas;
|
|
1434
|
+
}
|
|
1435
|
+
function wide(ch) {
|
|
1436
|
+
const n = ch.codePointAt(0);
|
|
1437
|
+
return n >= 4352 && (n <= 4447 || n >= 11904 && n <= 42191 || n >= 44032 && n <= 55203 || n >= 63744 && n <= 64255 || n >= 65280 && n <= 65376 || n >= 127744);
|
|
1438
|
+
}
|
|
1439
|
+
var Fonts = class {
|
|
1440
|
+
config;
|
|
1441
|
+
theme;
|
|
1442
|
+
cache;
|
|
1443
|
+
canvas;
|
|
1444
|
+
ctx;
|
|
1445
|
+
constructor(config, theme) {
|
|
1446
|
+
this.config = config;
|
|
1447
|
+
this.theme = theme;
|
|
1448
|
+
this.cache = /* @__PURE__ */ new Map();
|
|
1449
|
+
if (config.mode === "system") {
|
|
1450
|
+
this.canvas = createCanvas(8, 8);
|
|
1451
|
+
const ctx = this.canvas.getContext("2d", {
|
|
1452
|
+
willReadFrequently: true
|
|
1453
|
+
});
|
|
1454
|
+
check(ctx, "Canvas 2D is unavailable.");
|
|
1455
|
+
this.ctx = ctx;
|
|
1456
|
+
}
|
|
1457
|
+
}
|
|
1458
|
+
size(role, scale = 1) {
|
|
1459
|
+
return this.theme[`${role}Size`] * scale;
|
|
1460
|
+
}
|
|
1461
|
+
family(role) {
|
|
1462
|
+
return (role === "title" || role === "unit" || role === "caption" ? this.config[`${role}Family`] : null) || this.config.family;
|
|
1463
|
+
}
|
|
1464
|
+
setup(role, size) {
|
|
1465
|
+
this.ctx.font = (role === "caption" ? "bold " : "") + size + "px " + this.family(role);
|
|
1466
|
+
this.ctx.textBaseline = "alphabetic";
|
|
1467
|
+
this.ctx.fillStyle = "#000000";
|
|
1468
|
+
if ("fontKerning" in this.ctx) this.ctx.fontKerning = "none";
|
|
1469
|
+
}
|
|
1470
|
+
width(value, role, advance = 0, scale = 1) {
|
|
1471
|
+
if (advance > 0)
|
|
1472
|
+
return [...value].reduce((n, ch) => n + advance * (wide(ch) ? 2 : 1), 0);
|
|
1473
|
+
if (this.config.mode === "bitmap")
|
|
1474
|
+
return [...value].length * this.size(role, scale) * 0.6;
|
|
1475
|
+
this.setup(role, this.size(role, scale));
|
|
1476
|
+
return this.ctx.measureText(value).width;
|
|
1477
|
+
}
|
|
1478
|
+
fit(value, maxWidth, role, advance, scale = 1) {
|
|
1479
|
+
if (this.width(value, role, advance, scale) <= maxWidth) return value;
|
|
1480
|
+
const end = "...";
|
|
1481
|
+
if (this.width(end, role, advance, scale) > maxWidth) return "";
|
|
1482
|
+
const a = [...value];
|
|
1483
|
+
while (a.length && this.width(a.join("") + end, role, advance, scale) > maxWidth)
|
|
1484
|
+
a.pop();
|
|
1485
|
+
return a.join("") + end;
|
|
1486
|
+
}
|
|
1487
|
+
raster(value, role, advance = 0, scale = 1) {
|
|
1488
|
+
const key = JSON.stringify([value, role, advance, scale]);
|
|
1489
|
+
if (this.cache.has(key)) return this.cache.get(key);
|
|
1490
|
+
const size = this.size(role, scale), w = Math.max(1, Math.ceil(this.width(value, role, advance, scale)) + 4);
|
|
1491
|
+
let out;
|
|
1492
|
+
check(
|
|
1493
|
+
w <= 32768 && w * size < LIMITS.pixels,
|
|
1494
|
+
"Text allocation limit exceeded."
|
|
1495
|
+
);
|
|
1496
|
+
if (this.config.mode === "bitmap") {
|
|
1497
|
+
const h = Math.max(1, round(size * 0.72));
|
|
1498
|
+
out = new Surface(w, h + 2);
|
|
1499
|
+
let x = 1;
|
|
1500
|
+
for (const ch of value) {
|
|
1501
|
+
let rows = PIXEL_GLYPHS[ch];
|
|
1502
|
+
if (!rows) {
|
|
1503
|
+
check(
|
|
1504
|
+
!this.config.strictGlyphs,
|
|
1505
|
+
"Bitmap text supports printable ASCII only: " + ch
|
|
1506
|
+
);
|
|
1507
|
+
rows = PIXEL_GLYPHS["?"];
|
|
1508
|
+
}
|
|
1509
|
+
const cell = advance > 0 ? advance : size * 0.6, gw = Math.max(1, Math.min(round(size * 0.5), Math.floor(cell) - 1));
|
|
1510
|
+
for (let yy = 0; yy < h; yy++)
|
|
1511
|
+
for (let xx = 0; xx < gw; xx++)
|
|
1512
|
+
if (rows[Math.min(6, Math.floor(yy / h * 7))][Math.min(4, Math.floor(xx / gw * 5))] === "1")
|
|
1513
|
+
out.pixel(round(x) + xx, yy + 1, [0, 0, 0, 255]);
|
|
1514
|
+
x += cell * (wide(ch) ? 2 : 1);
|
|
1515
|
+
}
|
|
1516
|
+
} else {
|
|
1517
|
+
this.setup(role, size);
|
|
1518
|
+
const metric = this.ctx.measureText(value || "0");
|
|
1519
|
+
const ascent = Math.ceil(metric.actualBoundingBoxAscent || size * 0.8), descent = Math.ceil(metric.actualBoundingBoxDescent || 0), h = Math.max(1, ascent + descent) + 2;
|
|
1520
|
+
this.canvas.width = w;
|
|
1521
|
+
this.canvas.height = h;
|
|
1522
|
+
this.setup(role, size);
|
|
1523
|
+
let x = 1;
|
|
1524
|
+
for (const ch of value) {
|
|
1525
|
+
this.ctx.fillText(ch, round(x), 1 + ascent);
|
|
1526
|
+
x += advance > 0 ? advance * (wide(ch) ? 2 : 1) : this.ctx.measureText(ch).width;
|
|
1527
|
+
}
|
|
1528
|
+
const d = this.ctx.getImageData(0, 0, w, h);
|
|
1529
|
+
out = new Surface(w, h);
|
|
1530
|
+
out.data.set(d.data);
|
|
1531
|
+
}
|
|
1532
|
+
if (this.cache.size >= 512)
|
|
1533
|
+
this.cache.delete(this.cache.keys().next().value);
|
|
1534
|
+
this.cache.set(key, out);
|
|
1535
|
+
return out;
|
|
1536
|
+
}
|
|
1537
|
+
draw(im, x, y, value, role, fill, advance = 0, align = "left", centerY = false, scale = 1) {
|
|
1538
|
+
if (!value) return;
|
|
1539
|
+
const r = this.raster(value, role, advance, scale), width = this.width(value, role, advance, scale);
|
|
1540
|
+
if (align === "center") x -= width / 2;
|
|
1541
|
+
else if (align === "right") x -= width;
|
|
1542
|
+
if (centerY) y -= (r.height - 2) / 2;
|
|
1543
|
+
const xx = round(x) - 1, yy = round(y) - 1, p = [fill[0], fill[1], fill[2], 0];
|
|
1544
|
+
for (let sy = 0; sy < r.height; sy++)
|
|
1545
|
+
for (let sx = 0; sx < r.width; sx++) {
|
|
1546
|
+
const a = r.data[(sy * r.width + sx) * 4 + 3];
|
|
1547
|
+
if (a) {
|
|
1548
|
+
p[3] = round(a * fill[3] / 255);
|
|
1549
|
+
im.pixel(xx + sx, yy + sy, p, true);
|
|
1550
|
+
}
|
|
1551
|
+
}
|
|
1552
|
+
}
|
|
1553
|
+
rotated(value, role, fill, clockwise = false) {
|
|
1554
|
+
const r = this.raster(value, role), out = new Surface(r.height, r.width), p = [fill[0], fill[1], fill[2], 0];
|
|
1555
|
+
for (let y = 0; y < r.height; y++)
|
|
1556
|
+
for (let x = 0; x < r.width; x++) {
|
|
1557
|
+
p[3] = round(r.data[(y * r.width + x) * 4 + 3] * fill[3] / 255);
|
|
1558
|
+
if (p[3])
|
|
1559
|
+
out.pixel(
|
|
1560
|
+
clockwise ? r.height - 1 - y : y,
|
|
1561
|
+
clockwise ? x : r.width - 1 - x,
|
|
1562
|
+
p
|
|
1563
|
+
);
|
|
1564
|
+
}
|
|
1565
|
+
return out;
|
|
1566
|
+
}
|
|
1567
|
+
};
|
|
1568
|
+
function renderChart(c) {
|
|
1569
|
+
const l = c.layout, t = c.theme, [width, height] = dimensions(c), left = l.left, top = l.top, right = l.width - l.right, bottom = l.top + l.plotHeight, pw = right - left, ph = bottom - top;
|
|
1570
|
+
const [start, end] = timeRange(c);
|
|
1571
|
+
const runs = c.series.map((s) => visibleRuns(s, start, end));
|
|
1572
|
+
let loData = Infinity, hiData = -Infinity;
|
|
1573
|
+
for (let i = 0; i < runs.length; i++) {
|
|
1574
|
+
for (const run of runs[i])
|
|
1575
|
+
for (const p of run) {
|
|
1576
|
+
check(finite(p[1]), "Interpolated value overflow.");
|
|
1577
|
+
loData = Math.min(loData, p[1]);
|
|
1578
|
+
hiData = Math.max(hiData, p[1]);
|
|
1579
|
+
}
|
|
1580
|
+
if (c.series[i].kind === "area") {
|
|
1581
|
+
loData = Math.min(loData, c.series[i].baseline);
|
|
1582
|
+
hiData = Math.max(hiData, c.series[i].baseline);
|
|
1583
|
+
}
|
|
1584
|
+
}
|
|
1585
|
+
if (loData === Infinity) loData = hiData = 0;
|
|
1586
|
+
const ys = resolveY(c.yAxis, loData, hiData), xs = resolveX(c.timeAxis, start, end, pw), fonts = new Fonts(c.fonts, t), im = new Surface(width, height, t.background);
|
|
1587
|
+
im.rect(left, top, right + 1, bottom + 1, t.canvas);
|
|
1588
|
+
const xx = (v) => (v - start) / (end - start) * pw, yy = (v) => ph * (1 - (v / (ys.maximum - ys.minimum) - ys.minimum / (ys.maximum - ys.minimum)));
|
|
1589
|
+
const projected = runs.map(
|
|
1590
|
+
(rr) => rr.map(
|
|
1591
|
+
(r) => decimate(r, start, end, pw).map((p) => {
|
|
1592
|
+
const x = xx(p[0]), y = yy(p[1]);
|
|
1593
|
+
check(
|
|
1594
|
+
finite(x) && finite(y) && Math.abs(y) < 1e15,
|
|
1595
|
+
"Data magnitude is too large relative to the Y axis."
|
|
1596
|
+
);
|
|
1597
|
+
return [x, y];
|
|
1598
|
+
})
|
|
1599
|
+
)
|
|
1600
|
+
);
|
|
1601
|
+
function grid() {
|
|
1602
|
+
const lay = new Surface(pw + 1, ph + 1);
|
|
1603
|
+
for (const [values, col] of [
|
|
1604
|
+
[ys.minor, t.minorGrid],
|
|
1605
|
+
[ys.major, t.majorGrid]
|
|
1606
|
+
])
|
|
1607
|
+
for (const v of values) {
|
|
1608
|
+
const y = round(yy(v));
|
|
1609
|
+
if (y >= 0 && y <= ph) lay.dashed([0, y], [pw, y], col, t.gridDash);
|
|
1610
|
+
}
|
|
1611
|
+
for (const [values, col] of [
|
|
1612
|
+
[xs.minor, t.minorGrid],
|
|
1613
|
+
[xs.major, t.majorGrid]
|
|
1614
|
+
])
|
|
1615
|
+
for (const v of values) {
|
|
1616
|
+
const x = round(xx(v));
|
|
1617
|
+
lay.dashed([x, 0], [x, ph], col, t.gridDash);
|
|
1618
|
+
}
|
|
1619
|
+
im.over(lay, left, top);
|
|
1620
|
+
}
|
|
1621
|
+
const aa = l.antialias, layerWidth = (pw + 1) * aa, layerHeight = (ph + 1) * aa;
|
|
1622
|
+
if (!t.gridFront) grid();
|
|
1623
|
+
for (let i = 0; i < c.series.length; i++) {
|
|
1624
|
+
const s = c.series[i];
|
|
1625
|
+
if (s.kind !== "area") continue;
|
|
1626
|
+
const base = yy(s.baseline);
|
|
1627
|
+
check(
|
|
1628
|
+
finite(base) && Math.abs(base) < 1e15,
|
|
1629
|
+
"Baseline magnitude is too large."
|
|
1630
|
+
);
|
|
1631
|
+
const layer = new Surface(layerWidth, layerHeight);
|
|
1632
|
+
for (const ps of projected[i])
|
|
1633
|
+
if (ps.length > 1) {
|
|
1634
|
+
const poly = clipPolygon(
|
|
1635
|
+
[[ps[0][0], base], ...ps, [ps[ps.length - 1][0], base]],
|
|
1636
|
+
pw,
|
|
1637
|
+
ph
|
|
1638
|
+
).map((p) => [round(p[0] * aa), round(p[1] * aa)]);
|
|
1639
|
+
layer.polygon(poly, s.color);
|
|
1640
|
+
}
|
|
1641
|
+
im.over(layer.down(aa), left, top);
|
|
1642
|
+
}
|
|
1643
|
+
if (t.gridFront) grid();
|
|
1644
|
+
for (let i = 0; i < c.series.length; i++) {
|
|
1645
|
+
const s = c.series[i], col = s.kind === "line" ? s.color : s.outline;
|
|
1646
|
+
if (col === null) continue;
|
|
1647
|
+
const layer = new Surface(layerWidth, layerHeight), lw = Math.max(1, round(s.lineWidth * aa));
|
|
1648
|
+
for (const ps of projected[i]) {
|
|
1649
|
+
if (ps.length === 1) {
|
|
1650
|
+
const p = ps[0];
|
|
1651
|
+
if (p[0] >= 0 && p[0] <= pw && p[1] >= 0 && p[1] <= ph)
|
|
1652
|
+
layer.circle(p[0] * aa, p[1] * aa, Math.max(aa / 2, lw / 2), col);
|
|
1653
|
+
}
|
|
1654
|
+
for (let k = 1; k < ps.length; k++) {
|
|
1655
|
+
const seg = clipLine(ps[k - 1], ps[k], pw, ph);
|
|
1656
|
+
if (seg)
|
|
1657
|
+
layer.line(
|
|
1658
|
+
[round(seg[0][0] * aa), round(seg[0][1] * aa)],
|
|
1659
|
+
[round(seg[1][0] * aa), round(seg[1][1] * aa)],
|
|
1660
|
+
col,
|
|
1661
|
+
lw
|
|
1662
|
+
);
|
|
1663
|
+
}
|
|
1664
|
+
}
|
|
1665
|
+
im.over(layer.down(aa), left, top);
|
|
1666
|
+
}
|
|
1667
|
+
const rules = new Surface(pw + 1, ph + 1);
|
|
1668
|
+
for (const r of c.hRules)
|
|
1669
|
+
if (r.value >= ys.minimum && r.value <= ys.maximum)
|
|
1670
|
+
rules.dashed(
|
|
1671
|
+
[0, round(yy(r.value))],
|
|
1672
|
+
[pw, round(yy(r.value))],
|
|
1673
|
+
r.color,
|
|
1674
|
+
r.dash,
|
|
1675
|
+
Math.max(1, round(r.width))
|
|
1676
|
+
);
|
|
1677
|
+
for (const r of c.vRules)
|
|
1678
|
+
if (r.time >= start && r.time <= end)
|
|
1679
|
+
rules.dashed(
|
|
1680
|
+
[round(xx(r.time)), 0],
|
|
1681
|
+
[round(xx(r.time)), ph],
|
|
1682
|
+
r.color,
|
|
1683
|
+
r.dash,
|
|
1684
|
+
Math.max(1, round(r.width))
|
|
1685
|
+
);
|
|
1686
|
+
im.over(rules, left, top);
|
|
1687
|
+
im.line([left, top - 3], [left, bottom + 4], t.axis);
|
|
1688
|
+
im.line([left - 4, bottom], [right + 4, bottom], t.axis);
|
|
1689
|
+
im.polygon(
|
|
1690
|
+
[
|
|
1691
|
+
[left, top - 5],
|
|
1692
|
+
[left - 3, top],
|
|
1693
|
+
[left + 3, top]
|
|
1694
|
+
],
|
|
1695
|
+
t.arrow
|
|
1696
|
+
);
|
|
1697
|
+
im.polygon(
|
|
1698
|
+
[
|
|
1699
|
+
[right + 7, bottom],
|
|
1700
|
+
[right + 2, bottom - 3],
|
|
1701
|
+
[right + 2, bottom + 3]
|
|
1702
|
+
],
|
|
1703
|
+
t.arrow
|
|
1704
|
+
);
|
|
1705
|
+
for (const v of ys.major) {
|
|
1706
|
+
const y = top + yy(v), unit = v === 0 && !c.yAxis.showZeroSuffix ? { factor: ys.factor, suffix: "" } : ys, label = formatValue(v, unit, ys.decimals);
|
|
1707
|
+
check(
|
|
1708
|
+
fonts.width(label, "axis", t.axisAdvance) <= left - l.yLabelGap - 20,
|
|
1709
|
+
"Y labels overlap the vertical label. Increase layout.left or adjust units."
|
|
1710
|
+
);
|
|
1711
|
+
im.line([left - 3, round(y)], [left, round(y)], t.axis);
|
|
1712
|
+
fonts.draw(
|
|
1713
|
+
im,
|
|
1714
|
+
left - l.yLabelGap,
|
|
1715
|
+
y,
|
|
1716
|
+
label,
|
|
1717
|
+
"axis",
|
|
1718
|
+
t.text,
|
|
1719
|
+
t.axisAdvance,
|
|
1720
|
+
"right",
|
|
1721
|
+
true
|
|
1722
|
+
);
|
|
1723
|
+
}
|
|
1724
|
+
const xLabels = [];
|
|
1725
|
+
let lastRight = -Infinity;
|
|
1726
|
+
for (const tick of xs.labels) {
|
|
1727
|
+
const x = left + xx(tick.time), tw = fonts.width(tick.label, "axis", t.axisAdvance), a = x - tw / 2, b = x + tw / 2;
|
|
1728
|
+
if (c.timeAxis.ticks === null && a < lastRight + 3) continue;
|
|
1729
|
+
if (a < 2 || b > width - 3) continue;
|
|
1730
|
+
im.line(
|
|
1731
|
+
[round(x), bottom],
|
|
1732
|
+
[round(x), bottom + 3],
|
|
1733
|
+
[t.majorGrid[0], t.majorGrid[1], t.majorGrid[2], 255]
|
|
1734
|
+
);
|
|
1735
|
+
fonts.draw(
|
|
1736
|
+
im,
|
|
1737
|
+
x,
|
|
1738
|
+
bottom + l.xLabelGap,
|
|
1739
|
+
tick.label,
|
|
1740
|
+
"axis",
|
|
1741
|
+
t.text,
|
|
1742
|
+
t.axisAdvance,
|
|
1743
|
+
"center"
|
|
1744
|
+
);
|
|
1745
|
+
lastRight = b;
|
|
1746
|
+
xLabels.push({ ...tick, x });
|
|
1747
|
+
}
|
|
1748
|
+
const titleX = (left + right) / 2 + l.titleOffsetX, title = fonts.fit(
|
|
1749
|
+
c.title,
|
|
1750
|
+
2 * Math.min(titleX - 5, width - 14 - titleX),
|
|
1751
|
+
"title",
|
|
1752
|
+
t.titleAdvance
|
|
1753
|
+
);
|
|
1754
|
+
fonts.draw(
|
|
1755
|
+
im,
|
|
1756
|
+
titleX,
|
|
1757
|
+
l.titleY,
|
|
1758
|
+
title,
|
|
1759
|
+
"title",
|
|
1760
|
+
t.text,
|
|
1761
|
+
t.titleAdvance,
|
|
1762
|
+
"center"
|
|
1763
|
+
);
|
|
1764
|
+
if (c.verticalLabel) {
|
|
1765
|
+
const unit = fonts.rotated(c.verticalLabel, "unit", t.text);
|
|
1766
|
+
check(
|
|
1767
|
+
unit.height <= ph + 18 && l.unitX + unit.width <= left - l.yLabelGap,
|
|
1768
|
+
"Vertical label does not fit."
|
|
1769
|
+
);
|
|
1770
|
+
im.over(unit, l.unitX, round((top + bottom - unit.height) / 2));
|
|
1771
|
+
}
|
|
1772
|
+
if (c.watermark) {
|
|
1773
|
+
const mark = fonts.rotated(c.watermark, "watermark", t.watermark, true);
|
|
1774
|
+
check(
|
|
1775
|
+
mark.height <= height - 8 && mark.width <= l.right - 9,
|
|
1776
|
+
"Watermark does not fit."
|
|
1777
|
+
);
|
|
1778
|
+
im.over(mark, width - mark.width - 4, 4);
|
|
1779
|
+
}
|
|
1780
|
+
const stats = c.series.map((s) => statistics(s, start, end));
|
|
1781
|
+
if (l.legend !== "none") {
|
|
1782
|
+
const scale = Math.min(1, (width - 40) / 555), advance = t.legendAdvance * scale, ll = l.legendLayout, factor = ll.autoScaleColumns ? (width - ll.nameX) / (ll.referenceWidth - ll.nameX) : 1, anchor = (x) => ll.nameX + (x - ll.nameX) * factor;
|
|
1783
|
+
c.series.forEach((s, i) => {
|
|
1784
|
+
const pairs = l.legend === "aligned" ? ll.aligned : l.legend === "reference" && i === c.series.length - 1 && i > 0 ? ll.expanded : ll.compact, y = bottom + l.legendGap + i * l.legendRowHeight;
|
|
1785
|
+
check(
|
|
1786
|
+
ll.swatchHeight <= l.legendRowHeight && ll.swatchX + ll.swatchWidth < width && y + ll.swatchHeight <= height - 2,
|
|
1787
|
+
"Legend swatch does not fit."
|
|
1788
|
+
);
|
|
1789
|
+
im.rect(
|
|
1790
|
+
ll.swatchX,
|
|
1791
|
+
y,
|
|
1792
|
+
ll.swatchX + ll.swatchWidth,
|
|
1793
|
+
y + ll.swatchHeight,
|
|
1794
|
+
t.frame
|
|
1795
|
+
);
|
|
1796
|
+
const swatch = new Surface(
|
|
1797
|
+
ll.swatchWidth - 2,
|
|
1798
|
+
ll.swatchHeight - 2,
|
|
1799
|
+
s.color
|
|
1800
|
+
);
|
|
1801
|
+
im.over(swatch, ll.swatchX + 1, y + 1);
|
|
1802
|
+
const name = fonts.fit(
|
|
1803
|
+
s.name,
|
|
1804
|
+
anchor(pairs[0][0]) - ll.nameX - 12,
|
|
1805
|
+
"legend",
|
|
1806
|
+
advance,
|
|
1807
|
+
scale
|
|
1808
|
+
);
|
|
1809
|
+
fonts.draw(
|
|
1810
|
+
im,
|
|
1811
|
+
ll.nameX,
|
|
1812
|
+
y,
|
|
1813
|
+
name,
|
|
1814
|
+
"legend",
|
|
1815
|
+
t.text,
|
|
1816
|
+
advance,
|
|
1817
|
+
"left",
|
|
1818
|
+
false,
|
|
1819
|
+
scale
|
|
1820
|
+
);
|
|
1821
|
+
const display = s.legendValues || stats[i];
|
|
1822
|
+
["current", "average", "maximum"].forEach((key, j) => {
|
|
1823
|
+
const label = c.legendLabels[j], value = formatValue(
|
|
1824
|
+
display[key],
|
|
1825
|
+
ys,
|
|
1826
|
+
c.yAxis.legendDecimals,
|
|
1827
|
+
c.missingLabel
|
|
1828
|
+
), a = anchor(pairs[j][0]), b = anchor(pairs[j][1]);
|
|
1829
|
+
check(b < width - 3, "Legend column is outside the panel.");
|
|
1830
|
+
check(
|
|
1831
|
+
fonts.width(label, "legend", advance, scale) + fonts.width(value, "legend", advance, scale) + 7 * scale <= b - a + 1,
|
|
1832
|
+
"Legend statistic does not fit. Increase width, adjust anchors or reduce legendDecimals."
|
|
1833
|
+
);
|
|
1834
|
+
fonts.draw(
|
|
1835
|
+
im,
|
|
1836
|
+
a,
|
|
1837
|
+
y,
|
|
1838
|
+
label,
|
|
1839
|
+
"legend",
|
|
1840
|
+
t.text,
|
|
1841
|
+
advance,
|
|
1842
|
+
"left",
|
|
1843
|
+
false,
|
|
1844
|
+
scale
|
|
1845
|
+
);
|
|
1846
|
+
fonts.draw(
|
|
1847
|
+
im,
|
|
1848
|
+
b,
|
|
1849
|
+
y,
|
|
1850
|
+
value,
|
|
1851
|
+
"legend",
|
|
1852
|
+
t.text,
|
|
1853
|
+
advance,
|
|
1854
|
+
"right",
|
|
1855
|
+
false,
|
|
1856
|
+
scale
|
|
1857
|
+
);
|
|
1858
|
+
});
|
|
1859
|
+
});
|
|
1860
|
+
}
|
|
1861
|
+
for (const [a, b] of [
|
|
1862
|
+
[
|
|
1863
|
+
[0, 0],
|
|
1864
|
+
[width - 1, 0]
|
|
1865
|
+
],
|
|
1866
|
+
[
|
|
1867
|
+
[1, 1],
|
|
1868
|
+
[width - 2, 1]
|
|
1869
|
+
],
|
|
1870
|
+
[
|
|
1871
|
+
[0, 0],
|
|
1872
|
+
[0, height - 1]
|
|
1873
|
+
],
|
|
1874
|
+
[
|
|
1875
|
+
[1, 1],
|
|
1876
|
+
[1, height - 2]
|
|
1877
|
+
]
|
|
1878
|
+
])
|
|
1879
|
+
im.line(a, b, t.shadeLight);
|
|
1880
|
+
for (const [a, b] of [
|
|
1881
|
+
[
|
|
1882
|
+
[width - 2, 1],
|
|
1883
|
+
[width - 2, height - 1]
|
|
1884
|
+
],
|
|
1885
|
+
[
|
|
1886
|
+
[width - 1, 0],
|
|
1887
|
+
[width - 1, height - 1]
|
|
1888
|
+
],
|
|
1889
|
+
[
|
|
1890
|
+
[1, height - 2],
|
|
1891
|
+
[width - 1, height - 2]
|
|
1892
|
+
],
|
|
1893
|
+
[
|
|
1894
|
+
[0, height - 1],
|
|
1895
|
+
[width - 1, height - 1]
|
|
1896
|
+
]
|
|
1897
|
+
])
|
|
1898
|
+
im.line(a, b, t.shadeDark);
|
|
1899
|
+
for (let i = 3; i < im.data.length; i += 4) im.data[i] = 255;
|
|
1900
|
+
const out = im.scale(l.pixelScale);
|
|
1901
|
+
const metadata = {
|
|
1902
|
+
version: VERSION,
|
|
1903
|
+
imageSize: [out.width, out.height],
|
|
1904
|
+
logicalSize: [width, height],
|
|
1905
|
+
plotBox: [left, top, right, bottom],
|
|
1906
|
+
pixelScale: l.pixelScale,
|
|
1907
|
+
title: c.title,
|
|
1908
|
+
verticalLabel: c.verticalLabel,
|
|
1909
|
+
watermark: c.watermark,
|
|
1910
|
+
timeRange: [start, end],
|
|
1911
|
+
timezone: c.timeAxis.timezone,
|
|
1912
|
+
timeMode: xs.mode,
|
|
1913
|
+
yRange: [ys.minimum, ys.maximum],
|
|
1914
|
+
yStep: ys.step,
|
|
1915
|
+
yUnit: { factor: ys.factor, suffix: ys.suffix },
|
|
1916
|
+
xLabels,
|
|
1917
|
+
statistics: stats,
|
|
1918
|
+
statisticsPolicy: "inclusive viewport; original sample arithmetic mean; current includes final missing sample",
|
|
1919
|
+
font: {
|
|
1920
|
+
mode: c.fonts.mode,
|
|
1921
|
+
family: c.fonts.mode === "system" ? c.fonts.family : null,
|
|
1922
|
+
pixelAlphabet: c.fonts.mode === "bitmap" ? "ascii-5x7-v1" : null
|
|
1923
|
+
},
|
|
1924
|
+
layout: clone(l),
|
|
1925
|
+
theme: clone(t),
|
|
1926
|
+
warnings: c.fonts.mode === "system" ? ["System font selection and glyph rasterization depend on the host."] : []
|
|
1927
|
+
};
|
|
1928
|
+
return new RenderResult(out, metadata);
|
|
1929
|
+
}
|
|
1930
|
+
var CRC_TABLE = (() => {
|
|
1931
|
+
const t = new Uint32Array(256);
|
|
1932
|
+
for (let i = 0; i < 256; i++) {
|
|
1933
|
+
let c = i;
|
|
1934
|
+
for (let j = 0; j < 8; j++) c = c & 1 ? 3988292384 ^ c >>> 1 : c >>> 1;
|
|
1935
|
+
t[i] = c >>> 0;
|
|
1936
|
+
}
|
|
1937
|
+
return t;
|
|
1938
|
+
})();
|
|
1939
|
+
function crc32(bytes) {
|
|
1940
|
+
let c = 4294967295;
|
|
1941
|
+
for (const b of bytes) c = CRC_TABLE[(c ^ b) & 255] ^ c >>> 8;
|
|
1942
|
+
return (c ^ 4294967295) >>> 0;
|
|
1943
|
+
}
|
|
1944
|
+
function adler32(bytes) {
|
|
1945
|
+
let a = 1, b = 0;
|
|
1946
|
+
for (let i = 0; i < bytes.length; ) {
|
|
1947
|
+
const end = Math.min(i + 5552, bytes.length);
|
|
1948
|
+
for (; i < end; i++) {
|
|
1949
|
+
a += bytes[i];
|
|
1950
|
+
b += a;
|
|
1951
|
+
}
|
|
1952
|
+
a %= 65521;
|
|
1953
|
+
b %= 65521;
|
|
1954
|
+
}
|
|
1955
|
+
return (b << 16 | a) >>> 0;
|
|
1956
|
+
}
|
|
1957
|
+
function concat(parts) {
|
|
1958
|
+
const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
|
|
1959
|
+
let at = 0;
|
|
1960
|
+
for (const p of parts) {
|
|
1961
|
+
out.set(p, at);
|
|
1962
|
+
at += p.length;
|
|
1963
|
+
}
|
|
1964
|
+
return out;
|
|
1965
|
+
}
|
|
1966
|
+
function u32(n) {
|
|
1967
|
+
return new Uint8Array([
|
|
1968
|
+
n >>> 24 & 255,
|
|
1969
|
+
n >>> 16 & 255,
|
|
1970
|
+
n >>> 8 & 255,
|
|
1971
|
+
n & 255
|
|
1972
|
+
]);
|
|
1973
|
+
}
|
|
1974
|
+
function chunk(type, data) {
|
|
1975
|
+
const code = Uint8Array.from(type, (c) => c.charCodeAt(0)), body = concat([code, data]);
|
|
1976
|
+
return concat([u32(data.length), body, u32(crc32(body))]);
|
|
1977
|
+
}
|
|
1978
|
+
function reverseBits(n, bits) {
|
|
1979
|
+
let out = 0;
|
|
1980
|
+
for (let i = 0; i < bits; i++) {
|
|
1981
|
+
out = out << 1 | n & 1;
|
|
1982
|
+
n >>>= 1;
|
|
1983
|
+
}
|
|
1984
|
+
return out;
|
|
1985
|
+
}
|
|
1986
|
+
var FIXED = (() => {
|
|
1987
|
+
const out = [];
|
|
1988
|
+
for (let i = 0; i < 288; i++) {
|
|
1989
|
+
let code, bits;
|
|
1990
|
+
if (i <= 143) {
|
|
1991
|
+
bits = 8;
|
|
1992
|
+
code = 48 + i;
|
|
1993
|
+
} else if (i <= 255) {
|
|
1994
|
+
bits = 9;
|
|
1995
|
+
code = 400 + i - 144;
|
|
1996
|
+
} else if (i <= 279) {
|
|
1997
|
+
bits = 7;
|
|
1998
|
+
code = i - 256;
|
|
1999
|
+
} else {
|
|
2000
|
+
bits = 8;
|
|
2001
|
+
code = 192 + i - 280;
|
|
2002
|
+
}
|
|
2003
|
+
out.push([reverseBits(code, bits), bits]);
|
|
2004
|
+
}
|
|
2005
|
+
return out;
|
|
2006
|
+
})();
|
|
2007
|
+
var LEN_BASE = [
|
|
2008
|
+
3,
|
|
2009
|
+
4,
|
|
2010
|
+
5,
|
|
2011
|
+
6,
|
|
2012
|
+
7,
|
|
2013
|
+
8,
|
|
2014
|
+
9,
|
|
2015
|
+
10,
|
|
2016
|
+
11,
|
|
2017
|
+
13,
|
|
2018
|
+
15,
|
|
2019
|
+
17,
|
|
2020
|
+
19,
|
|
2021
|
+
23,
|
|
2022
|
+
27,
|
|
2023
|
+
31,
|
|
2024
|
+
35,
|
|
2025
|
+
43,
|
|
2026
|
+
51,
|
|
2027
|
+
59,
|
|
2028
|
+
67,
|
|
2029
|
+
83,
|
|
2030
|
+
99,
|
|
2031
|
+
115,
|
|
2032
|
+
131,
|
|
2033
|
+
163,
|
|
2034
|
+
195,
|
|
2035
|
+
227,
|
|
2036
|
+
258
|
|
2037
|
+
];
|
|
2038
|
+
var LEN_EXTRA = [
|
|
2039
|
+
0,
|
|
2040
|
+
0,
|
|
2041
|
+
0,
|
|
2042
|
+
0,
|
|
2043
|
+
0,
|
|
2044
|
+
0,
|
|
2045
|
+
0,
|
|
2046
|
+
0,
|
|
2047
|
+
1,
|
|
2048
|
+
1,
|
|
2049
|
+
1,
|
|
2050
|
+
1,
|
|
2051
|
+
2,
|
|
2052
|
+
2,
|
|
2053
|
+
2,
|
|
2054
|
+
2,
|
|
2055
|
+
3,
|
|
2056
|
+
3,
|
|
2057
|
+
3,
|
|
2058
|
+
3,
|
|
2059
|
+
4,
|
|
2060
|
+
4,
|
|
2061
|
+
4,
|
|
2062
|
+
4,
|
|
2063
|
+
5,
|
|
2064
|
+
5,
|
|
2065
|
+
5,
|
|
2066
|
+
5,
|
|
2067
|
+
0
|
|
2068
|
+
];
|
|
2069
|
+
var DIST_BASE = [
|
|
2070
|
+
1,
|
|
2071
|
+
2,
|
|
2072
|
+
3,
|
|
2073
|
+
4,
|
|
2074
|
+
5,
|
|
2075
|
+
7,
|
|
2076
|
+
9,
|
|
2077
|
+
13,
|
|
2078
|
+
17,
|
|
2079
|
+
25,
|
|
2080
|
+
33,
|
|
2081
|
+
49,
|
|
2082
|
+
65,
|
|
2083
|
+
97,
|
|
2084
|
+
129,
|
|
2085
|
+
193,
|
|
2086
|
+
257,
|
|
2087
|
+
385,
|
|
2088
|
+
513,
|
|
2089
|
+
769,
|
|
2090
|
+
1025,
|
|
2091
|
+
1537,
|
|
2092
|
+
2049,
|
|
2093
|
+
3073,
|
|
2094
|
+
4097,
|
|
2095
|
+
6145,
|
|
2096
|
+
8193,
|
|
2097
|
+
12289,
|
|
2098
|
+
16385,
|
|
2099
|
+
24577
|
|
2100
|
+
];
|
|
2101
|
+
var DIST_EXTRA = [
|
|
2102
|
+
0,
|
|
2103
|
+
0,
|
|
2104
|
+
0,
|
|
2105
|
+
0,
|
|
2106
|
+
1,
|
|
2107
|
+
1,
|
|
2108
|
+
2,
|
|
2109
|
+
2,
|
|
2110
|
+
3,
|
|
2111
|
+
3,
|
|
2112
|
+
4,
|
|
2113
|
+
4,
|
|
2114
|
+
5,
|
|
2115
|
+
5,
|
|
2116
|
+
6,
|
|
2117
|
+
6,
|
|
2118
|
+
7,
|
|
2119
|
+
7,
|
|
2120
|
+
8,
|
|
2121
|
+
8,
|
|
2122
|
+
9,
|
|
2123
|
+
9,
|
|
2124
|
+
10,
|
|
2125
|
+
10,
|
|
2126
|
+
11,
|
|
2127
|
+
11,
|
|
2128
|
+
12,
|
|
2129
|
+
12,
|
|
2130
|
+
13,
|
|
2131
|
+
13
|
|
2132
|
+
];
|
|
2133
|
+
function deflate(data) {
|
|
2134
|
+
let bytes = new Uint8Array(Math.max(128, data.length + 64)), len = 0, buffer = 0, count = 0;
|
|
2135
|
+
function byte(b) {
|
|
2136
|
+
if (len === bytes.length) {
|
|
2137
|
+
const next = new Uint8Array(bytes.length * 2);
|
|
2138
|
+
next.set(bytes);
|
|
2139
|
+
bytes = next;
|
|
2140
|
+
}
|
|
2141
|
+
bytes[len++] = b;
|
|
2142
|
+
}
|
|
2143
|
+
function bits(value, n) {
|
|
2144
|
+
buffer |= value << count;
|
|
2145
|
+
count += n;
|
|
2146
|
+
while (count >= 8) {
|
|
2147
|
+
byte(buffer & 255);
|
|
2148
|
+
buffer >>>= 8;
|
|
2149
|
+
count -= 8;
|
|
2150
|
+
}
|
|
2151
|
+
}
|
|
2152
|
+
const sym = (v) => bits(FIXED[v][0], FIXED[v][1]);
|
|
2153
|
+
byte(120);
|
|
2154
|
+
byte(1);
|
|
2155
|
+
bits(3, 3);
|
|
2156
|
+
const head = new Int32Array(65536).fill(-1), prev = new Int32Array(32768).fill(-1);
|
|
2157
|
+
const hash = (i) => (data[i] * 251 + data[i + 1]) * 251 + data[i + 2] & 65535;
|
|
2158
|
+
function insert(i) {
|
|
2159
|
+
if (i + 2 >= data.length) return;
|
|
2160
|
+
const h = hash(i);
|
|
2161
|
+
prev[i & 32767] = head[h];
|
|
2162
|
+
head[h] = i;
|
|
2163
|
+
}
|
|
2164
|
+
for (let i = 0; i < data.length; ) {
|
|
2165
|
+
let best = 0, dist = 0;
|
|
2166
|
+
if (i + 2 < data.length) {
|
|
2167
|
+
let candidate = head[hash(i)], tries = 64;
|
|
2168
|
+
const max = Math.min(258, data.length - i);
|
|
2169
|
+
while (candidate >= 0 && i - candidate <= 32768 && candidate < i && tries--) {
|
|
2170
|
+
if (data[candidate] === data[i] && data[candidate + best] === data[i + best]) {
|
|
2171
|
+
let n = 0;
|
|
2172
|
+
while (n < max && data[candidate + n] === data[i + n]) n++;
|
|
2173
|
+
if (n > best && n >= 3) {
|
|
2174
|
+
best = n;
|
|
2175
|
+
dist = i - candidate;
|
|
2176
|
+
if (n === max) break;
|
|
2177
|
+
}
|
|
2178
|
+
}
|
|
2179
|
+
const next = prev[candidate & 32767];
|
|
2180
|
+
if (next >= candidate) break;
|
|
2181
|
+
candidate = next;
|
|
2182
|
+
}
|
|
2183
|
+
}
|
|
2184
|
+
if (best >= 3) {
|
|
2185
|
+
let lc = 0;
|
|
2186
|
+
while (lc < 28 && LEN_BASE[lc + 1] <= best) lc++;
|
|
2187
|
+
sym(257 + lc);
|
|
2188
|
+
bits(best - LEN_BASE[lc], LEN_EXTRA[lc]);
|
|
2189
|
+
let dc = 0;
|
|
2190
|
+
while (dc < 29 && DIST_BASE[dc + 1] <= dist) dc++;
|
|
2191
|
+
bits(reverseBits(dc, 5), 5);
|
|
2192
|
+
bits(dist - DIST_BASE[dc], DIST_EXTRA[dc]);
|
|
2193
|
+
for (let j = 0; j < best; j++) insert(i + j);
|
|
2194
|
+
i += best;
|
|
2195
|
+
} else {
|
|
2196
|
+
sym(data[i]);
|
|
2197
|
+
insert(i);
|
|
2198
|
+
i++;
|
|
2199
|
+
}
|
|
2200
|
+
}
|
|
2201
|
+
sym(256);
|
|
2202
|
+
if (count) byte(buffer & 255);
|
|
2203
|
+
const sum = adler32(data);
|
|
2204
|
+
for (const b of u32(sum)) byte(b);
|
|
2205
|
+
return bytes.slice(0, len);
|
|
2206
|
+
}
|
|
2207
|
+
function paeth(a, b, c) {
|
|
2208
|
+
const p = a + b - c, pa = Math.abs(p - a), pb = Math.abs(p - b), pc = Math.abs(p - c);
|
|
2209
|
+
return pa <= pb && pa <= pc ? a : pb <= pc ? b : c;
|
|
2210
|
+
}
|
|
2211
|
+
function encodePNG(image, metadata = null) {
|
|
2212
|
+
validateImage(image);
|
|
2213
|
+
check(
|
|
2214
|
+
image.width * image.height <= LIMITS.pixels,
|
|
2215
|
+
"PNG image exceeds output limit."
|
|
2216
|
+
);
|
|
2217
|
+
const w = image.width, h = image.height, stride = w * 4, raw = new Uint8Array((stride + 1) * h), candidates = Array.from({ length: 5 }, () => new Uint8Array(stride));
|
|
2218
|
+
for (let y = 0; y < h; y++) {
|
|
2219
|
+
const scores = [0, 0, 0, 0, 0], off = y * stride;
|
|
2220
|
+
for (let x = 0; x < stride; x++) {
|
|
2221
|
+
const v = image.data[off + x], a = x >= 4 ? image.data[off + x - 4] : 0, b = y ? image.data[off + x - stride] : 0, c = y && x >= 4 ? image.data[off + x - stride - 4] : 0, predict = [0, a, b, Math.floor((a + b) / 2), paeth(a, b, c)];
|
|
2222
|
+
for (let f = 0; f < 5; f++) {
|
|
2223
|
+
const n = v - predict[f] & 255;
|
|
2224
|
+
candidates[f][x] = n;
|
|
2225
|
+
scores[f] += Math.min(n, 256 - n);
|
|
2226
|
+
}
|
|
2227
|
+
}
|
|
2228
|
+
let best = 0;
|
|
2229
|
+
for (let f = 1; f < 5; f++) if (scores[f] < scores[best]) best = f;
|
|
2230
|
+
raw[y * (stride + 1)] = best;
|
|
2231
|
+
raw.set(candidates[best], y * (stride + 1) + 1);
|
|
2232
|
+
}
|
|
2233
|
+
const ihdr = concat([u32(w), u32(h), new Uint8Array([8, 6, 0, 0, 0])]), parts = [
|
|
2234
|
+
new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10]),
|
|
2235
|
+
chunk("IHDR", ihdr),
|
|
2236
|
+
chunk("sRGB", new Uint8Array([0]))
|
|
2237
|
+
];
|
|
2238
|
+
if (metadata !== null) {
|
|
2239
|
+
const json = JSON.stringify(metadata);
|
|
2240
|
+
check(json.length <= 1048576, "PNG metadata is too large.");
|
|
2241
|
+
parts.push(
|
|
2242
|
+
chunk(
|
|
2243
|
+
"iTXt",
|
|
2244
|
+
concat([
|
|
2245
|
+
new TextEncoder().encode("chart"),
|
|
2246
|
+
new Uint8Array([0, 0, 0, 0, 0]),
|
|
2247
|
+
new TextEncoder().encode(json)
|
|
2248
|
+
])
|
|
2249
|
+
)
|
|
2250
|
+
);
|
|
2251
|
+
}
|
|
2252
|
+
parts.push(chunk("IDAT", deflate(raw)), chunk("IEND", new Uint8Array(0)));
|
|
2253
|
+
return concat(parts);
|
|
2254
|
+
}
|
|
2255
|
+
function validateImage(im) {
|
|
2256
|
+
check(
|
|
2257
|
+
im && Number.isInteger(im.width) && Number.isInteger(im.height) && im.width > 0 && im.height > 0 && im.width * im.height <= LIMITS.layerPixels && im.data && im.data.length === im.width * im.height * 4,
|
|
2258
|
+
"Expected an RGBA image with matching dimensions."
|
|
2259
|
+
);
|
|
2260
|
+
}
|
|
2261
|
+
function drawImageToCanvas(image, canvas) {
|
|
2262
|
+
validateImage(image);
|
|
2263
|
+
check(
|
|
2264
|
+
canvas && typeof canvas.getContext === "function",
|
|
2265
|
+
"Expected a canvas element."
|
|
2266
|
+
);
|
|
2267
|
+
const ctx = canvas.getContext("2d");
|
|
2268
|
+
check(ctx, "Canvas 2D context is unavailable.");
|
|
2269
|
+
canvas.width = image.width;
|
|
2270
|
+
canvas.height = image.height;
|
|
2271
|
+
const data = ctx.createImageData(image.width, image.height);
|
|
2272
|
+
data.data.set(image.data);
|
|
2273
|
+
ctx.putImageData(data, 0, 0);
|
|
2274
|
+
return canvas;
|
|
2275
|
+
}
|
|
2276
|
+
function downloadBytes(bytes, name, type = "application/octet-stream") {
|
|
2277
|
+
check(
|
|
2278
|
+
typeof document !== "undefined",
|
|
2279
|
+
"Downloads require a browser document."
|
|
2280
|
+
);
|
|
2281
|
+
text(name, "Filename");
|
|
2282
|
+
check(
|
|
2283
|
+
!/[\\/\x00-\x1f]/.test(name),
|
|
2284
|
+
"Filename must not contain path separators or controls."
|
|
2285
|
+
);
|
|
2286
|
+
const data = bytes.buffer instanceof ArrayBuffer ? bytes : new Uint8Array(bytes);
|
|
2287
|
+
const blob = new Blob([data], { type }), url = URL.createObjectURL(blob), a = document.createElement("a");
|
|
2288
|
+
a.href = url;
|
|
2289
|
+
a.download = name;
|
|
2290
|
+
a.hidden = true;
|
|
2291
|
+
document.body.appendChild(a);
|
|
2292
|
+
a.click();
|
|
2293
|
+
a.remove();
|
|
2294
|
+
setTimeout(() => URL.revokeObjectURL(url), 3e4);
|
|
2295
|
+
}
|
|
2296
|
+
var RenderResult = class {
|
|
2297
|
+
/** Pixels intentionally remain mutable; exports observe subsequent edits. */
|
|
2298
|
+
image;
|
|
2299
|
+
/** Immutable metadata captured at rendering time. */
|
|
2300
|
+
metadata;
|
|
2301
|
+
constructor(image, metadata) {
|
|
2302
|
+
this.image = image;
|
|
2303
|
+
this.metadata = freeze(metadata);
|
|
2304
|
+
}
|
|
2305
|
+
get width() {
|
|
2306
|
+
return this.image.width;
|
|
2307
|
+
}
|
|
2308
|
+
get height() {
|
|
2309
|
+
return this.image.height;
|
|
2310
|
+
}
|
|
2311
|
+
get data() {
|
|
2312
|
+
return this.image.data;
|
|
2313
|
+
}
|
|
2314
|
+
draw(canvas) {
|
|
2315
|
+
return drawImageToCanvas(this.image, canvas);
|
|
2316
|
+
}
|
|
2317
|
+
toPNG(options = {}) {
|
|
2318
|
+
return encodePNG(
|
|
2319
|
+
this.image,
|
|
2320
|
+
options.metadata === false ? null : this.metadata
|
|
2321
|
+
);
|
|
2322
|
+
}
|
|
2323
|
+
toBlob(options = {}) {
|
|
2324
|
+
return new Blob([this.toPNG(options)], { type: "image/png" });
|
|
2325
|
+
}
|
|
2326
|
+
download(filename = "chart.png", options = {}) {
|
|
2327
|
+
downloadBytes(this.toPNG(options), filename, "image/png");
|
|
2328
|
+
}
|
|
2329
|
+
};
|
|
2330
|
+
var Chart = class _Chart {
|
|
2331
|
+
/** Fully normalized, deeply frozen options used by every render. */
|
|
2332
|
+
config;
|
|
2333
|
+
constructor(options = {}) {
|
|
2334
|
+
this.config = normalize(options);
|
|
2335
|
+
Object.freeze(this);
|
|
2336
|
+
}
|
|
2337
|
+
with(patch) {
|
|
2338
|
+
return new _Chart(merge(this.config, patch));
|
|
2339
|
+
}
|
|
2340
|
+
render() {
|
|
2341
|
+
return renderChart(this.config);
|
|
2342
|
+
}
|
|
2343
|
+
draw(canvas) {
|
|
2344
|
+
const r = this.render();
|
|
2345
|
+
r.draw(canvas);
|
|
2346
|
+
return r;
|
|
2347
|
+
}
|
|
2348
|
+
toPNG(options = {}) {
|
|
2349
|
+
return this.render().toPNG(options);
|
|
2350
|
+
}
|
|
2351
|
+
mount(canvas, options = {}) {
|
|
2352
|
+
return new BrowserController(this, canvas, options);
|
|
2353
|
+
}
|
|
2354
|
+
nearest(input) {
|
|
2355
|
+
const time = epoch(input);
|
|
2356
|
+
const [start, end] = timeRange(this.config);
|
|
2357
|
+
return this.config.series.map((s) => {
|
|
2358
|
+
const lo = lowerBound(s.timestamps, start), hi = upperBound(s.timestamps, end);
|
|
2359
|
+
if (lo === hi)
|
|
2360
|
+
return { name: s.name, index: null, time: null, value: null };
|
|
2361
|
+
let i = clamp(lowerBound(s.timestamps, time), lo, hi - 1);
|
|
2362
|
+
if (i > lo && Math.abs(s.timestamps[i - 1] - time) <= Math.abs(s.timestamps[i] - time))
|
|
2363
|
+
i--;
|
|
2364
|
+
return {
|
|
2365
|
+
name: s.name,
|
|
2366
|
+
index: i,
|
|
2367
|
+
time: s.timestamps[i],
|
|
2368
|
+
value: finite(s.values[i]) ? s.values[i] : null
|
|
2369
|
+
};
|
|
2370
|
+
});
|
|
2371
|
+
}
|
|
2372
|
+
};
|
|
2373
|
+
function traffic(timestamps, inbound, outbound, options = {}) {
|
|
2374
|
+
const gap = options.gapAfter === void 0 ? 0 : options.gapAfter, opts = { ...options };
|
|
2375
|
+
delete opts.gapAfter;
|
|
2376
|
+
return new Chart(
|
|
2377
|
+
merge(
|
|
2378
|
+
{
|
|
2379
|
+
title: "Traffic - ether1",
|
|
2380
|
+
verticalLabel: "bits per second",
|
|
2381
|
+
series: [
|
|
2382
|
+
series("Inbound", timestamps, inbound, {
|
|
2383
|
+
kind: "area",
|
|
2384
|
+
color: "#00cc00",
|
|
2385
|
+
outline: "#003000",
|
|
2386
|
+
gapAfter: gap
|
|
2387
|
+
}),
|
|
2388
|
+
series("Outbound", timestamps, outbound, {
|
|
2389
|
+
kind: "line",
|
|
2390
|
+
color: "#0000cc",
|
|
2391
|
+
gapAfter: gap
|
|
2392
|
+
})
|
|
2393
|
+
]
|
|
2394
|
+
},
|
|
2395
|
+
opts
|
|
2396
|
+
)
|
|
2397
|
+
);
|
|
2398
|
+
}
|
|
2399
|
+
function dashboard(panels, options = {}) {
|
|
2400
|
+
check(
|
|
2401
|
+
Array.isArray(panels) && panels.length > 0 && panels.length <= 128,
|
|
2402
|
+
"Dashboard requires 1..128 panels."
|
|
2403
|
+
);
|
|
2404
|
+
const o = merge(
|
|
2405
|
+
{ gap: 24, padding: [4, 3, 6, 6], background: "#f3f3f3", cropHeight: null },
|
|
2406
|
+
options
|
|
2407
|
+
);
|
|
2408
|
+
integer(o.gap, "Panel gap", 0, 4096);
|
|
2409
|
+
check(
|
|
2410
|
+
Array.isArray(o.padding) && o.padding.length === 4,
|
|
2411
|
+
"Padding is [left, top, right, bottom]."
|
|
2412
|
+
);
|
|
2413
|
+
o.padding.forEach((v) => integer(v, "Padding", 0, 4096));
|
|
2414
|
+
if (o.cropHeight !== null) integer(o.cropHeight, "Crop height", 1, 65536);
|
|
2415
|
+
const pp = panels.map(
|
|
2416
|
+
(p) => p instanceof Chart ? { chart: p, caption: "" } : p
|
|
2417
|
+
);
|
|
2418
|
+
for (const p of pp) {
|
|
2419
|
+
check(p.chart instanceof Chart, "Panel needs a Chart.");
|
|
2420
|
+
text(p.caption || "", "Caption");
|
|
2421
|
+
}
|
|
2422
|
+
const scale = pp[0].chart.config.layout.pixelScale;
|
|
2423
|
+
check(
|
|
2424
|
+
pp.every((p) => p.chart.config.layout.pixelScale === scale),
|
|
2425
|
+
"Dashboard panels must have equal pixelScale."
|
|
2426
|
+
);
|
|
2427
|
+
const rendered = pp.map((p) => p.chart.render()), left = o.padding[0] * scale, top = o.padding[1] * scale, gap = o.gap * scale;
|
|
2428
|
+
const width = Math.max(...rendered.map((r) => r.width)) + (o.padding[0] + o.padding[2]) * scale;
|
|
2429
|
+
let height = rendered.reduce((n, r) => n + r.height, 0) + (o.padding[1] + o.padding[3]) * scale + gap * (pp.length - 1 + (pp[pp.length - 1].caption ? 1 : 0));
|
|
2430
|
+
if (o.cropHeight !== null) height = Math.min(height, o.cropHeight * scale);
|
|
2431
|
+
check(width * height <= LIMITS.pixels, "Dashboard is too large.");
|
|
2432
|
+
const out = new Surface(width, height, color(o.background));
|
|
2433
|
+
let y = top;
|
|
2434
|
+
const meta = [];
|
|
2435
|
+
for (let i = 0; i < pp.length; i++) {
|
|
2436
|
+
const r = rendered[i], p = pp[i];
|
|
2437
|
+
out.over(r.image, left, y);
|
|
2438
|
+
meta.push({
|
|
2439
|
+
position: [left, y],
|
|
2440
|
+
caption: p.caption || "",
|
|
2441
|
+
chart: r.metadata
|
|
2442
|
+
});
|
|
2443
|
+
y += r.height;
|
|
2444
|
+
if (p.caption) {
|
|
2445
|
+
const f = new Fonts(p.chart.config.fonts, p.chart.config.theme), w = f.width(p.caption, "caption"), h = p.chart.config.theme.captionSize;
|
|
2446
|
+
check(
|
|
2447
|
+
w <= width / scale - 8 && h + 4 <= o.gap,
|
|
2448
|
+
"Caption does not fit the dashboard gap."
|
|
2449
|
+
);
|
|
2450
|
+
const line = new Surface(width / scale, o.gap);
|
|
2451
|
+
f.draw(
|
|
2452
|
+
line,
|
|
2453
|
+
width / scale / 2,
|
|
2454
|
+
4,
|
|
2455
|
+
p.caption,
|
|
2456
|
+
"caption",
|
|
2457
|
+
p.chart.config.theme.text,
|
|
2458
|
+
0,
|
|
2459
|
+
"center"
|
|
2460
|
+
);
|
|
2461
|
+
out.over(line.scale(scale), 0, y);
|
|
2462
|
+
}
|
|
2463
|
+
y += gap;
|
|
2464
|
+
}
|
|
2465
|
+
return new RenderResult(out, {
|
|
2466
|
+
version: VERSION,
|
|
2467
|
+
imageSize: [width, height],
|
|
2468
|
+
pixelScale: scale,
|
|
2469
|
+
panels: meta
|
|
2470
|
+
});
|
|
2471
|
+
}
|
|
2472
|
+
function counterRate(timestamps, counters, options = {}) {
|
|
2473
|
+
const o = merge(
|
|
2474
|
+
{ factor: 1, onDecrease: "gap", counterBits: null, maxRate: null },
|
|
2475
|
+
options
|
|
2476
|
+
);
|
|
2477
|
+
number(o.factor, "Rate factor", Number.MIN_VALUE);
|
|
2478
|
+
check(
|
|
2479
|
+
["gap", "wrap"].includes(o.onDecrease),
|
|
2480
|
+
"Decrease policy must be gap or wrap."
|
|
2481
|
+
);
|
|
2482
|
+
if (o.counterBits !== null) integer(o.counterBits, "Counter bits", 1, 128);
|
|
2483
|
+
if (o.maxRate !== null) number(o.maxRate, "Maximum rate", 0);
|
|
2484
|
+
check(
|
|
2485
|
+
o.onDecrease !== "wrap" || o.counterBits !== null,
|
|
2486
|
+
"Wrapping requires an explicit counterBits."
|
|
2487
|
+
);
|
|
2488
|
+
check(
|
|
2489
|
+
(Array.isArray(counters) || ArrayBuffer.isView(counters)) && counters.length === timestamps.length,
|
|
2490
|
+
"Counter and timestamp lengths must match."
|
|
2491
|
+
);
|
|
2492
|
+
const s = samples(
|
|
2493
|
+
timestamps,
|
|
2494
|
+
Array.from(counters, () => 0)
|
|
2495
|
+
), max = (1n << BigInt(o.counterBits || 128)) - 1n;
|
|
2496
|
+
const cs = Array.from(counters, (v, i) => {
|
|
2497
|
+
if (missing(v)) return null;
|
|
2498
|
+
if (typeof v === "number") {
|
|
2499
|
+
check(
|
|
2500
|
+
Number.isSafeInteger(v) && v >= 0,
|
|
2501
|
+
"Counter " + i + " must be a safe nonnegative integer; use BigInt for large counters."
|
|
2502
|
+
);
|
|
2503
|
+
v = BigInt(v);
|
|
2504
|
+
}
|
|
2505
|
+
check(
|
|
2506
|
+
typeof v === "bigint" && v >= 0n && v <= max,
|
|
2507
|
+
"Counter must fit the configured unsigned bit width."
|
|
2508
|
+
);
|
|
2509
|
+
return v;
|
|
2510
|
+
});
|
|
2511
|
+
s.values.fill(NaN);
|
|
2512
|
+
for (let i = 1; i < cs.length; i++) {
|
|
2513
|
+
const current = cs[i], previous = cs[i - 1];
|
|
2514
|
+
if (current === null || previous === null) continue;
|
|
2515
|
+
let d = current - previous;
|
|
2516
|
+
if (d < 0n) {
|
|
2517
|
+
if (o.onDecrease === "gap") continue;
|
|
2518
|
+
d += max + 1n;
|
|
2519
|
+
}
|
|
2520
|
+
const v = Number(d) * o.factor / (s.timestamps[i] - s.timestamps[i - 1]);
|
|
2521
|
+
check(finite(v), "Counter rate overflow.");
|
|
2522
|
+
if (o.maxRate === null || v <= o.maxRate) s.values[i] = v;
|
|
2523
|
+
}
|
|
2524
|
+
return s;
|
|
2525
|
+
}
|
|
2526
|
+
function aggregate(timestamps, values, options = {}) {
|
|
2527
|
+
const o = merge(
|
|
2528
|
+
{
|
|
2529
|
+
interval: 300,
|
|
2530
|
+
method: "mean",
|
|
2531
|
+
origin: 0,
|
|
2532
|
+
minCoverage: 0,
|
|
2533
|
+
expectedStep: null,
|
|
2534
|
+
maxBuckets: 1e6
|
|
2535
|
+
},
|
|
2536
|
+
options
|
|
2537
|
+
);
|
|
2538
|
+
number(o.interval, "Aggregation interval", 1e-3);
|
|
2539
|
+
number(o.origin, "Aggregation origin");
|
|
2540
|
+
number(o.minCoverage, "Minimum coverage", 0, 1);
|
|
2541
|
+
if (o.expectedStep !== null)
|
|
2542
|
+
number(o.expectedStep, "Expected step", Number.MIN_VALUE);
|
|
2543
|
+
integer(o.maxBuckets, "Maximum buckets", 1, 1e6);
|
|
2544
|
+
check(
|
|
2545
|
+
["mean", "min", "max", "last", "sum"].includes(o.method),
|
|
2546
|
+
"Unknown aggregation method."
|
|
2547
|
+
);
|
|
2548
|
+
const s = samples(timestamps, values);
|
|
2549
|
+
if (!s.timestamps.length) return s;
|
|
2550
|
+
const bucket = (t) => Math.floor((t - o.origin) / o.interval), a = bucket(s.timestamps[0]), b = bucket(s.timestamps[s.timestamps.length - 1]);
|
|
2551
|
+
check(
|
|
2552
|
+
Number.isSafeInteger(a) && Number.isSafeInteger(b) && b - a + 1 <= o.maxBuckets,
|
|
2553
|
+
"Aggregation bucket limit or precision exceeded."
|
|
2554
|
+
);
|
|
2555
|
+
const out = { timestamps: [], values: [] };
|
|
2556
|
+
let i = 0;
|
|
2557
|
+
for (let k = a; k <= b; k++) {
|
|
2558
|
+
const ts = o.origin + k * o.interval;
|
|
2559
|
+
epoch(ts);
|
|
2560
|
+
out.timestamps.push(ts);
|
|
2561
|
+
let total = 0, last = NaN, mn = Infinity, mx = -Infinity;
|
|
2562
|
+
const finiteValues = [];
|
|
2563
|
+
while (i < s.timestamps.length && bucket(s.timestamps[i]) === k) {
|
|
2564
|
+
const v = s.values[i++];
|
|
2565
|
+
total++;
|
|
2566
|
+
last = v;
|
|
2567
|
+
if (finite(v)) {
|
|
2568
|
+
finiteValues.push(v);
|
|
2569
|
+
mn = Math.min(mn, v);
|
|
2570
|
+
mx = Math.max(mx, v);
|
|
2571
|
+
}
|
|
2572
|
+
}
|
|
2573
|
+
const denominator = Math.max(
|
|
2574
|
+
total,
|
|
2575
|
+
o.expectedStep === null ? 0 : o.interval / o.expectedStep
|
|
2576
|
+
);
|
|
2577
|
+
if (!finiteValues.length || finiteValues.length / denominator < o.minCoverage) {
|
|
2578
|
+
out.values.push(NaN);
|
|
2579
|
+
continue;
|
|
2580
|
+
}
|
|
2581
|
+
let value;
|
|
2582
|
+
if (o.method === "mean") value = stableMean(finiteValues);
|
|
2583
|
+
else if (o.method === "min") value = mn;
|
|
2584
|
+
else if (o.method === "max") value = mx;
|
|
2585
|
+
else if (o.method === "last") value = last;
|
|
2586
|
+
else {
|
|
2587
|
+
value = stableMean(finiteValues) * finiteValues.length;
|
|
2588
|
+
check(finite(value), "Aggregation sum overflow.");
|
|
2589
|
+
}
|
|
2590
|
+
out.values.push(value);
|
|
2591
|
+
}
|
|
2592
|
+
return out;
|
|
2593
|
+
}
|
|
2594
|
+
function parseCSV(input, options = {}) {
|
|
2595
|
+
const o = merge(
|
|
2596
|
+
{
|
|
2597
|
+
timestampColumn: "timestamp",
|
|
2598
|
+
columns: [
|
|
2599
|
+
{
|
|
2600
|
+
column: "inbound",
|
|
2601
|
+
name: "Inbound",
|
|
2602
|
+
kind: "area",
|
|
2603
|
+
color: "#00cc00",
|
|
2604
|
+
outline: "#003000"
|
|
2605
|
+
},
|
|
2606
|
+
{
|
|
2607
|
+
column: "outbound",
|
|
2608
|
+
name: "Outbound",
|
|
2609
|
+
kind: "line",
|
|
2610
|
+
color: "#0000cc"
|
|
2611
|
+
}
|
|
2612
|
+
],
|
|
2613
|
+
maxRows: 1e6,
|
|
2614
|
+
maxBytes: 16777216
|
|
2615
|
+
},
|
|
2616
|
+
options
|
|
2617
|
+
);
|
|
2618
|
+
check(typeof input === "string", "CSV must be a string.");
|
|
2619
|
+
integer(o.maxRows, "Maximum CSV rows", 1, 2e6);
|
|
2620
|
+
integer(o.maxBytes, "Maximum CSV bytes", 1, 67108864);
|
|
2621
|
+
check(
|
|
2622
|
+
input.length <= o.maxBytes && new TextEncoder().encode(input).length <= o.maxBytes,
|
|
2623
|
+
"CSV exceeds the configured byte limit."
|
|
2624
|
+
);
|
|
2625
|
+
check(
|
|
2626
|
+
Array.isArray(o.columns) && o.columns.length > 0 && o.columns.length <= 128,
|
|
2627
|
+
"CSV needs at least one column mapping."
|
|
2628
|
+
);
|
|
2629
|
+
if (input.charCodeAt(0) === 65279) input = input.slice(1);
|
|
2630
|
+
const rows = [];
|
|
2631
|
+
let row = [], cell = "", quoted = false, closed = false;
|
|
2632
|
+
function endCell() {
|
|
2633
|
+
row.push(cell);
|
|
2634
|
+
cell = "";
|
|
2635
|
+
closed = false;
|
|
2636
|
+
}
|
|
2637
|
+
function endRow() {
|
|
2638
|
+
endCell();
|
|
2639
|
+
if (row.some((v) => v !== "")) rows.push(row);
|
|
2640
|
+
row = [];
|
|
2641
|
+
check(rows.length <= o.maxRows + 1, "CSV row limit exceeded.");
|
|
2642
|
+
}
|
|
2643
|
+
for (let i = 0; i < input.length; i++) {
|
|
2644
|
+
const ch = input[i];
|
|
2645
|
+
if (quoted) {
|
|
2646
|
+
if (ch === '"') {
|
|
2647
|
+
if (input[i + 1] === '"') {
|
|
2648
|
+
cell += '"';
|
|
2649
|
+
i++;
|
|
2650
|
+
} else {
|
|
2651
|
+
quoted = false;
|
|
2652
|
+
closed = true;
|
|
2653
|
+
}
|
|
2654
|
+
} else cell += ch;
|
|
2655
|
+
continue;
|
|
2656
|
+
}
|
|
2657
|
+
if (closed) {
|
|
2658
|
+
check(
|
|
2659
|
+
ch === "," || ch === "\r" || ch === "\n",
|
|
2660
|
+
"Unexpected text after a closing CSV quote."
|
|
2661
|
+
);
|
|
2662
|
+
}
|
|
2663
|
+
if (ch === '"') {
|
|
2664
|
+
check(
|
|
2665
|
+
cell === "" && !closed,
|
|
2666
|
+
"Unexpected quote in an unquoted CSV field."
|
|
2667
|
+
);
|
|
2668
|
+
quoted = true;
|
|
2669
|
+
} else if (ch === ",") endCell();
|
|
2670
|
+
else if (ch === "\n" || ch === "\r") {
|
|
2671
|
+
if (ch === "\r" && input[i + 1] === "\n") i++;
|
|
2672
|
+
endRow();
|
|
2673
|
+
} else cell += ch;
|
|
2674
|
+
}
|
|
2675
|
+
check(!quoted, "Unterminated quoted CSV field.");
|
|
2676
|
+
if (cell !== "" || row.length || closed) endRow();
|
|
2677
|
+
check(rows.length >= 1, "CSV header is missing.");
|
|
2678
|
+
const header = rows.shift().map((v) => v.trim());
|
|
2679
|
+
check(
|
|
2680
|
+
header.every(Boolean) && new Set(header).size === header.length,
|
|
2681
|
+
"CSV headers must be nonempty and unique."
|
|
2682
|
+
);
|
|
2683
|
+
const ti = header.indexOf(o.timestampColumn);
|
|
2684
|
+
check(ti >= 0, "Timestamp column not found: " + o.timestampColumn);
|
|
2685
|
+
const ids = o.columns.map((v) => {
|
|
2686
|
+
const i = header.indexOf(v.column);
|
|
2687
|
+
check(i >= 0, "Column not found: " + v.column);
|
|
2688
|
+
return i;
|
|
2689
|
+
});
|
|
2690
|
+
const ts = [], vv = o.columns.map(() => []);
|
|
2691
|
+
for (let i = 0; i < rows.length; i++) {
|
|
2692
|
+
const r = rows[i];
|
|
2693
|
+
check(
|
|
2694
|
+
r.length === header.length,
|
|
2695
|
+
"CSV row " + (i + 2) + " has the wrong number of columns."
|
|
2696
|
+
);
|
|
2697
|
+
try {
|
|
2698
|
+
ts.push(epoch(r[ti]));
|
|
2699
|
+
} catch (e) {
|
|
2700
|
+
throw new RangeError(
|
|
2701
|
+
"CSV row " + (i + 2) + ": " + (e instanceof Error ? e.message : String(e))
|
|
2702
|
+
);
|
|
2703
|
+
}
|
|
2704
|
+
ids.forEach((id, j) => {
|
|
2705
|
+
const v = r[id].trim();
|
|
2706
|
+
if (/^(?:nan|none|null)?$/i.test(v)) {
|
|
2707
|
+
vv[j].push(NaN);
|
|
2708
|
+
return;
|
|
2709
|
+
}
|
|
2710
|
+
check(
|
|
2711
|
+
/^[+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?$/i.test(v),
|
|
2712
|
+
"Invalid numeric CSV value at row " + (i + 2) + "."
|
|
2713
|
+
);
|
|
2714
|
+
vv[j].push(number(Number(v), "CSV value"));
|
|
2715
|
+
});
|
|
2716
|
+
}
|
|
2717
|
+
return o.columns.map((v, j) => series(v.name || v.column, ts, vv[j], v));
|
|
2718
|
+
}
|
|
2719
|
+
function toCSV(list, options = {}) {
|
|
2720
|
+
check(
|
|
2721
|
+
Array.isArray(list) && list.length <= 128,
|
|
2722
|
+
"Expected an array of series."
|
|
2723
|
+
);
|
|
2724
|
+
const escaped = options.escapeFormulas !== false;
|
|
2725
|
+
const ss = list.map((s) => series(s.name, s.timestamps, s.values, s));
|
|
2726
|
+
let total = 0;
|
|
2727
|
+
for (const s of ss) total += s.timestamps.length;
|
|
2728
|
+
check(
|
|
2729
|
+
total <= LIMITS.samples,
|
|
2730
|
+
"CSV export exceeds the combined sample limit."
|
|
2731
|
+
);
|
|
2732
|
+
const times = [...new Set(ss.flatMap((s) => s.timestamps))].sort(
|
|
2733
|
+
(a, b) => a - b
|
|
2734
|
+
);
|
|
2735
|
+
check(
|
|
2736
|
+
new Set(ss.map((s) => s.name)).size === ss.length && !ss.some((s) => s.name === "timestamp"),
|
|
2737
|
+
"CSV series names must be unique and not timestamp."
|
|
2738
|
+
);
|
|
2739
|
+
const quote = (v) => {
|
|
2740
|
+
v = String(v);
|
|
2741
|
+
if (escaped && /^[=+\-@\t\r]/.test(v)) v = "'" + v;
|
|
2742
|
+
return /[,"\r\n]/.test(v) ? '"' + v.replace(/"/g, '""') + '"' : v;
|
|
2743
|
+
};
|
|
2744
|
+
const lines = [["timestamp", ...ss.map((s) => s.name)].map(quote).join(",")], indexes = ss.map(() => 0);
|
|
2745
|
+
for (const t of times) {
|
|
2746
|
+
const cells = [String(t)];
|
|
2747
|
+
ss.forEach((s, j) => {
|
|
2748
|
+
while (indexes[j] < s.timestamps.length && s.timestamps[indexes[j]] < t)
|
|
2749
|
+
indexes[j]++;
|
|
2750
|
+
const i = indexes[j];
|
|
2751
|
+
cells.push(
|
|
2752
|
+
i < s.timestamps.length && s.timestamps[i] === t && finite(s.values[i]) ? String(s.values[i]) : ""
|
|
2753
|
+
);
|
|
2754
|
+
});
|
|
2755
|
+
lines.push(cells.join(","));
|
|
2756
|
+
}
|
|
2757
|
+
return lines.join("\r\n") + "\r\n";
|
|
2758
|
+
}
|
|
2759
|
+
function compareImages(reference, actual, options = {}) {
|
|
2760
|
+
const a = reference instanceof RenderResult ? reference.image : reference, b = actual instanceof RenderResult ? actual.image : actual;
|
|
2761
|
+
validateImage(a);
|
|
2762
|
+
validateImage(b);
|
|
2763
|
+
check(
|
|
2764
|
+
a.width === b.width && a.height === b.height,
|
|
2765
|
+
"Image sizes must match; comparison never aligns or resizes."
|
|
2766
|
+
);
|
|
2767
|
+
const tolerance = options.tolerance === void 0 ? 0 : options.tolerance;
|
|
2768
|
+
integer(tolerance, "Tolerance", 0, 255);
|
|
2769
|
+
const box = options.box || [0, 0, a.width, a.height];
|
|
2770
|
+
check(
|
|
2771
|
+
Array.isArray(box) && box.length === 4,
|
|
2772
|
+
"Comparison box needs four coordinates."
|
|
2773
|
+
);
|
|
2774
|
+
box.forEach((v) => integer(v, "Comparison coordinate", 0, 65536));
|
|
2775
|
+
const [x0, y0, x1, y1] = box;
|
|
2776
|
+
check(
|
|
2777
|
+
x1 > x0 && y1 > y0 && x1 <= a.width && y1 <= a.height,
|
|
2778
|
+
"Comparison box is out of bounds."
|
|
2779
|
+
);
|
|
2780
|
+
let exact = 0, within = 0, sum = 0, square = 0, maxError = 0, loX = Infinity, loY = Infinity, hiX = -Infinity, hiY = -Infinity;
|
|
2781
|
+
for (let y = y0; y < y1; y++)
|
|
2782
|
+
for (let x = x0; x < x1; x++) {
|
|
2783
|
+
const i = (y * a.width + x) * 4;
|
|
2784
|
+
let err = 0;
|
|
2785
|
+
for (let k = 0; k < 3; k++) {
|
|
2786
|
+
const d = Math.abs(a.data[i + k] - b.data[i + k]);
|
|
2787
|
+
sum += d;
|
|
2788
|
+
square += d * d;
|
|
2789
|
+
err = Math.max(err, d);
|
|
2790
|
+
}
|
|
2791
|
+
maxError = Math.max(maxError, err);
|
|
2792
|
+
if (err === 0) exact++;
|
|
2793
|
+
else {
|
|
2794
|
+
loX = Math.min(loX, x - x0);
|
|
2795
|
+
loY = Math.min(loY, y - y0);
|
|
2796
|
+
hiX = Math.max(hiX, x - x0);
|
|
2797
|
+
hiY = Math.max(hiY, y - y0);
|
|
2798
|
+
}
|
|
2799
|
+
if (err <= tolerance) within++;
|
|
2800
|
+
}
|
|
2801
|
+
const count = (x1 - x0) * (y1 - y0);
|
|
2802
|
+
return {
|
|
2803
|
+
pixels: count,
|
|
2804
|
+
exactPixels: exact,
|
|
2805
|
+
exactRatio: exact / count,
|
|
2806
|
+
toleranceRatio: within / count,
|
|
2807
|
+
meanAbsoluteError: sum / (count * 3),
|
|
2808
|
+
rootMeanSquareError: Math.sqrt(square / (count * 3)),
|
|
2809
|
+
maxError,
|
|
2810
|
+
differenceBox: loX === Infinity ? null : [loX, loY, hiX + 1, hiY + 1]
|
|
2811
|
+
};
|
|
2812
|
+
}
|
|
2813
|
+
function differenceImage(reference, actual, amplify = 4) {
|
|
2814
|
+
number(amplify, "Difference amplification", 0, 255);
|
|
2815
|
+
const a = reference instanceof RenderResult ? reference.image : reference, b = actual instanceof RenderResult ? actual.image : actual;
|
|
2816
|
+
compareImages(a, b);
|
|
2817
|
+
const out = new Surface(a.width, a.height);
|
|
2818
|
+
for (let i = 0; i < a.data.length; i += 4) {
|
|
2819
|
+
for (let k = 0; k < 3; k++)
|
|
2820
|
+
out.data[i + k] = Math.min(
|
|
2821
|
+
255,
|
|
2822
|
+
round(Math.abs(a.data[i + k] - b.data[i + k]) * amplify)
|
|
2823
|
+
);
|
|
2824
|
+
out.data[i + 3] = 255;
|
|
2825
|
+
}
|
|
2826
|
+
return new RenderResult(out, { imageSize: [out.width, out.height], amplify });
|
|
2827
|
+
}
|
|
2828
|
+
function readCanvas(canvas) {
|
|
2829
|
+
check(
|
|
2830
|
+
canvas && typeof canvas.getContext === "function",
|
|
2831
|
+
"Expected a canvas."
|
|
2832
|
+
);
|
|
2833
|
+
const c = canvas.getContext("2d");
|
|
2834
|
+
check(c, "Canvas 2D is unavailable.");
|
|
2835
|
+
const data = c.getImageData(0, 0, canvas.width, canvas.height);
|
|
2836
|
+
const out = new Surface(data.width, data.height);
|
|
2837
|
+
out.data.set(data.data);
|
|
2838
|
+
return out;
|
|
2839
|
+
}
|
|
2840
|
+
async function decodeImage(blob) {
|
|
2841
|
+
check(
|
|
2842
|
+
typeof Blob !== "undefined" && blob instanceof Blob,
|
|
2843
|
+
"decodeImage expects a Blob or File."
|
|
2844
|
+
);
|
|
2845
|
+
check(blob.size <= 67108864, "Image file exceeds 64 MiB.");
|
|
2846
|
+
const head = new Uint8Array(await blob.slice(0, 24).arrayBuffer());
|
|
2847
|
+
check(
|
|
2848
|
+
head.length >= 24 && [137, 80, 78, 71, 13, 10, 26, 10].every((v, i) => head[i] === v) && head[12] === 73 && head[13] === 72 && head[14] === 68 && head[15] === 82,
|
|
2849
|
+
"decodeImage accepts PNG files only."
|
|
2850
|
+
);
|
|
2851
|
+
const headerView = new DataView(head.buffer), width = headerView.getUint32(16), height = headerView.getUint32(20);
|
|
2852
|
+
check(
|
|
2853
|
+
width > 0 && height > 0 && width * height <= LIMITS.pixels,
|
|
2854
|
+
"Decoded PNG exceeds the pixel limit."
|
|
2855
|
+
);
|
|
2856
|
+
check(
|
|
2857
|
+
typeof createImageBitmap === "function",
|
|
2858
|
+
"This browser does not expose createImageBitmap."
|
|
2859
|
+
);
|
|
2860
|
+
const bitmap = await createImageBitmap(blob);
|
|
2861
|
+
try {
|
|
2862
|
+
check(
|
|
2863
|
+
bitmap.width * bitmap.height <= LIMITS.pixels,
|
|
2864
|
+
"Decoded image exceeds the pixel limit."
|
|
2865
|
+
);
|
|
2866
|
+
const canvas = createCanvas(bitmap.width, bitmap.height);
|
|
2867
|
+
const ctx = canvas.getContext("2d");
|
|
2868
|
+
check(ctx, "Canvas 2D is unavailable.");
|
|
2869
|
+
ctx.drawImage(bitmap, 0, 0);
|
|
2870
|
+
return readCanvas(canvas);
|
|
2871
|
+
} finally {
|
|
2872
|
+
bitmap.close();
|
|
2873
|
+
}
|
|
2874
|
+
}
|
|
2875
|
+
var BrowserController = class {
|
|
2876
|
+
chart;
|
|
2877
|
+
canvas;
|
|
2878
|
+
options;
|
|
2879
|
+
destroyed;
|
|
2880
|
+
result;
|
|
2881
|
+
handlers;
|
|
2882
|
+
cursorTime;
|
|
2883
|
+
saved;
|
|
2884
|
+
marker;
|
|
2885
|
+
wrap;
|
|
2886
|
+
overlay;
|
|
2887
|
+
tip;
|
|
2888
|
+
status;
|
|
2889
|
+
constructor(chart, canvas, options = {}) {
|
|
2890
|
+
check(
|
|
2891
|
+
typeof document !== "undefined" && canvas && canvas.ownerDocument,
|
|
2892
|
+
"mount requires an HTML canvas in a document."
|
|
2893
|
+
);
|
|
2894
|
+
check(canvas.parentNode, "Attach the canvas to the document before mount.");
|
|
2895
|
+
check(
|
|
2896
|
+
!canvas.__bamtiGraphController,
|
|
2897
|
+
"A controller is already mounted on this canvas."
|
|
2898
|
+
);
|
|
2899
|
+
this.chart = chart;
|
|
2900
|
+
this.canvas = canvas;
|
|
2901
|
+
this.options = { interactive: true, ...options };
|
|
2902
|
+
this.destroyed = false;
|
|
2903
|
+
this.handlers = [];
|
|
2904
|
+
this.cursorTime = null;
|
|
2905
|
+
this.result = chart.render();
|
|
2906
|
+
this.saved = {
|
|
2907
|
+
style: canvas.getAttribute("style"),
|
|
2908
|
+
role: canvas.getAttribute("role"),
|
|
2909
|
+
label: canvas.getAttribute("aria-label"),
|
|
2910
|
+
tab: canvas.getAttribute("tabindex")
|
|
2911
|
+
};
|
|
2912
|
+
this.marker = document.createComment("canvas position");
|
|
2913
|
+
canvas.parentNode.insertBefore(this.marker, canvas);
|
|
2914
|
+
this.wrap = document.createElement("span");
|
|
2915
|
+
this.wrap.style.cssText = "position:relative;display:inline-block;vertical-align:top;line-height:0;max-width:none;";
|
|
2916
|
+
canvas.parentNode.insertBefore(this.wrap, canvas);
|
|
2917
|
+
this.wrap.appendChild(canvas);
|
|
2918
|
+
canvas.style.display = "block";
|
|
2919
|
+
canvas.style.maxWidth = "none";
|
|
2920
|
+
canvas.setAttribute("role", "img");
|
|
2921
|
+
this.overlay = document.createElement("canvas");
|
|
2922
|
+
this.overlay.setAttribute("aria-hidden", "true");
|
|
2923
|
+
this.overlay.style.cssText = "position:absolute;left:0;top:0;pointer-events:none;";
|
|
2924
|
+
this.wrap.appendChild(this.overlay);
|
|
2925
|
+
this.tip = document.createElement("div");
|
|
2926
|
+
this.tip.hidden = true;
|
|
2927
|
+
this.tip.setAttribute("role", "status");
|
|
2928
|
+
this.tip.style.cssText = "position:absolute;z-index:5;pointer-events:none;background:#111827;color:#fff;border:1px solid #4b5563;padding:9px 11px;border-radius:5px;font:11px/1.65 ui-monospace,monospace;white-space:pre;box-shadow:0 4px 18px #0003;text-align:left;";
|
|
2929
|
+
this.wrap.appendChild(this.tip);
|
|
2930
|
+
this.status = document.createElement("span");
|
|
2931
|
+
this.status.setAttribute("aria-live", "polite");
|
|
2932
|
+
this.status.style.cssText = "position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0,0,0,0);white-space:nowrap;";
|
|
2933
|
+
this.wrap.appendChild(this.status);
|
|
2934
|
+
this.apply(this.result);
|
|
2935
|
+
canvas.__bamtiGraphController = this;
|
|
2936
|
+
if (this.options.interactive) {
|
|
2937
|
+
canvas.tabIndex = 0;
|
|
2938
|
+
this.on(canvas, "pointermove", (e) => {
|
|
2939
|
+
const rect = canvas.getBoundingClientRect(), x = (e.clientX - rect.left) * canvas.width / rect.width, y = (e.clientY - rect.top) * canvas.height / rect.height, m = this.result.metadata, s = m.pixelScale, [l, t, r, b] = m.plotBox;
|
|
2940
|
+
if (x < l * s || x > r * s || y < t * s || y > b * s) {
|
|
2941
|
+
this.clear();
|
|
2942
|
+
return;
|
|
2943
|
+
}
|
|
2944
|
+
const time = m.timeRange[0] + (x / s - l) / (r - l) * (m.timeRange[1] - m.timeRange[0]);
|
|
2945
|
+
this.show(time, false);
|
|
2946
|
+
});
|
|
2947
|
+
this.on(canvas, "pointerleave", () => this.clear());
|
|
2948
|
+
this.on(canvas, "blur", () => this.clear());
|
|
2949
|
+
this.on(canvas, "keydown", (e) => {
|
|
2950
|
+
if (!["ArrowLeft", "ArrowRight", "Home", "End", "Escape"].includes(e.key))
|
|
2951
|
+
return;
|
|
2952
|
+
e.preventDefault();
|
|
2953
|
+
if (e.key === "Escape") {
|
|
2954
|
+
this.clear();
|
|
2955
|
+
return;
|
|
2956
|
+
}
|
|
2957
|
+
const [a, b] = this.result.metadata.timeRange, s = this.chart.config.series.find((s2) => s2.timestamps.length), visible = s ? s.timestamps.slice(
|
|
2958
|
+
lowerBound(s.timestamps, a),
|
|
2959
|
+
upperBound(s.timestamps, b)
|
|
2960
|
+
) : [];
|
|
2961
|
+
let time;
|
|
2962
|
+
if (e.key === "Home") time = visible[0] === void 0 ? a : visible[0];
|
|
2963
|
+
else if (e.key === "End")
|
|
2964
|
+
time = visible.length ? visible[visible.length - 1] : b;
|
|
2965
|
+
else if (visible.length) {
|
|
2966
|
+
let i = this.cursorTime === null ? e.key === "ArrowRight" ? -1 : visible.length : lowerBound(visible, this.cursorTime);
|
|
2967
|
+
i = clamp(
|
|
2968
|
+
i + (e.key === "ArrowRight" ? 1 : -1) * (e.shiftKey ? 10 : 1),
|
|
2969
|
+
0,
|
|
2970
|
+
visible.length - 1
|
|
2971
|
+
);
|
|
2972
|
+
time = visible[i];
|
|
2973
|
+
} else
|
|
2974
|
+
time = clamp(
|
|
2975
|
+
(this.cursorTime === null ? a : this.cursorTime) + (e.key === "ArrowRight" ? 1 : -1) * (b - a) / 100,
|
|
2976
|
+
a,
|
|
2977
|
+
b
|
|
2978
|
+
);
|
|
2979
|
+
this.show(time, true);
|
|
2980
|
+
});
|
|
2981
|
+
}
|
|
2982
|
+
}
|
|
2983
|
+
on(target, type, fn) {
|
|
2984
|
+
target.addEventListener(type, fn);
|
|
2985
|
+
this.handlers.push([target, type, fn]);
|
|
2986
|
+
}
|
|
2987
|
+
apply(r) {
|
|
2988
|
+
this.result = r;
|
|
2989
|
+
r.draw(this.canvas);
|
|
2990
|
+
this.overlay.width = r.width;
|
|
2991
|
+
this.overlay.height = r.height;
|
|
2992
|
+
const m = r.metadata;
|
|
2993
|
+
this.canvas.setAttribute(
|
|
2994
|
+
"aria-label",
|
|
2995
|
+
this.options.ariaLabel || [
|
|
2996
|
+
m.title || "Time-series chart",
|
|
2997
|
+
m.verticalLabel,
|
|
2998
|
+
...m.statistics.map(
|
|
2999
|
+
(s) => s.name + ": current " + (s.current === null ? "missing" : formatValue(s.current, m.yUnit, 2))
|
|
3000
|
+
)
|
|
3001
|
+
].join(". ")
|
|
3002
|
+
);
|
|
3003
|
+
this.wrap.style.width = r.width + "px";
|
|
3004
|
+
this.wrap.style.height = r.height + "px";
|
|
3005
|
+
this.clear();
|
|
3006
|
+
}
|
|
3007
|
+
show(time, announce) {
|
|
3008
|
+
if (this.destroyed) return;
|
|
3009
|
+
const m = this.result.metadata, s = m.pixelScale, [left, top, right, bottom] = m.plotBox, ctx = this.overlay.getContext("2d"), x = (left + (time - m.timeRange[0]) / (m.timeRange[1] - m.timeRange[0]) * (right - left)) * s;
|
|
3010
|
+
ctx.clearRect(0, 0, this.overlay.width, this.overlay.height);
|
|
3011
|
+
ctx.beginPath();
|
|
3012
|
+
ctx.strokeStyle = "#555";
|
|
3013
|
+
ctx.lineWidth = 1;
|
|
3014
|
+
ctx.setLineDash([2, 2]);
|
|
3015
|
+
ctx.moveTo(round(x) + 0.5, top * s);
|
|
3016
|
+
ctx.lineTo(round(x) + 0.5, bottom * s);
|
|
3017
|
+
ctx.stroke();
|
|
3018
|
+
const nearest = this.chart.nearest(time), rows = ["Nearest samples"];
|
|
3019
|
+
for (const p of nearest)
|
|
3020
|
+
rows.push(
|
|
3021
|
+
p.name + ": " + formatValue(p.value, m.yUnit, 2, this.chart.config.missingLabel) + (p.time !== null ? " " + formatTime(p.time, m.timezone, "%H:%M:%S") : " no observation")
|
|
3022
|
+
);
|
|
3023
|
+
this.tip.textContent = rows.join("\n");
|
|
3024
|
+
this.tip.hidden = false;
|
|
3025
|
+
this.tip.style.left = Math.max(
|
|
3026
|
+
4,
|
|
3027
|
+
Math.min(this.result.width - this.tip.offsetWidth - 4, x + 12)
|
|
3028
|
+
) + "px";
|
|
3029
|
+
this.tip.style.top = top * s + 8 + "px";
|
|
3030
|
+
this.cursorTime = time;
|
|
3031
|
+
if (announce) this.status.textContent = rows.join(". ");
|
|
3032
|
+
if (typeof this.options.onHover === "function")
|
|
3033
|
+
this.options.onHover({ time, samples: nearest });
|
|
3034
|
+
}
|
|
3035
|
+
clear() {
|
|
3036
|
+
if (this.overlay)
|
|
3037
|
+
this.overlay.getContext("2d").clearRect(0, 0, this.overlay.width, this.overlay.height);
|
|
3038
|
+
if (this.tip) this.tip.hidden = true;
|
|
3039
|
+
this.cursorTime = null;
|
|
3040
|
+
}
|
|
3041
|
+
update(patch) {
|
|
3042
|
+
check(!this.destroyed, "Controller has been destroyed.");
|
|
3043
|
+
const next = patch instanceof Chart ? patch : this.chart.with(patch), result = next.render();
|
|
3044
|
+
this.chart = next;
|
|
3045
|
+
this.apply(result);
|
|
3046
|
+
return result;
|
|
3047
|
+
}
|
|
3048
|
+
destroy() {
|
|
3049
|
+
if (this.destroyed) return;
|
|
3050
|
+
this.destroyed = true;
|
|
3051
|
+
for (const [el, type, fn] of this.handlers)
|
|
3052
|
+
el.removeEventListener(type, fn);
|
|
3053
|
+
this.handlers.length = 0;
|
|
3054
|
+
if (this.marker.parentNode) {
|
|
3055
|
+
this.marker.parentNode.insertBefore(this.canvas, this.marker);
|
|
3056
|
+
this.marker.remove();
|
|
3057
|
+
} else this.wrap.removeChild(this.canvas);
|
|
3058
|
+
this.wrap.remove();
|
|
3059
|
+
for (const [key, attr] of [
|
|
3060
|
+
["style", "style"],
|
|
3061
|
+
["role", "role"],
|
|
3062
|
+
["label", "aria-label"],
|
|
3063
|
+
["tab", "tabindex"]
|
|
3064
|
+
]) {
|
|
3065
|
+
if (this.saved[key] === null) this.canvas.removeAttribute(attr);
|
|
3066
|
+
else this.canvas.setAttribute(attr, this.saved[key]);
|
|
3067
|
+
}
|
|
3068
|
+
delete this.canvas.__bamtiGraphController;
|
|
3069
|
+
}
|
|
3070
|
+
};
|
|
3071
|
+
function defaults() {
|
|
3072
|
+
return clone(DEFAULTS);
|
|
3073
|
+
}
|
|
3074
|
+
function render(options = {}) {
|
|
3075
|
+
return new Chart(options).render();
|
|
3076
|
+
}
|
|
3077
|
+
var BamtiGraph = Object.freeze({
|
|
3078
|
+
VERSION,
|
|
3079
|
+
LIMITS,
|
|
3080
|
+
Chart,
|
|
3081
|
+
RenderResult,
|
|
3082
|
+
series,
|
|
3083
|
+
regularSeries,
|
|
3084
|
+
traffic,
|
|
3085
|
+
daily,
|
|
3086
|
+
weekly,
|
|
3087
|
+
monthly,
|
|
3088
|
+
yearly,
|
|
3089
|
+
dashboard,
|
|
3090
|
+
counterRate,
|
|
3091
|
+
aggregate,
|
|
3092
|
+
parseCSV,
|
|
3093
|
+
toCSV,
|
|
3094
|
+
compareImages,
|
|
3095
|
+
differenceImage,
|
|
3096
|
+
encodePNG,
|
|
3097
|
+
decodeImage,
|
|
3098
|
+
readCanvas,
|
|
3099
|
+
drawImageToCanvas,
|
|
3100
|
+
downloadBytes,
|
|
3101
|
+
formatTime,
|
|
3102
|
+
formatValue,
|
|
3103
|
+
epoch,
|
|
3104
|
+
color,
|
|
3105
|
+
defaults,
|
|
3106
|
+
render
|
|
3107
|
+
});
|
|
3108
|
+
var index_default = BamtiGraph;
|
|
3109
|
+
export {
|
|
3110
|
+
Chart,
|
|
3111
|
+
LIMITS,
|
|
3112
|
+
RenderResult,
|
|
3113
|
+
VERSION,
|
|
3114
|
+
aggregate,
|
|
3115
|
+
color,
|
|
3116
|
+
compareImages,
|
|
3117
|
+
counterRate,
|
|
3118
|
+
daily,
|
|
3119
|
+
dashboard,
|
|
3120
|
+
decodeImage,
|
|
3121
|
+
index_default as default,
|
|
3122
|
+
defaults,
|
|
3123
|
+
differenceImage,
|
|
3124
|
+
downloadBytes,
|
|
3125
|
+
drawImageToCanvas,
|
|
3126
|
+
encodePNG,
|
|
3127
|
+
epoch,
|
|
3128
|
+
formatTime,
|
|
3129
|
+
formatValue,
|
|
3130
|
+
monthly,
|
|
3131
|
+
parseCSV,
|
|
3132
|
+
readCanvas,
|
|
3133
|
+
regularSeries,
|
|
3134
|
+
render,
|
|
3135
|
+
series,
|
|
3136
|
+
toCSV,
|
|
3137
|
+
traffic,
|
|
3138
|
+
weekly,
|
|
3139
|
+
yearly
|
|
3140
|
+
};
|
|
3141
|
+
//# sourceMappingURL=index.js.map
|