12
const FEAT_TIME = true;
15
function closestIdx(num, arr, lo, hi) {
18
hi = hi || arr.length - 1;
19
let bitwise = hi <= 2147483647;
22
mid = bitwise ? (lo + hi) >> 1 : floor((lo + hi) / 2);
30
if (num - arr[lo] <= arr[hi] - num)
36
function nonNullIdx(data, _i0, _i1, dir) {
37
for (let i = dir == 1 ? _i0 : _i1; i >= _i0 && i <= _i1; i += dir) {
45
function getMinMax(data, _i0, _i1, sorted) {
55
else if (sorted == -1) {
60
for (let i = _i0; i <= _i1; i++) {
61
if (data[i] != null) {
62
_min = min(_min, data[i]);
63
_max = max(_max, data[i]);
71
function getMinMaxLog(data, _i0, _i1) {
77
for (let i = _i0; i <= _i1; i++) {
79
_min = min(_min, data[i]);
80
_max = max(_max, data[i]);
85
_min == inf ? 1 : _min,
86
_max == -inf ? 10 : _max,
90
const _fixedTuple = [0, 0];
92
function fixIncr(minIncr, maxIncr, minExp, maxExp) {
93
_fixedTuple[0] = minExp < 0 ? roundDec(minIncr, -minExp) : minIncr;
94
_fixedTuple[1] = maxExp < 0 ? roundDec(maxIncr, -maxExp) : maxIncr;
98
function rangeLog(min, max, base, fullMags) {
99
let minSign = sign(min);
101
let logFn = base == 10 ? log10 : log2;
114
let minExp, maxExp, minMaxIncrs;
117
minExp = floor(logFn(min));
118
maxExp = ceil(logFn(max));
120
minMaxIncrs = fixIncr(pow(base, minExp), pow(base, maxExp), minExp, maxExp);
122
min = minMaxIncrs[0];
123
max = minMaxIncrs[1];
126
minExp = floor(logFn(abs(min)));
127
maxExp = floor(logFn(abs(max)));
129
minMaxIncrs = fixIncr(pow(base, minExp), pow(base, maxExp), minExp, maxExp);
131
min = incrRoundDn(min, minMaxIncrs[0]);
132
max = incrRoundUp(max, minMaxIncrs[1]);
138
function rangeAsinh(min, max, base, fullMags) {
139
let minMax = rangeLog(min, max, base, fullMags);
152
const autoRangePart = {
157
const _eqRangePart = {
170
function rangeNum(_min, _max, mult, extra) {
172
return _rangeNum(_min, _max, mult);
174
_eqRangePart.pad = mult;
175
_eqRangePart.soft = extra ? 0 : null;
176
_eqRangePart.mode = extra ? 3 : 0;
178
return _rangeNum(_min, _max, _eqRange);
182
function ifNull(lh, rh) {
183
return lh == null ? rh : lh;
188
function hasData(data, idx0, idx1) {
189
idx0 = ifNull(idx0, 0);
190
idx1 = ifNull(idx1, data.length - 1);
192
while (idx0 <= idx1) {
193
if (data[idx0] != null)
201
function _rangeNum(_min, _max, cfg) {
205
let padMin = ifNull(cmin.pad, 0);
206
let padMax = ifNull(cmax.pad, 0);
208
let hardMin = ifNull(cmin.hard, -inf);
209
let hardMax = ifNull(cmax.hard, inf);
211
let softMin = ifNull(cmin.soft, inf);
212
let softMax = ifNull(cmax.soft, -inf);
214
let softMinMode = ifNull(cmin.mode, 0);
215
let softMaxMode = ifNull(cmax.mode, 0);
217
let delta = _max - _min;
230
if (_min == 0 || _max == 0) {
233
if (softMinMode == 2 && softMin != inf)
236
if (softMaxMode == 2 && softMax != -inf)
241
let nonZeroDelta = delta || abs(_max) || 1e3;
242
let mag = log10(nonZeroDelta);
243
let base = pow(10, floor(mag));
245
let _padMin = nonZeroDelta * (delta == 0 ? (_min == 0 ? .1 : 1) : padMin);
246
let _newMin = roundDec(incrRoundDn(_min - _padMin, base/10), 9);
247
let _softMin = _min >= softMin && (softMinMode == 1 || softMinMode == 3 && _newMin <= softMin || softMinMode == 2 && _newMin >= softMin) ? softMin : inf;
248
let minLim = max(hardMin, _newMin < _softMin && _min >= _softMin ? _softMin : min(_softMin, _newMin));
250
let _padMax = nonZeroDelta * (delta == 0 ? (_max == 0 ? .1 : 1) : padMax);
251
let _newMax = roundDec(incrRoundUp(_max + _padMax, base/10), 9);
252
let _softMax = _max <= softMax && (softMaxMode == 1 || softMaxMode == 3 && _newMax >= softMax || softMaxMode == 2 && _newMax <= softMax) ? softMax : -inf;
253
let maxLim = min(hardMax, _newMax > _softMax && _max <= _softMax ? _softMax : max(_softMax, _newMax));
255
if (minLim == maxLim && minLim == 0)
258
return [minLim, maxLim];
262
const fmtNum = new Intl.NumberFormat(navigator.language).format;
268
const floor = M.floor;
269
const round = M.round;
275
const log10 = M.log10;
278
const sinh = (v, linthresh = 1) => M.sinh(v) * linthresh;
279
const asinh = (v, linthresh = 1) => M.asinh(v / linthresh);
283
function numIntDigits(x) {
284
return (log10((x ^ (x >> 31)) - (x >> 31)) | 0) + 1;
287
function incrRound(num, incr) {
288
return round(num/incr)*incr;
291
function clamp(num, _min, _max) {
292
return min(max(num, _min), _max);
295
function fnOrSelf(v) {
296
return typeof v == "function" ? v : () => v;
299
const retArg0 = _0 => _0;
301
const retArg1 = (_0, _1) => _1;
303
const retNull = _ => null;
305
const retTrue = _ => true;
307
const retEq = (a, b) => a == b;
309
function incrRoundUp(num, incr) {
310
return ceil(num/incr)*incr;
313
function incrRoundDn(num, incr) {
314
return floor(num/incr)*incr;
317
function roundDec(val, dec) {
318
return round(val * (dec = 10**dec)) / dec;
321
const fixedDec = new Map();
323
function guessDec(num) {
324
return ((""+num).split(".")[1] || "").length;
327
function genIncrs(base, minExp, maxExp, mults) {
330
let multDec = mults.map(guessDec);
332
for (let exp = minExp; exp < maxExp; exp++) {
334
let mag = roundDec(pow(base, exp), expa);
336
for (let i = 0; i < mults.length; i++) {
337
let _incr = mults[i] * mag;
338
let dec = (_incr >= 0 && exp >= 0 ? 0 : expa) + (exp >= multDec[i] ? 0 : multDec[i]);
339
let incr = roundDec(_incr, dec);
341
fixedDec.set(incr, dec);
353
const nullNullTuple = [null, null];
355
const isArr = Array.isArray;
358
return typeof v == 'string';
365
let c = v.constructor;
366
is = c == null || c == Object;
372
function fastIsObj(v) {
373
return v != null && typeof v == 'object';
376
function copy(o, _isObj = isObj) {
380
let val = o.find(v => v != null);
382
if (isArr(val) || _isObj(val)) {
383
out = Array(o.length);
384
for (let i = 0; i < o.length; i++)
385
out[i] = copy(o[i], _isObj);
390
else if (_isObj(o)) {
393
out[k] = copy(o[k], _isObj);
401
function assign(targ) {
402
let args = arguments;
404
for (let i = 1; i < args.length; i++) {
407
for (let key in src) {
408
if (isObj(targ[key]))
409
assign(targ[key], copy(src[key]));
411
targ[key] = copy(src[key]);
419
const NULL_REMOVE = 0;
420
const NULL_RETAIN = 1;
421
const NULL_EXPAND = 2;
424
function nullExpand(yVals, nullIdxs, alignedLen) {
425
for (let i = 0, xi, lastNullIdx = -1; i < nullIdxs.length; i++) {
426
let nullIdx = nullIdxs[i];
428
if (nullIdx > lastNullIdx) {
430
while (xi >= 0 && yVals[xi] == null)
434
while (xi < alignedLen && yVals[xi] == null)
435
yVals[lastNullIdx = xi++] = null;
442
function join(tables, nullModes) {
443
let xVals = new Set();
445
for (let ti = 0; ti < tables.length; ti++) {
450
for (let i = 0; i < len; i++)
454
let data = [Array.from(xVals).sort((a, b) => a - b)];
456
let alignedLen = data[0].length;
458
let xIdxs = new Map();
460
for (let i = 0; i < alignedLen; i++)
461
xIdxs.set(data[0][i], i);
463
for (let ti = 0; ti < tables.length; ti++) {
467
for (let si = 1; si < t.length; si++) {
470
let yVals = Array(alignedLen).fill(undefined);
472
let nullMode = nullModes ? nullModes[ti][si] : NULL_RETAIN;
476
for (let i = 0; i < ys.length; i++) {
478
let alignedIdx = xIdxs.get(xs[i]);
481
if (nullMode != NULL_REMOVE) {
482
yVals[alignedIdx] = yVal;
484
if (nullMode == NULL_EXPAND)
485
nullIdxs.push(alignedIdx);
489
yVals[alignedIdx] = yVal;
492
nullExpand(yVals, nullIdxs, alignedLen);
501
const microTask = typeof queueMicrotask == "undefined" ? fn => Promise.resolve().then(fn) : queueMicrotask;
503
const WIDTH = "width";
504
const HEIGHT = "height";
506
const BOTTOM = "bottom";
508
const RIGHT = "right";
509
const hexBlack = "#000";
510
const transparent = hexBlack + "0";
512
const mousemove = "mousemove";
513
const mousedown = "mousedown";
514
const mouseup = "mouseup";
515
const mouseenter = "mouseenter";
516
const mouseleave = "mouseleave";
517
const dblclick = "dblclick";
518
const resize = "resize";
519
const scroll = "scroll";
521
const change = "change";
522
const dppxchange = "dppxchange";
526
const UPLOT = "uplot";
527
const ORI_HZ = pre + "hz";
528
const ORI_VT = pre + "vt";
529
const TITLE = pre + "title";
530
const WRAP = pre + "wrap";
531
const UNDER = pre + "under";
532
const OVER = pre + "over";
533
const AXIS = pre + "axis";
534
const OFF = pre + "off";
535
const SELECT = pre + "select";
536
const CURSOR_X = pre + "cursor-x";
537
const CURSOR_Y = pre + "cursor-y";
538
const CURSOR_PT = pre + "cursor-pt";
539
const LEGEND = pre + "legend";
540
const LEGEND_LIVE = pre + "live";
541
const LEGEND_INLINE = pre + "inline";
542
const LEGEND_THEAD = pre + "thead";
543
const LEGEND_SERIES = pre + "series";
544
const LEGEND_MARKER = pre + "marker";
545
const LEGEND_LABEL = pre + "label";
546
const LEGEND_VALUE = pre + "value";
554
function setPxRatio() {
555
let _pxRatio = devicePixelRatio;
558
if (pxRatio != _pxRatio) {
561
query && off(change, query, setPxRatio);
562
query = matchMedia(`(min-resolution: ${pxRatio - 0.001}dppx) and (max-resolution: ${pxRatio + 0.001}dppx)`);
563
on(change, query, setPxRatio);
565
win.dispatchEvent(new CustomEvent(dppxchange));
569
function addClass(el, c) {
571
let cl = el.classList;
572
!cl.contains(c) && cl.add(c);
576
function remClass(el, c) {
577
let cl = el.classList;
578
cl.contains(c) && cl.remove(c);
581
function setStylePx(el, name, value) {
582
el.style[name] = value + "px";
585
function placeTag(tag, cls, targ, refEl) {
586
let el = doc.createElement(tag);
592
targ.insertBefore(el, refEl);
597
function placeDiv(cls, targ) {
598
return placeTag("div", cls, targ);
601
const xformCache = new WeakMap();
603
function elTrans(el, xPos, yPos, xMax, yMax) {
604
let xform = "translate(" + xPos + "px," + yPos + "px)";
605
let xformOld = xformCache.get(el);
607
if (xform != xformOld) {
608
el.style.transform = xform;
609
xformCache.set(el, xform);
611
if (xPos < 0 || yPos < 0 || xPos > xMax || yPos > yMax)
618
const colorCache = new WeakMap();
620
function elColor(el, background, borderColor) {
621
let newColor = background + borderColor;
622
let oldColor = colorCache.get(el);
624
if (newColor != oldColor) {
625
colorCache.set(el, newColor);
626
el.style.background = background;
627
el.style.borderColor = borderColor;
631
const sizeCache = new WeakMap();
633
function elSize(el, newWid, newHgt, centered) {
634
let newSize = newWid + "" + newHgt;
635
let oldSize = sizeCache.get(el);
637
if (newSize != oldSize) {
638
sizeCache.set(el, newSize);
639
el.style.height = newHgt + "px";
640
el.style.width = newWid + "px";
641
el.style.marginLeft = centered ? -newWid/2 + "px" : 0;
642
el.style.marginTop = centered ? -newHgt/2 + "px" : 0;
646
const evOpts = {passive: true};
647
const evOpts2 = assign({capture: true}, evOpts);
649
function on(ev, el, cb, capt) {
650
el.addEventListener(ev, cb, capt ? evOpts2 : evOpts);
653
function off(ev, el, cb, capt) {
654
el.removeEventListener(ev, cb, capt ? evOpts2 : evOpts);
684
function slice3(str) {
685
return str.slice(0, 3);
688
const days3 = days.map(slice3);
690
const months3 = months.map(slice3);
699
function zeroPad2(int) {
700
return (int < 10 ? '0' : '') + int;
703
function zeroPad3(int) {
704
return (int < 10 ? '00' : int < 100 ? '0' : '') + int;
721
YYYY: d => d.getFullYear(),
723
YY: d => (d.getFullYear()+'').slice(2),
725
MMMM: (d, names) => names.MMMM[d.getMonth()],
727
MMM: (d, names) => names.MMM[d.getMonth()],
729
MM: d => zeroPad2(d.getMonth()+1),
731
M: d => d.getMonth()+1,
733
DD: d => zeroPad2(d.getDate()),
737
WWWW: (d, names) => names.WWWW[d.getDay()],
739
WWW: (d, names) => names.WWW[d.getDay()],
741
HH: d => zeroPad2(d.getHours()),
743
H: d => d.getHours(),
745
h: d => {let h = d.getHours(); return h == 0 ? 12 : h > 12 ? h - 12 : h;},
747
AA: d => d.getHours() >= 12 ? 'PM' : 'AM',
749
aa: d => d.getHours() >= 12 ? 'pm' : 'am',
751
a: d => d.getHours() >= 12 ? 'p' : 'a',
753
mm: d => zeroPad2(d.getMinutes()),
755
m: d => d.getMinutes(),
757
ss: d => zeroPad2(d.getSeconds()),
759
s: d => d.getSeconds(),
761
fff: d => zeroPad3(d.getMilliseconds()),
764
function fmtDate(tpl, names) {
765
names = names || engNames;
768
let R = /\{([a-z]+)\}|[^{]+/gi, m;
770
while (m = R.exec(tpl))
771
parts.push(m[0][0] == '{' ? subs[m[1]] : m[0]);
776
for (let i = 0; i < parts.length; i++)
777
out += typeof parts[i] == "string" ? parts[i] : parts[i](d, names);
783
const localTz = new Intl.DateTimeFormat().resolvedOptions().timeZone;
786
function tzDate(date, tz) {
790
if (tz == 'UTC' || tz == 'Etc/UTC')
791
date2 = new Date(+date + date.getTimezoneOffset() * 6e4);
792
else if (tz == localTz)
795
date2 = new Date(date.toLocaleString('en-US', {timeZone: tz}));
796
date2.setMilliseconds(date.getMilliseconds());
806
const onlyWhole = v => v % 1 == 0;
808
const allMults = [1,2,2.5,5];
811
const decIncrs = genIncrs(10, -16, 0, allMults);
814
const oneIncrs = genIncrs(10, 0, 16, allMults);
817
const wholeIncrs = oneIncrs.filter(onlyWhole);
819
const numIncrs = decIncrs.concat(oneIncrs);
823
const yyyy = "{YYYY}";
824
const NLyyyy = NL + yyyy;
827
const NLmdyy = NLmd + "/{YY}";
830
const hmm = "{h}:{mm}";
831
const hmmaa = hmm + aa;
832
const NLhmmaa = NL + hmmaa;
837
function genTimeStuffs(ms) {
846
let subSecIncrs = ms == 1 ? genIncrs(10, 0, 3, allMults).filter(onlyWhole) : genIncrs(10, -3, 0, allMults);
848
let timeIncrs = subSecIncrs.concat([
901
const _timeAxisStamps = [
903
[y, yyyy, _, _, _, _, _, _, 1],
904
[d * 28, "{MMM}", NLyyyy, _, _, _, _, _, 1],
905
[d, md, NLyyyy, _, _, _, _, _, 1],
906
[h, "{h}" + aa, NLmdyy, _, NLmd, _, _, _, 1],
907
[m, hmmaa, NLmdyy, _, NLmd, _, _, _, 1],
908
[s, ss, NLmdyy + " " + hmmaa, _, NLmd + " " + hmmaa, _, NLhmmaa, _, 1],
909
[ms, ss + ".{fff}", NLmdyy + " " + hmmaa, _, NLmd + " " + hmmaa, _, NLhmmaa, _, 1],
916
function timeAxisSplits(tzDate) {
917
return (self, axisIdx, scaleMin, scaleMax, foundIncr, foundSpace) => {
919
let isYr = foundIncr >= y;
920
let isMo = foundIncr >= mo && foundIncr < y;
923
let minDate = tzDate(scaleMin);
924
let minDateTs = roundDec(minDate * ms, 3);
927
let minMin = mkDate(minDate.getFullYear(), isYr ? 0 : minDate.getMonth(), isMo || isYr ? 1 : minDate.getDate());
928
let minMinTs = roundDec(minMin * ms, 3);
931
let moIncr = isMo ? foundIncr / mo : 0;
932
let yrIncr = isYr ? foundIncr / y : 0;
934
let split = minDateTs == minMinTs ? minDateTs : roundDec(mkDate(minMin.getFullYear() + yrIncr, minMin.getMonth() + moIncr, 1) * ms, 3);
935
let splitDate = new Date(round(split / ms));
936
let baseYear = splitDate.getFullYear();
937
let baseMonth = splitDate.getMonth();
939
for (let i = 0; split <= scaleMax; i++) {
940
let next = mkDate(baseYear + yrIncr * i, baseMonth + moIncr * i, 1);
941
let offs = next - tzDate(roundDec(next * ms, 3));
943
split = roundDec((+next + offs) * ms, 3);
945
if (split <= scaleMax)
950
let incr0 = foundIncr >= d ? d : foundIncr;
951
let tzOffset = floor(scaleMin) - floor(minDateTs);
952
let split = minMinTs + tzOffset + incrRoundUp(minDateTs - minMinTs, incr0);
955
let date0 = tzDate(split);
957
let prevHour = date0.getHours() + (date0.getMinutes() / m) + (date0.getSeconds() / h);
958
let incrHours = foundIncr / h;
960
let minSpace = self.axes[axisIdx]._space;
961
let pctSpace = foundSpace / minSpace;
964
split = roundDec(split + foundIncr, ms == 1 ? 0 : 3);
966
if (split > scaleMax)
970
let expectedHour = floor(roundDec(prevHour + incrHours, 6)) % 24;
971
let splitDate = tzDate(split);
972
let actualHour = splitDate.getHours();
974
let dstShift = actualHour - expectedHour;
979
split -= dstShift * h;
981
prevHour = (prevHour + incrHours) % 24;
984
let prevSplit = splits[splits.length - 1];
985
let pctIncr = roundDec((split - prevSplit) / foundIncr, 3);
987
if (pctIncr * pctSpace >= .7)
1006
const [ timeIncrsMs, _timeAxisStampsMs, timeAxisSplitsMs ] = genTimeStuffs(1);
1007
const [ timeIncrsS, _timeAxisStampsS, timeAxisSplitsS ] = genTimeStuffs(1e-3);
1010
genIncrs(2, -53, 53, [1]);
1023
function timeAxisStamps(stampCfg, fmtDate) {
1024
return stampCfg.map(s => s.map((v, i) =>
1025
i == 0 || i == 8 || v == null ? v : fmtDate(i == 1 || s[8] == 0 ? v : s[1] + v)
1031
function timeAxisVals(tzDate, stamps) {
1032
return (self, splits, axisIdx, foundSpace, foundIncr) => {
1033
let s = stamps.find(s => foundIncr >= s[0]) || stamps[stamps.length - 1];
1043
return splits.map(split => {
1044
let date = tzDate(split);
1046
let newYear = date.getFullYear();
1047
let newMnth = date.getMonth();
1048
let newDate = date.getDate();
1049
let newHour = date.getHours();
1050
let newMins = date.getMinutes();
1051
let newSecs = date.getSeconds();
1054
newYear != prevYear && s[2] ||
1055
newMnth != prevMnth && s[3] ||
1056
newDate != prevDate && s[4] ||
1057
newHour != prevHour && s[5] ||
1058
newMins != prevMins && s[6] ||
1059
newSecs != prevSecs && s[7] ||
1076
function timeAxisVal(tzDate, dateTpl) {
1077
let stamp = fmtDate(dateTpl);
1078
return (self, splits, axisIdx, foundSpace, foundIncr) => splits.map(split => stamp(tzDate(split)));
1081
function mkDate(y, m, d) {
1082
return new Date(y, m, d);
1085
function timeSeriesStamp(stampCfg, fmtDate) {
1086
return fmtDate(stampCfg);
1088
const _timeSeriesStamp = '{YYYY}-{MM}-{DD} {h}:{mm}{aa}';
1090
function timeSeriesVal(tzDate, stamp) {
1091
return (self, val) => stamp(tzDate(val));
1094
function legendStroke(self, seriesIdx) {
1095
let s = self.series[seriesIdx];
1096
return s.width ? s.stroke(self, seriesIdx) : s.points.width ? s.points.stroke(self, seriesIdx) : null;
1099
function legendFill(self, seriesIdx) {
1100
return self.series[seriesIdx].fill(self, seriesIdx);
1110
stroke: legendStroke,
1119
function cursorPointShow(self, si) {
1120
let o = self.cursor.points;
1122
let pt = placeDiv();
1124
let size = o.size(self, si);
1125
setStylePx(pt, WIDTH, size);
1126
setStylePx(pt, HEIGHT, size);
1128
let mar = size / -2;
1129
setStylePx(pt, "marginLeft", mar);
1130
setStylePx(pt, "marginTop", mar);
1132
let width = o.width(self, si, size);
1133
width && setStylePx(pt, "borderWidth", width);
1138
function cursorPointFill(self, si) {
1139
let sp = self.series[si].points;
1140
return sp._fill || sp._stroke;
1143
function cursorPointStroke(self, si) {
1144
let sp = self.series[si].points;
1145
return sp._stroke || sp._fill;
1148
function cursorPointSize(self, si) {
1149
let sp = self.series[si].points;
1150
return ptDia(sp.width, 1);
1153
function dataIdx(self, seriesIdx, cursorIdx) {
1157
const moveTuple = [0,0];
1159
function cursorMove(self, mouseLeft1, mouseTop1) {
1160
moveTuple[0] = mouseLeft1;
1161
moveTuple[1] = mouseTop1;
1165
function filtBtn0(self, targ, handle) {
1167
e.button == 0 && handle(e);
1171
function passThru(self, targ, handle) {
1182
show: cursorPointShow,
1183
size: cursorPointSize,
1185
stroke: cursorPointStroke,
1186
fill: cursorPointFill,
1190
mousedown: filtBtn0,
1195
mousemove: passThru,
1196
mouseleave: passThru,
1197
mouseenter: passThru,
1223
stroke: "rgba(0,0,0,0.07)",
1229
const ticks = assign({}, grid, {size: 10});
1231
const font = '12px system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"';
1232
const labelFont = "bold " + font;
1233
const lineMult = 1.5;
1256
const numSeriesLabel = "Value";
1257
const timeSeriesLabel = "Time";
1259
const xSeriesOpts = {
1273
function numAxisVals(self, splits, axisIdx, foundSpace, foundIncr) {
1274
return splits.map(v => v == null ? "" : fmtNum(v));
1277
function numAxisSplits(self, axisIdx, scaleMin, scaleMax, foundIncr, foundSpace, forceMin) {
1280
let numDec = fixedDec.get(foundIncr) || 0;
1282
scaleMin = forceMin ? scaleMin : roundDec(incrRoundUp(scaleMin, foundIncr), numDec);
1284
for (let val = scaleMin; val <= scaleMax; val = roundDec(val + foundIncr, numDec))
1285
splits.push(Object.is(val, -0) ? 0 : val);
1291
function logAxisSplits(self, axisIdx, scaleMin, scaleMax, foundIncr, foundSpace, forceMin) {
1294
const logBase = self.scales[self.axes[axisIdx].scale].log;
1296
const logFn = logBase == 10 ? log10 : log2;
1298
const exp = floor(logFn(scaleMin));
1300
foundIncr = pow(logBase, exp);
1303
foundIncr = roundDec(foundIncr, -exp);
1305
let split = scaleMin;
1309
split = roundDec(split + foundIncr, fixedDec.get(foundIncr));
1311
if (split >= foundIncr * logBase)
1314
} while (split <= scaleMax);
1319
function asinhAxisSplits(self, axisIdx, scaleMin, scaleMax, foundIncr, foundSpace, forceMin) {
1320
let sc = self.scales[self.axes[axisIdx].scale];
1322
let linthresh = sc.asinh;
1324
let posSplits = scaleMax > linthresh ? logAxisSplits(self, axisIdx, max(linthresh, scaleMin), scaleMax, foundIncr) : [linthresh];
1325
let zero = scaleMax >= 0 && scaleMin <= 0 ? [0] : [];
1326
let negSplits = scaleMin < -linthresh ? logAxisSplits(self, axisIdx, max(linthresh, -scaleMax), -scaleMin, foundIncr): [linthresh];
1328
return negSplits.reverse().map(v => -v).concat(zero, posSplits);
1332
const RE_12357 = /[12357]/;
1333
const RE_125 = /[125]/;
1336
function logAxisValsFilt(self, splits, axisIdx, foundSpace, foundIncr) {
1337
let axis = self.axes[axisIdx];
1338
let scaleKey = axis.scale;
1339
let sc = self.scales[scaleKey];
1341
if (sc.distr == 3 && sc.log == 2)
1344
let valToPos = self.valToPos;
1346
let minSpace = axis._space;
1348
let _10 = valToPos(10, scaleKey);
1351
valToPos(9, scaleKey) - _10 >= minSpace ? RE_ALL :
1352
valToPos(7, scaleKey) - _10 >= minSpace ? RE_12357 :
1353
valToPos(5, scaleKey) - _10 >= minSpace ? RE_125 :
1357
return splits.map(v => ((sc.distr == 4 && v == 0) || re.test(v)) ? v : null);
1360
function numSeriesVal(self, val) {
1361
return val == null ? "" : fmtNum(val);
1386
function ptDia(width, mult) {
1387
let dia = 3 + (width || 1) * 2;
1388
return roundDec(dia * mult, 3);
1391
function seriesPointsShow(self, si) {
1392
let { scale, idxs } = self.series[0];
1393
let xData = self._data[0];
1394
let p0 = self.valToPos(xData[idxs[0]], scale, true);
1395
let p1 = self.valToPos(xData[idxs[1]], scale, true);
1396
let dim = abs(p1 - p0);
1398
let s = self.series[si];
1400
let maxPts = dim / (s.points.space * pxRatio);
1401
return idxs[1] - idxs[0] <= maxPts;
1404
function seriesFillTo(self, seriesIdx, dataMin, dataMax) {
1405
let scale = self.scales[self.series[seriesIdx].scale];
1406
let isUpperBandEdge = self.bands && self.bands.some(b => b.series[0] == seriesIdx);
1407
return scale.distr == 3 || isUpperBandEdge ? scale.min : 0;
1419
const xySeriesOpts = {
1425
assign({}, facet, {scale: 'x'}),
1426
assign({}, facet, {scale: 'y'}),
1430
const ySeriesOpts = {
1436
gaps: (self, seriesIdx, idx0, idx1, nullGaps) => nullGaps,
1439
show: seriesPointsShow,
1460
function clampScale(self, val, scaleMin, scaleMax, scaleKey) {
1469
return scaleMin / 10;
1484
const yScaleOpts = assign({}, xScaleOpts, {
1491
function _sync(key, opts) {
1502
s.plots = s.plots.filter(c => c != plot);
1504
pub(type, self, x, y, w, h, i) {
1505
for (let j = 0; j < s.plots.length; j++)
1506
s.plots[j] != self && s.plots[j].pub(type, self, x, y, w, h, i);
1517
const BAND_CLIP_FILL = 1 << 0;
1518
const BAND_CLIP_STROKE = 1 << 1;
1520
function orient(u, seriesIdx, cb) {
1521
const series = u.series[seriesIdx];
1522
const scales = u.scales;
1523
const bbox = u.bbox;
1524
const scaleX = u.mode == 2 ? scales[series.facets[0].scale] : scales[u.series[0].scale];
1526
let dx = u._data[0],
1527
dy = u._data[seriesIdx],
1529
sy = u.mode == 2 ? scales[series.facets[1].scale] : scales[series.scale],
1578
function clipBandLine(self, seriesIdx, idx0, idx1, strokePath) {
1579
return orient(self, seriesIdx, (series, dataX, dataY, scaleX, scaleY, valToPosX, valToPosY, xOff, yOff, xDim, yDim) => {
1580
let pxRound = series.pxRound;
1582
const dir = scaleX.dir * (scaleX.ori == 0 ? 1 : -1);
1583
const lineTo = scaleX.ori == 0 ? lineToH : lineToV;
1597
let x0 = pxRound(valToPosX(dataX[frIdx], scaleX, xDim, xOff));
1598
let y0 = pxRound(valToPosY(dataY[frIdx], scaleY, yDim, yOff));
1600
let x1 = pxRound(valToPosX(dataX[toIdx], scaleX, xDim, xOff));
1602
let yLimit = pxRound(valToPosY(scaleY.max, scaleY, yDim, yOff));
1604
let clip = new Path2D(strokePath);
1606
lineTo(clip, x1, yLimit);
1607
lineTo(clip, x0, yLimit);
1608
lineTo(clip, x0, y0);
1614
function clipGaps(gaps, ori, plotLft, plotTop, plotWid, plotHgt) {
1618
if (gaps.length > 0) {
1619
clip = new Path2D();
1621
const rect = ori == 0 ? rectH : rectV;
1623
let prevGapEnd = plotLft;
1625
for (let i = 0; i < gaps.length; i++) {
1629
let w = g[0] - prevGapEnd;
1631
w > 0 && rect(clip, prevGapEnd, plotTop, w, plotTop + plotHgt);
1637
let w = plotLft + plotWid - prevGapEnd;
1639
w > 0 && rect(clip, prevGapEnd, plotTop, w, plotTop + plotHgt);
1645
function addGap(gaps, fromX, toX) {
1646
let prevGap = gaps[gaps.length - 1];
1648
if (prevGap && prevGap[0] == fromX)
1651
gaps.push([fromX, toX]);
1654
function pxRoundGen(pxAlign) {
1655
return pxAlign == 0 ? retArg0 : pxAlign == 1 ? round : v => incrRound(v, pxAlign);
1659
let moveTo = ori == 0 ?
1663
let arcTo = ori == 0 ?
1664
(p, x1, y1, x2, y2, r) => { p.arcTo(x1, y1, x2, y2, r); } :
1665
(p, y1, x1, y2, x2, r) => { p.arcTo(x1, y1, x2, y2, r); };
1667
let rect = ori == 0 ?
1668
(p, x, y, w, h) => { p.rect(x, y, w, h); } :
1669
(p, y, x, h, w) => { p.rect(x, y, w, h); };
1671
return (p, x, y, w, h, r = 0) => {
1673
rect(p, x, y, w, h);
1675
r = min(r, w / 2, h / 2);
1678
moveTo(p, x + r, y);
1679
arcTo(p, x + w, y, x + w, y + h, r);
1680
arcTo(p, x + w, y + h, x, y + h, r);
1681
arcTo(p, x, y + h, x, y, r);
1682
arcTo(p, x, y, x + w, y, r);
1689
const moveToH = (p, x, y) => { p.moveTo(x, y); };
1690
const moveToV = (p, y, x) => { p.moveTo(x, y); };
1691
const lineToH = (p, x, y) => { p.lineTo(x, y); };
1692
const lineToV = (p, y, x) => { p.lineTo(x, y); };
1693
const rectH = rect(0);
1694
const rectV = rect(1);
1695
const arcH = (p, x, y, r, startAngle, endAngle) => { p.arc(x, y, r, startAngle, endAngle); };
1696
const arcV = (p, y, x, r, startAngle, endAngle) => { p.arc(x, y, r, startAngle, endAngle); };
1697
const bezierCurveToH = (p, bp1x, bp1y, bp2x, bp2y, p2x, p2y) => { p.bezierCurveTo(bp1x, bp1y, bp2x, bp2y, p2x, p2y); };
1698
const bezierCurveToV = (p, bp1y, bp1x, bp2y, bp2x, p2y, p2x) => { p.bezierCurveTo(bp1x, bp1y, bp2x, bp2y, p2x, p2y); };
1701
function points(opts) {
1702
return (u, seriesIdx, idx0, idx1, filtIdxs) => {
1705
return orient(u, seriesIdx, (series, dataX, dataY, scaleX, scaleY, valToPosX, valToPosY, xOff, yOff, xDim, yDim) => {
1706
let { pxRound, points } = series;
1710
if (scaleX.ori == 0) {
1719
const width = roundDec(points.width * pxRatio, 3);
1721
let rad = (points.size - points.width) / 2 * pxRatio;
1722
let dia = roundDec(rad * 2, 3);
1724
let fill = new Path2D();
1725
let clip = new Path2D();
1727
let { left: lft, top: top, width: wid, height: hgt } = u.bbox;
1736
const drawPoint = pi => {
1737
if (dataY[pi] != null) {
1738
let x = pxRound(valToPosX(dataX[pi], scaleX, xDim, xOff));
1739
let y = pxRound(valToPosY(dataY[pi], scaleY, yDim, yOff));
1741
moveTo(fill, x + rad, y);
1742
arc(fill, x, y, rad, 0, PI * 2);
1747
filtIdxs.forEach(drawPoint);
1749
for (let pi = idx0; pi <= idx1; pi++)
1754
stroke: width > 0 ? fill : null,
1757
flags: BAND_CLIP_FILL | BAND_CLIP_STROKE,
1763
function _drawAcc(lineTo) {
1764
return (stroke, accX, minY, maxY, inY, outY) => {
1766
if (inY != minY && outY != minY)
1767
lineTo(stroke, accX, minY);
1768
if (inY != maxY && outY != maxY)
1769
lineTo(stroke, accX, maxY);
1771
lineTo(stroke, accX, outY);
1776
const drawAccH = _drawAcc(lineToH);
1777
const drawAccV = _drawAcc(lineToV);
1780
return (u, seriesIdx, idx0, idx1) => {
1781
return orient(u, seriesIdx, (series, dataX, dataY, scaleX, scaleY, valToPosX, valToPosY, xOff, yOff, xDim, yDim) => {
1782
let pxRound = series.pxRound;
1784
let lineTo, drawAcc;
1786
if (scaleX.ori == 0) {
1795
const dir = scaleX.dir * (scaleX.ori == 0 ? 1 : -1);
1797
const _paths = {stroke: new Path2D(), fill: null, clip: null, band: null, gaps: null, flags: BAND_CLIP_FILL};
1798
const stroke = _paths.stroke;
1802
inY, outY, outX, drawnAtX;
1806
let accX = pxRound(valToPosX(dataX[dir == 1 ? idx0 : idx1], scaleX, xDim, xOff));
1807
let accGaps = false;
1808
let prevYNull = false;
1811
let lftIdx = nonNullIdx(dataY, idx0, idx1, 1 * dir);
1812
let rgtIdx = nonNullIdx(dataY, idx0, idx1, -1 * dir);
1813
let lftX = pxRound(valToPosX(dataX[lftIdx], scaleX, xDim, xOff));
1814
let rgtX = pxRound(valToPosX(dataX[rgtIdx], scaleX, xDim, xOff));
1817
addGap(gaps, xOff, lftX);
1819
for (let i = dir == 1 ? idx0 : idx1; i >= idx0 && i <= idx1; i += dir) {
1820
let x = pxRound(valToPosX(dataX[i], scaleX, xDim, xOff));
1823
if (dataY[i] != null) {
1824
outY = pxRound(valToPosY(dataY[i], scaleY, yDim, yOff));
1827
lineTo(stroke, x, outY);
1831
minY = min(outY, minY);
1832
maxY = max(outY, maxY);
1834
else if (dataY[i] === null)
1835
accGaps = prevYNull = true;
1838
let _addGap = false;
1841
drawAcc(stroke, accX, minY, maxY, inY, outY);
1842
outX = drawnAtX = accX;
1849
if (dataY[i] != null) {
1850
outY = pxRound(valToPosY(dataY[i], scaleY, yDim, yOff));
1851
lineTo(stroke, x, outY);
1852
minY = maxY = inY = outY;
1855
if (prevYNull && x - accX > 1)
1864
if (dataY[i] === null) {
1872
_addGap && addGap(gaps, outX, x);
1878
if (minY != inf && minY != maxY && drawnAtX != accX)
1879
drawAcc(stroke, accX, minY, maxY, inY, outY);
1881
if (rgtX < xOff + xDim)
1882
addGap(gaps, rgtX, xOff + xDim);
1884
if (series.fill != null) {
1885
let fill = _paths.fill = new Path2D(stroke);
1887
let fillTo = pxRound(valToPosY(series.fillTo(u, seriesIdx, series.min, series.max), scaleY, yDim, yOff));
1889
lineTo(fill, rgtX, fillTo);
1890
lineTo(fill, lftX, fillTo);
1893
_paths.gaps = gaps = series.gaps(u, seriesIdx, idx0, idx1, gaps);
1895
if (!series.spanGaps)
1896
_paths.clip = clipGaps(gaps, scaleX.ori, xOff, yOff, xDim, yDim);
1898
if (u.bands.length > 0) {
1901
_paths.band = clipBandLine(u, seriesIdx, idx0, idx1, stroke);
1909
function stepped(opts) {
1910
const align = ifNull(opts.align, 1);
1912
const ascDesc = ifNull(opts.ascDesc, false);
1914
return (u, seriesIdx, idx0, idx1) => {
1915
return orient(u, seriesIdx, (series, dataX, dataY, scaleX, scaleY, valToPosX, valToPosY, xOff, yOff, xDim, yDim) => {
1916
let pxRound = series.pxRound;
1918
let lineTo = scaleX.ori == 0 ? lineToH : lineToV;
1920
const _paths = {stroke: new Path2D(), fill: null, clip: null, band: null, gaps: null, flags: BAND_CLIP_FILL};
1921
const stroke = _paths.stroke;
1923
const _dir = 1 * scaleX.dir * (scaleX.ori == 0 ? 1 : -1);
1925
idx0 = nonNullIdx(dataY, idx0, idx1, 1);
1926
idx1 = nonNullIdx(dataY, idx0, idx1, -1);
1930
let prevYPos = pxRound(valToPosY(dataY[_dir == 1 ? idx0 : idx1], scaleY, yDim, yOff));
1931
let firstXPos = pxRound(valToPosX(dataX[_dir == 1 ? idx0 : idx1], scaleX, xDim, xOff));
1932
let prevXPos = firstXPos;
1934
lineTo(stroke, firstXPos, prevYPos);
1936
for (let i = _dir == 1 ? idx0 : idx1; i >= idx0 && i <= idx1; i += _dir) {
1937
let yVal1 = dataY[i];
1939
let x1 = pxRound(valToPosX(dataX[i], scaleX, xDim, xOff));
1941
if (yVal1 == null) {
1942
if (yVal1 === null) {
1943
addGap(gaps, prevXPos, x1);
1949
let y1 = pxRound(valToPosY(yVal1, scaleY, yDim, yOff));
1952
addGap(gaps, prevXPos, x1);
1957
lineTo(stroke, x1, prevYPos);
1959
lineTo(stroke, prevXPos, y1);
1961
lineTo(stroke, x1, y1);
1967
if (series.fill != null) {
1968
let fill = _paths.fill = new Path2D(stroke);
1970
let fillTo = series.fillTo(u, seriesIdx, series.min, series.max);
1971
let minY = pxRound(valToPosY(fillTo, scaleY, yDim, yOff));
1973
lineTo(fill, prevXPos, minY);
1974
lineTo(fill, firstXPos, minY);
1977
_paths.gaps = gaps = series.gaps(u, seriesIdx, idx0, idx1, gaps);
1980
let halfStroke = (series.width * pxRatio) / 2;
1981
let startsOffset = (ascDesc || align == 1) ? halfStroke : -halfStroke;
1982
let endsOffset = (ascDesc || align == -1) ? -halfStroke : halfStroke;
1985
g[0] += startsOffset;
1989
if (!series.spanGaps)
1990
_paths.clip = clipGaps(gaps, scaleX.ori, xOff, yOff, xDim, yDim);
1992
if (u.bands.length > 0) {
1995
_paths.band = clipBandLine(u, seriesIdx, idx0, idx1, stroke);
2003
function bars(opts) {
2004
opts = opts || EMPTY_OBJ;
2005
const size = ifNull(opts.size, [0.6, inf, 1]);
2006
const align = opts.align || 0;
2007
const extraGap = (opts.gap || 0) * pxRatio;
2009
const radius = ifNull(opts.radius, 0);
2011
const gapFactor = 1 - size[0];
2012
const maxWidth = ifNull(size[1], inf) * pxRatio;
2013
const minWidth = ifNull(size[2], 1) * pxRatio;
2015
const disp = ifNull(opts.disp, EMPTY_OBJ);
2016
const _each = ifNull(opts.each, _ => {});
2018
const { fill: dispFills, stroke: dispStrokes } = disp;
2020
return (u, seriesIdx, idx0, idx1) => {
2021
return orient(u, seriesIdx, (series, dataX, dataY, scaleX, scaleY, valToPosX, valToPosY, xOff, yOff, xDim, yDim) => {
2022
let pxRound = series.pxRound;
2024
const _dirX = scaleX.dir * (scaleX.ori == 0 ? 1 : -1);
2025
const _dirY = scaleY.dir * (scaleY.ori == 1 ? 1 : -1);
2027
let rect = scaleX.ori == 0 ? rectH : rectV;
2029
let each = scaleX.ori == 0 ? _each : (u, seriesIdx, i, top, lft, hgt, wid) => {
2030
_each(u, seriesIdx, i, lft, top, wid, hgt);
2033
let fillToY = series.fillTo(u, seriesIdx, series.min, series.max);
2035
let y0Pos = valToPosY(fillToY, scaleY, yDim, yOff);
2040
let strokeWidth = pxRound(series.width * pxRatio);
2042
let multiPath = false;
2044
let fillColors = null;
2045
let fillPaths = null;
2046
let strokeColors = null;
2047
let strokePaths = null;
2049
if (dispFills != null && dispStrokes != null) {
2052
fillColors = dispFills.values(u, seriesIdx, idx0, idx1);
2053
fillPaths = new Map();
2054
(new Set(fillColors)).forEach(color => {
2056
fillPaths.set(color, new Path2D());
2059
strokeColors = dispStrokes.values(u, seriesIdx, idx0, idx1);
2060
strokePaths = new Map();
2061
(new Set(strokeColors)).forEach(color => {
2063
strokePaths.set(color, new Path2D());
2067
let { x0, size } = disp;
2069
if (x0 != null && size != null) {
2070
dataX = x0.values(u, seriesIdx, idx0, idx1);
2073
dataX = dataX.map(pct => u.posToVal(xOff + pct * xDim, scaleX.key, true));
2076
let sizes = size.values(u, seriesIdx, idx0, idx1);
2079
barWid = sizes[0] * xDim;
2081
barWid = valToPosX(sizes[0], scaleX, xDim, xOff) - valToPosX(0, scaleX, xDim, xOff);
2083
barWid = pxRound(barWid - strokeWidth);
2085
xShift = (_dirX == 1 ? -strokeWidth / 2 : barWid + strokeWidth / 2);
2090
if (dataX.length > 1) {
2096
for (let i = 0, minDelta = Infinity; i < dataX.length; i++) {
2097
if (dataY[i] !== undefined) {
2098
if (prevIdx != null) {
2099
let delta = abs(dataX[i] - dataX[prevIdx]);
2101
if (delta < minDelta) {
2103
colWid = abs(valToPosX(dataX[i], scaleX, xDim, xOff) - valToPosX(dataX[prevIdx], scaleX, xDim, xOff));
2112
let gapWid = colWid * gapFactor;
2114
barWid = pxRound(min(maxWidth, max(minWidth, colWid - gapWid)) - strokeWidth - extraGap);
2116
xShift = (align == 0 ? barWid / 2 : align == _dirX ? 0 : barWid) - align * _dirX * extraGap / 2;
2119
const _paths = {stroke: null, fill: null, clip: null, band: null, gaps: null, flags: BAND_CLIP_FILL | BAND_CLIP_STROKE};
2121
const hasBands = u.bands.length > 0;
2127
_paths.band = new Path2D();
2128
yLimit = pxRound(valToPosY(scaleY.max, scaleY, yDim, yOff));
2131
const stroke = multiPath ? null : new Path2D();
2132
const band = _paths.band;
2134
for (let i = _dirX == 1 ? idx0 : idx1; i >= idx0 && i <= idx1; i += _dirX) {
2135
let yVal = dataY[i];
2147
let xVal = scaleX.distr != 2 || disp != null ? dataX[i] : i;
2150
let xPos = valToPosX(xVal, scaleX, xDim, xOff);
2151
let yPos = valToPosY(ifNull(yVal, fillToY) , scaleY, yDim, yOff);
2153
let lft = pxRound(xPos - xShift);
2154
let btm = pxRound(max(yPos, y0Pos));
2155
let top = pxRound(min(yPos, y0Pos));
2157
let barHgt = btm - top;
2159
let r = radius * barWid;
2163
if (strokeWidth > 0 && strokeColors[i] != null)
2164
rect(strokePaths.get(strokeColors[i]), lft, top + floor(strokeWidth / 2), barWid, max(0, barHgt - strokeWidth), r);
2166
if (fillColors[i] != null)
2167
rect(fillPaths.get(fillColors[i]), lft, top + floor(strokeWidth / 2), barWid, max(0, barHgt - strokeWidth), r);
2170
rect(stroke, lft, top + floor(strokeWidth / 2), barWid, max(0, barHgt - strokeWidth), r);
2172
each(u, seriesIdx, i,
2173
lft - strokeWidth / 2,
2175
barWid + strokeWidth,
2192
rect(band, lft - strokeWidth / 2, top, barWid + strokeWidth, max(0, barHgt), 0);
2196
if (strokeWidth > 0)
2197
_paths.stroke = multiPath ? strokePaths : stroke;
2199
_paths.fill = multiPath ? fillPaths : stroke;
2206
function splineInterp(interp, opts) {
2207
return (u, seriesIdx, idx0, idx1) => {
2208
return orient(u, seriesIdx, (series, dataX, dataY, scaleX, scaleY, valToPosX, valToPosY, xOff, yOff, xDim, yDim) => {
2209
let pxRound = series.pxRound;
2211
let moveTo, bezierCurveTo, lineTo;
2213
if (scaleX.ori == 0) {
2216
bezierCurveTo = bezierCurveToH;
2221
bezierCurveTo = bezierCurveToV;
2224
const _dir = 1 * scaleX.dir * (scaleX.ori == 0 ? 1 : -1);
2226
idx0 = nonNullIdx(dataY, idx0, idx1, 1);
2227
idx1 = nonNullIdx(dataY, idx0, idx1, -1);
2231
let firstXPos = pxRound(valToPosX(dataX[_dir == 1 ? idx0 : idx1], scaleX, xDim, xOff));
2232
let prevXPos = firstXPos;
2237
for (let i = _dir == 1 ? idx0 : idx1; i >= idx0 && i <= idx1; i += _dir) {
2238
let yVal = dataY[i];
2239
let xVal = dataX[i];
2240
let xPos = valToPosX(xVal, scaleX, xDim, xOff);
2243
if (yVal === null) {
2244
addGap(gaps, prevXPos, xPos);
2251
addGap(gaps, prevXPos, xPos);
2255
xCoords.push((prevXPos = xPos));
2256
yCoords.push(valToPosY(dataY[i], scaleY, yDim, yOff));
2260
const _paths = {stroke: interp(xCoords, yCoords, moveTo, lineTo, bezierCurveTo, pxRound), fill: null, clip: null, band: null, gaps: null, flags: BAND_CLIP_FILL};
2261
const stroke = _paths.stroke;
2263
if (series.fill != null && stroke != null) {
2264
let fill = _paths.fill = new Path2D(stroke);
2266
let fillTo = series.fillTo(u, seriesIdx, series.min, series.max);
2267
let minY = pxRound(valToPosY(fillTo, scaleY, yDim, yOff));
2269
lineTo(fill, prevXPos, minY);
2270
lineTo(fill, firstXPos, minY);
2273
_paths.gaps = gaps = series.gaps(u, seriesIdx, idx0, idx1, gaps);
2275
if (!series.spanGaps)
2276
_paths.clip = clipGaps(gaps, scaleX.ori, xOff, yOff, xDim, yDim);
2278
if (u.bands.length > 0) {
2281
_paths.band = clipBandLine(u, seriesIdx, idx0, idx1, stroke);
2301
function monotoneCubic(opts) {
2302
return splineInterp(_monotoneCubic);
2307
function _monotoneCubic(xs, ys, moveTo, lineTo, bezierCurveTo, pxRound) {
2308
const n = xs.length;
2313
const path = new Path2D();
2315
moveTo(path, xs[0], ys[0]);
2318
lineTo(path, xs[1], ys[1]);
2326
for (let i = 0; i < n - 1; i++) {
2327
dys[i] = ys[i + 1] - ys[i];
2328
dxs[i] = xs[i + 1] - xs[i];
2329
ds[i] = dys[i] / dxs[i];
2336
for (let i = 1; i < n - 1; i++) {
2337
if (ds[i] === 0 || ds[i - 1] === 0 || (ds[i - 1] > 0) !== (ds[i] > 0))
2340
ms[i] = 3 * (dxs[i - 1] + dxs[i]) / (
2341
(2 * dxs[i] + dxs[i - 1]) / ds[i - 1] +
2342
(dxs[i] + 2 * dxs[i - 1]) / ds[i]
2345
if (!isFinite(ms[i]))
2350
ms[n - 1] = ds[n - 2];
2352
for (let i = 0; i < n - 1; i++) {
2356
ys[i] + ms[i] * dxs[i] / 3,
2357
xs[i + 1] - dxs[i] / 3,
2358
ys[i + 1] - ms[i + 1] * dxs[i] / 3,
2368
const cursorPlots = new Set();
2370
function invalidateRects() {
2371
cursorPlots.forEach(u => {
2376
on(resize, win, invalidateRects);
2377
on(scroll, win, invalidateRects, true);
2379
const linearPath = linear() ;
2380
const pointsPath = points() ;
2382
function setDefaults(d, xo, yo, initY) {
2383
let d2 = initY ? [d[0], d[1]].concat(d.slice(2)) : [d[0]].concat(d.slice(1));
2384
return d2.map((o, i) => setDefault(o, i, xo, yo));
2387
function setDefaults2(d, xyo) {
2388
return d.map((o, i) => i == 0 ? null : assign({}, xyo, o));
2391
function setDefault(o, i, xo, yo) {
2392
return assign({}, (i == 0 ? xo : yo), o);
2395
function snapNumX(self, dataMin, dataMax) {
2396
return dataMin == null ? nullNullTuple : [dataMin, dataMax];
2399
const snapTimeX = snapNumX;
2403
function snapNumY(self, dataMin, dataMax) {
2404
return dataMin == null ? nullNullTuple : rangeNum(dataMin, dataMax, rangePad, true);
2407
function snapLogY(self, dataMin, dataMax, scale) {
2408
return dataMin == null ? nullNullTuple : rangeLog(dataMin, dataMax, self.scales[scale].log, false);
2411
const snapLogX = snapLogY;
2413
function snapAsinhY(self, dataMin, dataMax, scale) {
2414
return dataMin == null ? nullNullTuple : rangeAsinh(dataMin, dataMax, self.scales[scale].log, false);
2417
const snapAsinhX = snapAsinhY;
2420
function findIncr(minVal, maxVal, incrs, dim, minSpace) {
2421
let intDigits = max(numIntDigits(minVal), numIntDigits(maxVal));
2423
let delta = maxVal - minVal;
2425
let incrIdx = closestIdx((minSpace / dim) * delta, incrs);
2428
let foundIncr = incrs[incrIdx];
2429
let foundSpace = dim * foundIncr / delta;
2431
if (foundSpace >= minSpace && intDigits + (foundIncr < 5 ? fixedDec.get(foundIncr) : 0) <= 17)
2432
return [foundIncr, foundSpace];
2433
} while (++incrIdx < incrs.length);
2438
function pxRatioFont(font) {
2439
let fontSize, fontSizeCss;
2440
font = font.replace(/(\d+)px/, (m, p1) => (fontSize = round((fontSizeCss = +p1) * pxRatio)) + 'px');
2441
return [font, fontSize, fontSizeCss];
2444
function syncFontSize(axis) {
2446
[axis.font, axis.labelFont].forEach(f => {
2447
let size = roundDec(f[2] * pxRatio, 1);
2448
f[0] = f[0].replace(/[0-9.]+px/, size + 'px');
2454
function uPlot(opts, data, then) {
2456
mode: ifNull(opts.mode, 1),
2459
const mode = self.mode;
2462
function getValPct(val, scale) {
2464
scale.distr == 3 ? log10(val > 0 ? val : scale.clamp(self, val, scale.min, scale.max, scale.key)) :
2465
scale.distr == 4 ? asinh(val, scale.asinh) :
2469
return (_val - scale._min) / (scale._max - scale._min);
2472
function getHPos(val, scale, dim, off) {
2473
let pct = getValPct(val, scale);
2474
return off + dim * (scale.dir == -1 ? (1 - pct) : pct);
2477
function getVPos(val, scale, dim, off) {
2478
let pct = getValPct(val, scale);
2479
return off + dim * (scale.dir == -1 ? pct : (1 - pct));
2482
function getPos(val, scale, dim, off) {
2483
return scale.ori == 0 ? getHPos(val, scale, dim, off) : getVPos(val, scale, dim, off);
2486
self.valToPosH = getHPos;
2487
self.valToPosV = getVPos;
2492
const root = self.root = placeDiv(UPLOT);
2494
if (opts.id != null)
2497
addClass(root, opts.class);
2500
let title = placeDiv(TITLE, root);
2501
title.textContent = opts.title;
2504
const can = placeTag("canvas");
2505
const ctx = self.ctx = can.getContext("2d");
2507
const wrap = placeDiv(WRAP, root);
2508
const under = self.under = placeDiv(UNDER, wrap);
2509
wrap.appendChild(can);
2510
const over = self.over = placeDiv(OVER, wrap);
2514
const pxAlign = +ifNull(opts.pxAlign, 1);
2516
const pxRound = pxRoundGen(pxAlign);
2518
(opts.plugins || []).forEach(p => {
2520
opts = p.opts(self, opts) || opts;
2523
const ms = opts.ms || 1e-3;
2525
const series = self.series = mode == 1 ?
2526
setDefaults(opts.series || [], xSeriesOpts, ySeriesOpts, false) :
2527
setDefaults2(opts.series || [null], xySeriesOpts);
2528
const axes = self.axes = setDefaults(opts.axes || [], xAxisOpts, yAxisOpts, true);
2529
const scales = self.scales = {};
2530
const bands = self.bands = opts.bands || [];
2532
bands.forEach(b => {
2533
b.fill = fnOrSelf(b.fill || null);
2536
const xScaleKey = mode == 2 ? series[1].facets[0].scale : series[0].scale;
2538
const drawOrderMap = {
2543
const drawOrder = (opts.drawOrder || ["axes", "series"]).map(key => drawOrderMap[key]);
2545
function initScale(scaleKey) {
2546
let sc = scales[scaleKey];
2549
let scaleOpts = (opts.scales || EMPTY_OBJ)[scaleKey] || EMPTY_OBJ;
2551
if (scaleOpts.from != null) {
2553
initScale(scaleOpts.from);
2555
scales[scaleKey] = assign({}, scales[scaleOpts.from], scaleOpts, {key: scaleKey});
2558
sc = scales[scaleKey] = assign({}, (scaleKey == xScaleKey ? xScaleOpts : yScaleOpts), scaleOpts);
2565
let isTime = sc.time;
2569
let rangeIsArr = isArr(rn);
2571
if (scaleKey != xScaleKey || mode == 2) {
2573
if (rangeIsArr && (rn[0] == null || rn[1] == null)) {
2575
min: rn[0] == null ? autoRangePart : {
2580
max: rn[1] == null ? autoRangePart : {
2589
if (!rangeIsArr && isObj(rn)) {
2592
rn = (self, dataMin, dataMax) => dataMin == null ? nullNullTuple : rangeNum(dataMin, dataMax, cfg);
2596
sc.range = fnOrSelf(rn || (isTime ? snapTimeX : scaleKey == xScaleKey ?
2597
(sc.distr == 3 ? snapLogX : sc.distr == 4 ? snapAsinhX : snapNumX) :
2598
(sc.distr == 3 ? snapLogY : sc.distr == 4 ? snapAsinhY : snapNumY)
2601
sc.auto = fnOrSelf(rangeIsArr ? false : sc.auto);
2603
sc.clamp = fnOrSelf(sc.clamp || clampScale);
2606
sc._min = sc._max = null;
2616
series.forEach(s => {
2625
for (let k in opts.scales)
2628
const scaleX = scales[xScaleKey];
2630
const xScaleDistr = scaleX.distr;
2632
let valToPosX, valToPosY;
2634
if (scaleX.ori == 0) {
2635
addClass(root, ORI_HZ);
2636
valToPosX = getHPos;
2637
valToPosY = getVPos;
2653
addClass(root, ORI_VT);
2654
valToPosX = getVPos;
2655
valToPosY = getHPos;
2671
const pendScales = {};
2674
for (let k in scales) {
2677
if (sc.min != null || sc.max != null) {
2678
pendScales[k] = {min: sc.min, max: sc.max};
2679
sc.min = sc.max = null;
2684
const _tzDate = (opts.tzDate || (ts => new Date(round(ts / ms))));
2685
const _fmtDate = (opts.fmtDate || fmtDate);
2687
const _timeAxisSplits = (ms == 1 ? timeAxisSplitsMs(_tzDate) : timeAxisSplitsS(_tzDate));
2688
const _timeAxisVals = timeAxisVals(_tzDate, timeAxisStamps((ms == 1 ? _timeAxisStampsMs : _timeAxisStampsS), _fmtDate));
2689
const _timeSeriesVal = timeSeriesVal(_tzDate, timeSeriesStamp(_timeSeriesStamp, _fmtDate));
2691
const activeIdxs = [];
2693
const legend = (self.legend = assign({}, legendOpts, opts.legend));
2694
const showLegend = legend.show;
2695
const markers = legend.markers;
2698
legend.idxs = activeIdxs;
2700
markers.width = fnOrSelf(markers.width);
2701
markers.dash = fnOrSelf(markers.dash);
2702
markers.stroke = fnOrSelf(markers.stroke);
2703
markers.fill = fnOrSelf(markers.fill);
2707
let legendRows = [];
2708
let legendCells = [];
2710
let multiValLegend = false;
2711
let NULL_LEGEND_VALUES = {};
2714
const getMultiVals = series[1] ? series[1].values : null;
2715
multiValLegend = getMultiVals != null;
2716
legendCols = multiValLegend ? getMultiVals(self, 1, 0) : {_: 0};
2718
for (let k in legendCols)
2719
NULL_LEGEND_VALUES[k] = "--";
2723
legendEl = placeTag("table", LEGEND, root);
2725
if (multiValLegend) {
2726
let head = placeTag("tr", LEGEND_THEAD, legendEl);
2727
placeTag("th", null, head);
2729
for (var key in legendCols)
2730
placeTag("th", LEGEND_LABEL, head).textContent = key;
2733
addClass(legendEl, LEGEND_INLINE);
2734
legend.live && addClass(legendEl, LEGEND_LIVE);
2738
const son = {show: true};
2739
const soff = {show: false};
2741
function initLegendRow(s, i) {
2742
if (i == 0 && (multiValLegend || !legend.live || mode == 2))
2743
return nullNullTuple;
2747
let row = placeTag("tr", LEGEND_SERIES, legendEl, legendEl.childNodes[i]);
2749
addClass(row, s.class);
2754
let label = placeTag("th", null, row);
2757
let indic = placeDiv(LEGEND_MARKER, label);
2760
let width = markers.width(self, i);
2763
indic.style.border = width + "px " + markers.dash(self, i) + " " + markers.stroke(self, i);
2765
indic.style.background = markers.fill(self, i);
2769
let text = placeDiv(LEGEND_LABEL, label);
2770
text.textContent = s.label;
2774
text.style.color = s.width > 0 ? markers.stroke(self, i) : markers.fill(self, i);
2776
onMouse("click", label, e => {
2780
let seriesIdx = series.indexOf(s);
2782
if ((e.ctrlKey || e.metaKey) != legend.isolate) {
2784
let isolate = series.some((s, i) => i > 0 && i != seriesIdx && s.show);
2786
series.forEach((s, i) => {
2787
i > 0 && setSeries(i, isolate ? (i == seriesIdx ? son : soff) : son, true, syncOpts.setSeries);
2791
setSeries(seriesIdx, {show: !s.show}, true, syncOpts.setSeries);
2795
onMouse(mouseenter, label, e => {
2799
setSeries(series.indexOf(s), FOCUS_TRUE, true, syncOpts.setSeries);
2804
for (var key in legendCols) {
2805
let v = placeTag("td", LEGEND_VALUE, row);
2806
v.textContent = "--";
2810
return [row, cells];
2813
const mouseListeners = new Map();
2815
function onMouse(ev, targ, fn) {
2816
const targListeners = mouseListeners.get(targ) || {};
2817
const listener = cursor.bind[ev](self, targ, fn);
2820
on(ev, targ, targListeners[ev] = listener);
2821
mouseListeners.set(targ, targListeners);
2825
function offMouse(ev, targ, fn) {
2826
const targListeners = mouseListeners.get(targ) || {};
2828
for (let k in targListeners) {
2829
if (ev == null || k == ev) {
2830
off(k, targ, targListeners[k]);
2831
delete targListeners[k];
2836
mouseListeners.delete(targ);
2856
let shouldSetScales = false;
2857
let shouldSetSize = false;
2858
let shouldConvergeSize = false;
2859
let shouldSetCursor = false;
2860
let shouldSetLegend = false;
2862
function _setSize(width, height, force) {
2863
if (force || (width != self.width || height != self.height))
2864
calcSize(width, height);
2866
resetYSeries(false);
2868
shouldConvergeSize = true;
2869
shouldSetSize = true;
2870
shouldSetCursor = shouldSetLegend = cursor.left >= 0;
2874
function calcSize(width, height) {
2877
self.width = fullWidCss = plotWidCss = width;
2878
self.height = fullHgtCss = plotHgtCss = height;
2879
plotLftCss = plotTopCss = 0;
2886
plotLft = bb.left = incrRound(plotLftCss * pxRatio, 0.5);
2887
plotTop = bb.top = incrRound(plotTopCss * pxRatio, 0.5);
2888
plotWid = bb.width = incrRound(plotWidCss * pxRatio, 0.5);
2889
plotHgt = bb.height = incrRound(plotHgtCss * pxRatio, 0.5);
2895
const CYCLE_LIMIT = 3;
2897
function convergeSize() {
2898
let converged = false;
2902
while (!converged) {
2905
let axesConverged = axesCalc(cycleNum);
2906
let paddingConverged = paddingCalc(cycleNum);
2908
converged = cycleNum == CYCLE_LIMIT || (axesConverged && paddingConverged);
2911
calcSize(self.width, self.height);
2912
shouldSetSize = true;
2917
function setSize({width, height}) {
2918
_setSize(width, height);
2921
self.setSize = setSize;
2924
function calcPlotRect() {
2926
let hasTopAxis = false;
2927
let hasBtmAxis = false;
2928
let hasRgtAxis = false;
2929
let hasLftAxis = false;
2931
axes.forEach((axis, i) => {
2932
if (axis.show && axis._show) {
2933
let {side, _size} = axis;
2934
let isVt = side % 2;
2935
let labelSize = axis.label != null ? axis.labelSize : 0;
2937
let fullSize = _size + labelSize;
2941
plotWidCss -= fullSize;
2944
plotLftCss += fullSize;
2951
plotHgtCss -= fullSize;
2954
plotTopCss += fullSize;
2964
sidesWithAxes[0] = hasTopAxis;
2965
sidesWithAxes[1] = hasRgtAxis;
2966
sidesWithAxes[2] = hasBtmAxis;
2967
sidesWithAxes[3] = hasLftAxis;
2970
plotWidCss -= _padding[1] + _padding[3];
2971
plotLftCss += _padding[3];
2974
plotHgtCss -= _padding[2] + _padding[0];
2975
plotTopCss += _padding[0];
2978
function calcAxesRects() {
2980
let off1 = plotLftCss + plotWidCss;
2981
let off2 = plotTopCss + plotHgtCss;
2983
let off3 = plotLftCss;
2984
let off0 = plotTopCss;
2986
function incrOffset(side, size) {
2988
case 1: off1 += size; return off1 - size;
2989
case 2: off2 += size; return off2 - size;
2990
case 3: off3 -= size; return off3 + size;
2991
case 0: off0 -= size; return off0 + size;
2995
axes.forEach((axis, i) => {
2996
if (axis.show && axis._show) {
2997
let side = axis.side;
2999
axis._pos = incrOffset(side, axis._size);
3001
if (axis.label != null)
3002
axis._lpos = incrOffset(side, axis.labelSize);
3007
const cursor = (self.cursor = assign({}, cursorOpts, {drag: {y: mode == 2}}, opts.cursor));
3010
cursor.idxs = activeIdxs;
3012
cursor._lock = false;
3014
let points = cursor.points;
3016
points.show = fnOrSelf(points.show);
3017
points.size = fnOrSelf(points.size);
3018
points.stroke = fnOrSelf(points.stroke);
3019
points.width = fnOrSelf(points.width);
3020
points.fill = fnOrSelf(points.fill);
3023
const focus = self.focus = assign({}, opts.focus || {alpha: 0.3}, cursor.focus);
3024
const cursorFocus = focus.prox >= 0;
3027
let cursorPts = [null];
3029
function initCursorPt(s, si) {
3031
let pt = cursor.points.show(self, si);
3034
addClass(pt, CURSOR_PT);
3035
addClass(pt, s.class);
3036
elTrans(pt, -10, -10, plotWidCss, plotHgtCss);
3037
over.insertBefore(pt, cursorPts[si]);
3044
function initSeries(s, i) {
3045
if (mode == 1 || i > 0) {
3046
let isTime = mode == 1 && scales[s.scale].time;
3049
s.value = isTime ? (isStr(sv) ? timeSeriesVal(_tzDate, timeSeriesStamp(sv, _fmtDate)) : sv || _timeSeriesVal) : sv || numSeriesVal;
3050
s.label = s.label || (isTime ? timeSeriesLabel : numSeriesLabel);
3054
s.width = s.width == null ? 1 : s.width;
3055
s.paths = s.paths || linearPath || retNull;
3056
s.fillTo = fnOrSelf(s.fillTo || seriesFillTo);
3057
s.pxAlign = +ifNull(s.pxAlign, pxAlign);
3058
s.pxRound = pxRoundGen(s.pxAlign);
3060
s.stroke = fnOrSelf(s.stroke || null);
3061
s.fill = fnOrSelf(s.fill || null);
3062
s._stroke = s._fill = s._paths = s._focus = null;
3064
let _ptDia = ptDia(s.width, 1);
3065
let points = s.points = assign({}, {
3067
width: max(1, _ptDia * .2),
3074
points.show = fnOrSelf(points.show);
3075
points.filter = fnOrSelf(points.filter);
3076
points.fill = fnOrSelf(points.fill);
3077
points.stroke = fnOrSelf(points.stroke);
3078
points.paths = fnOrSelf(points.paths);
3079
points.pxAlign = s.pxAlign;
3083
let rowCells = initLegendRow(s, i);
3084
legendRows.splice(i, 0, rowCells[0]);
3085
legendCells.splice(i, 0, rowCells[1]);
3086
legend.values.push(null);
3090
activeIdxs.splice(i, 0, null);
3092
let pt = initCursorPt(s, i);
3093
pt && cursorPts.splice(i, 0, pt);
3097
function addSeries(opts, si) {
3098
si = si == null ? series.length : si;
3100
opts = setDefault(opts, si, xSeriesOpts, ySeriesOpts);
3101
series.splice(si, 0, opts);
3102
initSeries(series[si], si);
3105
self.addSeries = addSeries;
3107
function delSeries(i) {
3108
series.splice(i, 1);
3111
legend.values.splice(i, 1);
3113
legendCells.splice(i, 1);
3114
let tr = legendRows.splice(i, 1)[0];
3115
offMouse(null, tr.firstChild);
3120
activeIdxs.splice(i, 1);
3122
cursorPts.length > 1 && cursorPts.splice(i, 1)[0].remove();
3128
self.delSeries = delSeries;
3130
const sidesWithAxes = [false, false, false, false];
3132
function initAxis(axis, i) {
3133
axis._show = axis.show;
3136
let isVt = axis.side % 2;
3138
let sc = scales[axis.scale];
3142
axis.scale = isVt ? series[1].scale : xScaleKey;
3143
sc = scales[axis.scale];
3147
let isTime = sc.time;
3149
axis.size = fnOrSelf(axis.size);
3150
axis.space = fnOrSelf(axis.space);
3151
axis.rotate = fnOrSelf(axis.rotate);
3152
axis.incrs = fnOrSelf(axis.incrs || ( sc.distr == 2 ? wholeIncrs : (isTime ? (ms == 1 ? timeIncrsMs : timeIncrsS) : numIncrs)));
3153
axis.splits = fnOrSelf(axis.splits || (isTime && sc.distr == 1 ? _timeAxisSplits : sc.distr == 3 ? logAxisSplits : sc.distr == 4 ? asinhAxisSplits : numAxisSplits));
3155
axis.stroke = fnOrSelf(axis.stroke);
3156
axis.grid.stroke = fnOrSelf(axis.grid.stroke);
3157
axis.ticks.stroke = fnOrSelf(axis.ticks.stroke);
3159
let av = axis.values;
3163
isArr(av) && !isArr(av[0]) ? fnOrSelf(av) :
3168
timeAxisVals(_tzDate, timeAxisStamps(av, _fmtDate)) :
3171
timeAxisVal(_tzDate, av) :
3173
) : av || numAxisVals
3176
axis.filter = fnOrSelf(axis.filter || ( sc.distr >= 3 ? logAxisValsFilt : retArg1));
3178
axis.font = pxRatioFont(axis.font);
3179
axis.labelFont = pxRatioFont(axis.labelFont);
3181
axis._size = axis.size(self, null, i, 0);
3188
axis._values = null;
3191
sidesWithAxes[i] = true;
3193
axis._el = placeDiv(AXIS, wrap);
3200
function autoPadSide(self, side, sidesWithAxes, cycleNum) {
3201
let [hasTopAxis, hasRgtAxis, hasBtmAxis, hasLftAxis] = sidesWithAxes;
3206
if (ori == 0 && (hasLftAxis || hasRgtAxis))
3207
size = (side == 0 && !hasTopAxis || side == 2 && !hasBtmAxis ? round(xAxisOpts.size / 3) : 0);
3208
if (ori == 1 && (hasTopAxis || hasBtmAxis))
3209
size = (side == 1 && !hasRgtAxis || side == 3 && !hasLftAxis ? round(yAxisOpts.size / 2) : 0);
3214
const padding = self.padding = (opts.padding || [autoPadSide,autoPadSide,autoPadSide,autoPadSide]).map(p => fnOrSelf(ifNull(p, autoPadSide)));
3215
const _padding = self._padding = padding.map((p, i) => p(self, i, sidesWithAxes, 0));
3222
const idxs = mode == 1 ? series[0].idxs : null;
3226
let viaAutoScaleX = false;
3228
function setData(_data, _resetScales) {
3231
for (let i = 1; i < series.length; i++)
3232
dataLen += data[i][0].length;
3233
self.data = data = _data;
3236
data = (_data || []).slice();
3237
data[0] = data[0] || [];
3239
self.data = data.slice();
3241
dataLen = data0.length;
3243
if (xScaleDistr == 2)
3244
data[0] = data0.map((v, i) => i);
3253
if (_resetScales !== false) {
3256
if (xsc.auto(self, viaAutoScaleX))
3259
_setScale(xScaleKey, xsc.min, xsc.max);
3261
shouldSetCursor = cursor.left >= 0;
3262
shouldSetLegend = true;
3267
self.setData = setData;
3269
function autoScaleX() {
3270
viaAutoScaleX = true;
3277
i1 = idxs[1] = dataLen - 1;
3282
if (xScaleDistr == 2) {
3286
else if (dataLen == 1) {
3287
if (xScaleDistr == 3)
3288
[_min, _max] = rangeLog(_min, _min, scaleX.log, false);
3289
else if (xScaleDistr == 4)
3290
[_min, _max] = rangeAsinh(_min, _min, scaleX.log, false);
3291
else if (scaleX.time)
3292
_max = _min + round(86400 / ms);
3294
[_min, _max] = rangeNum(_min, _max, rangePad, true);
3298
i0 = idxs[0] = _min = null;
3299
i1 = idxs[1] = _max = null;
3303
_setScale(xScaleKey, _min, _max);
3306
let ctxStroke, ctxFill, ctxWidth, ctxDash, ctxJoin, ctxCap, ctxFont, ctxAlign, ctxBaseline;
3309
function setCtxStyle(stroke = transparent, width, dash = EMPTY_ARR, cap = "butt", fill = transparent, join = "round") {
3310
if (stroke != ctxStroke)
3311
ctx.strokeStyle = ctxStroke = stroke;
3312
if (fill != ctxFill)
3313
ctx.fillStyle = ctxFill = fill;
3314
if (width != ctxWidth)
3315
ctx.lineWidth = ctxWidth = width;
3316
if (join != ctxJoin)
3317
ctx.lineJoin = ctxJoin = join;
3319
ctx.lineCap = ctxCap = cap;
3320
if (dash != ctxDash)
3321
ctx.setLineDash(ctxDash = dash);
3324
function setFontStyle(font, fill, align, baseline) {
3325
if (fill != ctxFill)
3326
ctx.fillStyle = ctxFill = fill;
3327
if (font != ctxFont)
3328
ctx.font = ctxFont = font;
3329
if (align != ctxAlign)
3330
ctx.textAlign = ctxAlign = align;
3331
if (baseline != ctxBaseline)
3332
ctx.textBaseline = ctxBaseline = baseline;
3335
function accScale(wsc, psc, facet, data) {
3336
if (wsc.auto(self, viaAutoScaleX) && (psc == null || psc.min == null)) {
3337
let _i0 = ifNull(i0, 0);
3338
let _i1 = ifNull(i1, data.length - 1);
3341
let minMax = facet.min == null ? (wsc.distr == 3 ? getMinMaxLog(data, _i0, _i1) : getMinMax(data, _i0, _i1)) : [facet.min, facet.max];
3344
wsc.min = min(wsc.min, facet.min = minMax[0]);
3345
wsc.max = max(wsc.max, facet.max = minMax[1]);
3349
function setScales() {
3353
let wipScales = copy(scales, fastIsObj);
3355
for (let k in wipScales) {
3356
let wsc = wipScales[k];
3357
let psc = pendScales[k];
3359
if (psc != null && psc.min != null) {
3366
else if (k != xScaleKey || mode == 2) {
3367
if (dataLen == 0 && wsc.from == null) {
3368
let minMax = wsc.range(self, null, null, k);
3369
wsc.min = minMax[0];
3370
wsc.max = minMax[1];
3381
series.forEach((s, i) => {
3384
let wsc = wipScales[k];
3385
let psc = pendScales[k];
3388
let minMax = wsc.range(self, wsc.min, wsc.max, k);
3390
wsc.min = minMax[0];
3391
wsc.max = minMax[1];
3393
i0 = closestIdx(wsc.min, data[0]);
3394
i1 = closestIdx(wsc.max, data[0]);
3397
if (data[0][i0] < wsc.min)
3399
if (data[0][i1] > wsc.max)
3405
else if (s.show && s.auto)
3406
accScale(wsc, psc, s, data[i]);
3413
if (s.show && s.auto) {
3415
let [ xFacet, yFacet ] = s.facets;
3416
let xScaleKey = xFacet.scale;
3417
let yScaleKey = yFacet.scale;
3418
let [ xData, yData ] = data[i];
3420
accScale(wipScales[xScaleKey], pendScales[xScaleKey], xFacet, xData);
3421
accScale(wipScales[yScaleKey], pendScales[yScaleKey], yFacet, yData);
3432
for (let k in wipScales) {
3433
let wsc = wipScales[k];
3434
let psc = pendScales[k];
3436
if (wsc.from == null && (psc == null || psc.min == null)) {
3437
let minMax = wsc.range(
3439
wsc.min == inf ? null : wsc.min,
3440
wsc.max == -inf ? null : wsc.max,
3443
wsc.min = minMax[0];
3444
wsc.max = minMax[1];
3450
for (let k in wipScales) {
3451
let wsc = wipScales[k];
3453
if (wsc.from != null) {
3454
let base = wipScales[wsc.from];
3456
if (base.min == null)
3457
wsc.min = wsc.max = null;
3459
let minMax = wsc.range(self, base.min, base.max, k);
3460
wsc.min = minMax[0];
3461
wsc.max = minMax[1];
3467
let anyChanged = false;
3469
for (let k in wipScales) {
3470
let wsc = wipScales[k];
3473
if (sc.min != wsc.min || sc.max != wsc.max) {
3477
let distr = sc.distr;
3479
sc._min = distr == 3 ? log10(sc.min) : distr == 4 ? asinh(sc.min, sc.asinh) : sc.min;
3480
sc._max = distr == 3 ? log10(sc.max) : distr == 4 ? asinh(sc.max, sc.asinh) : sc.max;
3482
changed[k] = anyChanged = true;
3488
series.forEach((s, i) => {
3490
if (i > 0 && changed.y)
3494
if (changed[s.scale])
3499
for (let k in changed) {
3500
shouldConvergeSize = true;
3501
fire("setScale", k);
3505
shouldSetCursor = shouldSetLegend = cursor.left >= 0;
3508
for (let k in pendScales)
3509
pendScales[k] = null;
3513
function getOuterIdxs(ydata) {
3514
let _i0 = clamp(i0 - 1, 0, dataLen - 1);
3515
let _i1 = clamp(i1 + 1, 0, dataLen - 1);
3517
while (ydata[_i0] == null && _i0 > 0)
3520
while (ydata[_i1] == null && _i1 < dataLen - 1)
3526
function drawSeries() {
3528
series.forEach((s, i) => {
3529
if (i > 0 && s.show && s._paths == null) {
3530
let _idxs = getOuterIdxs(data[i]);
3531
s._paths = s.paths(self, i, _idxs[0], _idxs[1]);
3535
series.forEach((s, i) => {
3536
if (i > 0 && s.show) {
3537
if (ctxAlpha != s.alpha)
3538
ctx.globalAlpha = ctxAlpha = s.alpha;
3541
cacheStrokeFill(i, false);
3542
s._paths && drawPath(i, false);
3546
cacheStrokeFill(i, true);
3548
let show = s.points.show(self, i, i0, i1);
3549
let idxs = s.points.filter(self, i, show, s._paths ? s._paths.gaps : null);
3552
s.points._paths = s.points.paths(self, i, i0, i1, idxs);
3558
ctx.globalAlpha = ctxAlpha = 1;
3560
fire("drawSeries", i);
3566
function cacheStrokeFill(si, _points) {
3567
let s = _points ? series[si].points : series[si];
3569
s._stroke = s.stroke(self, si);
3570
s._fill = s.fill(self, si);
3573
function drawPath(si, _points) {
3574
let s = _points ? series[si].points : series[si];
3576
let strokeStyle = s._stroke;
3577
let fillStyle = s._fill;
3579
let { stroke, fill, clip: gapsClip, flags } = s._paths;
3580
let boundsClip = null;
3581
let width = roundDec(s.width * pxRatio, 3);
3582
let offset = (width % 2) / 2;
3584
if (_points && fillStyle == null)
3585
fillStyle = width > 0 ? "#fff" : strokeStyle;
3587
let _pxAlign = s.pxAlign == 1;
3589
_pxAlign && ctx.translate(offset, offset);
3597
let halfWid = width * pxRatio / 2;
3607
boundsClip = new Path2D();
3608
boundsClip.rect(lft, top, wid, hgt);
3613
strokeFill(strokeStyle, width, s.dash, s.cap, fillStyle, stroke, fill, flags, gapsClip);
3615
fillStroke(si, strokeStyle, width, s.dash, s.cap, fillStyle, stroke, fill, flags, boundsClip, gapsClip);
3617
_pxAlign && ctx.translate(-offset, -offset);
3620
function fillStroke(si, strokeStyle, lineWidth, lineDash, lineCap, fillStyle, strokePath, fillPath, flags, boundsClip, gapsClip) {
3621
let didStrokeFill = false;
3625
bands.forEach((b, bi) => {
3627
if (b.series[0] == si) {
3628
let lowerEdge = series[b.series[1]];
3629
let lowerData = data[b.series[1]];
3631
let bandClip = (lowerEdge._paths || EMPTY_OBJ).band;
3634
let _fillStyle = null;
3637
if (lowerEdge.show && bandClip && hasData(lowerData, i0, i1)) {
3638
_fillStyle = b.fill(self, bi) || fillStyle;
3639
gapsClip2 = lowerEdge._paths.clip;
3644
strokeFill(strokeStyle, lineWidth, lineDash, lineCap, _fillStyle, strokePath, fillPath, flags, boundsClip, gapsClip, gapsClip2, bandClip);
3646
didStrokeFill = true;
3651
strokeFill(strokeStyle, lineWidth, lineDash, lineCap, fillStyle, strokePath, fillPath, flags, boundsClip, gapsClip);
3654
const CLIP_FILL_STROKE = BAND_CLIP_FILL | BAND_CLIP_STROKE;
3656
function strokeFill(strokeStyle, lineWidth, lineDash, lineCap, fillStyle, strokePath, fillPath, flags, boundsClip, gapsClip, gapsClip2, bandClip) {
3657
setCtxStyle(strokeStyle, lineWidth, lineDash, lineCap, fillStyle);
3659
if (boundsClip || gapsClip || bandClip) {
3661
boundsClip && ctx.clip(boundsClip);
3662
gapsClip && ctx.clip(gapsClip);
3666
if ((flags & CLIP_FILL_STROKE) == CLIP_FILL_STROKE) {
3668
gapsClip2 && ctx.clip(gapsClip2);
3669
doFill(fillStyle, fillPath);
3670
doStroke(strokeStyle, strokePath, lineWidth);
3672
else if (flags & BAND_CLIP_STROKE) {
3673
doFill(fillStyle, fillPath);
3675
doStroke(strokeStyle, strokePath, lineWidth);
3677
else if (flags & BAND_CLIP_FILL) {
3680
gapsClip2 && ctx.clip(gapsClip2);
3681
doFill(fillStyle, fillPath);
3683
doStroke(strokeStyle, strokePath, lineWidth);
3687
doFill(fillStyle, fillPath);
3688
doStroke(strokeStyle, strokePath, lineWidth);
3691
if (boundsClip || gapsClip || bandClip)
3695
function doStroke(strokeStyle, strokePath, lineWidth) {
3696
if (lineWidth > 0) {
3697
if (strokePath instanceof Map) {
3698
strokePath.forEach((strokePath, strokeStyle) => {
3699
ctx.strokeStyle = ctxStroke = strokeStyle;
3700
ctx.stroke(strokePath);
3704
strokePath != null && strokeStyle && ctx.stroke(strokePath);
3708
function doFill(fillStyle, fillPath) {
3709
if (fillPath instanceof Map) {
3710
fillPath.forEach((fillPath, fillStyle) => {
3711
ctx.fillStyle = ctxFill = fillStyle;
3716
fillPath != null && fillStyle && ctx.fill(fillPath);
3719
function getIncrSpace(axisIdx, min, max, fullDim) {
3720
let axis = axes[axisIdx];
3727
let minSpace = axis._space = axis.space(self, axisIdx, min, max, fullDim);
3728
let incrs = axis._incrs = axis.incrs(self, axisIdx, min, max, fullDim, minSpace);
3729
incrSpace = findIncr(min, max, incrs, fullDim, minSpace);
3732
return (axis._found = incrSpace);
3735
function drawOrthoLines(offs, filts, ori, side, pos0, len, width, stroke, dash, cap) {
3736
let offset = (width % 2) / 2;
3738
pxAlign == 1 && ctx.translate(offset, offset);
3740
setCtxStyle(stroke, width, dash, cap, stroke);
3744
let x0, y0, x1, y1, pos1 = pos0 + (side == 0 || side == 3 ? -len : len);
3755
for (let i = 0; i < offs.length; i++) {
3756
if (filts[i] != null) {
3769
pxAlign == 1 && ctx.translate(-offset, -offset);
3772
function axesCalc(cycleNum) {
3775
let converged = true;
3777
axes.forEach((axis, i) => {
3781
let scale = scales[axis.scale];
3783
if (scale.min == null) {
3787
resetYSeries(false);
3795
resetYSeries(false);
3799
let side = axis.side;
3802
let {min, max} = scale;
3804
let [_incr, _space] = getIncrSpace(i, min, max, ori == 0 ? plotWidCss : plotHgtCss);
3810
let forceMin = scale.distr == 2;
3812
let _splits = axis._splits = axis.splits(self, i, min, max, _incr, _space, forceMin);
3816
let splits = scale.distr == 2 ? _splits.map(i => data0[i]) : _splits;
3817
let incr = scale.distr == 2 ? data0[_splits[1]] - data0[_splits[0]] : _incr;
3819
let values = axis._values = axis.values(self, axis.filter(self, splits, i, _space, incr), i, _space, incr);
3822
axis._rotate = side == 2 ? axis.rotate(self, values, i, _space) : 0;
3824
let oldSize = axis._size;
3826
axis._size = ceil(axis.size(self, values, i, cycleNum));
3828
if (oldSize != null && axis._size != oldSize)
3835
function paddingCalc(cycleNum) {
3836
let converged = true;
3838
padding.forEach((p, i) => {
3839
let _p = p(self, i, sidesWithAxes, cycleNum);
3841
if (_p != _padding[i])
3850
function drawAxesGrid() {
3851
for (let i = 0; i < axes.length; i++) {
3854
if (!axis.show || !axis._show)
3857
let side = axis.side;
3862
let fillStyle = axis.stroke(self, i);
3864
let shiftDir = side == 0 || side == 3 ? -1 : 1;
3868
let shiftAmt = axis.labelGap * shiftDir;
3869
let baseLpos = round((axis._lpos + shiftAmt) * pxRatio);
3871
setFontStyle(axis.labelFont[0], fillStyle, "center", side == 2 ? TOP : BOTTOM);
3880
round(plotTop + plotHgt / 2),
3882
ctx.rotate((side == 3 ? -PI : PI) / 2);
3886
x = round(plotLft + plotWid / 2);
3890
ctx.fillText(axis.label, x, y);
3895
let [_incr, _space] = axis._found;
3900
let scale = scales[axis.scale];
3902
let plotDim = ori == 0 ? plotWid : plotHgt;
3903
let plotOff = ori == 0 ? plotLft : plotTop;
3905
let axisGap = round(axis.gap * pxRatio);
3907
let _splits = axis._splits;
3911
let splits = scale.distr == 2 ? _splits.map(i => data0[i]) : _splits;
3912
let incr = scale.distr == 2 ? data0[_splits[1]] - data0[_splits[0]] : _incr;
3914
let ticks = axis.ticks;
3915
let tickSize = ticks.show ? round(ticks.size * pxRatio) : 0;
3918
let angle = axis._rotate * -PI/180;
3920
let basePos = pxRound(axis._pos * pxRatio);
3921
let shiftAmt = (tickSize + axisGap) * shiftDir;
3922
let finalPos = basePos + shiftAmt;
3923
y = ori == 0 ? finalPos : 0;
3924
x = ori == 1 ? finalPos : 0;
3926
let font = axis.font[0];
3927
let textAlign = axis.align == 1 ? LEFT :
3928
axis.align == 2 ? RIGHT :
3931
ori == 0 ? "center" : side == 3 ? RIGHT : LEFT;
3932
let textBaseline = angle ||
3933
ori == 1 ? "middle" : side == 2 ? TOP : BOTTOM;
3935
setFontStyle(font, fillStyle, textAlign, textBaseline);
3937
let lineHeight = axis.font[1] * lineMult;
3939
let canOffs = _splits.map(val => pxRound(getPos(val, scale, plotDim, plotOff)));
3941
let _values = axis._values;
3943
for (let i = 0; i < _values.length; i++) {
3944
let val = _values[i];
3954
let _parts = val.indexOf("\n") == -1 ? [val] : val.split(/\n/gm);
3956
for (let j = 0; j < _parts.length; j++) {
3957
let text = _parts[j];
3961
ctx.translate(x, y + j * lineHeight);
3963
ctx.fillText(text, 0, 0);
3967
ctx.fillText(text, x, y + j * lineHeight);
3976
ticks.filter(self, splits, i, _space, incr),
3981
roundDec(ticks.width * pxRatio, 3),
3982
ticks.stroke(self, i),
3989
let grid = axis.grid;
3994
grid.filter(self, splits, i, _space, incr),
3997
ori == 0 ? plotTop : plotLft,
3998
ori == 0 ? plotHgt : plotWid,
3999
roundDec(grid.width * pxRatio, 3),
4000
grid.stroke(self, i),
4010
function resetYSeries(minMax) {
4013
series.forEach((s, i) => {
4023
s.facets.forEach(f => {
4033
let queuedCommit = false;
4036
if (!queuedCommit) {
4038
queuedCommit = true;
4042
function _commit() {
4045
if (shouldSetScales) {
4047
shouldSetScales = false;
4050
if (shouldConvergeSize) {
4052
shouldConvergeSize = false;
4055
if (shouldSetSize) {
4056
setStylePx(under, LEFT, plotLftCss);
4057
setStylePx(under, TOP, plotTopCss);
4058
setStylePx(under, WIDTH, plotWidCss);
4059
setStylePx(under, HEIGHT, plotHgtCss);
4061
setStylePx(over, LEFT, plotLftCss);
4062
setStylePx(over, TOP, plotTopCss);
4063
setStylePx(over, WIDTH, plotWidCss);
4064
setStylePx(over, HEIGHT, plotHgtCss);
4066
setStylePx(wrap, WIDTH, fullWidCss);
4067
setStylePx(wrap, HEIGHT, fullHgtCss);
4071
can.width = round(fullWidCss * pxRatio);
4072
can.height = round(fullHgtCss * pxRatio);
4076
let { _show, _el, _size, _pos, side } = a;
4079
let posOffset = (side === 3 || side === 0 ? _size : 0);
4080
let isVt = side % 2 == 1;
4082
setStylePx(_el, isVt ? "left" : "top", _pos - posOffset);
4083
setStylePx(_el, isVt ? "width" : "height", _size);
4084
setStylePx(_el, isVt ? "top" : "left", isVt ? plotTopCss : plotLftCss);
4085
setStylePx(_el, isVt ? "height" : "width", isVt ? plotHgtCss : plotWidCss);
4087
_el && remClass(_el, OFF);
4090
_el && addClass(_el, OFF);
4094
ctxStroke = ctxFill = ctxWidth = ctxJoin = ctxCap = ctxFont = ctxAlign = ctxBaseline = ctxDash = null;
4101
shouldSetSize = false;
4104
if (fullWidCss > 0 && fullHgtCss > 0) {
4105
ctx.clearRect(0, 0, can.width, can.height);
4107
drawOrder.forEach(fn => fn());
4120
if (cursor.show && shouldSetCursor) {
4121
updateCursor(null, true, false);
4122
shouldSetCursor = false;
4134
viaAutoScaleX = false;
4136
queuedCommit = false;
4139
self.redraw = (rebuildPaths, recalcAxes) => {
4140
shouldConvergeSize = recalcAxes || false;
4142
if (rebuildPaths !== false)
4143
_setScale(xScaleKey, scaleX.min, scaleX.max);
4151
function setScale(key, opts) {
4152
let sc = scales[key];
4154
if (sc.from == null) {
4156
let minMax = sc.range(self, opts.min, opts.max, key);
4157
opts.min = minMax[0];
4158
opts.max = minMax[1];
4161
if (opts.min > opts.max) {
4162
let _min = opts.min;
4163
opts.min = opts.max;
4167
if (dataLen > 1 && opts.min != null && opts.max != null && opts.max - opts.min < 1e-16)
4170
if (key == xScaleKey) {
4171
if (sc.distr == 2 && dataLen > 0) {
4172
opts.min = closestIdx(opts.min, data[0]);
4173
opts.max = closestIdx(opts.max, data[0]);
4175
if (opts.min == opts.max)
4182
pendScales[key] = opts;
4184
shouldSetScales = true;
4189
self.setScale = setScale;
4214
let dragging = false;
4216
const drag = cursor.drag;
4223
xCursor = placeDiv(CURSOR_X, over);
4225
yCursor = placeDiv(CURSOR_Y, over);
4227
if (scaleX.ori == 0) {
4236
mouseLeft1 = cursor.left;
4237
mouseTop1 = cursor.top;
4240
const select = self.select = assign({
4249
const selectDiv = select.show ? placeDiv(SELECT, select.over ? over : under) : null;
4251
function setSelect(opts, _fire) {
4253
for (let prop in opts)
4254
setStylePx(selectDiv, prop, select[prop] = opts[prop]);
4256
_fire !== false && fire("setSelect");
4260
self.setSelect = setSelect;
4262
function toggleDOM(i, onOff) {
4264
let label = showLegend ? legendRows[i] : null;
4267
label && remClass(label, OFF);
4269
label && addClass(label, OFF);
4270
cursorPts.length > 1 && elTrans(cursorPts[i], -10, -10, plotWidCss, plotHgtCss);
4274
function _setScale(key, min, max) {
4275
setScale(key, {min, max});
4278
function setSeries(i, opts, _fire, _pub) {
4283
if (opts.focus != null)
4286
if (opts.show != null) {
4288
toggleDOM(i, opts.show);
4290
_setScale(mode == 2 ? s.facets[1].scale : s.scale, null, null);
4294
_fire !== false && fire("setSeries", i, opts);
4296
_pub && pubSync("setSeries", self, i, opts);
4299
self.setSeries = setSeries;
4301
function setBand(bi, opts) {
4302
assign(bands[bi], opts);
4305
function addBand(opts, bi) {
4306
opts.fill = fnOrSelf(opts.fill || null);
4307
bi = bi == null ? bands.length : bi;
4308
bands.splice(bi, 0, opts);
4311
function delBand(bi) {
4315
bands.splice(bi, 1);
4318
self.addBand = addBand;
4319
self.setBand = setBand;
4320
self.delBand = delBand;
4322
function setAlpha(i, value) {
4323
series[i].alpha = value;
4325
if (cursor.show && cursorPts[i])
4326
cursorPts[i].style.opacity = value;
4328
if (showLegend && legendRows[i])
4329
legendRows[i].style.opacity = value;
4336
const FOCUS_TRUE = {focus: true};
4337
const FOCUS_FALSE = {focus: false};
4339
function setFocus(i) {
4340
if (i != focusedSeries) {
4343
let allFocused = i == null;
4345
let _setAlpha = focus.alpha != 1;
4347
series.forEach((s, i2) => {
4348
let isFocused = allFocused || i2 == 0 || i2 == i;
4349
s._focus = allFocused ? null : isFocused;
4350
_setAlpha && setAlpha(i2, isFocused ? 1 : focus.alpha);
4354
_setAlpha && commit();
4358
if (showLegend && cursorFocus) {
4359
on(mouseleave, legendEl, e => {
4362
setSeries(null, FOCUS_FALSE, true, syncOpts.setSeries);
4363
updateCursor(null, true, false);
4367
function posToVal(pos, scale, can) {
4368
let sc = scales[scale];
4371
pos = pos / pxRatio - (sc.ori == 1 ? plotTopCss : plotLftCss);
4373
let dim = plotWidCss;
4387
let sv = _min + (_max - _min) * pct;
4389
let distr = sc.distr;
4392
distr == 3 ? pow(10, sv) :
4393
distr == 4 ? sinh(sv, sc.asinh) :
4398
function closestIdxFromXpos(pos, can) {
4399
let v = posToVal(pos, xScaleKey, can);
4400
return closestIdx(v, data[0], i0, i1);
4403
self.valToIdx = val => closestIdx(val, data[0]);
4404
self.posToIdx = closestIdxFromXpos;
4405
self.posToVal = posToVal;
4406
self.valToPos = (val, scale, can) => (
4407
scales[scale].ori == 0 ?
4408
getHPos(val, scales[scale],
4409
can ? plotWid : plotWidCss,
4412
getVPos(val, scales[scale],
4413
can ? plotHgt : plotHgtCss,
4419
function batch(fn) {
4426
(self.setCursor = (opts, _fire, _pub) => {
4427
mouseLeft1 = opts.left;
4428
mouseTop1 = opts.top;
4430
updateCursor(null, _fire, _pub);
4433
function setSelH(off, dim) {
4434
setStylePx(selectDiv, LEFT, select.left = off);
4435
setStylePx(selectDiv, WIDTH, select.width = dim);
4438
function setSelV(off, dim) {
4439
setStylePx(selectDiv, TOP, select.top = off);
4440
setStylePx(selectDiv, HEIGHT, select.height = dim);
4443
let setSelX = scaleX.ori == 0 ? setSelH : setSelV;
4444
let setSelY = scaleX.ori == 1 ? setSelH : setSelV;
4446
function syncLegend() {
4447
if (showLegend && legend.live) {
4448
for (let i = mode == 2 ? 1 : 0; i < series.length; i++) {
4449
if (i == 0 && multiValLegend)
4452
let vals = legend.values[i];
4457
legendCells[i][j++].firstChild.nodeValue = vals[k];
4462
function setLegend(opts, _fire) {
4467
series.forEach((s, sidx) => {
4468
(sidx > 0 || !multiValLegend) && setLegendValues(sidx, idx);
4472
if (showLegend && legend.live)
4475
shouldSetLegend = false;
4477
_fire !== false && fire("setLegend");
4480
self.setLegend = setLegend;
4482
function setLegendValues(sidx, idx) {
4486
val = NULL_LEGEND_VALUES;
4488
let s = series[sidx];
4489
let src = sidx == 0 && xScaleDistr == 2 ? data0 : data[sidx];
4490
val = multiValLegend ? s.values(self, sidx, idx) : {_: s.value(self, src[idx], sidx, idx)};
4493
legend.values[sidx] = val;
4496
function updateCursor(src, _fire, _pub) {
4499
rawMouseLeft1 = mouseLeft1;
4500
rawMouseTop1 = mouseTop1;
4502
[mouseLeft1, mouseTop1] = cursor.move(self, mouseLeft1, mouseTop1);
4505
vCursor && elTrans(vCursor, round(mouseLeft1), 0, plotWidCss, plotHgtCss);
4506
hCursor && elTrans(hCursor, 0, round(mouseTop1), plotWidCss, plotHgtCss);
4513
let noDataInRange = i0 > i1;
4518
let xDim = scaleX.ori == 0 ? plotWidCss : plotHgtCss;
4519
let yDim = scaleX.ori == 1 ? plotWidCss : plotHgtCss;
4522
if (mouseLeft1 < 0 || dataLen == 0 || noDataInRange) {
4525
for (let i = 0; i < series.length; i++) {
4527
cursorPts.length > 1 && elTrans(cursorPts[i], -10, -10, plotWidCss, plotHgtCss);
4532
setSeries(null, FOCUS_TRUE, true, src == null && syncOpts.setSeries);
4535
activeIdxs.fill(null);
4536
shouldSetLegend = true;
4538
for (let i = 0; i < series.length; i++)
4539
legend.values[i] = NULL_LEGEND_VALUES;
4545
let mouseXPos, valAtPosX, xPos;
4548
mouseXPos = scaleX.ori == 0 ? mouseLeft1 : mouseTop1;
4549
valAtPosX = posToVal(mouseXPos, xScaleKey);
4550
idx = closestIdx(valAtPosX, data[0], i0, i1);
4551
xPos = incrRoundUp(valToPosX(data[0][idx], scaleX, xDim, 0), 0.5);
4554
for (let i = mode == 2 ? 1 : 0; i < series.length; i++) {
4557
let idx1 = activeIdxs[i];
4558
let yVal1 = mode == 1 ? data[i][idx1] : data[i][1][idx1];
4560
let idx2 = cursor.dataIdx(self, i, idx, valAtPosX);
4561
let yVal2 = mode == 1 ? data[i][idx2] : data[i][1][idx2];
4563
shouldSetLegend = shouldSetLegend || yVal2 != yVal1 || idx2 != idx1;
4565
activeIdxs[i] = idx2;
4567
let xPos2 = idx2 == idx ? xPos : incrRoundUp(valToPosX(mode == 1 ? data[0][idx2] : data[i][0][idx2], scaleX, xDim, 0), 0.5);
4569
if (i > 0 && s.show) {
4570
let yPos = yVal2 == null ? -10 : incrRoundUp(valToPosY(yVal2, mode == 1 ? scales[s.scale] : scales[s.facets[1].scale], yDim, 0), 0.5);
4572
if (yPos > 0 && mode == 1) {
4573
let dist = abs(yPos - mouseTop1);
4575
if (dist <= closestDist) {
4583
if (scaleX.ori == 0) {
4592
if (shouldSetLegend && cursorPts.length > 1) {
4593
elColor(cursorPts[i], cursor.points.fill(self, i), cursor.points.stroke(self, i));
4595
let ptWid, ptHgt, ptLft, ptTop,
4597
getBBox = cursor.points.bbox;
4599
if (getBBox != null) {
4602
let bbox = getBBox(self, i);
4607
ptHgt = bbox.height;
4612
ptWid = ptHgt = cursor.points.size(self, i);
4615
elSize(cursorPts[i], ptWid, ptHgt, centered);
4616
elTrans(cursorPts[i], ptLft, ptTop, plotWidCss, plotHgtCss);
4621
if (!shouldSetLegend || i == 0 && multiValLegend)
4624
setLegendValues(i, idx2);
4630
cursor.left = mouseLeft1;
4631
cursor.top = mouseTop1;
4633
if (shouldSetLegend) {
4639
if (select.show && dragging) {
4641
let [xKey, yKey] = syncOpts.scales;
4642
let [matchXKeys, matchYKeys] = syncOpts.match;
4643
let [xKeySrc, yKeySrc] = src.cursor.sync.scales;
4646
let sdrag = src.cursor.drag;
4650
let { left, top, width, height } = src.select;
4652
let sori = src.scales[xKey].ori;
4653
let sPosToVal = src.posToVal;
4655
let sOff, sDim, sc, a, b;
4657
let matchingX = xKey != null && matchXKeys(xKey, xKeySrc);
4658
let matchingY = yKey != null && matchYKeys(yKey, yKeySrc);
4673
a = valToPosX(sPosToVal(sOff, xKeySrc), sc, xDim, 0);
4674
b = valToPosX(sPosToVal(sOff + sDim, xKeySrc), sc, xDim, 0);
4676
setSelX(min(a,b), abs(b-a));
4698
a = valToPosY(sPosToVal(sOff, yKeySrc), sc, yDim, 0);
4699
b = valToPosY(sPosToVal(sOff + sDim, yKeySrc), sc, yDim, 0);
4701
setSelY(min(a,b), abs(b-a));
4711
let rawDX = abs(rawMouseLeft1 - rawMouseLeft0);
4712
let rawDY = abs(rawMouseTop1 - rawMouseTop0);
4714
if (scaleX.ori == 1) {
4720
dragX = drag.x && rawDX >= drag.dist;
4721
dragY = drag.y && rawDY >= drag.dist;
4727
if (dragX && dragY) {
4728
dragX = rawDX >= uni;
4729
dragY = rawDY >= uni;
4732
if (!dragX && !dragY) {
4740
else if (drag.x && drag.y && (dragX || dragY))
4742
dragX = dragY = true;
4747
if (scaleX.ori == 0) {
4756
setSelX(min(p0, p1), abs(p1 - p0));
4763
if (scaleX.ori == 1) {
4772
setSelY(min(p0, p1), abs(p1 - p0));
4779
if (!dragX && !dragY) {
4791
if (syncKey != null) {
4792
let [xSyncKey, ySyncKey] = syncOpts.scales;
4794
syncOpts.values[0] = xSyncKey != null ? posToVal(scaleX.ori == 0 ? mouseLeft1 : mouseTop1, xSyncKey) : null;
4795
syncOpts.values[1] = ySyncKey != null ? posToVal(scaleX.ori == 1 ? mouseLeft1 : mouseTop1, ySyncKey) : null;
4798
pubSync(mousemove, self, mouseLeft1, mouseTop1, plotWidCss, plotHgtCss, idx);
4802
let shouldPub = _pub && syncOpts.setSeries;
4805
if (focusedSeries == null) {
4806
if (closestDist <= p)
4807
setSeries(closestSeries, FOCUS_TRUE, true, shouldPub);
4810
if (closestDist > p)
4811
setSeries(null, FOCUS_TRUE, true, shouldPub);
4812
else if (closestSeries != focusedSeries)
4813
setSeries(closestSeries, FOCUS_TRUE, true, shouldPub);
4818
ready && _fire !== false && fire("setCursor");
4823
function syncRect(defer) {
4827
rect = over.getBoundingClientRect();
4828
fire("syncRect", rect);
4832
function mouseMove(e, src, _l, _t, _w, _h, _i) {
4836
cacheMouse(e, src, _l, _t, _w, _h, _i, false, e != null);
4839
updateCursor(null, true, true);
4841
updateCursor(src, true, false);
4844
function cacheMouse(e, src, _l, _t, _w, _h, _i, initial, snap) {
4849
_l = e.clientX - rect.left;
4850
_t = e.clientY - rect.top;
4853
if (_l < 0 || _t < 0) {
4859
let [xKey, yKey] = syncOpts.scales;
4861
let syncOptsSrc = src.cursor.sync;
4862
let [xValSrc, yValSrc] = syncOptsSrc.values;
4863
let [xKeySrc, yKeySrc] = syncOptsSrc.scales;
4864
let [matchXKeys, matchYKeys] = syncOpts.match;
4866
let rotSrc = src.scales[xKeySrc].ori == 1;
4868
let xDim = scaleX.ori == 0 ? plotWidCss : plotHgtCss,
4869
yDim = scaleX.ori == 1 ? plotWidCss : plotHgtCss,
4870
_xDim = rotSrc ? _h : _w,
4871
_yDim = rotSrc ? _w : _h,
4872
_xPos = rotSrc ? _t : _l,
4873
_yPos = rotSrc ? _l : _t;
4875
if (xKeySrc != null)
4876
_l = matchXKeys(xKey, xKeySrc) ? getPos(xValSrc, scales[xKey], xDim, 0) : -10;
4878
_l = xDim * (_xPos/_xDim);
4880
if (yKeySrc != null)
4881
_t = matchYKeys(yKey, yKeySrc) ? getPos(yValSrc, scales[yKey], yDim, 0) : -10;
4883
_t = yDim * (_yPos/_yDim);
4885
if (scaleX.ori == 1) {
4893
if (_l <= 1 || _l >= plotWidCss - 1)
4894
_l = incrRound(_l, plotWidCss);
4896
if (_t <= 1 || _t >= plotHgtCss - 1)
4897
_t = incrRound(_t, plotHgtCss);
4904
[mouseLeft0, mouseTop0] = cursor.move(self, _l, _t);
4912
function hideSelect() {
4919
function mouseDown(e, src, _l, _t, _w, _h, _i) {
4921
dragX = dragY = drag._x = drag._y = false;
4923
cacheMouse(e, src, _l, _t, _w, _h, _i, true, false);
4926
onMouse(mouseup, doc, mouseUp);
4927
pubSync(mousedown, self, mouseLeft0, mouseTop0, plotWidCss, plotHgtCss, null);
4931
function mouseUp(e, src, _l, _t, _w, _h, _i) {
4932
dragging = drag._x = drag._y = false;
4934
cacheMouse(e, src, _l, _t, _w, _h, _i, false, true);
4936
let { left, top, width, height } = select;
4938
let hasSelect = width > 0 || height > 0;
4940
hasSelect && setSelect(select);
4942
if (drag.setScale && hasSelect) {
4953
if (scaleX.ori == 1) {
4961
_setScale(xScaleKey,
4962
posToVal(xOff, xScaleKey),
4963
posToVal(xOff + xDim, xScaleKey)
4968
for (let k in scales) {
4971
if (k != xScaleKey && sc.from == null && sc.min != inf) {
4973
posToVal(yOff + yDim, k),
4982
else if (cursor.lock) {
4983
cursor._lock = !cursor._lock;
4986
updateCursor(null, true, false);
4990
offMouse(mouseup, doc);
4991
pubSync(mouseup, self, mouseLeft1, mouseTop1, plotWidCss, plotHgtCss, null);
4995
function mouseLeave(e, src, _l, _t, _w, _h, _i) {
4996
if (!cursor._lock) {
4997
let _dragging = dragging;
5007
if (scaleX.ori == 0) {
5016
if (dragH && dragV) {
5018
snapH = mouseLeft1 <= snapProx || mouseLeft1 >= plotWidCss - snapProx;
5019
snapV = mouseTop1 <= snapProx || mouseTop1 >= plotHgtCss - snapProx;
5023
mouseLeft1 = mouseLeft1 < mouseLeft0 ? 0 : plotWidCss;
5026
mouseTop1 = mouseTop1 < mouseTop0 ? 0 : plotHgtCss;
5028
updateCursor(null, true, true);
5037
updateCursor(null, true, true);
5040
dragging = _dragging;
5044
function dblClick(e, src, _l, _t, _w, _h, _i) {
5050
pubSync(dblclick, self, mouseLeft1, mouseTop1, plotWidCss, plotHgtCss, null);
5053
function syncPxRatio() {
5054
axes.forEach(syncFontSize);
5055
_setSize(self.width, self.height, true);
5058
on(dppxchange, win, syncPxRatio);
5063
events.mousedown = mouseDown;
5064
events.mousemove = mouseMove;
5065
events.mouseup = mouseUp;
5066
events.dblclick = dblClick;
5067
events["setSeries"] = (e, src, idx, opts) => {
5068
setSeries(idx, opts, true, false);
5072
onMouse(mousedown, over, mouseDown);
5073
onMouse(mousemove, over, mouseMove);
5074
onMouse(mouseenter, over, syncRect);
5075
onMouse(mouseleave, over, mouseLeave);
5077
onMouse(dblclick, over, dblClick);
5079
cursorPlots.add(self);
5081
self.syncRect = syncRect;
5085
const hooks = self.hooks = opts.hooks || {};
5087
function fire(evName, a1, a2) {
5088
if (evName in hooks) {
5089
hooks[evName].forEach(fn => {
5090
fn.call(null, self, a1, a2);
5095
(opts.plugins || []).forEach(p => {
5096
for (let evName in p.hooks)
5097
hooks[evName] = (hooks[evName] || []).concat(p.hooks[evName]);
5100
const syncOpts = assign({
5107
scales: [xScaleKey, series[1] ? series[1].scale : null],
5108
match: [retEq, retEq],
5109
values: [null, null],
5112
(cursor.sync = syncOpts);
5114
const syncKey = syncOpts.key;
5116
const sync = _sync(syncKey);
5118
function pubSync(type, src, x, y, w, h, i) {
5119
if (syncOpts.filters.pub(type, src, x, y, w, h, i))
5120
sync.pub(type, src, x, y, w, h, i);
5125
function pub(type, src, x, y, w, h, i) {
5126
if (syncOpts.filters.sub(type, src, x, y, w, h, i))
5127
events[type](null, src, x, y, w, h, i);
5132
function destroy() {
5134
cursorPlots.delete(self);
5135
mouseListeners.clear();
5136
off(dppxchange, win, syncPxRatio);
5141
self.destroy = destroy;
5144
fire("init", opts, data);
5146
setData(data || opts.data, false);
5148
if (pendScales[xScaleKey])
5149
setScale(xScaleKey, pendScales[xScaleKey]);
5153
_setSize(opts.width, opts.height);
5155
updateCursor(null, true, false);
5157
setSelect(select, false);
5160
series.forEach(initSeries);
5162
axes.forEach(initAxis);
5165
if (then instanceof HTMLElement) {
5166
then.appendChild(root);
5178
uPlot.assign = assign;
5179
uPlot.fmtNum = fmtNum;
5180
uPlot.rangeNum = rangeNum;
5181
uPlot.rangeLog = rangeLog;
5182
uPlot.rangeAsinh = rangeAsinh;
5183
uPlot.orient = orient;
5190
uPlot.fmtDate = fmtDate;
5191
uPlot.tzDate = tzDate;
5199
uPlot.addGap = addGap;
5200
uPlot.clipGaps = clipGaps;
5202
let paths = uPlot.paths = {
5206
(paths.linear = linear);
5207
(paths.stepped = stepped);
5208
(paths.bars = bars);
5209
(paths.spline = monotoneCubic);
5212
module.exports = uPlot;