10
var uPlot = (function () {
13
const FEAT_TIME = true;
16
function closestIdx(num, arr, lo, hi) {
19
hi = hi || arr.length - 1;
20
let bitwise = hi <= 2147483647;
23
mid = bitwise ? (lo + hi) >> 1 : floor((lo + hi) / 2);
31
if (num - arr[lo] <= arr[hi] - num)
37
function nonNullIdx(data, _i0, _i1, dir) {
38
for (let i = dir == 1 ? _i0 : _i1; i >= _i0 && i <= _i1; i += dir) {
46
function getMinMax(data, _i0, _i1, sorted) {
56
else if (sorted == -1) {
61
for (let i = _i0; i <= _i1; i++) {
62
if (data[i] != null) {
63
_min = min(_min, data[i]);
64
_max = max(_max, data[i]);
72
function getMinMaxLog(data, _i0, _i1) {
78
for (let i = _i0; i <= _i1; i++) {
80
_min = min(_min, data[i]);
81
_max = max(_max, data[i]);
86
_min == inf ? 1 : _min,
87
_max == -inf ? 10 : _max,
91
const _fixedTuple = [0, 0];
93
function fixIncr(minIncr, maxIncr, minExp, maxExp) {
94
_fixedTuple[0] = minExp < 0 ? roundDec(minIncr, -minExp) : minIncr;
95
_fixedTuple[1] = maxExp < 0 ? roundDec(maxIncr, -maxExp) : maxIncr;
99
function rangeLog(min, max, base, fullMags) {
100
let minSign = sign(min);
102
let logFn = base == 10 ? log10 : log2;
115
let minExp, maxExp, minMaxIncrs;
118
minExp = floor(logFn(min));
119
maxExp = ceil(logFn(max));
121
minMaxIncrs = fixIncr(pow(base, minExp), pow(base, maxExp), minExp, maxExp);
123
min = minMaxIncrs[0];
124
max = minMaxIncrs[1];
127
minExp = floor(logFn(abs(min)));
128
maxExp = floor(logFn(abs(max)));
130
minMaxIncrs = fixIncr(pow(base, minExp), pow(base, maxExp), minExp, maxExp);
132
min = incrRoundDn(min, minMaxIncrs[0]);
133
max = incrRoundUp(max, minMaxIncrs[1]);
139
function rangeAsinh(min, max, base, fullMags) {
140
let minMax = rangeLog(min, max, base, fullMags);
151
const rangePad = 0.1;
153
const autoRangePart = {
158
const _eqRangePart = {
171
function rangeNum(_min, _max, mult, extra) {
173
return _rangeNum(_min, _max, mult);
175
_eqRangePart.pad = mult;
176
_eqRangePart.soft = extra ? 0 : null;
177
_eqRangePart.mode = extra ? 3 : 0;
179
return _rangeNum(_min, _max, _eqRange);
183
function ifNull(lh, rh) {
184
return lh == null ? rh : lh;
189
function hasData(data, idx0, idx1) {
190
idx0 = ifNull(idx0, 0);
191
idx1 = ifNull(idx1, data.length - 1);
193
while (idx0 <= idx1) {
194
if (data[idx0] != null)
202
function _rangeNum(_min, _max, cfg) {
206
let padMin = ifNull(cmin.pad, 0);
207
let padMax = ifNull(cmax.pad, 0);
209
let hardMin = ifNull(cmin.hard, -inf);
210
let hardMax = ifNull(cmax.hard, inf);
212
let softMin = ifNull(cmin.soft, inf);
213
let softMax = ifNull(cmax.soft, -inf);
215
let softMinMode = ifNull(cmin.mode, 0);
216
let softMaxMode = ifNull(cmax.mode, 0);
218
let delta = _max - _min;
231
if (_min == 0 || _max == 0) {
234
if (softMinMode == 2 && softMin != inf)
237
if (softMaxMode == 2 && softMax != -inf)
242
let nonZeroDelta = delta || abs(_max) || 1e3;
243
let mag = log10(nonZeroDelta);
244
let base = pow(10, floor(mag));
246
let _padMin = nonZeroDelta * (delta == 0 ? (_min == 0 ? .1 : 1) : padMin);
247
let _newMin = roundDec(incrRoundDn(_min - _padMin, base/10), 9);
248
let _softMin = _min >= softMin && (softMinMode == 1 || softMinMode == 3 && _newMin <= softMin || softMinMode == 2 && _newMin >= softMin) ? softMin : inf;
249
let minLim = max(hardMin, _newMin < _softMin && _min >= _softMin ? _softMin : min(_softMin, _newMin));
251
let _padMax = nonZeroDelta * (delta == 0 ? (_max == 0 ? .1 : 1) : padMax);
252
let _newMax = roundDec(incrRoundUp(_max + _padMax, base/10), 9);
253
let _softMax = _max <= softMax && (softMaxMode == 1 || softMaxMode == 3 && _newMax >= softMax || softMaxMode == 2 && _newMax <= softMax) ? softMax : -inf;
254
let maxLim = min(hardMax, _newMax > _softMax && _max <= _softMax ? _softMax : max(_softMax, _newMax));
256
if (minLim == maxLim && minLim == 0)
259
return [minLim, maxLim];
263
const fmtNum = new Intl.NumberFormat(navigator.language).format;
269
const floor = M.floor;
270
const round = M.round;
276
const log10 = M.log10;
279
const sinh = (v, linthresh = 1) => M.sinh(v) * linthresh;
280
const asinh = (v, linthresh = 1) => M.asinh(v / linthresh);
282
const inf = Infinity;
284
function numIntDigits(x) {
285
return (log10((x ^ (x >> 31)) - (x >> 31)) | 0) + 1;
288
function incrRound(num, incr) {
289
return round(num/incr)*incr;
292
function clamp(num, _min, _max) {
293
return min(max(num, _min), _max);
296
function fnOrSelf(v) {
297
return typeof v == "function" ? v : () => v;
300
const retArg0 = _0 => _0;
302
const retArg1 = (_0, _1) => _1;
304
const retNull = _ => null;
306
const retTrue = _ => true;
308
const retEq = (a, b) => a == b;
310
function incrRoundUp(num, incr) {
311
return ceil(num/incr)*incr;
314
function incrRoundDn(num, incr) {
315
return floor(num/incr)*incr;
318
function roundDec(val, dec) {
319
return round(val * (dec = 10**dec)) / dec;
322
const fixedDec = new Map();
324
function guessDec(num) {
325
return ((""+num).split(".")[1] || "").length;
328
function genIncrs(base, minExp, maxExp, mults) {
331
let multDec = mults.map(guessDec);
333
for (let exp = minExp; exp < maxExp; exp++) {
335
let mag = roundDec(pow(base, exp), expa);
337
for (let i = 0; i < mults.length; i++) {
338
let _incr = mults[i] * mag;
339
let dec = (_incr >= 0 && exp >= 0 ? 0 : expa) + (exp >= multDec[i] ? 0 : multDec[i]);
340
let incr = roundDec(_incr, dec);
342
fixedDec.set(incr, dec);
351
const EMPTY_OBJ = {};
352
const EMPTY_ARR = [];
354
const nullNullTuple = [null, null];
356
const isArr = Array.isArray;
359
return typeof v == 'string';
366
let c = v.constructor;
367
is = c == null || c == Object;
373
function fastIsObj(v) {
374
return v != null && typeof v == 'object';
377
function copy(o, _isObj = isObj) {
381
let val = o.find(v => v != null);
383
if (isArr(val) || _isObj(val)) {
384
out = Array(o.length);
385
for (let i = 0; i < o.length; i++)
386
out[i] = copy(o[i], _isObj);
391
else if (_isObj(o)) {
394
out[k] = copy(o[k], _isObj);
402
function assign(targ) {
403
let args = arguments;
405
for (let i = 1; i < args.length; i++) {
408
for (let key in src) {
409
if (isObj(targ[key]))
410
assign(targ[key], copy(src[key]));
412
targ[key] = copy(src[key]);
420
const NULL_REMOVE = 0;
421
const NULL_RETAIN = 1;
422
const NULL_EXPAND = 2;
425
function nullExpand(yVals, nullIdxs, alignedLen) {
426
for (let i = 0, xi, lastNullIdx = -1; i < nullIdxs.length; i++) {
427
let nullIdx = nullIdxs[i];
429
if (nullIdx > lastNullIdx) {
431
while (xi >= 0 && yVals[xi] == null)
435
while (xi < alignedLen && yVals[xi] == null)
436
yVals[lastNullIdx = xi++] = null;
443
function join(tables, nullModes) {
444
let xVals = new Set();
446
for (let ti = 0; ti < tables.length; ti++) {
451
for (let i = 0; i < len; i++)
455
let data = [Array.from(xVals).sort((a, b) => a - b)];
457
let alignedLen = data[0].length;
459
let xIdxs = new Map();
461
for (let i = 0; i < alignedLen; i++)
462
xIdxs.set(data[0][i], i);
464
for (let ti = 0; ti < tables.length; ti++) {
468
for (let si = 1; si < t.length; si++) {
471
let yVals = Array(alignedLen).fill(undefined);
473
let nullMode = nullModes ? nullModes[ti][si] : NULL_RETAIN;
477
for (let i = 0; i < ys.length; i++) {
479
let alignedIdx = xIdxs.get(xs[i]);
482
if (nullMode != NULL_REMOVE) {
483
yVals[alignedIdx] = yVal;
485
if (nullMode == NULL_EXPAND)
486
nullIdxs.push(alignedIdx);
490
yVals[alignedIdx] = yVal;
493
nullExpand(yVals, nullIdxs, alignedLen);
502
const microTask = typeof queueMicrotask == "undefined" ? fn => Promise.resolve().then(fn) : queueMicrotask;
504
const WIDTH = "width";
505
const HEIGHT = "height";
507
const BOTTOM = "bottom";
509
const RIGHT = "right";
510
const hexBlack = "#000";
511
const transparent = hexBlack + "0";
513
const mousemove = "mousemove";
514
const mousedown = "mousedown";
515
const mouseup = "mouseup";
516
const mouseenter = "mouseenter";
517
const mouseleave = "mouseleave";
518
const dblclick = "dblclick";
519
const resize = "resize";
520
const scroll = "scroll";
522
const change = "change";
523
const dppxchange = "dppxchange";
527
const UPLOT = "uplot";
528
const ORI_HZ = pre + "hz";
529
const ORI_VT = pre + "vt";
530
const TITLE = pre + "title";
531
const WRAP = pre + "wrap";
532
const UNDER = pre + "under";
533
const OVER = pre + "over";
534
const AXIS = pre + "axis";
535
const OFF = pre + "off";
536
const SELECT = pre + "select";
537
const CURSOR_X = pre + "cursor-x";
538
const CURSOR_Y = pre + "cursor-y";
539
const CURSOR_PT = pre + "cursor-pt";
540
const LEGEND = pre + "legend";
541
const LEGEND_LIVE = pre + "live";
542
const LEGEND_INLINE = pre + "inline";
543
const LEGEND_THEAD = pre + "thead";
544
const LEGEND_SERIES = pre + "series";
545
const LEGEND_MARKER = pre + "marker";
546
const LEGEND_LABEL = pre + "label";
547
const LEGEND_VALUE = pre + "value";
549
const doc = document;
555
function setPxRatio() {
556
let _pxRatio = devicePixelRatio;
559
if (pxRatio != _pxRatio) {
562
query && off(change, query, setPxRatio);
563
query = matchMedia(`(min-resolution: ${pxRatio - 0.001}dppx) and (max-resolution: ${pxRatio + 0.001}dppx)`);
564
on(change, query, setPxRatio);
566
win.dispatchEvent(new CustomEvent(dppxchange));
570
function addClass(el, c) {
572
let cl = el.classList;
573
!cl.contains(c) && cl.add(c);
577
function remClass(el, c) {
578
let cl = el.classList;
579
cl.contains(c) && cl.remove(c);
582
function setStylePx(el, name, value) {
583
el.style[name] = value + "px";
586
function placeTag(tag, cls, targ, refEl) {
587
let el = doc.createElement(tag);
593
targ.insertBefore(el, refEl);
598
function placeDiv(cls, targ) {
599
return placeTag("div", cls, targ);
602
const xformCache = new WeakMap();
604
function elTrans(el, xPos, yPos, xMax, yMax) {
605
let xform = "translate(" + xPos + "px," + yPos + "px)";
606
let xformOld = xformCache.get(el);
608
if (xform != xformOld) {
609
el.style.transform = xform;
610
xformCache.set(el, xform);
612
if (xPos < 0 || yPos < 0 || xPos > xMax || yPos > yMax)
619
const colorCache = new WeakMap();
621
function elColor(el, background, borderColor) {
622
let newColor = background + borderColor;
623
let oldColor = colorCache.get(el);
625
if (newColor != oldColor) {
626
colorCache.set(el, newColor);
627
el.style.background = background;
628
el.style.borderColor = borderColor;
632
const sizeCache = new WeakMap();
634
function elSize(el, newWid, newHgt, centered) {
635
let newSize = newWid + "" + newHgt;
636
let oldSize = sizeCache.get(el);
638
if (newSize != oldSize) {
639
sizeCache.set(el, newSize);
640
el.style.height = newHgt + "px";
641
el.style.width = newWid + "px";
642
el.style.marginLeft = centered ? -newWid/2 + "px" : 0;
643
el.style.marginTop = centered ? -newHgt/2 + "px" : 0;
647
const evOpts = {passive: true};
648
const evOpts2 = assign({capture: true}, evOpts);
650
function on(ev, el, cb, capt) {
651
el.addEventListener(ev, cb, capt ? evOpts2 : evOpts);
654
function off(ev, el, cb, capt) {
655
el.removeEventListener(ev, cb, capt ? evOpts2 : evOpts);
685
function slice3(str) {
686
return str.slice(0, 3);
689
const days3 = days.map(slice3);
691
const months3 = months.map(slice3);
700
function zeroPad2(int) {
701
return (int < 10 ? '0' : '') + int;
704
function zeroPad3(int) {
705
return (int < 10 ? '00' : int < 100 ? '0' : '') + int;
722
YYYY: d => d.getFullYear(),
724
YY: d => (d.getFullYear()+'').slice(2),
726
MMMM: (d, names) => names.MMMM[d.getMonth()],
728
MMM: (d, names) => names.MMM[d.getMonth()],
730
MM: d => zeroPad2(d.getMonth()+1),
732
M: d => d.getMonth()+1,
734
DD: d => zeroPad2(d.getDate()),
738
WWWW: (d, names) => names.WWWW[d.getDay()],
740
WWW: (d, names) => names.WWW[d.getDay()],
742
HH: d => zeroPad2(d.getHours()),
744
H: d => d.getHours(),
746
h: d => {let h = d.getHours(); return h == 0 ? 12 : h > 12 ? h - 12 : h;},
748
AA: d => d.getHours() >= 12 ? 'PM' : 'AM',
750
aa: d => d.getHours() >= 12 ? 'pm' : 'am',
752
a: d => d.getHours() >= 12 ? 'p' : 'a',
754
mm: d => zeroPad2(d.getMinutes()),
756
m: d => d.getMinutes(),
758
ss: d => zeroPad2(d.getSeconds()),
760
s: d => d.getSeconds(),
762
fff: d => zeroPad3(d.getMilliseconds()),
765
function fmtDate(tpl, names) {
766
names = names || engNames;
769
let R = /\{([a-z]+)\}|[^{]+/gi, m;
771
while (m = R.exec(tpl))
772
parts.push(m[0][0] == '{' ? subs[m[1]] : m[0]);
777
for (let i = 0; i < parts.length; i++)
778
out += typeof parts[i] == "string" ? parts[i] : parts[i](d, names);
784
const localTz = new Intl.DateTimeFormat().resolvedOptions().timeZone;
787
function tzDate(date, tz) {
791
if (tz == 'UTC' || tz == 'Etc/UTC')
792
date2 = new Date(+date + date.getTimezoneOffset() * 6e4);
793
else if (tz == localTz)
796
date2 = new Date(date.toLocaleString('en-US', {timeZone: tz}));
797
date2.setMilliseconds(date.getMilliseconds());
807
const onlyWhole = v => v % 1 == 0;
809
const allMults = [1,2,2.5,5];
812
const decIncrs = genIncrs(10, -16, 0, allMults);
815
const oneIncrs = genIncrs(10, 0, 16, allMults);
818
const wholeIncrs = oneIncrs.filter(onlyWhole);
820
const numIncrs = decIncrs.concat(oneIncrs);
824
const yyyy = "{YYYY}";
825
const NLyyyy = NL + yyyy;
826
const md = "{M}/{D}";
827
const NLmd = NL + md;
828
const NLmdyy = NLmd + "/{YY}";
831
const hmm = "{h}:{mm}";
832
const hmmaa = hmm + aa;
833
const NLhmmaa = NL + hmmaa;
838
function genTimeStuffs(ms) {
847
let subSecIncrs = ms == 1 ? genIncrs(10, 0, 3, allMults).filter(onlyWhole) : genIncrs(10, -3, 0, allMults);
849
let timeIncrs = subSecIncrs.concat([
902
const _timeAxisStamps = [
904
[y, yyyy, _, _, _, _, _, _, 1],
905
[d * 28, "{MMM}", NLyyyy, _, _, _, _, _, 1],
906
[d, md, NLyyyy, _, _, _, _, _, 1],
907
[h, "{h}" + aa, NLmdyy, _, NLmd, _, _, _, 1],
908
[m, hmmaa, NLmdyy, _, NLmd, _, _, _, 1],
909
[s, ss, NLmdyy + " " + hmmaa, _, NLmd + " " + hmmaa, _, NLhmmaa, _, 1],
910
[ms, ss + ".{fff}", NLmdyy + " " + hmmaa, _, NLmd + " " + hmmaa, _, NLhmmaa, _, 1],
917
function timeAxisSplits(tzDate) {
918
return (self, axisIdx, scaleMin, scaleMax, foundIncr, foundSpace) => {
920
let isYr = foundIncr >= y;
921
let isMo = foundIncr >= mo && foundIncr < y;
924
let minDate = tzDate(scaleMin);
925
let minDateTs = roundDec(minDate * ms, 3);
928
let minMin = mkDate(minDate.getFullYear(), isYr ? 0 : minDate.getMonth(), isMo || isYr ? 1 : minDate.getDate());
929
let minMinTs = roundDec(minMin * ms, 3);
932
let moIncr = isMo ? foundIncr / mo : 0;
933
let yrIncr = isYr ? foundIncr / y : 0;
935
let split = minDateTs == minMinTs ? minDateTs : roundDec(mkDate(minMin.getFullYear() + yrIncr, minMin.getMonth() + moIncr, 1) * ms, 3);
936
let splitDate = new Date(round(split / ms));
937
let baseYear = splitDate.getFullYear();
938
let baseMonth = splitDate.getMonth();
940
for (let i = 0; split <= scaleMax; i++) {
941
let next = mkDate(baseYear + yrIncr * i, baseMonth + moIncr * i, 1);
942
let offs = next - tzDate(roundDec(next * ms, 3));
944
split = roundDec((+next + offs) * ms, 3);
946
if (split <= scaleMax)
951
let incr0 = foundIncr >= d ? d : foundIncr;
952
let tzOffset = floor(scaleMin) - floor(minDateTs);
953
let split = minMinTs + tzOffset + incrRoundUp(minDateTs - minMinTs, incr0);
956
let date0 = tzDate(split);
958
let prevHour = date0.getHours() + (date0.getMinutes() / m) + (date0.getSeconds() / h);
959
let incrHours = foundIncr / h;
961
let minSpace = self.axes[axisIdx]._space;
962
let pctSpace = foundSpace / minSpace;
965
split = roundDec(split + foundIncr, ms == 1 ? 0 : 3);
967
if (split > scaleMax)
971
let expectedHour = floor(roundDec(prevHour + incrHours, 6)) % 24;
972
let splitDate = tzDate(split);
973
let actualHour = splitDate.getHours();
975
let dstShift = actualHour - expectedHour;
980
split -= dstShift * h;
982
prevHour = (prevHour + incrHours) % 24;
985
let prevSplit = splits[splits.length - 1];
986
let pctIncr = roundDec((split - prevSplit) / foundIncr, 3);
988
if (pctIncr * pctSpace >= .7)
1007
const [ timeIncrsMs, _timeAxisStampsMs, timeAxisSplitsMs ] = genTimeStuffs(1);
1008
const [ timeIncrsS, _timeAxisStampsS, timeAxisSplitsS ] = genTimeStuffs(1e-3);
1011
genIncrs(2, -53, 53, [1]);
1024
function timeAxisStamps(stampCfg, fmtDate) {
1025
return stampCfg.map(s => s.map((v, i) =>
1026
i == 0 || i == 8 || v == null ? v : fmtDate(i == 1 || s[8] == 0 ? v : s[1] + v)
1032
function timeAxisVals(tzDate, stamps) {
1033
return (self, splits, axisIdx, foundSpace, foundIncr) => {
1034
let s = stamps.find(s => foundIncr >= s[0]) || stamps[stamps.length - 1];
1044
return splits.map(split => {
1045
let date = tzDate(split);
1047
let newYear = date.getFullYear();
1048
let newMnth = date.getMonth();
1049
let newDate = date.getDate();
1050
let newHour = date.getHours();
1051
let newMins = date.getMinutes();
1052
let newSecs = date.getSeconds();
1055
newYear != prevYear && s[2] ||
1056
newMnth != prevMnth && s[3] ||
1057
newDate != prevDate && s[4] ||
1058
newHour != prevHour && s[5] ||
1059
newMins != prevMins && s[6] ||
1060
newSecs != prevSecs && s[7] ||
1077
function timeAxisVal(tzDate, dateTpl) {
1078
let stamp = fmtDate(dateTpl);
1079
return (self, splits, axisIdx, foundSpace, foundIncr) => splits.map(split => stamp(tzDate(split)));
1082
function mkDate(y, m, d) {
1083
return new Date(y, m, d);
1086
function timeSeriesStamp(stampCfg, fmtDate) {
1087
return fmtDate(stampCfg);
1089
const _timeSeriesStamp = '{YYYY}-{MM}-{DD} {h}:{mm}{aa}';
1091
function timeSeriesVal(tzDate, stamp) {
1092
return (self, val) => stamp(tzDate(val));
1095
function legendStroke(self, seriesIdx) {
1096
let s = self.series[seriesIdx];
1097
return s.width ? s.stroke(self, seriesIdx) : s.points.width ? s.points.stroke(self, seriesIdx) : null;
1100
function legendFill(self, seriesIdx) {
1101
return self.series[seriesIdx].fill(self, seriesIdx);
1104
const legendOpts = {
1111
stroke: legendStroke,
1120
function cursorPointShow(self, si) {
1121
let o = self.cursor.points;
1123
let pt = placeDiv();
1125
let size = o.size(self, si);
1126
setStylePx(pt, WIDTH, size);
1127
setStylePx(pt, HEIGHT, size);
1129
let mar = size / -2;
1130
setStylePx(pt, "marginLeft", mar);
1131
setStylePx(pt, "marginTop", mar);
1133
let width = o.width(self, si, size);
1134
width && setStylePx(pt, "borderWidth", width);
1139
function cursorPointFill(self, si) {
1140
let sp = self.series[si].points;
1141
return sp._fill || sp._stroke;
1144
function cursorPointStroke(self, si) {
1145
let sp = self.series[si].points;
1146
return sp._stroke || sp._fill;
1149
function cursorPointSize(self, si) {
1150
let sp = self.series[si].points;
1151
return ptDia(sp.width, 1);
1154
function dataIdx(self, seriesIdx, cursorIdx) {
1158
const moveTuple = [0,0];
1160
function cursorMove(self, mouseLeft1, mouseTop1) {
1161
moveTuple[0] = mouseLeft1;
1162
moveTuple[1] = mouseTop1;
1166
function filtBtn0(self, targ, handle) {
1168
e.button == 0 && handle(e);
1172
function passThru(self, targ, handle) {
1176
const cursorOpts = {
1183
show: cursorPointShow,
1184
size: cursorPointSize,
1186
stroke: cursorPointStroke,
1187
fill: cursorPointFill,
1191
mousedown: filtBtn0,
1196
mousemove: passThru,
1197
mouseleave: passThru,
1198
mouseenter: passThru,
1224
stroke: "rgba(0,0,0,0.07)",
1230
const ticks = assign({}, grid, {size: 10});
1232
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"';
1233
const labelFont = "bold " + font;
1234
const lineMult = 1.5;
1257
const numSeriesLabel = "Value";
1258
const timeSeriesLabel = "Time";
1260
const xSeriesOpts = {
1274
function numAxisVals(self, splits, axisIdx, foundSpace, foundIncr) {
1275
return splits.map(v => v == null ? "" : fmtNum(v));
1278
function numAxisSplits(self, axisIdx, scaleMin, scaleMax, foundIncr, foundSpace, forceMin) {
1281
let numDec = fixedDec.get(foundIncr) || 0;
1283
scaleMin = forceMin ? scaleMin : roundDec(incrRoundUp(scaleMin, foundIncr), numDec);
1285
for (let val = scaleMin; val <= scaleMax; val = roundDec(val + foundIncr, numDec))
1286
splits.push(Object.is(val, -0) ? 0 : val);
1292
function logAxisSplits(self, axisIdx, scaleMin, scaleMax, foundIncr, foundSpace, forceMin) {
1295
const logBase = self.scales[self.axes[axisIdx].scale].log;
1297
const logFn = logBase == 10 ? log10 : log2;
1299
const exp = floor(logFn(scaleMin));
1301
foundIncr = pow(logBase, exp);
1304
foundIncr = roundDec(foundIncr, -exp);
1306
let split = scaleMin;
1310
split = roundDec(split + foundIncr, fixedDec.get(foundIncr));
1312
if (split >= foundIncr * logBase)
1315
} while (split <= scaleMax);
1320
function asinhAxisSplits(self, axisIdx, scaleMin, scaleMax, foundIncr, foundSpace, forceMin) {
1321
let sc = self.scales[self.axes[axisIdx].scale];
1323
let linthresh = sc.asinh;
1325
let posSplits = scaleMax > linthresh ? logAxisSplits(self, axisIdx, max(linthresh, scaleMin), scaleMax, foundIncr) : [linthresh];
1326
let zero = scaleMax >= 0 && scaleMin <= 0 ? [0] : [];
1327
let negSplits = scaleMin < -linthresh ? logAxisSplits(self, axisIdx, max(linthresh, -scaleMax), -scaleMin, foundIncr): [linthresh];
1329
return negSplits.reverse().map(v => -v).concat(zero, posSplits);
1333
const RE_12357 = /[12357]/;
1334
const RE_125 = /[125]/;
1337
function logAxisValsFilt(self, splits, axisIdx, foundSpace, foundIncr) {
1338
let axis = self.axes[axisIdx];
1339
let scaleKey = axis.scale;
1340
let sc = self.scales[scaleKey];
1342
if (sc.distr == 3 && sc.log == 2)
1345
let valToPos = self.valToPos;
1347
let minSpace = axis._space;
1349
let _10 = valToPos(10, scaleKey);
1352
valToPos(9, scaleKey) - _10 >= minSpace ? RE_ALL :
1353
valToPos(7, scaleKey) - _10 >= minSpace ? RE_12357 :
1354
valToPos(5, scaleKey) - _10 >= minSpace ? RE_125 :
1358
return splits.map(v => ((sc.distr == 4 && v == 0) || re.test(v)) ? v : null);
1361
function numSeriesVal(self, val) {
1362
return val == null ? "" : fmtNum(val);
1387
function ptDia(width, mult) {
1388
let dia = 3 + (width || 1) * 2;
1389
return roundDec(dia * mult, 3);
1392
function seriesPointsShow(self, si) {
1393
let { scale, idxs } = self.series[0];
1394
let xData = self._data[0];
1395
let p0 = self.valToPos(xData[idxs[0]], scale, true);
1396
let p1 = self.valToPos(xData[idxs[1]], scale, true);
1397
let dim = abs(p1 - p0);
1399
let s = self.series[si];
1401
let maxPts = dim / (s.points.space * pxRatio);
1402
return idxs[1] - idxs[0] <= maxPts;
1405
function seriesFillTo(self, seriesIdx, dataMin, dataMax) {
1406
let scale = self.scales[self.series[seriesIdx].scale];
1407
let isUpperBandEdge = self.bands && self.bands.some(b => b.series[0] == seriesIdx);
1408
return scale.distr == 3 || isUpperBandEdge ? scale.min : 0;
1420
const xySeriesOpts = {
1426
assign({}, facet, {scale: 'x'}),
1427
assign({}, facet, {scale: 'y'}),
1431
const ySeriesOpts = {
1437
gaps: (self, seriesIdx, idx0, idx1, nullGaps) => nullGaps,
1440
show: seriesPointsShow,
1461
function clampScale(self, val, scaleMin, scaleMax, scaleKey) {
1470
return scaleMin / 10;
1473
const xScaleOpts = {
1485
const yScaleOpts = assign({}, xScaleOpts, {
1492
function _sync(key, opts) {
1503
s.plots = s.plots.filter(c => c != plot);
1505
pub(type, self, x, y, w, h, i) {
1506
for (let j = 0; j < s.plots.length; j++)
1507
s.plots[j] != self && s.plots[j].pub(type, self, x, y, w, h, i);
1518
const BAND_CLIP_FILL = 1 << 0;
1519
const BAND_CLIP_STROKE = 1 << 1;
1521
function orient(u, seriesIdx, cb) {
1522
const series = u.series[seriesIdx];
1523
const scales = u.scales;
1524
const bbox = u.bbox;
1525
const scaleX = u.mode == 2 ? scales[series.facets[0].scale] : scales[u.series[0].scale];
1527
let dx = u._data[0],
1528
dy = u._data[seriesIdx],
1530
sy = u.mode == 2 ? scales[series.facets[1].scale] : scales[series.scale],
1579
function clipBandLine(self, seriesIdx, idx0, idx1, strokePath) {
1580
return orient(self, seriesIdx, (series, dataX, dataY, scaleX, scaleY, valToPosX, valToPosY, xOff, yOff, xDim, yDim) => {
1581
let pxRound = series.pxRound;
1583
const dir = scaleX.dir * (scaleX.ori == 0 ? 1 : -1);
1584
const lineTo = scaleX.ori == 0 ? lineToH : lineToV;
1598
let x0 = pxRound(valToPosX(dataX[frIdx], scaleX, xDim, xOff));
1599
let y0 = pxRound(valToPosY(dataY[frIdx], scaleY, yDim, yOff));
1601
let x1 = pxRound(valToPosX(dataX[toIdx], scaleX, xDim, xOff));
1603
let yLimit = pxRound(valToPosY(scaleY.max, scaleY, yDim, yOff));
1605
let clip = new Path2D(strokePath);
1607
lineTo(clip, x1, yLimit);
1608
lineTo(clip, x0, yLimit);
1609
lineTo(clip, x0, y0);
1615
function clipGaps(gaps, ori, plotLft, plotTop, plotWid, plotHgt) {
1619
if (gaps.length > 0) {
1620
clip = new Path2D();
1622
const rect = ori == 0 ? rectH : rectV;
1624
let prevGapEnd = plotLft;
1626
for (let i = 0; i < gaps.length; i++) {
1630
let w = g[0] - prevGapEnd;
1632
w > 0 && rect(clip, prevGapEnd, plotTop, w, plotTop + plotHgt);
1638
let w = plotLft + plotWid - prevGapEnd;
1640
w > 0 && rect(clip, prevGapEnd, plotTop, w, plotTop + plotHgt);
1646
function addGap(gaps, fromX, toX) {
1647
let prevGap = gaps[gaps.length - 1];
1649
if (prevGap && prevGap[0] == fromX)
1652
gaps.push([fromX, toX]);
1655
function pxRoundGen(pxAlign) {
1656
return pxAlign == 0 ? retArg0 : pxAlign == 1 ? round : v => incrRound(v, pxAlign);
1659
function rect(ori) {
1660
let moveTo = ori == 0 ?
1664
let arcTo = ori == 0 ?
1665
(p, x1, y1, x2, y2, r) => { p.arcTo(x1, y1, x2, y2, r); } :
1666
(p, y1, x1, y2, x2, r) => { p.arcTo(x1, y1, x2, y2, r); };
1668
let rect = ori == 0 ?
1669
(p, x, y, w, h) => { p.rect(x, y, w, h); } :
1670
(p, y, x, h, w) => { p.rect(x, y, w, h); };
1672
return (p, x, y, w, h, r = 0) => {
1674
rect(p, x, y, w, h);
1676
r = min(r, w / 2, h / 2);
1679
moveTo(p, x + r, y);
1680
arcTo(p, x + w, y, x + w, y + h, r);
1681
arcTo(p, x + w, y + h, x, y + h, r);
1682
arcTo(p, x, y + h, x, y, r);
1683
arcTo(p, x, y, x + w, y, r);
1690
const moveToH = (p, x, y) => { p.moveTo(x, y); };
1691
const moveToV = (p, y, x) => { p.moveTo(x, y); };
1692
const lineToH = (p, x, y) => { p.lineTo(x, y); };
1693
const lineToV = (p, y, x) => { p.lineTo(x, y); };
1694
const rectH = rect(0);
1695
const rectV = rect(1);
1696
const arcH = (p, x, y, r, startAngle, endAngle) => { p.arc(x, y, r, startAngle, endAngle); };
1697
const arcV = (p, y, x, r, startAngle, endAngle) => { p.arc(x, y, r, startAngle, endAngle); };
1698
const bezierCurveToH = (p, bp1x, bp1y, bp2x, bp2y, p2x, p2y) => { p.bezierCurveTo(bp1x, bp1y, bp2x, bp2y, p2x, p2y); };
1699
const bezierCurveToV = (p, bp1y, bp1x, bp2y, bp2x, p2y, p2x) => { p.bezierCurveTo(bp1x, bp1y, bp2x, bp2y, p2x, p2y); };
1702
function points(opts) {
1703
return (u, seriesIdx, idx0, idx1, filtIdxs) => {
1706
return orient(u, seriesIdx, (series, dataX, dataY, scaleX, scaleY, valToPosX, valToPosY, xOff, yOff, xDim, yDim) => {
1707
let { pxRound, points } = series;
1711
if (scaleX.ori == 0) {
1720
const width = roundDec(points.width * pxRatio, 3);
1722
let rad = (points.size - points.width) / 2 * pxRatio;
1723
let dia = roundDec(rad * 2, 3);
1725
let fill = new Path2D();
1726
let clip = new Path2D();
1728
let { left: lft, top: top, width: wid, height: hgt } = u.bbox;
1737
const drawPoint = pi => {
1738
if (dataY[pi] != null) {
1739
let x = pxRound(valToPosX(dataX[pi], scaleX, xDim, xOff));
1740
let y = pxRound(valToPosY(dataY[pi], scaleY, yDim, yOff));
1742
moveTo(fill, x + rad, y);
1743
arc(fill, x, y, rad, 0, PI * 2);
1748
filtIdxs.forEach(drawPoint);
1750
for (let pi = idx0; pi <= idx1; pi++)
1755
stroke: width > 0 ? fill : null,
1758
flags: BAND_CLIP_FILL | BAND_CLIP_STROKE,
1764
function _drawAcc(lineTo) {
1765
return (stroke, accX, minY, maxY, inY, outY) => {
1767
if (inY != minY && outY != minY)
1768
lineTo(stroke, accX, minY);
1769
if (inY != maxY && outY != maxY)
1770
lineTo(stroke, accX, maxY);
1772
lineTo(stroke, accX, outY);
1777
const drawAccH = _drawAcc(lineToH);
1778
const drawAccV = _drawAcc(lineToV);
1781
return (u, seriesIdx, idx0, idx1) => {
1782
return orient(u, seriesIdx, (series, dataX, dataY, scaleX, scaleY, valToPosX, valToPosY, xOff, yOff, xDim, yDim) => {
1783
let pxRound = series.pxRound;
1785
let lineTo, drawAcc;
1787
if (scaleX.ori == 0) {
1796
const dir = scaleX.dir * (scaleX.ori == 0 ? 1 : -1);
1798
const _paths = {stroke: new Path2D(), fill: null, clip: null, band: null, gaps: null, flags: BAND_CLIP_FILL};
1799
const stroke = _paths.stroke;
1803
inY, outY, outX, drawnAtX;
1807
let accX = pxRound(valToPosX(dataX[dir == 1 ? idx0 : idx1], scaleX, xDim, xOff));
1808
let accGaps = false;
1809
let prevYNull = false;
1812
let lftIdx = nonNullIdx(dataY, idx0, idx1, 1 * dir);
1813
let rgtIdx = nonNullIdx(dataY, idx0, idx1, -1 * dir);
1814
let lftX = pxRound(valToPosX(dataX[lftIdx], scaleX, xDim, xOff));
1815
let rgtX = pxRound(valToPosX(dataX[rgtIdx], scaleX, xDim, xOff));
1818
addGap(gaps, xOff, lftX);
1820
for (let i = dir == 1 ? idx0 : idx1; i >= idx0 && i <= idx1; i += dir) {
1821
let x = pxRound(valToPosX(dataX[i], scaleX, xDim, xOff));
1824
if (dataY[i] != null) {
1825
outY = pxRound(valToPosY(dataY[i], scaleY, yDim, yOff));
1828
lineTo(stroke, x, outY);
1832
minY = min(outY, minY);
1833
maxY = max(outY, maxY);
1835
else if (dataY[i] === null)
1836
accGaps = prevYNull = true;
1839
let _addGap = false;
1842
drawAcc(stroke, accX, minY, maxY, inY, outY);
1843
outX = drawnAtX = accX;
1850
if (dataY[i] != null) {
1851
outY = pxRound(valToPosY(dataY[i], scaleY, yDim, yOff));
1852
lineTo(stroke, x, outY);
1853
minY = maxY = inY = outY;
1856
if (prevYNull && x - accX > 1)
1865
if (dataY[i] === null) {
1873
_addGap && addGap(gaps, outX, x);
1879
if (minY != inf && minY != maxY && drawnAtX != accX)
1880
drawAcc(stroke, accX, minY, maxY, inY, outY);
1882
if (rgtX < xOff + xDim)
1883
addGap(gaps, rgtX, xOff + xDim);
1885
if (series.fill != null) {
1886
let fill = _paths.fill = new Path2D(stroke);
1888
let fillTo = pxRound(valToPosY(series.fillTo(u, seriesIdx, series.min, series.max), scaleY, yDim, yOff));
1890
lineTo(fill, rgtX, fillTo);
1891
lineTo(fill, lftX, fillTo);
1894
_paths.gaps = gaps = series.gaps(u, seriesIdx, idx0, idx1, gaps);
1896
if (!series.spanGaps)
1897
_paths.clip = clipGaps(gaps, scaleX.ori, xOff, yOff, xDim, yDim);
1899
if (u.bands.length > 0) {
1902
_paths.band = clipBandLine(u, seriesIdx, idx0, idx1, stroke);
1910
function stepped(opts) {
1911
const align = ifNull(opts.align, 1);
1913
const ascDesc = ifNull(opts.ascDesc, false);
1915
return (u, seriesIdx, idx0, idx1) => {
1916
return orient(u, seriesIdx, (series, dataX, dataY, scaleX, scaleY, valToPosX, valToPosY, xOff, yOff, xDim, yDim) => {
1917
let pxRound = series.pxRound;
1919
let lineTo = scaleX.ori == 0 ? lineToH : lineToV;
1921
const _paths = {stroke: new Path2D(), fill: null, clip: null, band: null, gaps: null, flags: BAND_CLIP_FILL};
1922
const stroke = _paths.stroke;
1924
const _dir = 1 * scaleX.dir * (scaleX.ori == 0 ? 1 : -1);
1926
idx0 = nonNullIdx(dataY, idx0, idx1, 1);
1927
idx1 = nonNullIdx(dataY, idx0, idx1, -1);
1931
let prevYPos = pxRound(valToPosY(dataY[_dir == 1 ? idx0 : idx1], scaleY, yDim, yOff));
1932
let firstXPos = pxRound(valToPosX(dataX[_dir == 1 ? idx0 : idx1], scaleX, xDim, xOff));
1933
let prevXPos = firstXPos;
1935
lineTo(stroke, firstXPos, prevYPos);
1937
for (let i = _dir == 1 ? idx0 : idx1; i >= idx0 && i <= idx1; i += _dir) {
1938
let yVal1 = dataY[i];
1940
let x1 = pxRound(valToPosX(dataX[i], scaleX, xDim, xOff));
1942
if (yVal1 == null) {
1943
if (yVal1 === null) {
1944
addGap(gaps, prevXPos, x1);
1950
let y1 = pxRound(valToPosY(yVal1, scaleY, yDim, yOff));
1953
addGap(gaps, prevXPos, x1);
1958
lineTo(stroke, x1, prevYPos);
1960
lineTo(stroke, prevXPos, y1);
1962
lineTo(stroke, x1, y1);
1968
if (series.fill != null) {
1969
let fill = _paths.fill = new Path2D(stroke);
1971
let fillTo = series.fillTo(u, seriesIdx, series.min, series.max);
1972
let minY = pxRound(valToPosY(fillTo, scaleY, yDim, yOff));
1974
lineTo(fill, prevXPos, minY);
1975
lineTo(fill, firstXPos, minY);
1978
_paths.gaps = gaps = series.gaps(u, seriesIdx, idx0, idx1, gaps);
1981
let halfStroke = (series.width * pxRatio) / 2;
1982
let startsOffset = (ascDesc || align == 1) ? halfStroke : -halfStroke;
1983
let endsOffset = (ascDesc || align == -1) ? -halfStroke : halfStroke;
1986
g[0] += startsOffset;
1990
if (!series.spanGaps)
1991
_paths.clip = clipGaps(gaps, scaleX.ori, xOff, yOff, xDim, yDim);
1993
if (u.bands.length > 0) {
1996
_paths.band = clipBandLine(u, seriesIdx, idx0, idx1, stroke);
2004
function bars(opts) {
2005
opts = opts || EMPTY_OBJ;
2006
const size = ifNull(opts.size, [0.6, inf, 1]);
2007
const align = opts.align || 0;
2008
const extraGap = (opts.gap || 0) * pxRatio;
2010
const radius = ifNull(opts.radius, 0);
2012
const gapFactor = 1 - size[0];
2013
const maxWidth = ifNull(size[1], inf) * pxRatio;
2014
const minWidth = ifNull(size[2], 1) * pxRatio;
2016
const disp = ifNull(opts.disp, EMPTY_OBJ);
2017
const _each = ifNull(opts.each, _ => {});
2019
const { fill: dispFills, stroke: dispStrokes } = disp;
2021
return (u, seriesIdx, idx0, idx1) => {
2022
return orient(u, seriesIdx, (series, dataX, dataY, scaleX, scaleY, valToPosX, valToPosY, xOff, yOff, xDim, yDim) => {
2023
let pxRound = series.pxRound;
2025
const _dirX = scaleX.dir * (scaleX.ori == 0 ? 1 : -1);
2026
const _dirY = scaleY.dir * (scaleY.ori == 1 ? 1 : -1);
2028
let rect = scaleX.ori == 0 ? rectH : rectV;
2030
let each = scaleX.ori == 0 ? _each : (u, seriesIdx, i, top, lft, hgt, wid) => {
2031
_each(u, seriesIdx, i, lft, top, wid, hgt);
2034
let fillToY = series.fillTo(u, seriesIdx, series.min, series.max);
2036
let y0Pos = valToPosY(fillToY, scaleY, yDim, yOff);
2041
let strokeWidth = pxRound(series.width * pxRatio);
2043
let multiPath = false;
2045
let fillColors = null;
2046
let fillPaths = null;
2047
let strokeColors = null;
2048
let strokePaths = null;
2050
if (dispFills != null && dispStrokes != null) {
2053
fillColors = dispFills.values(u, seriesIdx, idx0, idx1);
2054
fillPaths = new Map();
2055
(new Set(fillColors)).forEach(color => {
2057
fillPaths.set(color, new Path2D());
2060
strokeColors = dispStrokes.values(u, seriesIdx, idx0, idx1);
2061
strokePaths = new Map();
2062
(new Set(strokeColors)).forEach(color => {
2064
strokePaths.set(color, new Path2D());
2068
let { x0, size } = disp;
2070
if (x0 != null && size != null) {
2071
dataX = x0.values(u, seriesIdx, idx0, idx1);
2074
dataX = dataX.map(pct => u.posToVal(xOff + pct * xDim, scaleX.key, true));
2077
let sizes = size.values(u, seriesIdx, idx0, idx1);
2080
barWid = sizes[0] * xDim;
2082
barWid = valToPosX(sizes[0], scaleX, xDim, xOff) - valToPosX(0, scaleX, xDim, xOff);
2084
barWid = pxRound(barWid - strokeWidth);
2086
xShift = (_dirX == 1 ? -strokeWidth / 2 : barWid + strokeWidth / 2);
2091
if (dataX.length > 1) {
2097
for (let i = 0, minDelta = Infinity; i < dataX.length; i++) {
2098
if (dataY[i] !== undefined) {
2099
if (prevIdx != null) {
2100
let delta = abs(dataX[i] - dataX[prevIdx]);
2102
if (delta < minDelta) {
2104
colWid = abs(valToPosX(dataX[i], scaleX, xDim, xOff) - valToPosX(dataX[prevIdx], scaleX, xDim, xOff));
2113
let gapWid = colWid * gapFactor;
2115
barWid = pxRound(min(maxWidth, max(minWidth, colWid - gapWid)) - strokeWidth - extraGap);
2117
xShift = (align == 0 ? barWid / 2 : align == _dirX ? 0 : barWid) - align * _dirX * extraGap / 2;
2120
const _paths = {stroke: null, fill: null, clip: null, band: null, gaps: null, flags: BAND_CLIP_FILL | BAND_CLIP_STROKE};
2122
const hasBands = u.bands.length > 0;
2128
_paths.band = new Path2D();
2129
yLimit = pxRound(valToPosY(scaleY.max, scaleY, yDim, yOff));
2132
const stroke = multiPath ? null : new Path2D();
2133
const band = _paths.band;
2135
for (let i = _dirX == 1 ? idx0 : idx1; i >= idx0 && i <= idx1; i += _dirX) {
2136
let yVal = dataY[i];
2148
let xVal = scaleX.distr != 2 || disp != null ? dataX[i] : i;
2151
let xPos = valToPosX(xVal, scaleX, xDim, xOff);
2152
let yPos = valToPosY(ifNull(yVal, fillToY) , scaleY, yDim, yOff);
2154
let lft = pxRound(xPos - xShift);
2155
let btm = pxRound(max(yPos, y0Pos));
2156
let top = pxRound(min(yPos, y0Pos));
2158
let barHgt = btm - top;
2160
let r = radius * barWid;
2164
if (strokeWidth > 0 && strokeColors[i] != null)
2165
rect(strokePaths.get(strokeColors[i]), lft, top + floor(strokeWidth / 2), barWid, max(0, barHgt - strokeWidth), r);
2167
if (fillColors[i] != null)
2168
rect(fillPaths.get(fillColors[i]), lft, top + floor(strokeWidth / 2), barWid, max(0, barHgt - strokeWidth), r);
2171
rect(stroke, lft, top + floor(strokeWidth / 2), barWid, max(0, barHgt - strokeWidth), r);
2173
each(u, seriesIdx, i,
2174
lft - strokeWidth / 2,
2176
barWid + strokeWidth,
2193
rect(band, lft - strokeWidth / 2, top, barWid + strokeWidth, max(0, barHgt), 0);
2197
if (strokeWidth > 0)
2198
_paths.stroke = multiPath ? strokePaths : stroke;
2200
_paths.fill = multiPath ? fillPaths : stroke;
2207
function splineInterp(interp, opts) {
2208
return (u, seriesIdx, idx0, idx1) => {
2209
return orient(u, seriesIdx, (series, dataX, dataY, scaleX, scaleY, valToPosX, valToPosY, xOff, yOff, xDim, yDim) => {
2210
let pxRound = series.pxRound;
2212
let moveTo, bezierCurveTo, lineTo;
2214
if (scaleX.ori == 0) {
2217
bezierCurveTo = bezierCurveToH;
2222
bezierCurveTo = bezierCurveToV;
2225
const _dir = 1 * scaleX.dir * (scaleX.ori == 0 ? 1 : -1);
2227
idx0 = nonNullIdx(dataY, idx0, idx1, 1);
2228
idx1 = nonNullIdx(dataY, idx0, idx1, -1);
2232
let firstXPos = pxRound(valToPosX(dataX[_dir == 1 ? idx0 : idx1], scaleX, xDim, xOff));
2233
let prevXPos = firstXPos;
2238
for (let i = _dir == 1 ? idx0 : idx1; i >= idx0 && i <= idx1; i += _dir) {
2239
let yVal = dataY[i];
2240
let xVal = dataX[i];
2241
let xPos = valToPosX(xVal, scaleX, xDim, xOff);
2244
if (yVal === null) {
2245
addGap(gaps, prevXPos, xPos);
2252
addGap(gaps, prevXPos, xPos);
2256
xCoords.push((prevXPos = xPos));
2257
yCoords.push(valToPosY(dataY[i], scaleY, yDim, yOff));
2261
const _paths = {stroke: interp(xCoords, yCoords, moveTo, lineTo, bezierCurveTo, pxRound), fill: null, clip: null, band: null, gaps: null, flags: BAND_CLIP_FILL};
2262
const stroke = _paths.stroke;
2264
if (series.fill != null && stroke != null) {
2265
let fill = _paths.fill = new Path2D(stroke);
2267
let fillTo = series.fillTo(u, seriesIdx, series.min, series.max);
2268
let minY = pxRound(valToPosY(fillTo, scaleY, yDim, yOff));
2270
lineTo(fill, prevXPos, minY);
2271
lineTo(fill, firstXPos, minY);
2274
_paths.gaps = gaps = series.gaps(u, seriesIdx, idx0, idx1, gaps);
2276
if (!series.spanGaps)
2277
_paths.clip = clipGaps(gaps, scaleX.ori, xOff, yOff, xDim, yDim);
2279
if (u.bands.length > 0) {
2282
_paths.band = clipBandLine(u, seriesIdx, idx0, idx1, stroke);
2302
function monotoneCubic(opts) {
2303
return splineInterp(_monotoneCubic);
2308
function _monotoneCubic(xs, ys, moveTo, lineTo, bezierCurveTo, pxRound) {
2309
const n = xs.length;
2314
const path = new Path2D();
2316
moveTo(path, xs[0], ys[0]);
2319
lineTo(path, xs[1], ys[1]);
2327
for (let i = 0; i < n - 1; i++) {
2328
dys[i] = ys[i + 1] - ys[i];
2329
dxs[i] = xs[i + 1] - xs[i];
2330
ds[i] = dys[i] / dxs[i];
2337
for (let i = 1; i < n - 1; i++) {
2338
if (ds[i] === 0 || ds[i - 1] === 0 || (ds[i - 1] > 0) !== (ds[i] > 0))
2341
ms[i] = 3 * (dxs[i - 1] + dxs[i]) / (
2342
(2 * dxs[i] + dxs[i - 1]) / ds[i - 1] +
2343
(dxs[i] + 2 * dxs[i - 1]) / ds[i]
2346
if (!isFinite(ms[i]))
2351
ms[n - 1] = ds[n - 2];
2353
for (let i = 0; i < n - 1; i++) {
2357
ys[i] + ms[i] * dxs[i] / 3,
2358
xs[i + 1] - dxs[i] / 3,
2359
ys[i + 1] - ms[i + 1] * dxs[i] / 3,
2369
const cursorPlots = new Set();
2371
function invalidateRects() {
2372
cursorPlots.forEach(u => {
2377
on(resize, win, invalidateRects);
2378
on(scroll, win, invalidateRects, true);
2380
const linearPath = linear() ;
2381
const pointsPath = points() ;
2383
function setDefaults(d, xo, yo, initY) {
2384
let d2 = initY ? [d[0], d[1]].concat(d.slice(2)) : [d[0]].concat(d.slice(1));
2385
return d2.map((o, i) => setDefault(o, i, xo, yo));
2388
function setDefaults2(d, xyo) {
2389
return d.map((o, i) => i == 0 ? null : assign({}, xyo, o));
2392
function setDefault(o, i, xo, yo) {
2393
return assign({}, (i == 0 ? xo : yo), o);
2396
function snapNumX(self, dataMin, dataMax) {
2397
return dataMin == null ? nullNullTuple : [dataMin, dataMax];
2400
const snapTimeX = snapNumX;
2404
function snapNumY(self, dataMin, dataMax) {
2405
return dataMin == null ? nullNullTuple : rangeNum(dataMin, dataMax, rangePad, true);
2408
function snapLogY(self, dataMin, dataMax, scale) {
2409
return dataMin == null ? nullNullTuple : rangeLog(dataMin, dataMax, self.scales[scale].log, false);
2412
const snapLogX = snapLogY;
2414
function snapAsinhY(self, dataMin, dataMax, scale) {
2415
return dataMin == null ? nullNullTuple : rangeAsinh(dataMin, dataMax, self.scales[scale].log, false);
2418
const snapAsinhX = snapAsinhY;
2421
function findIncr(minVal, maxVal, incrs, dim, minSpace) {
2422
let intDigits = max(numIntDigits(minVal), numIntDigits(maxVal));
2424
let delta = maxVal - minVal;
2426
let incrIdx = closestIdx((minSpace / dim) * delta, incrs);
2429
let foundIncr = incrs[incrIdx];
2430
let foundSpace = dim * foundIncr / delta;
2432
if (foundSpace >= minSpace && intDigits + (foundIncr < 5 ? fixedDec.get(foundIncr) : 0) <= 17)
2433
return [foundIncr, foundSpace];
2434
} while (++incrIdx < incrs.length);
2439
function pxRatioFont(font) {
2440
let fontSize, fontSizeCss;
2441
font = font.replace(/(\d+)px/, (m, p1) => (fontSize = round((fontSizeCss = +p1) * pxRatio)) + 'px');
2442
return [font, fontSize, fontSizeCss];
2445
function syncFontSize(axis) {
2447
[axis.font, axis.labelFont].forEach(f => {
2448
let size = roundDec(f[2] * pxRatio, 1);
2449
f[0] = f[0].replace(/[0-9.]+px/, size + 'px');
2455
function uPlot(opts, data, then) {
2457
mode: ifNull(opts.mode, 1),
2460
const mode = self.mode;
2463
function getValPct(val, scale) {
2465
scale.distr == 3 ? log10(val > 0 ? val : scale.clamp(self, val, scale.min, scale.max, scale.key)) :
2466
scale.distr == 4 ? asinh(val, scale.asinh) :
2470
return (_val - scale._min) / (scale._max - scale._min);
2473
function getHPos(val, scale, dim, off) {
2474
let pct = getValPct(val, scale);
2475
return off + dim * (scale.dir == -1 ? (1 - pct) : pct);
2478
function getVPos(val, scale, dim, off) {
2479
let pct = getValPct(val, scale);
2480
return off + dim * (scale.dir == -1 ? pct : (1 - pct));
2483
function getPos(val, scale, dim, off) {
2484
return scale.ori == 0 ? getHPos(val, scale, dim, off) : getVPos(val, scale, dim, off);
2487
self.valToPosH = getHPos;
2488
self.valToPosV = getVPos;
2493
const root = self.root = placeDiv(UPLOT);
2495
if (opts.id != null)
2498
addClass(root, opts.class);
2501
let title = placeDiv(TITLE, root);
2502
title.textContent = opts.title;
2505
const can = placeTag("canvas");
2506
const ctx = self.ctx = can.getContext("2d");
2508
const wrap = placeDiv(WRAP, root);
2509
const under = self.under = placeDiv(UNDER, wrap);
2510
wrap.appendChild(can);
2511
const over = self.over = placeDiv(OVER, wrap);
2515
const pxAlign = +ifNull(opts.pxAlign, 1);
2517
const pxRound = pxRoundGen(pxAlign);
2519
(opts.plugins || []).forEach(p => {
2521
opts = p.opts(self, opts) || opts;
2524
const ms = opts.ms || 1e-3;
2526
const series = self.series = mode == 1 ?
2527
setDefaults(opts.series || [], xSeriesOpts, ySeriesOpts, false) :
2528
setDefaults2(opts.series || [null], xySeriesOpts);
2529
const axes = self.axes = setDefaults(opts.axes || [], xAxisOpts, yAxisOpts, true);
2530
const scales = self.scales = {};
2531
const bands = self.bands = opts.bands || [];
2533
bands.forEach(b => {
2534
b.fill = fnOrSelf(b.fill || null);
2537
const xScaleKey = mode == 2 ? series[1].facets[0].scale : series[0].scale;
2539
const drawOrderMap = {
2544
const drawOrder = (opts.drawOrder || ["axes", "series"]).map(key => drawOrderMap[key]);
2546
function initScale(scaleKey) {
2547
let sc = scales[scaleKey];
2550
let scaleOpts = (opts.scales || EMPTY_OBJ)[scaleKey] || EMPTY_OBJ;
2552
if (scaleOpts.from != null) {
2554
initScale(scaleOpts.from);
2556
scales[scaleKey] = assign({}, scales[scaleOpts.from], scaleOpts, {key: scaleKey});
2559
sc = scales[scaleKey] = assign({}, (scaleKey == xScaleKey ? xScaleOpts : yScaleOpts), scaleOpts);
2566
let isTime = sc.time;
2570
let rangeIsArr = isArr(rn);
2572
if (scaleKey != xScaleKey || mode == 2) {
2574
if (rangeIsArr && (rn[0] == null || rn[1] == null)) {
2576
min: rn[0] == null ? autoRangePart : {
2581
max: rn[1] == null ? autoRangePart : {
2590
if (!rangeIsArr && isObj(rn)) {
2593
rn = (self, dataMin, dataMax) => dataMin == null ? nullNullTuple : rangeNum(dataMin, dataMax, cfg);
2597
sc.range = fnOrSelf(rn || (isTime ? snapTimeX : scaleKey == xScaleKey ?
2598
(sc.distr == 3 ? snapLogX : sc.distr == 4 ? snapAsinhX : snapNumX) :
2599
(sc.distr == 3 ? snapLogY : sc.distr == 4 ? snapAsinhY : snapNumY)
2602
sc.auto = fnOrSelf(rangeIsArr ? false : sc.auto);
2604
sc.clamp = fnOrSelf(sc.clamp || clampScale);
2607
sc._min = sc._max = null;
2617
series.forEach(s => {
2626
for (let k in opts.scales)
2629
const scaleX = scales[xScaleKey];
2631
const xScaleDistr = scaleX.distr;
2633
let valToPosX, valToPosY;
2635
if (scaleX.ori == 0) {
2636
addClass(root, ORI_HZ);
2637
valToPosX = getHPos;
2638
valToPosY = getVPos;
2654
addClass(root, ORI_VT);
2655
valToPosX = getVPos;
2656
valToPosY = getHPos;
2672
const pendScales = {};
2675
for (let k in scales) {
2678
if (sc.min != null || sc.max != null) {
2679
pendScales[k] = {min: sc.min, max: sc.max};
2680
sc.min = sc.max = null;
2685
const _tzDate = (opts.tzDate || (ts => new Date(round(ts / ms))));
2686
const _fmtDate = (opts.fmtDate || fmtDate);
2688
const _timeAxisSplits = (ms == 1 ? timeAxisSplitsMs(_tzDate) : timeAxisSplitsS(_tzDate));
2689
const _timeAxisVals = timeAxisVals(_tzDate, timeAxisStamps((ms == 1 ? _timeAxisStampsMs : _timeAxisStampsS), _fmtDate));
2690
const _timeSeriesVal = timeSeriesVal(_tzDate, timeSeriesStamp(_timeSeriesStamp, _fmtDate));
2692
const activeIdxs = [];
2694
const legend = (self.legend = assign({}, legendOpts, opts.legend));
2695
const showLegend = legend.show;
2696
const markers = legend.markers;
2699
legend.idxs = activeIdxs;
2701
markers.width = fnOrSelf(markers.width);
2702
markers.dash = fnOrSelf(markers.dash);
2703
markers.stroke = fnOrSelf(markers.stroke);
2704
markers.fill = fnOrSelf(markers.fill);
2708
let legendRows = [];
2709
let legendCells = [];
2711
let multiValLegend = false;
2712
let NULL_LEGEND_VALUES = {};
2715
const getMultiVals = series[1] ? series[1].values : null;
2716
multiValLegend = getMultiVals != null;
2717
legendCols = multiValLegend ? getMultiVals(self, 1, 0) : {_: 0};
2719
for (let k in legendCols)
2720
NULL_LEGEND_VALUES[k] = "--";
2724
legendEl = placeTag("table", LEGEND, root);
2726
if (multiValLegend) {
2727
let head = placeTag("tr", LEGEND_THEAD, legendEl);
2728
placeTag("th", null, head);
2730
for (var key in legendCols)
2731
placeTag("th", LEGEND_LABEL, head).textContent = key;
2734
addClass(legendEl, LEGEND_INLINE);
2735
legend.live && addClass(legendEl, LEGEND_LIVE);
2739
const son = {show: true};
2740
const soff = {show: false};
2742
function initLegendRow(s, i) {
2743
if (i == 0 && (multiValLegend || !legend.live || mode == 2))
2744
return nullNullTuple;
2748
let row = placeTag("tr", LEGEND_SERIES, legendEl, legendEl.childNodes[i]);
2750
addClass(row, s.class);
2755
let label = placeTag("th", null, row);
2758
let indic = placeDiv(LEGEND_MARKER, label);
2761
let width = markers.width(self, i);
2764
indic.style.border = width + "px " + markers.dash(self, i) + " " + markers.stroke(self, i);
2766
indic.style.background = markers.fill(self, i);
2770
let text = placeDiv(LEGEND_LABEL, label);
2771
text.textContent = s.label;
2775
text.style.color = s.width > 0 ? markers.stroke(self, i) : markers.fill(self, i);
2777
onMouse("click", label, e => {
2781
let seriesIdx = series.indexOf(s);
2783
if ((e.ctrlKey || e.metaKey) != legend.isolate) {
2785
let isolate = series.some((s, i) => i > 0 && i != seriesIdx && s.show);
2787
series.forEach((s, i) => {
2788
i > 0 && setSeries(i, isolate ? (i == seriesIdx ? son : soff) : son, true, syncOpts.setSeries);
2792
setSeries(seriesIdx, {show: !s.show}, true, syncOpts.setSeries);
2796
onMouse(mouseenter, label, e => {
2800
setSeries(series.indexOf(s), FOCUS_TRUE, true, syncOpts.setSeries);
2805
for (var key in legendCols) {
2806
let v = placeTag("td", LEGEND_VALUE, row);
2807
v.textContent = "--";
2811
return [row, cells];
2814
const mouseListeners = new Map();
2816
function onMouse(ev, targ, fn) {
2817
const targListeners = mouseListeners.get(targ) || {};
2818
const listener = cursor.bind[ev](self, targ, fn);
2821
on(ev, targ, targListeners[ev] = listener);
2822
mouseListeners.set(targ, targListeners);
2826
function offMouse(ev, targ, fn) {
2827
const targListeners = mouseListeners.get(targ) || {};
2829
for (let k in targListeners) {
2830
if (ev == null || k == ev) {
2831
off(k, targ, targListeners[k]);
2832
delete targListeners[k];
2837
mouseListeners.delete(targ);
2857
let shouldSetScales = false;
2858
let shouldSetSize = false;
2859
let shouldConvergeSize = false;
2860
let shouldSetCursor = false;
2861
let shouldSetLegend = false;
2863
function _setSize(width, height, force) {
2864
if (force || (width != self.width || height != self.height))
2865
calcSize(width, height);
2867
resetYSeries(false);
2869
shouldConvergeSize = true;
2870
shouldSetSize = true;
2871
shouldSetCursor = shouldSetLegend = cursor.left >= 0;
2875
function calcSize(width, height) {
2878
self.width = fullWidCss = plotWidCss = width;
2879
self.height = fullHgtCss = plotHgtCss = height;
2880
plotLftCss = plotTopCss = 0;
2887
plotLft = bb.left = incrRound(plotLftCss * pxRatio, 0.5);
2888
plotTop = bb.top = incrRound(plotTopCss * pxRatio, 0.5);
2889
plotWid = bb.width = incrRound(plotWidCss * pxRatio, 0.5);
2890
plotHgt = bb.height = incrRound(plotHgtCss * pxRatio, 0.5);
2896
const CYCLE_LIMIT = 3;
2898
function convergeSize() {
2899
let converged = false;
2903
while (!converged) {
2906
let axesConverged = axesCalc(cycleNum);
2907
let paddingConverged = paddingCalc(cycleNum);
2909
converged = cycleNum == CYCLE_LIMIT || (axesConverged && paddingConverged);
2912
calcSize(self.width, self.height);
2913
shouldSetSize = true;
2918
function setSize({width, height}) {
2919
_setSize(width, height);
2922
self.setSize = setSize;
2925
function calcPlotRect() {
2927
let hasTopAxis = false;
2928
let hasBtmAxis = false;
2929
let hasRgtAxis = false;
2930
let hasLftAxis = false;
2932
axes.forEach((axis, i) => {
2933
if (axis.show && axis._show) {
2934
let {side, _size} = axis;
2935
let isVt = side % 2;
2936
let labelSize = axis.label != null ? axis.labelSize : 0;
2938
let fullSize = _size + labelSize;
2942
plotWidCss -= fullSize;
2945
plotLftCss += fullSize;
2952
plotHgtCss -= fullSize;
2955
plotTopCss += fullSize;
2965
sidesWithAxes[0] = hasTopAxis;
2966
sidesWithAxes[1] = hasRgtAxis;
2967
sidesWithAxes[2] = hasBtmAxis;
2968
sidesWithAxes[3] = hasLftAxis;
2971
plotWidCss -= _padding[1] + _padding[3];
2972
plotLftCss += _padding[3];
2975
plotHgtCss -= _padding[2] + _padding[0];
2976
plotTopCss += _padding[0];
2979
function calcAxesRects() {
2981
let off1 = plotLftCss + plotWidCss;
2982
let off2 = plotTopCss + plotHgtCss;
2984
let off3 = plotLftCss;
2985
let off0 = plotTopCss;
2987
function incrOffset(side, size) {
2989
case 1: off1 += size; return off1 - size;
2990
case 2: off2 += size; return off2 - size;
2991
case 3: off3 -= size; return off3 + size;
2992
case 0: off0 -= size; return off0 + size;
2996
axes.forEach((axis, i) => {
2997
if (axis.show && axis._show) {
2998
let side = axis.side;
3000
axis._pos = incrOffset(side, axis._size);
3002
if (axis.label != null)
3003
axis._lpos = incrOffset(side, axis.labelSize);
3008
const cursor = (self.cursor = assign({}, cursorOpts, {drag: {y: mode == 2}}, opts.cursor));
3011
cursor.idxs = activeIdxs;
3013
cursor._lock = false;
3015
let points = cursor.points;
3017
points.show = fnOrSelf(points.show);
3018
points.size = fnOrSelf(points.size);
3019
points.stroke = fnOrSelf(points.stroke);
3020
points.width = fnOrSelf(points.width);
3021
points.fill = fnOrSelf(points.fill);
3024
const focus = self.focus = assign({}, opts.focus || {alpha: 0.3}, cursor.focus);
3025
const cursorFocus = focus.prox >= 0;
3028
let cursorPts = [null];
3030
function initCursorPt(s, si) {
3032
let pt = cursor.points.show(self, si);
3035
addClass(pt, CURSOR_PT);
3036
addClass(pt, s.class);
3037
elTrans(pt, -10, -10, plotWidCss, plotHgtCss);
3038
over.insertBefore(pt, cursorPts[si]);
3045
function initSeries(s, i) {
3046
if (mode == 1 || i > 0) {
3047
let isTime = mode == 1 && scales[s.scale].time;
3050
s.value = isTime ? (isStr(sv) ? timeSeriesVal(_tzDate, timeSeriesStamp(sv, _fmtDate)) : sv || _timeSeriesVal) : sv || numSeriesVal;
3051
s.label = s.label || (isTime ? timeSeriesLabel : numSeriesLabel);
3055
s.width = s.width == null ? 1 : s.width;
3056
s.paths = s.paths || linearPath || retNull;
3057
s.fillTo = fnOrSelf(s.fillTo || seriesFillTo);
3058
s.pxAlign = +ifNull(s.pxAlign, pxAlign);
3059
s.pxRound = pxRoundGen(s.pxAlign);
3061
s.stroke = fnOrSelf(s.stroke || null);
3062
s.fill = fnOrSelf(s.fill || null);
3063
s._stroke = s._fill = s._paths = s._focus = null;
3065
let _ptDia = ptDia(s.width, 1);
3066
let points = s.points = assign({}, {
3068
width: max(1, _ptDia * .2),
3075
points.show = fnOrSelf(points.show);
3076
points.filter = fnOrSelf(points.filter);
3077
points.fill = fnOrSelf(points.fill);
3078
points.stroke = fnOrSelf(points.stroke);
3079
points.paths = fnOrSelf(points.paths);
3080
points.pxAlign = s.pxAlign;
3084
let rowCells = initLegendRow(s, i);
3085
legendRows.splice(i, 0, rowCells[0]);
3086
legendCells.splice(i, 0, rowCells[1]);
3087
legend.values.push(null);
3091
activeIdxs.splice(i, 0, null);
3093
let pt = initCursorPt(s, i);
3094
pt && cursorPts.splice(i, 0, pt);
3098
function addSeries(opts, si) {
3099
si = si == null ? series.length : si;
3101
opts = setDefault(opts, si, xSeriesOpts, ySeriesOpts);
3102
series.splice(si, 0, opts);
3103
initSeries(series[si], si);
3106
self.addSeries = addSeries;
3108
function delSeries(i) {
3109
series.splice(i, 1);
3112
legend.values.splice(i, 1);
3114
legendCells.splice(i, 1);
3115
let tr = legendRows.splice(i, 1)[0];
3116
offMouse(null, tr.firstChild);
3121
activeIdxs.splice(i, 1);
3123
cursorPts.length > 1 && cursorPts.splice(i, 1)[0].remove();
3129
self.delSeries = delSeries;
3131
const sidesWithAxes = [false, false, false, false];
3133
function initAxis(axis, i) {
3134
axis._show = axis.show;
3137
let isVt = axis.side % 2;
3139
let sc = scales[axis.scale];
3143
axis.scale = isVt ? series[1].scale : xScaleKey;
3144
sc = scales[axis.scale];
3148
let isTime = sc.time;
3150
axis.size = fnOrSelf(axis.size);
3151
axis.space = fnOrSelf(axis.space);
3152
axis.rotate = fnOrSelf(axis.rotate);
3153
axis.incrs = fnOrSelf(axis.incrs || ( sc.distr == 2 ? wholeIncrs : (isTime ? (ms == 1 ? timeIncrsMs : timeIncrsS) : numIncrs)));
3154
axis.splits = fnOrSelf(axis.splits || (isTime && sc.distr == 1 ? _timeAxisSplits : sc.distr == 3 ? logAxisSplits : sc.distr == 4 ? asinhAxisSplits : numAxisSplits));
3156
axis.stroke = fnOrSelf(axis.stroke);
3157
axis.grid.stroke = fnOrSelf(axis.grid.stroke);
3158
axis.ticks.stroke = fnOrSelf(axis.ticks.stroke);
3160
let av = axis.values;
3164
isArr(av) && !isArr(av[0]) ? fnOrSelf(av) :
3169
timeAxisVals(_tzDate, timeAxisStamps(av, _fmtDate)) :
3172
timeAxisVal(_tzDate, av) :
3174
) : av || numAxisVals
3177
axis.filter = fnOrSelf(axis.filter || ( sc.distr >= 3 ? logAxisValsFilt : retArg1));
3179
axis.font = pxRatioFont(axis.font);
3180
axis.labelFont = pxRatioFont(axis.labelFont);
3182
axis._size = axis.size(self, null, i, 0);
3189
axis._values = null;
3192
sidesWithAxes[i] = true;
3194
axis._el = placeDiv(AXIS, wrap);
3201
function autoPadSide(self, side, sidesWithAxes, cycleNum) {
3202
let [hasTopAxis, hasRgtAxis, hasBtmAxis, hasLftAxis] = sidesWithAxes;
3207
if (ori == 0 && (hasLftAxis || hasRgtAxis))
3208
size = (side == 0 && !hasTopAxis || side == 2 && !hasBtmAxis ? round(xAxisOpts.size / 3) : 0);
3209
if (ori == 1 && (hasTopAxis || hasBtmAxis))
3210
size = (side == 1 && !hasRgtAxis || side == 3 && !hasLftAxis ? round(yAxisOpts.size / 2) : 0);
3215
const padding = self.padding = (opts.padding || [autoPadSide,autoPadSide,autoPadSide,autoPadSide]).map(p => fnOrSelf(ifNull(p, autoPadSide)));
3216
const _padding = self._padding = padding.map((p, i) => p(self, i, sidesWithAxes, 0));
3223
const idxs = mode == 1 ? series[0].idxs : null;
3227
let viaAutoScaleX = false;
3229
function setData(_data, _resetScales) {
3232
for (let i = 1; i < series.length; i++)
3233
dataLen += data[i][0].length;
3234
self.data = data = _data;
3237
data = (_data || []).slice();
3238
data[0] = data[0] || [];
3240
self.data = data.slice();
3242
dataLen = data0.length;
3244
if (xScaleDistr == 2)
3245
data[0] = data0.map((v, i) => i);
3254
if (_resetScales !== false) {
3257
if (xsc.auto(self, viaAutoScaleX))
3260
_setScale(xScaleKey, xsc.min, xsc.max);
3262
shouldSetCursor = cursor.left >= 0;
3263
shouldSetLegend = true;
3268
self.setData = setData;
3270
function autoScaleX() {
3271
viaAutoScaleX = true;
3278
i1 = idxs[1] = dataLen - 1;
3283
if (xScaleDistr == 2) {
3287
else if (dataLen == 1) {
3288
if (xScaleDistr == 3)
3289
[_min, _max] = rangeLog(_min, _min, scaleX.log, false);
3290
else if (xScaleDistr == 4)
3291
[_min, _max] = rangeAsinh(_min, _min, scaleX.log, false);
3292
else if (scaleX.time)
3293
_max = _min + round(86400 / ms);
3295
[_min, _max] = rangeNum(_min, _max, rangePad, true);
3299
i0 = idxs[0] = _min = null;
3300
i1 = idxs[1] = _max = null;
3304
_setScale(xScaleKey, _min, _max);
3307
let ctxStroke, ctxFill, ctxWidth, ctxDash, ctxJoin, ctxCap, ctxFont, ctxAlign, ctxBaseline;
3310
function setCtxStyle(stroke = transparent, width, dash = EMPTY_ARR, cap = "butt", fill = transparent, join = "round") {
3311
if (stroke != ctxStroke)
3312
ctx.strokeStyle = ctxStroke = stroke;
3313
if (fill != ctxFill)
3314
ctx.fillStyle = ctxFill = fill;
3315
if (width != ctxWidth)
3316
ctx.lineWidth = ctxWidth = width;
3317
if (join != ctxJoin)
3318
ctx.lineJoin = ctxJoin = join;
3320
ctx.lineCap = ctxCap = cap;
3321
if (dash != ctxDash)
3322
ctx.setLineDash(ctxDash = dash);
3325
function setFontStyle(font, fill, align, baseline) {
3326
if (fill != ctxFill)
3327
ctx.fillStyle = ctxFill = fill;
3328
if (font != ctxFont)
3329
ctx.font = ctxFont = font;
3330
if (align != ctxAlign)
3331
ctx.textAlign = ctxAlign = align;
3332
if (baseline != ctxBaseline)
3333
ctx.textBaseline = ctxBaseline = baseline;
3336
function accScale(wsc, psc, facet, data) {
3337
if (wsc.auto(self, viaAutoScaleX) && (psc == null || psc.min == null)) {
3338
let _i0 = ifNull(i0, 0);
3339
let _i1 = ifNull(i1, data.length - 1);
3342
let minMax = facet.min == null ? (wsc.distr == 3 ? getMinMaxLog(data, _i0, _i1) : getMinMax(data, _i0, _i1)) : [facet.min, facet.max];
3345
wsc.min = min(wsc.min, facet.min = minMax[0]);
3346
wsc.max = max(wsc.max, facet.max = minMax[1]);
3350
function setScales() {
3354
let wipScales = copy(scales, fastIsObj);
3356
for (let k in wipScales) {
3357
let wsc = wipScales[k];
3358
let psc = pendScales[k];
3360
if (psc != null && psc.min != null) {
3367
else if (k != xScaleKey || mode == 2) {
3368
if (dataLen == 0 && wsc.from == null) {
3369
let minMax = wsc.range(self, null, null, k);
3370
wsc.min = minMax[0];
3371
wsc.max = minMax[1];
3382
series.forEach((s, i) => {
3385
let wsc = wipScales[k];
3386
let psc = pendScales[k];
3389
let minMax = wsc.range(self, wsc.min, wsc.max, k);
3391
wsc.min = minMax[0];
3392
wsc.max = minMax[1];
3394
i0 = closestIdx(wsc.min, data[0]);
3395
i1 = closestIdx(wsc.max, data[0]);
3398
if (data[0][i0] < wsc.min)
3400
if (data[0][i1] > wsc.max)
3406
else if (s.show && s.auto)
3407
accScale(wsc, psc, s, data[i]);
3414
if (s.show && s.auto) {
3416
let [ xFacet, yFacet ] = s.facets;
3417
let xScaleKey = xFacet.scale;
3418
let yScaleKey = yFacet.scale;
3419
let [ xData, yData ] = data[i];
3421
accScale(wipScales[xScaleKey], pendScales[xScaleKey], xFacet, xData);
3422
accScale(wipScales[yScaleKey], pendScales[yScaleKey], yFacet, yData);
3433
for (let k in wipScales) {
3434
let wsc = wipScales[k];
3435
let psc = pendScales[k];
3437
if (wsc.from == null && (psc == null || psc.min == null)) {
3438
let minMax = wsc.range(
3440
wsc.min == inf ? null : wsc.min,
3441
wsc.max == -inf ? null : wsc.max,
3444
wsc.min = minMax[0];
3445
wsc.max = minMax[1];
3451
for (let k in wipScales) {
3452
let wsc = wipScales[k];
3454
if (wsc.from != null) {
3455
let base = wipScales[wsc.from];
3457
if (base.min == null)
3458
wsc.min = wsc.max = null;
3460
let minMax = wsc.range(self, base.min, base.max, k);
3461
wsc.min = minMax[0];
3462
wsc.max = minMax[1];
3468
let anyChanged = false;
3470
for (let k in wipScales) {
3471
let wsc = wipScales[k];
3474
if (sc.min != wsc.min || sc.max != wsc.max) {
3478
let distr = sc.distr;
3480
sc._min = distr == 3 ? log10(sc.min) : distr == 4 ? asinh(sc.min, sc.asinh) : sc.min;
3481
sc._max = distr == 3 ? log10(sc.max) : distr == 4 ? asinh(sc.max, sc.asinh) : sc.max;
3483
changed[k] = anyChanged = true;
3489
series.forEach((s, i) => {
3491
if (i > 0 && changed.y)
3495
if (changed[s.scale])
3500
for (let k in changed) {
3501
shouldConvergeSize = true;
3502
fire("setScale", k);
3506
shouldSetCursor = shouldSetLegend = cursor.left >= 0;
3509
for (let k in pendScales)
3510
pendScales[k] = null;
3514
function getOuterIdxs(ydata) {
3515
let _i0 = clamp(i0 - 1, 0, dataLen - 1);
3516
let _i1 = clamp(i1 + 1, 0, dataLen - 1);
3518
while (ydata[_i0] == null && _i0 > 0)
3521
while (ydata[_i1] == null && _i1 < dataLen - 1)
3527
function drawSeries() {
3529
series.forEach((s, i) => {
3530
if (i > 0 && s.show && s._paths == null) {
3531
let _idxs = getOuterIdxs(data[i]);
3532
s._paths = s.paths(self, i, _idxs[0], _idxs[1]);
3536
series.forEach((s, i) => {
3537
if (i > 0 && s.show) {
3538
if (ctxAlpha != s.alpha)
3539
ctx.globalAlpha = ctxAlpha = s.alpha;
3542
cacheStrokeFill(i, false);
3543
s._paths && drawPath(i, false);
3547
cacheStrokeFill(i, true);
3549
let show = s.points.show(self, i, i0, i1);
3550
let idxs = s.points.filter(self, i, show, s._paths ? s._paths.gaps : null);
3553
s.points._paths = s.points.paths(self, i, i0, i1, idxs);
3559
ctx.globalAlpha = ctxAlpha = 1;
3561
fire("drawSeries", i);
3567
function cacheStrokeFill(si, _points) {
3568
let s = _points ? series[si].points : series[si];
3570
s._stroke = s.stroke(self, si);
3571
s._fill = s.fill(self, si);
3574
function drawPath(si, _points) {
3575
let s = _points ? series[si].points : series[si];
3577
let strokeStyle = s._stroke;
3578
let fillStyle = s._fill;
3580
let { stroke, fill, clip: gapsClip, flags } = s._paths;
3581
let boundsClip = null;
3582
let width = roundDec(s.width * pxRatio, 3);
3583
let offset = (width % 2) / 2;
3585
if (_points && fillStyle == null)
3586
fillStyle = width > 0 ? "#fff" : strokeStyle;
3588
let _pxAlign = s.pxAlign == 1;
3590
_pxAlign && ctx.translate(offset, offset);
3598
let halfWid = width * pxRatio / 2;
3608
boundsClip = new Path2D();
3609
boundsClip.rect(lft, top, wid, hgt);
3614
strokeFill(strokeStyle, width, s.dash, s.cap, fillStyle, stroke, fill, flags, gapsClip);
3616
fillStroke(si, strokeStyle, width, s.dash, s.cap, fillStyle, stroke, fill, flags, boundsClip, gapsClip);
3618
_pxAlign && ctx.translate(-offset, -offset);
3621
function fillStroke(si, strokeStyle, lineWidth, lineDash, lineCap, fillStyle, strokePath, fillPath, flags, boundsClip, gapsClip) {
3622
let didStrokeFill = false;
3626
bands.forEach((b, bi) => {
3628
if (b.series[0] == si) {
3629
let lowerEdge = series[b.series[1]];
3630
let lowerData = data[b.series[1]];
3632
let bandClip = (lowerEdge._paths || EMPTY_OBJ).band;
3635
let _fillStyle = null;
3638
if (lowerEdge.show && bandClip && hasData(lowerData, i0, i1)) {
3639
_fillStyle = b.fill(self, bi) || fillStyle;
3640
gapsClip2 = lowerEdge._paths.clip;
3645
strokeFill(strokeStyle, lineWidth, lineDash, lineCap, _fillStyle, strokePath, fillPath, flags, boundsClip, gapsClip, gapsClip2, bandClip);
3647
didStrokeFill = true;
3652
strokeFill(strokeStyle, lineWidth, lineDash, lineCap, fillStyle, strokePath, fillPath, flags, boundsClip, gapsClip);
3655
const CLIP_FILL_STROKE = BAND_CLIP_FILL | BAND_CLIP_STROKE;
3657
function strokeFill(strokeStyle, lineWidth, lineDash, lineCap, fillStyle, strokePath, fillPath, flags, boundsClip, gapsClip, gapsClip2, bandClip) {
3658
setCtxStyle(strokeStyle, lineWidth, lineDash, lineCap, fillStyle);
3660
if (boundsClip || gapsClip || bandClip) {
3662
boundsClip && ctx.clip(boundsClip);
3663
gapsClip && ctx.clip(gapsClip);
3667
if ((flags & CLIP_FILL_STROKE) == CLIP_FILL_STROKE) {
3669
gapsClip2 && ctx.clip(gapsClip2);
3670
doFill(fillStyle, fillPath);
3671
doStroke(strokeStyle, strokePath, lineWidth);
3673
else if (flags & BAND_CLIP_STROKE) {
3674
doFill(fillStyle, fillPath);
3676
doStroke(strokeStyle, strokePath, lineWidth);
3678
else if (flags & BAND_CLIP_FILL) {
3681
gapsClip2 && ctx.clip(gapsClip2);
3682
doFill(fillStyle, fillPath);
3684
doStroke(strokeStyle, strokePath, lineWidth);
3688
doFill(fillStyle, fillPath);
3689
doStroke(strokeStyle, strokePath, lineWidth);
3692
if (boundsClip || gapsClip || bandClip)
3696
function doStroke(strokeStyle, strokePath, lineWidth) {
3697
if (lineWidth > 0) {
3698
if (strokePath instanceof Map) {
3699
strokePath.forEach((strokePath, strokeStyle) => {
3700
ctx.strokeStyle = ctxStroke = strokeStyle;
3701
ctx.stroke(strokePath);
3705
strokePath != null && strokeStyle && ctx.stroke(strokePath);
3709
function doFill(fillStyle, fillPath) {
3710
if (fillPath instanceof Map) {
3711
fillPath.forEach((fillPath, fillStyle) => {
3712
ctx.fillStyle = ctxFill = fillStyle;
3717
fillPath != null && fillStyle && ctx.fill(fillPath);
3720
function getIncrSpace(axisIdx, min, max, fullDim) {
3721
let axis = axes[axisIdx];
3728
let minSpace = axis._space = axis.space(self, axisIdx, min, max, fullDim);
3729
let incrs = axis._incrs = axis.incrs(self, axisIdx, min, max, fullDim, minSpace);
3730
incrSpace = findIncr(min, max, incrs, fullDim, minSpace);
3733
return (axis._found = incrSpace);
3736
function drawOrthoLines(offs, filts, ori, side, pos0, len, width, stroke, dash, cap) {
3737
let offset = (width % 2) / 2;
3739
pxAlign == 1 && ctx.translate(offset, offset);
3741
setCtxStyle(stroke, width, dash, cap, stroke);
3745
let x0, y0, x1, y1, pos1 = pos0 + (side == 0 || side == 3 ? -len : len);
3756
for (let i = 0; i < offs.length; i++) {
3757
if (filts[i] != null) {
3770
pxAlign == 1 && ctx.translate(-offset, -offset);
3773
function axesCalc(cycleNum) {
3776
let converged = true;
3778
axes.forEach((axis, i) => {
3782
let scale = scales[axis.scale];
3784
if (scale.min == null) {
3788
resetYSeries(false);
3796
resetYSeries(false);
3800
let side = axis.side;
3803
let {min, max} = scale;
3805
let [_incr, _space] = getIncrSpace(i, min, max, ori == 0 ? plotWidCss : plotHgtCss);
3811
let forceMin = scale.distr == 2;
3813
let _splits = axis._splits = axis.splits(self, i, min, max, _incr, _space, forceMin);
3817
let splits = scale.distr == 2 ? _splits.map(i => data0[i]) : _splits;
3818
let incr = scale.distr == 2 ? data0[_splits[1]] - data0[_splits[0]] : _incr;
3820
let values = axis._values = axis.values(self, axis.filter(self, splits, i, _space, incr), i, _space, incr);
3823
axis._rotate = side == 2 ? axis.rotate(self, values, i, _space) : 0;
3825
let oldSize = axis._size;
3827
axis._size = ceil(axis.size(self, values, i, cycleNum));
3829
if (oldSize != null && axis._size != oldSize)
3836
function paddingCalc(cycleNum) {
3837
let converged = true;
3839
padding.forEach((p, i) => {
3840
let _p = p(self, i, sidesWithAxes, cycleNum);
3842
if (_p != _padding[i])
3851
function drawAxesGrid() {
3852
for (let i = 0; i < axes.length; i++) {
3855
if (!axis.show || !axis._show)
3858
let side = axis.side;
3863
let fillStyle = axis.stroke(self, i);
3865
let shiftDir = side == 0 || side == 3 ? -1 : 1;
3869
let shiftAmt = axis.labelGap * shiftDir;
3870
let baseLpos = round((axis._lpos + shiftAmt) * pxRatio);
3872
setFontStyle(axis.labelFont[0], fillStyle, "center", side == 2 ? TOP : BOTTOM);
3881
round(plotTop + plotHgt / 2),
3883
ctx.rotate((side == 3 ? -PI : PI) / 2);
3887
x = round(plotLft + plotWid / 2);
3891
ctx.fillText(axis.label, x, y);
3896
let [_incr, _space] = axis._found;
3901
let scale = scales[axis.scale];
3903
let plotDim = ori == 0 ? plotWid : plotHgt;
3904
let plotOff = ori == 0 ? plotLft : plotTop;
3906
let axisGap = round(axis.gap * pxRatio);
3908
let _splits = axis._splits;
3912
let splits = scale.distr == 2 ? _splits.map(i => data0[i]) : _splits;
3913
let incr = scale.distr == 2 ? data0[_splits[1]] - data0[_splits[0]] : _incr;
3915
let ticks = axis.ticks;
3916
let tickSize = ticks.show ? round(ticks.size * pxRatio) : 0;
3919
let angle = axis._rotate * -PI/180;
3921
let basePos = pxRound(axis._pos * pxRatio);
3922
let shiftAmt = (tickSize + axisGap) * shiftDir;
3923
let finalPos = basePos + shiftAmt;
3924
y = ori == 0 ? finalPos : 0;
3925
x = ori == 1 ? finalPos : 0;
3927
let font = axis.font[0];
3928
let textAlign = axis.align == 1 ? LEFT :
3929
axis.align == 2 ? RIGHT :
3932
ori == 0 ? "center" : side == 3 ? RIGHT : LEFT;
3933
let textBaseline = angle ||
3934
ori == 1 ? "middle" : side == 2 ? TOP : BOTTOM;
3936
setFontStyle(font, fillStyle, textAlign, textBaseline);
3938
let lineHeight = axis.font[1] * lineMult;
3940
let canOffs = _splits.map(val => pxRound(getPos(val, scale, plotDim, plotOff)));
3942
let _values = axis._values;
3944
for (let i = 0; i < _values.length; i++) {
3945
let val = _values[i];
3955
let _parts = val.indexOf("\n") == -1 ? [val] : val.split(/\n/gm);
3957
for (let j = 0; j < _parts.length; j++) {
3958
let text = _parts[j];
3962
ctx.translate(x, y + j * lineHeight);
3964
ctx.fillText(text, 0, 0);
3968
ctx.fillText(text, x, y + j * lineHeight);
3977
ticks.filter(self, splits, i, _space, incr),
3982
roundDec(ticks.width * pxRatio, 3),
3983
ticks.stroke(self, i),
3990
let grid = axis.grid;
3995
grid.filter(self, splits, i, _space, incr),
3998
ori == 0 ? plotTop : plotLft,
3999
ori == 0 ? plotHgt : plotWid,
4000
roundDec(grid.width * pxRatio, 3),
4001
grid.stroke(self, i),
4011
function resetYSeries(minMax) {
4014
series.forEach((s, i) => {
4024
s.facets.forEach(f => {
4034
let queuedCommit = false;
4037
if (!queuedCommit) {
4039
queuedCommit = true;
4043
function _commit() {
4046
if (shouldSetScales) {
4048
shouldSetScales = false;
4051
if (shouldConvergeSize) {
4053
shouldConvergeSize = false;
4056
if (shouldSetSize) {
4057
setStylePx(under, LEFT, plotLftCss);
4058
setStylePx(under, TOP, plotTopCss);
4059
setStylePx(under, WIDTH, plotWidCss);
4060
setStylePx(under, HEIGHT, plotHgtCss);
4062
setStylePx(over, LEFT, plotLftCss);
4063
setStylePx(over, TOP, plotTopCss);
4064
setStylePx(over, WIDTH, plotWidCss);
4065
setStylePx(over, HEIGHT, plotHgtCss);
4067
setStylePx(wrap, WIDTH, fullWidCss);
4068
setStylePx(wrap, HEIGHT, fullHgtCss);
4072
can.width = round(fullWidCss * pxRatio);
4073
can.height = round(fullHgtCss * pxRatio);
4077
let { _show, _el, _size, _pos, side } = a;
4080
let posOffset = (side === 3 || side === 0 ? _size : 0);
4081
let isVt = side % 2 == 1;
4083
setStylePx(_el, isVt ? "left" : "top", _pos - posOffset);
4084
setStylePx(_el, isVt ? "width" : "height", _size);
4085
setStylePx(_el, isVt ? "top" : "left", isVt ? plotTopCss : plotLftCss);
4086
setStylePx(_el, isVt ? "height" : "width", isVt ? plotHgtCss : plotWidCss);
4088
_el && remClass(_el, OFF);
4091
_el && addClass(_el, OFF);
4095
ctxStroke = ctxFill = ctxWidth = ctxJoin = ctxCap = ctxFont = ctxAlign = ctxBaseline = ctxDash = null;
4102
shouldSetSize = false;
4105
if (fullWidCss > 0 && fullHgtCss > 0) {
4106
ctx.clearRect(0, 0, can.width, can.height);
4108
drawOrder.forEach(fn => fn());
4121
if (cursor.show && shouldSetCursor) {
4122
updateCursor(null, true, false);
4123
shouldSetCursor = false;
4135
viaAutoScaleX = false;
4137
queuedCommit = false;
4140
self.redraw = (rebuildPaths, recalcAxes) => {
4141
shouldConvergeSize = recalcAxes || false;
4143
if (rebuildPaths !== false)
4144
_setScale(xScaleKey, scaleX.min, scaleX.max);
4152
function setScale(key, opts) {
4153
let sc = scales[key];
4155
if (sc.from == null) {
4157
let minMax = sc.range(self, opts.min, opts.max, key);
4158
opts.min = minMax[0];
4159
opts.max = minMax[1];
4162
if (opts.min > opts.max) {
4163
let _min = opts.min;
4164
opts.min = opts.max;
4168
if (dataLen > 1 && opts.min != null && opts.max != null && opts.max - opts.min < 1e-16)
4171
if (key == xScaleKey) {
4172
if (sc.distr == 2 && dataLen > 0) {
4173
opts.min = closestIdx(opts.min, data[0]);
4174
opts.max = closestIdx(opts.max, data[0]);
4176
if (opts.min == opts.max)
4183
pendScales[key] = opts;
4185
shouldSetScales = true;
4190
self.setScale = setScale;
4215
let dragging = false;
4217
const drag = cursor.drag;
4224
xCursor = placeDiv(CURSOR_X, over);
4226
yCursor = placeDiv(CURSOR_Y, over);
4228
if (scaleX.ori == 0) {
4237
mouseLeft1 = cursor.left;
4238
mouseTop1 = cursor.top;
4241
const select = self.select = assign({
4250
const selectDiv = select.show ? placeDiv(SELECT, select.over ? over : under) : null;
4252
function setSelect(opts, _fire) {
4254
for (let prop in opts)
4255
setStylePx(selectDiv, prop, select[prop] = opts[prop]);
4257
_fire !== false && fire("setSelect");
4261
self.setSelect = setSelect;
4263
function toggleDOM(i, onOff) {
4265
let label = showLegend ? legendRows[i] : null;
4268
label && remClass(label, OFF);
4270
label && addClass(label, OFF);
4271
cursorPts.length > 1 && elTrans(cursorPts[i], -10, -10, plotWidCss, plotHgtCss);
4275
function _setScale(key, min, max) {
4276
setScale(key, {min, max});
4279
function setSeries(i, opts, _fire, _pub) {
4284
if (opts.focus != null)
4287
if (opts.show != null) {
4289
toggleDOM(i, opts.show);
4291
_setScale(mode == 2 ? s.facets[1].scale : s.scale, null, null);
4295
_fire !== false && fire("setSeries", i, opts);
4297
_pub && pubSync("setSeries", self, i, opts);
4300
self.setSeries = setSeries;
4302
function setBand(bi, opts) {
4303
assign(bands[bi], opts);
4306
function addBand(opts, bi) {
4307
opts.fill = fnOrSelf(opts.fill || null);
4308
bi = bi == null ? bands.length : bi;
4309
bands.splice(bi, 0, opts);
4312
function delBand(bi) {
4316
bands.splice(bi, 1);
4319
self.addBand = addBand;
4320
self.setBand = setBand;
4321
self.delBand = delBand;
4323
function setAlpha(i, value) {
4324
series[i].alpha = value;
4326
if (cursor.show && cursorPts[i])
4327
cursorPts[i].style.opacity = value;
4329
if (showLegend && legendRows[i])
4330
legendRows[i].style.opacity = value;
4337
const FOCUS_TRUE = {focus: true};
4338
const FOCUS_FALSE = {focus: false};
4340
function setFocus(i) {
4341
if (i != focusedSeries) {
4344
let allFocused = i == null;
4346
let _setAlpha = focus.alpha != 1;
4348
series.forEach((s, i2) => {
4349
let isFocused = allFocused || i2 == 0 || i2 == i;
4350
s._focus = allFocused ? null : isFocused;
4351
_setAlpha && setAlpha(i2, isFocused ? 1 : focus.alpha);
4355
_setAlpha && commit();
4359
if (showLegend && cursorFocus) {
4360
on(mouseleave, legendEl, e => {
4363
setSeries(null, FOCUS_FALSE, true, syncOpts.setSeries);
4364
updateCursor(null, true, false);
4368
function posToVal(pos, scale, can) {
4369
let sc = scales[scale];
4372
pos = pos / pxRatio - (sc.ori == 1 ? plotTopCss : plotLftCss);
4374
let dim = plotWidCss;
4388
let sv = _min + (_max - _min) * pct;
4390
let distr = sc.distr;
4393
distr == 3 ? pow(10, sv) :
4394
distr == 4 ? sinh(sv, sc.asinh) :
4399
function closestIdxFromXpos(pos, can) {
4400
let v = posToVal(pos, xScaleKey, can);
4401
return closestIdx(v, data[0], i0, i1);
4404
self.valToIdx = val => closestIdx(val, data[0]);
4405
self.posToIdx = closestIdxFromXpos;
4406
self.posToVal = posToVal;
4407
self.valToPos = (val, scale, can) => (
4408
scales[scale].ori == 0 ?
4409
getHPos(val, scales[scale],
4410
can ? plotWid : plotWidCss,
4413
getVPos(val, scales[scale],
4414
can ? plotHgt : plotHgtCss,
4420
function batch(fn) {
4427
(self.setCursor = (opts, _fire, _pub) => {
4428
mouseLeft1 = opts.left;
4429
mouseTop1 = opts.top;
4431
updateCursor(null, _fire, _pub);
4434
function setSelH(off, dim) {
4435
setStylePx(selectDiv, LEFT, select.left = off);
4436
setStylePx(selectDiv, WIDTH, select.width = dim);
4439
function setSelV(off, dim) {
4440
setStylePx(selectDiv, TOP, select.top = off);
4441
setStylePx(selectDiv, HEIGHT, select.height = dim);
4444
let setSelX = scaleX.ori == 0 ? setSelH : setSelV;
4445
let setSelY = scaleX.ori == 1 ? setSelH : setSelV;
4447
function syncLegend() {
4448
if (showLegend && legend.live) {
4449
for (let i = mode == 2 ? 1 : 0; i < series.length; i++) {
4450
if (i == 0 && multiValLegend)
4453
let vals = legend.values[i];
4458
legendCells[i][j++].firstChild.nodeValue = vals[k];
4463
function setLegend(opts, _fire) {
4468
series.forEach((s, sidx) => {
4469
(sidx > 0 || !multiValLegend) && setLegendValues(sidx, idx);
4473
if (showLegend && legend.live)
4476
shouldSetLegend = false;
4478
_fire !== false && fire("setLegend");
4481
self.setLegend = setLegend;
4483
function setLegendValues(sidx, idx) {
4487
val = NULL_LEGEND_VALUES;
4489
let s = series[sidx];
4490
let src = sidx == 0 && xScaleDistr == 2 ? data0 : data[sidx];
4491
val = multiValLegend ? s.values(self, sidx, idx) : {_: s.value(self, src[idx], sidx, idx)};
4494
legend.values[sidx] = val;
4497
function updateCursor(src, _fire, _pub) {
4500
rawMouseLeft1 = mouseLeft1;
4501
rawMouseTop1 = mouseTop1;
4503
[mouseLeft1, mouseTop1] = cursor.move(self, mouseLeft1, mouseTop1);
4506
vCursor && elTrans(vCursor, round(mouseLeft1), 0, plotWidCss, plotHgtCss);
4507
hCursor && elTrans(hCursor, 0, round(mouseTop1), plotWidCss, plotHgtCss);
4514
let noDataInRange = i0 > i1;
4519
let xDim = scaleX.ori == 0 ? plotWidCss : plotHgtCss;
4520
let yDim = scaleX.ori == 1 ? plotWidCss : plotHgtCss;
4523
if (mouseLeft1 < 0 || dataLen == 0 || noDataInRange) {
4526
for (let i = 0; i < series.length; i++) {
4528
cursorPts.length > 1 && elTrans(cursorPts[i], -10, -10, plotWidCss, plotHgtCss);
4533
setSeries(null, FOCUS_TRUE, true, src == null && syncOpts.setSeries);
4536
activeIdxs.fill(null);
4537
shouldSetLegend = true;
4539
for (let i = 0; i < series.length; i++)
4540
legend.values[i] = NULL_LEGEND_VALUES;
4546
let mouseXPos, valAtPosX, xPos;
4549
mouseXPos = scaleX.ori == 0 ? mouseLeft1 : mouseTop1;
4550
valAtPosX = posToVal(mouseXPos, xScaleKey);
4551
idx = closestIdx(valAtPosX, data[0], i0, i1);
4552
xPos = incrRoundUp(valToPosX(data[0][idx], scaleX, xDim, 0), 0.5);
4555
for (let i = mode == 2 ? 1 : 0; i < series.length; i++) {
4558
let idx1 = activeIdxs[i];
4559
let yVal1 = mode == 1 ? data[i][idx1] : data[i][1][idx1];
4561
let idx2 = cursor.dataIdx(self, i, idx, valAtPosX);
4562
let yVal2 = mode == 1 ? data[i][idx2] : data[i][1][idx2];
4564
shouldSetLegend = shouldSetLegend || yVal2 != yVal1 || idx2 != idx1;
4566
activeIdxs[i] = idx2;
4568
let xPos2 = idx2 == idx ? xPos : incrRoundUp(valToPosX(mode == 1 ? data[0][idx2] : data[i][0][idx2], scaleX, xDim, 0), 0.5);
4570
if (i > 0 && s.show) {
4571
let yPos = yVal2 == null ? -10 : incrRoundUp(valToPosY(yVal2, mode == 1 ? scales[s.scale] : scales[s.facets[1].scale], yDim, 0), 0.5);
4573
if (yPos > 0 && mode == 1) {
4574
let dist = abs(yPos - mouseTop1);
4576
if (dist <= closestDist) {
4584
if (scaleX.ori == 0) {
4593
if (shouldSetLegend && cursorPts.length > 1) {
4594
elColor(cursorPts[i], cursor.points.fill(self, i), cursor.points.stroke(self, i));
4596
let ptWid, ptHgt, ptLft, ptTop,
4598
getBBox = cursor.points.bbox;
4600
if (getBBox != null) {
4603
let bbox = getBBox(self, i);
4608
ptHgt = bbox.height;
4613
ptWid = ptHgt = cursor.points.size(self, i);
4616
elSize(cursorPts[i], ptWid, ptHgt, centered);
4617
elTrans(cursorPts[i], ptLft, ptTop, plotWidCss, plotHgtCss);
4622
if (!shouldSetLegend || i == 0 && multiValLegend)
4625
setLegendValues(i, idx2);
4631
cursor.left = mouseLeft1;
4632
cursor.top = mouseTop1;
4634
if (shouldSetLegend) {
4640
if (select.show && dragging) {
4642
let [xKey, yKey] = syncOpts.scales;
4643
let [matchXKeys, matchYKeys] = syncOpts.match;
4644
let [xKeySrc, yKeySrc] = src.cursor.sync.scales;
4647
let sdrag = src.cursor.drag;
4651
let { left, top, width, height } = src.select;
4653
let sori = src.scales[xKey].ori;
4654
let sPosToVal = src.posToVal;
4656
let sOff, sDim, sc, a, b;
4658
let matchingX = xKey != null && matchXKeys(xKey, xKeySrc);
4659
let matchingY = yKey != null && matchYKeys(yKey, yKeySrc);
4674
a = valToPosX(sPosToVal(sOff, xKeySrc), sc, xDim, 0);
4675
b = valToPosX(sPosToVal(sOff + sDim, xKeySrc), sc, xDim, 0);
4677
setSelX(min(a,b), abs(b-a));
4699
a = valToPosY(sPosToVal(sOff, yKeySrc), sc, yDim, 0);
4700
b = valToPosY(sPosToVal(sOff + sDim, yKeySrc), sc, yDim, 0);
4702
setSelY(min(a,b), abs(b-a));
4712
let rawDX = abs(rawMouseLeft1 - rawMouseLeft0);
4713
let rawDY = abs(rawMouseTop1 - rawMouseTop0);
4715
if (scaleX.ori == 1) {
4721
dragX = drag.x && rawDX >= drag.dist;
4722
dragY = drag.y && rawDY >= drag.dist;
4728
if (dragX && dragY) {
4729
dragX = rawDX >= uni;
4730
dragY = rawDY >= uni;
4733
if (!dragX && !dragY) {
4741
else if (drag.x && drag.y && (dragX || dragY))
4743
dragX = dragY = true;
4748
if (scaleX.ori == 0) {
4757
setSelX(min(p0, p1), abs(p1 - p0));
4764
if (scaleX.ori == 1) {
4773
setSelY(min(p0, p1), abs(p1 - p0));
4780
if (!dragX && !dragY) {
4792
if (syncKey != null) {
4793
let [xSyncKey, ySyncKey] = syncOpts.scales;
4795
syncOpts.values[0] = xSyncKey != null ? posToVal(scaleX.ori == 0 ? mouseLeft1 : mouseTop1, xSyncKey) : null;
4796
syncOpts.values[1] = ySyncKey != null ? posToVal(scaleX.ori == 1 ? mouseLeft1 : mouseTop1, ySyncKey) : null;
4799
pubSync(mousemove, self, mouseLeft1, mouseTop1, plotWidCss, plotHgtCss, idx);
4803
let shouldPub = _pub && syncOpts.setSeries;
4806
if (focusedSeries == null) {
4807
if (closestDist <= p)
4808
setSeries(closestSeries, FOCUS_TRUE, true, shouldPub);
4811
if (closestDist > p)
4812
setSeries(null, FOCUS_TRUE, true, shouldPub);
4813
else if (closestSeries != focusedSeries)
4814
setSeries(closestSeries, FOCUS_TRUE, true, shouldPub);
4819
ready && _fire !== false && fire("setCursor");
4824
function syncRect(defer) {
4828
rect = over.getBoundingClientRect();
4829
fire("syncRect", rect);
4833
function mouseMove(e, src, _l, _t, _w, _h, _i) {
4837
cacheMouse(e, src, _l, _t, _w, _h, _i, false, e != null);
4840
updateCursor(null, true, true);
4842
updateCursor(src, true, false);
4845
function cacheMouse(e, src, _l, _t, _w, _h, _i, initial, snap) {
4850
_l = e.clientX - rect.left;
4851
_t = e.clientY - rect.top;
4854
if (_l < 0 || _t < 0) {
4860
let [xKey, yKey] = syncOpts.scales;
4862
let syncOptsSrc = src.cursor.sync;
4863
let [xValSrc, yValSrc] = syncOptsSrc.values;
4864
let [xKeySrc, yKeySrc] = syncOptsSrc.scales;
4865
let [matchXKeys, matchYKeys] = syncOpts.match;
4867
let rotSrc = src.scales[xKeySrc].ori == 1;
4869
let xDim = scaleX.ori == 0 ? plotWidCss : plotHgtCss,
4870
yDim = scaleX.ori == 1 ? plotWidCss : plotHgtCss,
4871
_xDim = rotSrc ? _h : _w,
4872
_yDim = rotSrc ? _w : _h,
4873
_xPos = rotSrc ? _t : _l,
4874
_yPos = rotSrc ? _l : _t;
4876
if (xKeySrc != null)
4877
_l = matchXKeys(xKey, xKeySrc) ? getPos(xValSrc, scales[xKey], xDim, 0) : -10;
4879
_l = xDim * (_xPos/_xDim);
4881
if (yKeySrc != null)
4882
_t = matchYKeys(yKey, yKeySrc) ? getPos(yValSrc, scales[yKey], yDim, 0) : -10;
4884
_t = yDim * (_yPos/_yDim);
4886
if (scaleX.ori == 1) {
4894
if (_l <= 1 || _l >= plotWidCss - 1)
4895
_l = incrRound(_l, plotWidCss);
4897
if (_t <= 1 || _t >= plotHgtCss - 1)
4898
_t = incrRound(_t, plotHgtCss);
4905
[mouseLeft0, mouseTop0] = cursor.move(self, _l, _t);
4913
function hideSelect() {
4920
function mouseDown(e, src, _l, _t, _w, _h, _i) {
4922
dragX = dragY = drag._x = drag._y = false;
4924
cacheMouse(e, src, _l, _t, _w, _h, _i, true, false);
4927
onMouse(mouseup, doc, mouseUp);
4928
pubSync(mousedown, self, mouseLeft0, mouseTop0, plotWidCss, plotHgtCss, null);
4932
function mouseUp(e, src, _l, _t, _w, _h, _i) {
4933
dragging = drag._x = drag._y = false;
4935
cacheMouse(e, src, _l, _t, _w, _h, _i, false, true);
4937
let { left, top, width, height } = select;
4939
let hasSelect = width > 0 || height > 0;
4941
hasSelect && setSelect(select);
4943
if (drag.setScale && hasSelect) {
4954
if (scaleX.ori == 1) {
4962
_setScale(xScaleKey,
4963
posToVal(xOff, xScaleKey),
4964
posToVal(xOff + xDim, xScaleKey)
4969
for (let k in scales) {
4972
if (k != xScaleKey && sc.from == null && sc.min != inf) {
4974
posToVal(yOff + yDim, k),
4983
else if (cursor.lock) {
4984
cursor._lock = !cursor._lock;
4987
updateCursor(null, true, false);
4991
offMouse(mouseup, doc);
4992
pubSync(mouseup, self, mouseLeft1, mouseTop1, plotWidCss, plotHgtCss, null);
4996
function mouseLeave(e, src, _l, _t, _w, _h, _i) {
4997
if (!cursor._lock) {
4998
let _dragging = dragging;
5008
if (scaleX.ori == 0) {
5017
if (dragH && dragV) {
5019
snapH = mouseLeft1 <= snapProx || mouseLeft1 >= plotWidCss - snapProx;
5020
snapV = mouseTop1 <= snapProx || mouseTop1 >= plotHgtCss - snapProx;
5024
mouseLeft1 = mouseLeft1 < mouseLeft0 ? 0 : plotWidCss;
5027
mouseTop1 = mouseTop1 < mouseTop0 ? 0 : plotHgtCss;
5029
updateCursor(null, true, true);
5038
updateCursor(null, true, true);
5041
dragging = _dragging;
5045
function dblClick(e, src, _l, _t, _w, _h, _i) {
5051
pubSync(dblclick, self, mouseLeft1, mouseTop1, plotWidCss, plotHgtCss, null);
5054
function syncPxRatio() {
5055
axes.forEach(syncFontSize);
5056
_setSize(self.width, self.height, true);
5059
on(dppxchange, win, syncPxRatio);
5064
events.mousedown = mouseDown;
5065
events.mousemove = mouseMove;
5066
events.mouseup = mouseUp;
5067
events.dblclick = dblClick;
5068
events["setSeries"] = (e, src, idx, opts) => {
5069
setSeries(idx, opts, true, false);
5073
onMouse(mousedown, over, mouseDown);
5074
onMouse(mousemove, over, mouseMove);
5075
onMouse(mouseenter, over, syncRect);
5076
onMouse(mouseleave, over, mouseLeave);
5078
onMouse(dblclick, over, dblClick);
5080
cursorPlots.add(self);
5082
self.syncRect = syncRect;
5086
const hooks = self.hooks = opts.hooks || {};
5088
function fire(evName, a1, a2) {
5089
if (evName in hooks) {
5090
hooks[evName].forEach(fn => {
5091
fn.call(null, self, a1, a2);
5096
(opts.plugins || []).forEach(p => {
5097
for (let evName in p.hooks)
5098
hooks[evName] = (hooks[evName] || []).concat(p.hooks[evName]);
5101
const syncOpts = assign({
5108
scales: [xScaleKey, series[1] ? series[1].scale : null],
5109
match: [retEq, retEq],
5110
values: [null, null],
5113
(cursor.sync = syncOpts);
5115
const syncKey = syncOpts.key;
5117
const sync = _sync(syncKey);
5119
function pubSync(type, src, x, y, w, h, i) {
5120
if (syncOpts.filters.pub(type, src, x, y, w, h, i))
5121
sync.pub(type, src, x, y, w, h, i);
5126
function pub(type, src, x, y, w, h, i) {
5127
if (syncOpts.filters.sub(type, src, x, y, w, h, i))
5128
events[type](null, src, x, y, w, h, i);
5133
function destroy() {
5135
cursorPlots.delete(self);
5136
mouseListeners.clear();
5137
off(dppxchange, win, syncPxRatio);
5142
self.destroy = destroy;
5145
fire("init", opts, data);
5147
setData(data || opts.data, false);
5149
if (pendScales[xScaleKey])
5150
setScale(xScaleKey, pendScales[xScaleKey]);
5154
_setSize(opts.width, opts.height);
5156
updateCursor(null, true, false);
5158
setSelect(select, false);
5161
series.forEach(initSeries);
5163
axes.forEach(initAxis);
5166
if (then instanceof HTMLElement) {
5167
then.appendChild(root);
5179
uPlot.assign = assign;
5180
uPlot.fmtNum = fmtNum;
5181
uPlot.rangeNum = rangeNum;
5182
uPlot.rangeLog = rangeLog;
5183
uPlot.rangeAsinh = rangeAsinh;
5184
uPlot.orient = orient;
5191
uPlot.fmtDate = fmtDate;
5192
uPlot.tzDate = tzDate;
5200
uPlot.addGap = addGap;
5201
uPlot.clipGaps = clipGaps;
5203
let paths = uPlot.paths = {
5207
(paths.linear = linear);
5208
(paths.stepped = stepped);
5209
(paths.bars = bars);
5210
(paths.spline = monotoneCubic);