10
const FEAT_TIME = true;
13
function closestIdx(num, arr, lo, hi) {
16
hi = hi || arr.length - 1;
17
let bitwise = hi <= 2147483647;
20
mid = bitwise ? (lo + hi) >> 1 : floor((lo + hi) / 2);
28
if (num - arr[lo] <= arr[hi] - num)
34
function nonNullIdx(data, _i0, _i1, dir) {
35
for (let i = dir == 1 ? _i0 : _i1; i >= _i0 && i <= _i1; i += dir) {
43
function getMinMax(data, _i0, _i1, sorted) {
53
else if (sorted == -1) {
58
for (let i = _i0; i <= _i1; i++) {
59
if (data[i] != null) {
60
_min = min(_min, data[i]);
61
_max = max(_max, data[i]);
69
function getMinMaxLog(data, _i0, _i1) {
75
for (let i = _i0; i <= _i1; i++) {
77
_min = min(_min, data[i]);
78
_max = max(_max, data[i]);
83
_min == inf ? 1 : _min,
84
_max == -inf ? 10 : _max,
88
const _fixedTuple = [0, 0];
90
function fixIncr(minIncr, maxIncr, minExp, maxExp) {
91
_fixedTuple[0] = minExp < 0 ? roundDec(minIncr, -minExp) : minIncr;
92
_fixedTuple[1] = maxExp < 0 ? roundDec(maxIncr, -maxExp) : maxIncr;
96
function rangeLog(min, max, base, fullMags) {
97
let minSign = sign(min);
99
let logFn = base == 10 ? log10 : log2;
112
let minExp, maxExp, minMaxIncrs;
115
minExp = floor(logFn(min));
116
maxExp = ceil(logFn(max));
118
minMaxIncrs = fixIncr(pow(base, minExp), pow(base, maxExp), minExp, maxExp);
120
min = minMaxIncrs[0];
121
max = minMaxIncrs[1];
124
minExp = floor(logFn(abs(min)));
125
maxExp = floor(logFn(abs(max)));
127
minMaxIncrs = fixIncr(pow(base, minExp), pow(base, maxExp), minExp, maxExp);
129
min = incrRoundDn(min, minMaxIncrs[0]);
130
max = incrRoundUp(max, minMaxIncrs[1]);
136
function rangeAsinh(min, max, base, fullMags) {
137
let minMax = rangeLog(min, max, base, fullMags);
150
const autoRangePart = {
155
const _eqRangePart = {
168
function rangeNum(_min, _max, mult, extra) {
170
return _rangeNum(_min, _max, mult);
172
_eqRangePart.pad = mult;
173
_eqRangePart.soft = extra ? 0 : null;
174
_eqRangePart.mode = extra ? 3 : 0;
176
return _rangeNum(_min, _max, _eqRange);
180
function ifNull(lh, rh) {
181
return lh == null ? rh : lh;
186
function hasData(data, idx0, idx1) {
187
idx0 = ifNull(idx0, 0);
188
idx1 = ifNull(idx1, data.length - 1);
190
while (idx0 <= idx1) {
191
if (data[idx0] != null)
199
function _rangeNum(_min, _max, cfg) {
203
let padMin = ifNull(cmin.pad, 0);
204
let padMax = ifNull(cmax.pad, 0);
206
let hardMin = ifNull(cmin.hard, -inf);
207
let hardMax = ifNull(cmax.hard, inf);
209
let softMin = ifNull(cmin.soft, inf);
210
let softMax = ifNull(cmax.soft, -inf);
212
let softMinMode = ifNull(cmin.mode, 0);
213
let softMaxMode = ifNull(cmax.mode, 0);
215
let delta = _max - _min;
228
if (_min == 0 || _max == 0) {
231
if (softMinMode == 2 && softMin != inf)
234
if (softMaxMode == 2 && softMax != -inf)
239
let nonZeroDelta = delta || abs(_max) || 1e3;
240
let mag = log10(nonZeroDelta);
241
let base = pow(10, floor(mag));
243
let _padMin = nonZeroDelta * (delta == 0 ? (_min == 0 ? .1 : 1) : padMin);
244
let _newMin = roundDec(incrRoundDn(_min - _padMin, base/10), 9);
245
let _softMin = _min >= softMin && (softMinMode == 1 || softMinMode == 3 && _newMin <= softMin || softMinMode == 2 && _newMin >= softMin) ? softMin : inf;
246
let minLim = max(hardMin, _newMin < _softMin && _min >= _softMin ? _softMin : min(_softMin, _newMin));
248
let _padMax = nonZeroDelta * (delta == 0 ? (_max == 0 ? .1 : 1) : padMax);
249
let _newMax = roundDec(incrRoundUp(_max + _padMax, base/10), 9);
250
let _softMax = _max <= softMax && (softMaxMode == 1 || softMaxMode == 3 && _newMax >= softMax || softMaxMode == 2 && _newMax <= softMax) ? softMax : -inf;
251
let maxLim = min(hardMax, _newMax > _softMax && _max <= _softMax ? _softMax : max(_softMax, _newMax));
253
if (minLim == maxLim && minLim == 0)
256
return [minLim, maxLim];
260
const fmtNum = new Intl.NumberFormat(navigator.language).format;
266
const floor = M.floor;
267
const round = M.round;
273
const log10 = M.log10;
276
const sinh = (v, linthresh = 1) => M.sinh(v) * linthresh;
277
const asinh = (v, linthresh = 1) => M.asinh(v / linthresh);
281
function numIntDigits(x) {
282
return (log10((x ^ (x >> 31)) - (x >> 31)) | 0) + 1;
285
function incrRound(num, incr) {
286
return round(num/incr)*incr;
289
function clamp(num, _min, _max) {
290
return min(max(num, _min), _max);
293
function fnOrSelf(v) {
294
return typeof v == "function" ? v : () => v;
297
const retArg0 = _0 => _0;
299
const retArg1 = (_0, _1) => _1;
301
const retNull = _ => null;
303
const retTrue = _ => true;
305
const retEq = (a, b) => a == b;
307
function incrRoundUp(num, incr) {
308
return ceil(num/incr)*incr;
311
function incrRoundDn(num, incr) {
312
return floor(num/incr)*incr;
315
function roundDec(val, dec) {
316
return round(val * (dec = 10**dec)) / dec;
319
const fixedDec = new Map();
321
function guessDec(num) {
322
return ((""+num).split(".")[1] || "").length;
325
function genIncrs(base, minExp, maxExp, mults) {
328
let multDec = mults.map(guessDec);
330
for (let exp = minExp; exp < maxExp; exp++) {
332
let mag = roundDec(pow(base, exp), expa);
334
for (let i = 0; i < mults.length; i++) {
335
let _incr = mults[i] * mag;
336
let dec = (_incr >= 0 && exp >= 0 ? 0 : expa) + (exp >= multDec[i] ? 0 : multDec[i]);
337
let incr = roundDec(_incr, dec);
339
fixedDec.set(incr, dec);
351
const nullNullTuple = [null, null];
353
const isArr = Array.isArray;
356
return typeof v == 'string';
363
let c = v.constructor;
364
is = c == null || c == Object;
370
function fastIsObj(v) {
371
return v != null && typeof v == 'object';
374
function copy(o, _isObj = isObj) {
378
let val = o.find(v => v != null);
380
if (isArr(val) || _isObj(val)) {
381
out = Array(o.length);
382
for (let i = 0; i < o.length; i++)
383
out[i] = copy(o[i], _isObj);
388
else if (_isObj(o)) {
391
out[k] = copy(o[k], _isObj);
399
function assign(targ) {
400
let args = arguments;
402
for (let i = 1; i < args.length; i++) {
405
for (let key in src) {
406
if (isObj(targ[key]))
407
assign(targ[key], copy(src[key]));
409
targ[key] = copy(src[key]);
417
const NULL_REMOVE = 0;
418
const NULL_RETAIN = 1;
419
const NULL_EXPAND = 2;
422
function nullExpand(yVals, nullIdxs, alignedLen) {
423
for (let i = 0, xi, lastNullIdx = -1; i < nullIdxs.length; i++) {
424
let nullIdx = nullIdxs[i];
426
if (nullIdx > lastNullIdx) {
428
while (xi >= 0 && yVals[xi] == null)
432
while (xi < alignedLen && yVals[xi] == null)
433
yVals[lastNullIdx = xi++] = null;
440
function join(tables, nullModes) {
441
let xVals = new Set();
443
for (let ti = 0; ti < tables.length; ti++) {
448
for (let i = 0; i < len; i++)
452
let data = [Array.from(xVals).sort((a, b) => a - b)];
454
let alignedLen = data[0].length;
456
let xIdxs = new Map();
458
for (let i = 0; i < alignedLen; i++)
459
xIdxs.set(data[0][i], i);
461
for (let ti = 0; ti < tables.length; ti++) {
465
for (let si = 1; si < t.length; si++) {
468
let yVals = Array(alignedLen).fill(undefined);
470
let nullMode = nullModes ? nullModes[ti][si] : NULL_RETAIN;
474
for (let i = 0; i < ys.length; i++) {
476
let alignedIdx = xIdxs.get(xs[i]);
479
if (nullMode != NULL_REMOVE) {
480
yVals[alignedIdx] = yVal;
482
if (nullMode == NULL_EXPAND)
483
nullIdxs.push(alignedIdx);
487
yVals[alignedIdx] = yVal;
490
nullExpand(yVals, nullIdxs, alignedLen);
499
const microTask = typeof queueMicrotask == "undefined" ? fn => Promise.resolve().then(fn) : queueMicrotask;
501
const WIDTH = "width";
502
const HEIGHT = "height";
504
const BOTTOM = "bottom";
506
const RIGHT = "right";
507
const hexBlack = "#000";
508
const transparent = hexBlack + "0";
510
const mousemove = "mousemove";
511
const mousedown = "mousedown";
512
const mouseup = "mouseup";
513
const mouseenter = "mouseenter";
514
const mouseleave = "mouseleave";
515
const dblclick = "dblclick";
516
const resize = "resize";
517
const scroll = "scroll";
519
const change = "change";
520
const dppxchange = "dppxchange";
524
const UPLOT = "uplot";
525
const ORI_HZ = pre + "hz";
526
const ORI_VT = pre + "vt";
527
const TITLE = pre + "title";
528
const WRAP = pre + "wrap";
529
const UNDER = pre + "under";
530
const OVER = pre + "over";
531
const AXIS = pre + "axis";
532
const OFF = pre + "off";
533
const SELECT = pre + "select";
534
const CURSOR_X = pre + "cursor-x";
535
const CURSOR_Y = pre + "cursor-y";
536
const CURSOR_PT = pre + "cursor-pt";
537
const LEGEND = pre + "legend";
538
const LEGEND_LIVE = pre + "live";
539
const LEGEND_INLINE = pre + "inline";
540
const LEGEND_THEAD = pre + "thead";
541
const LEGEND_SERIES = pre + "series";
542
const LEGEND_MARKER = pre + "marker";
543
const LEGEND_LABEL = pre + "label";
544
const LEGEND_VALUE = pre + "value";
552
function setPxRatio() {
553
let _pxRatio = devicePixelRatio;
556
if (pxRatio != _pxRatio) {
559
query && off(change, query, setPxRatio);
560
query = matchMedia(`(min-resolution: ${pxRatio - 0.001}dppx) and (max-resolution: ${pxRatio + 0.001}dppx)`);
561
on(change, query, setPxRatio);
563
win.dispatchEvent(new CustomEvent(dppxchange));
567
function addClass(el, c) {
569
let cl = el.classList;
570
!cl.contains(c) && cl.add(c);
574
function remClass(el, c) {
575
let cl = el.classList;
576
cl.contains(c) && cl.remove(c);
579
function setStylePx(el, name, value) {
580
el.style[name] = value + "px";
583
function placeTag(tag, cls, targ, refEl) {
584
let el = doc.createElement(tag);
590
targ.insertBefore(el, refEl);
595
function placeDiv(cls, targ) {
596
return placeTag("div", cls, targ);
599
const xformCache = new WeakMap();
601
function elTrans(el, xPos, yPos, xMax, yMax) {
602
let xform = "translate(" + xPos + "px," + yPos + "px)";
603
let xformOld = xformCache.get(el);
605
if (xform != xformOld) {
606
el.style.transform = xform;
607
xformCache.set(el, xform);
609
if (xPos < 0 || yPos < 0 || xPos > xMax || yPos > yMax)
616
const colorCache = new WeakMap();
618
function elColor(el, background, borderColor) {
619
let newColor = background + borderColor;
620
let oldColor = colorCache.get(el);
622
if (newColor != oldColor) {
623
colorCache.set(el, newColor);
624
el.style.background = background;
625
el.style.borderColor = borderColor;
629
const sizeCache = new WeakMap();
631
function elSize(el, newWid, newHgt, centered) {
632
let newSize = newWid + "" + newHgt;
633
let oldSize = sizeCache.get(el);
635
if (newSize != oldSize) {
636
sizeCache.set(el, newSize);
637
el.style.height = newHgt + "px";
638
el.style.width = newWid + "px";
639
el.style.marginLeft = centered ? -newWid/2 + "px" : 0;
640
el.style.marginTop = centered ? -newHgt/2 + "px" : 0;
644
const evOpts = {passive: true};
645
const evOpts2 = assign({capture: true}, evOpts);
647
function on(ev, el, cb, capt) {
648
el.addEventListener(ev, cb, capt ? evOpts2 : evOpts);
651
function off(ev, el, cb, capt) {
652
el.removeEventListener(ev, cb, capt ? evOpts2 : evOpts);
682
function slice3(str) {
683
return str.slice(0, 3);
686
const days3 = days.map(slice3);
688
const months3 = months.map(slice3);
697
function zeroPad2(int) {
698
return (int < 10 ? '0' : '') + int;
701
function zeroPad3(int) {
702
return (int < 10 ? '00' : int < 100 ? '0' : '') + int;
719
YYYY: d => d.getFullYear(),
721
YY: d => (d.getFullYear()+'').slice(2),
723
MMMM: (d, names) => names.MMMM[d.getMonth()],
725
MMM: (d, names) => names.MMM[d.getMonth()],
727
MM: d => zeroPad2(d.getMonth()+1),
729
M: d => d.getMonth()+1,
731
DD: d => zeroPad2(d.getDate()),
735
WWWW: (d, names) => names.WWWW[d.getDay()],
737
WWW: (d, names) => names.WWW[d.getDay()],
739
HH: d => zeroPad2(d.getHours()),
741
H: d => d.getHours(),
743
h: d => {let h = d.getHours(); return h == 0 ? 12 : h > 12 ? h - 12 : h;},
745
AA: d => d.getHours() >= 12 ? 'PM' : 'AM',
747
aa: d => d.getHours() >= 12 ? 'pm' : 'am',
749
a: d => d.getHours() >= 12 ? 'p' : 'a',
751
mm: d => zeroPad2(d.getMinutes()),
753
m: d => d.getMinutes(),
755
ss: d => zeroPad2(d.getSeconds()),
757
s: d => d.getSeconds(),
759
fff: d => zeroPad3(d.getMilliseconds()),
762
function fmtDate(tpl, names) {
763
names = names || engNames;
766
let R = /\{([a-z]+)\}|[^{]+/gi, m;
768
while (m = R.exec(tpl))
769
parts.push(m[0][0] == '{' ? subs[m[1]] : m[0]);
774
for (let i = 0; i < parts.length; i++)
775
out += typeof parts[i] == "string" ? parts[i] : parts[i](d, names);
781
const localTz = new Intl.DateTimeFormat().resolvedOptions().timeZone;
784
function tzDate(date, tz) {
788
if (tz == 'UTC' || tz == 'Etc/UTC')
789
date2 = new Date(+date + date.getTimezoneOffset() * 6e4);
790
else if (tz == localTz)
793
date2 = new Date(date.toLocaleString('en-US', {timeZone: tz}));
794
date2.setMilliseconds(date.getMilliseconds());
804
const onlyWhole = v => v % 1 == 0;
806
const allMults = [1,2,2.5,5];
809
const decIncrs = genIncrs(10, -16, 0, allMults);
812
const oneIncrs = genIncrs(10, 0, 16, allMults);
815
const wholeIncrs = oneIncrs.filter(onlyWhole);
817
const numIncrs = decIncrs.concat(oneIncrs);
821
const yyyy = "{YYYY}";
822
const NLyyyy = NL + yyyy;
825
const NLmdyy = NLmd + "/{YY}";
828
const hmm = "{h}:{mm}";
829
const hmmaa = hmm + aa;
830
const NLhmmaa = NL + hmmaa;
835
function genTimeStuffs(ms) {
844
let subSecIncrs = ms == 1 ? genIncrs(10, 0, 3, allMults).filter(onlyWhole) : genIncrs(10, -3, 0, allMults);
846
let timeIncrs = subSecIncrs.concat([
899
const _timeAxisStamps = [
901
[y, yyyy, _, _, _, _, _, _, 1],
902
[d * 28, "{MMM}", NLyyyy, _, _, _, _, _, 1],
903
[d, md, NLyyyy, _, _, _, _, _, 1],
904
[h, "{h}" + aa, NLmdyy, _, NLmd, _, _, _, 1],
905
[m, hmmaa, NLmdyy, _, NLmd, _, _, _, 1],
906
[s, ss, NLmdyy + " " + hmmaa, _, NLmd + " " + hmmaa, _, NLhmmaa, _, 1],
907
[ms, ss + ".{fff}", NLmdyy + " " + hmmaa, _, NLmd + " " + hmmaa, _, NLhmmaa, _, 1],
914
function timeAxisSplits(tzDate) {
915
return (self, axisIdx, scaleMin, scaleMax, foundIncr, foundSpace) => {
917
let isYr = foundIncr >= y;
918
let isMo = foundIncr >= mo && foundIncr < y;
921
let minDate = tzDate(scaleMin);
922
let minDateTs = roundDec(minDate * ms, 3);
925
let minMin = mkDate(minDate.getFullYear(), isYr ? 0 : minDate.getMonth(), isMo || isYr ? 1 : minDate.getDate());
926
let minMinTs = roundDec(minMin * ms, 3);
929
let moIncr = isMo ? foundIncr / mo : 0;
930
let yrIncr = isYr ? foundIncr / y : 0;
932
let split = minDateTs == minMinTs ? minDateTs : roundDec(mkDate(minMin.getFullYear() + yrIncr, minMin.getMonth() + moIncr, 1) * ms, 3);
933
let splitDate = new Date(round(split / ms));
934
let baseYear = splitDate.getFullYear();
935
let baseMonth = splitDate.getMonth();
937
for (let i = 0; split <= scaleMax; i++) {
938
let next = mkDate(baseYear + yrIncr * i, baseMonth + moIncr * i, 1);
939
let offs = next - tzDate(roundDec(next * ms, 3));
941
split = roundDec((+next + offs) * ms, 3);
943
if (split <= scaleMax)
948
let incr0 = foundIncr >= d ? d : foundIncr;
949
let tzOffset = floor(scaleMin) - floor(minDateTs);
950
let split = minMinTs + tzOffset + incrRoundUp(minDateTs - minMinTs, incr0);
953
let date0 = tzDate(split);
955
let prevHour = date0.getHours() + (date0.getMinutes() / m) + (date0.getSeconds() / h);
956
let incrHours = foundIncr / h;
958
let minSpace = self.axes[axisIdx]._space;
959
let pctSpace = foundSpace / minSpace;
962
split = roundDec(split + foundIncr, ms == 1 ? 0 : 3);
964
if (split > scaleMax)
968
let expectedHour = floor(roundDec(prevHour + incrHours, 6)) % 24;
969
let splitDate = tzDate(split);
970
let actualHour = splitDate.getHours();
972
let dstShift = actualHour - expectedHour;
977
split -= dstShift * h;
979
prevHour = (prevHour + incrHours) % 24;
982
let prevSplit = splits[splits.length - 1];
983
let pctIncr = roundDec((split - prevSplit) / foundIncr, 3);
985
if (pctIncr * pctSpace >= .7)
1004
const [ timeIncrsMs, _timeAxisStampsMs, timeAxisSplitsMs ] = genTimeStuffs(1);
1005
const [ timeIncrsS, _timeAxisStampsS, timeAxisSplitsS ] = genTimeStuffs(1e-3);
1008
genIncrs(2, -53, 53, [1]);
1021
function timeAxisStamps(stampCfg, fmtDate) {
1022
return stampCfg.map(s => s.map((v, i) =>
1023
i == 0 || i == 8 || v == null ? v : fmtDate(i == 1 || s[8] == 0 ? v : s[1] + v)
1029
function timeAxisVals(tzDate, stamps) {
1030
return (self, splits, axisIdx, foundSpace, foundIncr) => {
1031
let s = stamps.find(s => foundIncr >= s[0]) || stamps[stamps.length - 1];
1041
return splits.map(split => {
1042
let date = tzDate(split);
1044
let newYear = date.getFullYear();
1045
let newMnth = date.getMonth();
1046
let newDate = date.getDate();
1047
let newHour = date.getHours();
1048
let newMins = date.getMinutes();
1049
let newSecs = date.getSeconds();
1052
newYear != prevYear && s[2] ||
1053
newMnth != prevMnth && s[3] ||
1054
newDate != prevDate && s[4] ||
1055
newHour != prevHour && s[5] ||
1056
newMins != prevMins && s[6] ||
1057
newSecs != prevSecs && s[7] ||
1074
function timeAxisVal(tzDate, dateTpl) {
1075
let stamp = fmtDate(dateTpl);
1076
return (self, splits, axisIdx, foundSpace, foundIncr) => splits.map(split => stamp(tzDate(split)));
1079
function mkDate(y, m, d) {
1080
return new Date(y, m, d);
1083
function timeSeriesStamp(stampCfg, fmtDate) {
1084
return fmtDate(stampCfg);
1086
const _timeSeriesStamp = '{YYYY}-{MM}-{DD} {h}:{mm}{aa}';
1088
function timeSeriesVal(tzDate, stamp) {
1089
return (self, val) => stamp(tzDate(val));
1092
function legendStroke(self, seriesIdx) {
1093
let s = self.series[seriesIdx];
1094
return s.width ? s.stroke(self, seriesIdx) : s.points.width ? s.points.stroke(self, seriesIdx) : null;
1097
function legendFill(self, seriesIdx) {
1098
return self.series[seriesIdx].fill(self, seriesIdx);
1108
stroke: legendStroke,
1117
function cursorPointShow(self, si) {
1118
let o = self.cursor.points;
1120
let pt = placeDiv();
1122
let size = o.size(self, si);
1123
setStylePx(pt, WIDTH, size);
1124
setStylePx(pt, HEIGHT, size);
1126
let mar = size / -2;
1127
setStylePx(pt, "marginLeft", mar);
1128
setStylePx(pt, "marginTop", mar);
1130
let width = o.width(self, si, size);
1131
width && setStylePx(pt, "borderWidth", width);
1136
function cursorPointFill(self, si) {
1137
let sp = self.series[si].points;
1138
return sp._fill || sp._stroke;
1141
function cursorPointStroke(self, si) {
1142
let sp = self.series[si].points;
1143
return sp._stroke || sp._fill;
1146
function cursorPointSize(self, si) {
1147
let sp = self.series[si].points;
1148
return ptDia(sp.width, 1);
1151
function dataIdx(self, seriesIdx, cursorIdx) {
1155
const moveTuple = [0,0];
1157
function cursorMove(self, mouseLeft1, mouseTop1) {
1158
moveTuple[0] = mouseLeft1;
1159
moveTuple[1] = mouseTop1;
1163
function filtBtn0(self, targ, handle) {
1165
e.button == 0 && handle(e);
1169
function passThru(self, targ, handle) {
1180
show: cursorPointShow,
1181
size: cursorPointSize,
1183
stroke: cursorPointStroke,
1184
fill: cursorPointFill,
1188
mousedown: filtBtn0,
1193
mousemove: passThru,
1194
mouseleave: passThru,
1195
mouseenter: passThru,
1221
stroke: "rgba(0,0,0,0.07)",
1227
const ticks = assign({}, grid, {size: 10});
1229
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"';
1230
const labelFont = "bold " + font;
1231
const lineMult = 1.5;
1254
const numSeriesLabel = "Value";
1255
const timeSeriesLabel = "Time";
1257
const xSeriesOpts = {
1271
function numAxisVals(self, splits, axisIdx, foundSpace, foundIncr) {
1272
return splits.map(v => v == null ? "" : fmtNum(v));
1275
function numAxisSplits(self, axisIdx, scaleMin, scaleMax, foundIncr, foundSpace, forceMin) {
1278
let numDec = fixedDec.get(foundIncr) || 0;
1280
scaleMin = forceMin ? scaleMin : roundDec(incrRoundUp(scaleMin, foundIncr), numDec);
1282
for (let val = scaleMin; val <= scaleMax; val = roundDec(val + foundIncr, numDec))
1283
splits.push(Object.is(val, -0) ? 0 : val);
1289
function logAxisSplits(self, axisIdx, scaleMin, scaleMax, foundIncr, foundSpace, forceMin) {
1292
const logBase = self.scales[self.axes[axisIdx].scale].log;
1294
const logFn = logBase == 10 ? log10 : log2;
1296
const exp = floor(logFn(scaleMin));
1298
foundIncr = pow(logBase, exp);
1301
foundIncr = roundDec(foundIncr, -exp);
1303
let split = scaleMin;
1307
split = roundDec(split + foundIncr, fixedDec.get(foundIncr));
1309
if (split >= foundIncr * logBase)
1312
} while (split <= scaleMax);
1317
function asinhAxisSplits(self, axisIdx, scaleMin, scaleMax, foundIncr, foundSpace, forceMin) {
1318
let sc = self.scales[self.axes[axisIdx].scale];
1320
let linthresh = sc.asinh;
1322
let posSplits = scaleMax > linthresh ? logAxisSplits(self, axisIdx, max(linthresh, scaleMin), scaleMax, foundIncr) : [linthresh];
1323
let zero = scaleMax >= 0 && scaleMin <= 0 ? [0] : [];
1324
let negSplits = scaleMin < -linthresh ? logAxisSplits(self, axisIdx, max(linthresh, -scaleMax), -scaleMin, foundIncr): [linthresh];
1326
return negSplits.reverse().map(v => -v).concat(zero, posSplits);
1330
const RE_12357 = /[12357]/;
1331
const RE_125 = /[125]/;
1334
function logAxisValsFilt(self, splits, axisIdx, foundSpace, foundIncr) {
1335
let axis = self.axes[axisIdx];
1336
let scaleKey = axis.scale;
1337
let sc = self.scales[scaleKey];
1339
if (sc.distr == 3 && sc.log == 2)
1342
let valToPos = self.valToPos;
1344
let minSpace = axis._space;
1346
let _10 = valToPos(10, scaleKey);
1349
valToPos(9, scaleKey) - _10 >= minSpace ? RE_ALL :
1350
valToPos(7, scaleKey) - _10 >= minSpace ? RE_12357 :
1351
valToPos(5, scaleKey) - _10 >= minSpace ? RE_125 :
1355
return splits.map(v => ((sc.distr == 4 && v == 0) || re.test(v)) ? v : null);
1358
function numSeriesVal(self, val) {
1359
return val == null ? "" : fmtNum(val);
1384
function ptDia(width, mult) {
1385
let dia = 3 + (width || 1) * 2;
1386
return roundDec(dia * mult, 3);
1389
function seriesPointsShow(self, si) {
1390
let { scale, idxs } = self.series[0];
1391
let xData = self._data[0];
1392
let p0 = self.valToPos(xData[idxs[0]], scale, true);
1393
let p1 = self.valToPos(xData[idxs[1]], scale, true);
1394
let dim = abs(p1 - p0);
1396
let s = self.series[si];
1398
let maxPts = dim / (s.points.space * pxRatio);
1399
return idxs[1] - idxs[0] <= maxPts;
1402
function seriesFillTo(self, seriesIdx, dataMin, dataMax) {
1403
let scale = self.scales[self.series[seriesIdx].scale];
1404
let isUpperBandEdge = self.bands && self.bands.some(b => b.series[0] == seriesIdx);
1405
return scale.distr == 3 || isUpperBandEdge ? scale.min : 0;
1417
const xySeriesOpts = {
1423
assign({}, facet, {scale: 'x'}),
1424
assign({}, facet, {scale: 'y'}),
1428
const ySeriesOpts = {
1434
gaps: (self, seriesIdx, idx0, idx1, nullGaps) => nullGaps,
1437
show: seriesPointsShow,
1458
function clampScale(self, val, scaleMin, scaleMax, scaleKey) {
1467
return scaleMin / 10;
1482
const yScaleOpts = assign({}, xScaleOpts, {
1489
function _sync(key, opts) {
1500
s.plots = s.plots.filter(c => c != plot);
1502
pub(type, self, x, y, w, h, i) {
1503
for (let j = 0; j < s.plots.length; j++)
1504
s.plots[j] != self && s.plots[j].pub(type, self, x, y, w, h, i);
1515
const BAND_CLIP_FILL = 1 << 0;
1516
const BAND_CLIP_STROKE = 1 << 1;
1518
function orient(u, seriesIdx, cb) {
1519
const series = u.series[seriesIdx];
1520
const scales = u.scales;
1521
const bbox = u.bbox;
1522
const scaleX = u.mode == 2 ? scales[series.facets[0].scale] : scales[u.series[0].scale];
1524
let dx = u._data[0],
1525
dy = u._data[seriesIdx],
1527
sy = u.mode == 2 ? scales[series.facets[1].scale] : scales[series.scale],
1576
function clipBandLine(self, seriesIdx, idx0, idx1, strokePath) {
1577
return orient(self, seriesIdx, (series, dataX, dataY, scaleX, scaleY, valToPosX, valToPosY, xOff, yOff, xDim, yDim) => {
1578
let pxRound = series.pxRound;
1580
const dir = scaleX.dir * (scaleX.ori == 0 ? 1 : -1);
1581
const lineTo = scaleX.ori == 0 ? lineToH : lineToV;
1595
let x0 = pxRound(valToPosX(dataX[frIdx], scaleX, xDim, xOff));
1596
let y0 = pxRound(valToPosY(dataY[frIdx], scaleY, yDim, yOff));
1598
let x1 = pxRound(valToPosX(dataX[toIdx], scaleX, xDim, xOff));
1600
let yLimit = pxRound(valToPosY(scaleY.max, scaleY, yDim, yOff));
1602
let clip = new Path2D(strokePath);
1604
lineTo(clip, x1, yLimit);
1605
lineTo(clip, x0, yLimit);
1606
lineTo(clip, x0, y0);
1612
function clipGaps(gaps, ori, plotLft, plotTop, plotWid, plotHgt) {
1616
if (gaps.length > 0) {
1617
clip = new Path2D();
1619
const rect = ori == 0 ? rectH : rectV;
1621
let prevGapEnd = plotLft;
1623
for (let i = 0; i < gaps.length; i++) {
1627
let w = g[0] - prevGapEnd;
1629
w > 0 && rect(clip, prevGapEnd, plotTop, w, plotTop + plotHgt);
1635
let w = plotLft + plotWid - prevGapEnd;
1637
w > 0 && rect(clip, prevGapEnd, plotTop, w, plotTop + plotHgt);
1643
function addGap(gaps, fromX, toX) {
1644
let prevGap = gaps[gaps.length - 1];
1646
if (prevGap && prevGap[0] == fromX)
1649
gaps.push([fromX, toX]);
1652
function pxRoundGen(pxAlign) {
1653
return pxAlign == 0 ? retArg0 : pxAlign == 1 ? round : v => incrRound(v, pxAlign);
1657
let moveTo = ori == 0 ?
1661
let arcTo = ori == 0 ?
1662
(p, x1, y1, x2, y2, r) => { p.arcTo(x1, y1, x2, y2, r); } :
1663
(p, y1, x1, y2, x2, r) => { p.arcTo(x1, y1, x2, y2, r); };
1665
let rect = ori == 0 ?
1666
(p, x, y, w, h) => { p.rect(x, y, w, h); } :
1667
(p, y, x, h, w) => { p.rect(x, y, w, h); };
1669
return (p, x, y, w, h, r = 0) => {
1671
rect(p, x, y, w, h);
1673
r = min(r, w / 2, h / 2);
1676
moveTo(p, x + r, y);
1677
arcTo(p, x + w, y, x + w, y + h, r);
1678
arcTo(p, x + w, y + h, x, y + h, r);
1679
arcTo(p, x, y + h, x, y, r);
1680
arcTo(p, x, y, x + w, y, r);
1687
const moveToH = (p, x, y) => { p.moveTo(x, y); };
1688
const moveToV = (p, y, x) => { p.moveTo(x, y); };
1689
const lineToH = (p, x, y) => { p.lineTo(x, y); };
1690
const lineToV = (p, y, x) => { p.lineTo(x, y); };
1691
const rectH = rect(0);
1692
const rectV = rect(1);
1693
const arcH = (p, x, y, r, startAngle, endAngle) => { p.arc(x, y, r, startAngle, endAngle); };
1694
const arcV = (p, y, x, r, startAngle, endAngle) => { p.arc(x, y, r, startAngle, endAngle); };
1695
const bezierCurveToH = (p, bp1x, bp1y, bp2x, bp2y, p2x, p2y) => { p.bezierCurveTo(bp1x, bp1y, bp2x, bp2y, p2x, p2y); };
1696
const bezierCurveToV = (p, bp1y, bp1x, bp2y, bp2x, p2y, p2x) => { p.bezierCurveTo(bp1x, bp1y, bp2x, bp2y, p2x, p2y); };
1699
function points(opts) {
1700
return (u, seriesIdx, idx0, idx1, filtIdxs) => {
1703
return orient(u, seriesIdx, (series, dataX, dataY, scaleX, scaleY, valToPosX, valToPosY, xOff, yOff, xDim, yDim) => {
1704
let { pxRound, points } = series;
1708
if (scaleX.ori == 0) {
1717
const width = roundDec(points.width * pxRatio, 3);
1719
let rad = (points.size - points.width) / 2 * pxRatio;
1720
let dia = roundDec(rad * 2, 3);
1722
let fill = new Path2D();
1723
let clip = new Path2D();
1725
let { left: lft, top: top, width: wid, height: hgt } = u.bbox;
1734
const drawPoint = pi => {
1735
if (dataY[pi] != null) {
1736
let x = pxRound(valToPosX(dataX[pi], scaleX, xDim, xOff));
1737
let y = pxRound(valToPosY(dataY[pi], scaleY, yDim, yOff));
1739
moveTo(fill, x + rad, y);
1740
arc(fill, x, y, rad, 0, PI * 2);
1745
filtIdxs.forEach(drawPoint);
1747
for (let pi = idx0; pi <= idx1; pi++)
1752
stroke: width > 0 ? fill : null,
1755
flags: BAND_CLIP_FILL | BAND_CLIP_STROKE,
1761
function _drawAcc(lineTo) {
1762
return (stroke, accX, minY, maxY, inY, outY) => {
1764
if (inY != minY && outY != minY)
1765
lineTo(stroke, accX, minY);
1766
if (inY != maxY && outY != maxY)
1767
lineTo(stroke, accX, maxY);
1769
lineTo(stroke, accX, outY);
1774
const drawAccH = _drawAcc(lineToH);
1775
const drawAccV = _drawAcc(lineToV);
1778
return (u, seriesIdx, idx0, idx1) => {
1779
return orient(u, seriesIdx, (series, dataX, dataY, scaleX, scaleY, valToPosX, valToPosY, xOff, yOff, xDim, yDim) => {
1780
let pxRound = series.pxRound;
1782
let lineTo, drawAcc;
1784
if (scaleX.ori == 0) {
1793
const dir = scaleX.dir * (scaleX.ori == 0 ? 1 : -1);
1795
const _paths = {stroke: new Path2D(), fill: null, clip: null, band: null, gaps: null, flags: BAND_CLIP_FILL};
1796
const stroke = _paths.stroke;
1800
inY, outY, outX, drawnAtX;
1804
let accX = pxRound(valToPosX(dataX[dir == 1 ? idx0 : idx1], scaleX, xDim, xOff));
1805
let accGaps = false;
1806
let prevYNull = false;
1809
let lftIdx = nonNullIdx(dataY, idx0, idx1, 1 * dir);
1810
let rgtIdx = nonNullIdx(dataY, idx0, idx1, -1 * dir);
1811
let lftX = pxRound(valToPosX(dataX[lftIdx], scaleX, xDim, xOff));
1812
let rgtX = pxRound(valToPosX(dataX[rgtIdx], scaleX, xDim, xOff));
1815
addGap(gaps, xOff, lftX);
1817
for (let i = dir == 1 ? idx0 : idx1; i >= idx0 && i <= idx1; i += dir) {
1818
let x = pxRound(valToPosX(dataX[i], scaleX, xDim, xOff));
1821
if (dataY[i] != null) {
1822
outY = pxRound(valToPosY(dataY[i], scaleY, yDim, yOff));
1825
lineTo(stroke, x, outY);
1829
minY = min(outY, minY);
1830
maxY = max(outY, maxY);
1832
else if (dataY[i] === null)
1833
accGaps = prevYNull = true;
1836
let _addGap = false;
1839
drawAcc(stroke, accX, minY, maxY, inY, outY);
1840
outX = drawnAtX = accX;
1847
if (dataY[i] != null) {
1848
outY = pxRound(valToPosY(dataY[i], scaleY, yDim, yOff));
1849
lineTo(stroke, x, outY);
1850
minY = maxY = inY = outY;
1853
if (prevYNull && x - accX > 1)
1862
if (dataY[i] === null) {
1870
_addGap && addGap(gaps, outX, x);
1876
if (minY != inf && minY != maxY && drawnAtX != accX)
1877
drawAcc(stroke, accX, minY, maxY, inY, outY);
1879
if (rgtX < xOff + xDim)
1880
addGap(gaps, rgtX, xOff + xDim);
1882
if (series.fill != null) {
1883
let fill = _paths.fill = new Path2D(stroke);
1885
let fillTo = pxRound(valToPosY(series.fillTo(u, seriesIdx, series.min, series.max), scaleY, yDim, yOff));
1887
lineTo(fill, rgtX, fillTo);
1888
lineTo(fill, lftX, fillTo);
1891
_paths.gaps = gaps = series.gaps(u, seriesIdx, idx0, idx1, gaps);
1893
if (!series.spanGaps)
1894
_paths.clip = clipGaps(gaps, scaleX.ori, xOff, yOff, xDim, yDim);
1896
if (u.bands.length > 0) {
1899
_paths.band = clipBandLine(u, seriesIdx, idx0, idx1, stroke);
1907
function stepped(opts) {
1908
const align = ifNull(opts.align, 1);
1910
const ascDesc = ifNull(opts.ascDesc, false);
1912
return (u, seriesIdx, idx0, idx1) => {
1913
return orient(u, seriesIdx, (series, dataX, dataY, scaleX, scaleY, valToPosX, valToPosY, xOff, yOff, xDim, yDim) => {
1914
let pxRound = series.pxRound;
1916
let lineTo = scaleX.ori == 0 ? lineToH : lineToV;
1918
const _paths = {stroke: new Path2D(), fill: null, clip: null, band: null, gaps: null, flags: BAND_CLIP_FILL};
1919
const stroke = _paths.stroke;
1921
const _dir = 1 * scaleX.dir * (scaleX.ori == 0 ? 1 : -1);
1923
idx0 = nonNullIdx(dataY, idx0, idx1, 1);
1924
idx1 = nonNullIdx(dataY, idx0, idx1, -1);
1928
let prevYPos = pxRound(valToPosY(dataY[_dir == 1 ? idx0 : idx1], scaleY, yDim, yOff));
1929
let firstXPos = pxRound(valToPosX(dataX[_dir == 1 ? idx0 : idx1], scaleX, xDim, xOff));
1930
let prevXPos = firstXPos;
1932
lineTo(stroke, firstXPos, prevYPos);
1934
for (let i = _dir == 1 ? idx0 : idx1; i >= idx0 && i <= idx1; i += _dir) {
1935
let yVal1 = dataY[i];
1937
let x1 = pxRound(valToPosX(dataX[i], scaleX, xDim, xOff));
1939
if (yVal1 == null) {
1940
if (yVal1 === null) {
1941
addGap(gaps, prevXPos, x1);
1947
let y1 = pxRound(valToPosY(yVal1, scaleY, yDim, yOff));
1950
addGap(gaps, prevXPos, x1);
1955
lineTo(stroke, x1, prevYPos);
1957
lineTo(stroke, prevXPos, y1);
1959
lineTo(stroke, x1, y1);
1965
if (series.fill != null) {
1966
let fill = _paths.fill = new Path2D(stroke);
1968
let fillTo = series.fillTo(u, seriesIdx, series.min, series.max);
1969
let minY = pxRound(valToPosY(fillTo, scaleY, yDim, yOff));
1971
lineTo(fill, prevXPos, minY);
1972
lineTo(fill, firstXPos, minY);
1975
_paths.gaps = gaps = series.gaps(u, seriesIdx, idx0, idx1, gaps);
1978
let halfStroke = (series.width * pxRatio) / 2;
1979
let startsOffset = (ascDesc || align == 1) ? halfStroke : -halfStroke;
1980
let endsOffset = (ascDesc || align == -1) ? -halfStroke : halfStroke;
1983
g[0] += startsOffset;
1987
if (!series.spanGaps)
1988
_paths.clip = clipGaps(gaps, scaleX.ori, xOff, yOff, xDim, yDim);
1990
if (u.bands.length > 0) {
1993
_paths.band = clipBandLine(u, seriesIdx, idx0, idx1, stroke);
2001
function bars(opts) {
2002
opts = opts || EMPTY_OBJ;
2003
const size = ifNull(opts.size, [0.6, inf, 1]);
2004
const align = opts.align || 0;
2005
const extraGap = (opts.gap || 0) * pxRatio;
2007
const radius = ifNull(opts.radius, 0);
2009
const gapFactor = 1 - size[0];
2010
const maxWidth = ifNull(size[1], inf) * pxRatio;
2011
const minWidth = ifNull(size[2], 1) * pxRatio;
2013
const disp = ifNull(opts.disp, EMPTY_OBJ);
2014
const _each = ifNull(opts.each, _ => {});
2016
const { fill: dispFills, stroke: dispStrokes } = disp;
2018
return (u, seriesIdx, idx0, idx1) => {
2019
return orient(u, seriesIdx, (series, dataX, dataY, scaleX, scaleY, valToPosX, valToPosY, xOff, yOff, xDim, yDim) => {
2020
let pxRound = series.pxRound;
2022
const _dirX = scaleX.dir * (scaleX.ori == 0 ? 1 : -1);
2023
const _dirY = scaleY.dir * (scaleY.ori == 1 ? 1 : -1);
2025
let rect = scaleX.ori == 0 ? rectH : rectV;
2027
let each = scaleX.ori == 0 ? _each : (u, seriesIdx, i, top, lft, hgt, wid) => {
2028
_each(u, seriesIdx, i, lft, top, wid, hgt);
2031
let fillToY = series.fillTo(u, seriesIdx, series.min, series.max);
2033
let y0Pos = valToPosY(fillToY, scaleY, yDim, yOff);
2038
let strokeWidth = pxRound(series.width * pxRatio);
2040
let multiPath = false;
2042
let fillColors = null;
2043
let fillPaths = null;
2044
let strokeColors = null;
2045
let strokePaths = null;
2047
if (dispFills != null && dispStrokes != null) {
2050
fillColors = dispFills.values(u, seriesIdx, idx0, idx1);
2051
fillPaths = new Map();
2052
(new Set(fillColors)).forEach(color => {
2054
fillPaths.set(color, new Path2D());
2057
strokeColors = dispStrokes.values(u, seriesIdx, idx0, idx1);
2058
strokePaths = new Map();
2059
(new Set(strokeColors)).forEach(color => {
2061
strokePaths.set(color, new Path2D());
2065
let { x0, size } = disp;
2067
if (x0 != null && size != null) {
2068
dataX = x0.values(u, seriesIdx, idx0, idx1);
2071
dataX = dataX.map(pct => u.posToVal(xOff + pct * xDim, scaleX.key, true));
2074
let sizes = size.values(u, seriesIdx, idx0, idx1);
2077
barWid = sizes[0] * xDim;
2079
barWid = valToPosX(sizes[0], scaleX, xDim, xOff) - valToPosX(0, scaleX, xDim, xOff);
2081
barWid = pxRound(barWid - strokeWidth);
2083
xShift = (_dirX == 1 ? -strokeWidth / 2 : barWid + strokeWidth / 2);
2088
if (dataX.length > 1) {
2094
for (let i = 0, minDelta = Infinity; i < dataX.length; i++) {
2095
if (dataY[i] !== undefined) {
2096
if (prevIdx != null) {
2097
let delta = abs(dataX[i] - dataX[prevIdx]);
2099
if (delta < minDelta) {
2101
colWid = abs(valToPosX(dataX[i], scaleX, xDim, xOff) - valToPosX(dataX[prevIdx], scaleX, xDim, xOff));
2110
let gapWid = colWid * gapFactor;
2112
barWid = pxRound(min(maxWidth, max(minWidth, colWid - gapWid)) - strokeWidth - extraGap);
2114
xShift = (align == 0 ? barWid / 2 : align == _dirX ? 0 : barWid) - align * _dirX * extraGap / 2;
2117
const _paths = {stroke: null, fill: null, clip: null, band: null, gaps: null, flags: BAND_CLIP_FILL | BAND_CLIP_STROKE};
2119
const hasBands = u.bands.length > 0;
2125
_paths.band = new Path2D();
2126
yLimit = pxRound(valToPosY(scaleY.max, scaleY, yDim, yOff));
2129
const stroke = multiPath ? null : new Path2D();
2130
const band = _paths.band;
2132
for (let i = _dirX == 1 ? idx0 : idx1; i >= idx0 && i <= idx1; i += _dirX) {
2133
let yVal = dataY[i];
2145
let xVal = scaleX.distr != 2 || disp != null ? dataX[i] : i;
2148
let xPos = valToPosX(xVal, scaleX, xDim, xOff);
2149
let yPos = valToPosY(ifNull(yVal, fillToY) , scaleY, yDim, yOff);
2151
let lft = pxRound(xPos - xShift);
2152
let btm = pxRound(max(yPos, y0Pos));
2153
let top = pxRound(min(yPos, y0Pos));
2155
let barHgt = btm - top;
2157
let r = radius * barWid;
2161
if (strokeWidth > 0 && strokeColors[i] != null)
2162
rect(strokePaths.get(strokeColors[i]), lft, top + floor(strokeWidth / 2), barWid, max(0, barHgt - strokeWidth), r);
2164
if (fillColors[i] != null)
2165
rect(fillPaths.get(fillColors[i]), lft, top + floor(strokeWidth / 2), barWid, max(0, barHgt - strokeWidth), r);
2168
rect(stroke, lft, top + floor(strokeWidth / 2), barWid, max(0, barHgt - strokeWidth), r);
2170
each(u, seriesIdx, i,
2171
lft - strokeWidth / 2,
2173
barWid + strokeWidth,
2190
rect(band, lft - strokeWidth / 2, top, barWid + strokeWidth, max(0, barHgt), 0);
2194
if (strokeWidth > 0)
2195
_paths.stroke = multiPath ? strokePaths : stroke;
2197
_paths.fill = multiPath ? fillPaths : stroke;
2204
function splineInterp(interp, opts) {
2205
return (u, seriesIdx, idx0, idx1) => {
2206
return orient(u, seriesIdx, (series, dataX, dataY, scaleX, scaleY, valToPosX, valToPosY, xOff, yOff, xDim, yDim) => {
2207
let pxRound = series.pxRound;
2209
let moveTo, bezierCurveTo, lineTo;
2211
if (scaleX.ori == 0) {
2214
bezierCurveTo = bezierCurveToH;
2219
bezierCurveTo = bezierCurveToV;
2222
const _dir = 1 * scaleX.dir * (scaleX.ori == 0 ? 1 : -1);
2224
idx0 = nonNullIdx(dataY, idx0, idx1, 1);
2225
idx1 = nonNullIdx(dataY, idx0, idx1, -1);
2229
let firstXPos = pxRound(valToPosX(dataX[_dir == 1 ? idx0 : idx1], scaleX, xDim, xOff));
2230
let prevXPos = firstXPos;
2235
for (let i = _dir == 1 ? idx0 : idx1; i >= idx0 && i <= idx1; i += _dir) {
2236
let yVal = dataY[i];
2237
let xVal = dataX[i];
2238
let xPos = valToPosX(xVal, scaleX, xDim, xOff);
2241
if (yVal === null) {
2242
addGap(gaps, prevXPos, xPos);
2249
addGap(gaps, prevXPos, xPos);
2253
xCoords.push((prevXPos = xPos));
2254
yCoords.push(valToPosY(dataY[i], scaleY, yDim, yOff));
2258
const _paths = {stroke: interp(xCoords, yCoords, moveTo, lineTo, bezierCurveTo, pxRound), fill: null, clip: null, band: null, gaps: null, flags: BAND_CLIP_FILL};
2259
const stroke = _paths.stroke;
2261
if (series.fill != null && stroke != null) {
2262
let fill = _paths.fill = new Path2D(stroke);
2264
let fillTo = series.fillTo(u, seriesIdx, series.min, series.max);
2265
let minY = pxRound(valToPosY(fillTo, scaleY, yDim, yOff));
2267
lineTo(fill, prevXPos, minY);
2268
lineTo(fill, firstXPos, minY);
2271
_paths.gaps = gaps = series.gaps(u, seriesIdx, idx0, idx1, gaps);
2273
if (!series.spanGaps)
2274
_paths.clip = clipGaps(gaps, scaleX.ori, xOff, yOff, xDim, yDim);
2276
if (u.bands.length > 0) {
2279
_paths.band = clipBandLine(u, seriesIdx, idx0, idx1, stroke);
2299
function monotoneCubic(opts) {
2300
return splineInterp(_monotoneCubic);
2305
function _monotoneCubic(xs, ys, moveTo, lineTo, bezierCurveTo, pxRound) {
2306
const n = xs.length;
2311
const path = new Path2D();
2313
moveTo(path, xs[0], ys[0]);
2316
lineTo(path, xs[1], ys[1]);
2324
for (let i = 0; i < n - 1; i++) {
2325
dys[i] = ys[i + 1] - ys[i];
2326
dxs[i] = xs[i + 1] - xs[i];
2327
ds[i] = dys[i] / dxs[i];
2334
for (let i = 1; i < n - 1; i++) {
2335
if (ds[i] === 0 || ds[i - 1] === 0 || (ds[i - 1] > 0) !== (ds[i] > 0))
2338
ms[i] = 3 * (dxs[i - 1] + dxs[i]) / (
2339
(2 * dxs[i] + dxs[i - 1]) / ds[i - 1] +
2340
(dxs[i] + 2 * dxs[i - 1]) / ds[i]
2343
if (!isFinite(ms[i]))
2348
ms[n - 1] = ds[n - 2];
2350
for (let i = 0; i < n - 1; i++) {
2354
ys[i] + ms[i] * dxs[i] / 3,
2355
xs[i + 1] - dxs[i] / 3,
2356
ys[i + 1] - ms[i + 1] * dxs[i] / 3,
2366
const cursorPlots = new Set();
2368
function invalidateRects() {
2369
cursorPlots.forEach(u => {
2374
on(resize, win, invalidateRects);
2375
on(scroll, win, invalidateRects, true);
2377
const linearPath = linear() ;
2378
const pointsPath = points() ;
2380
function setDefaults(d, xo, yo, initY) {
2381
let d2 = initY ? [d[0], d[1]].concat(d.slice(2)) : [d[0]].concat(d.slice(1));
2382
return d2.map((o, i) => setDefault(o, i, xo, yo));
2385
function setDefaults2(d, xyo) {
2386
return d.map((o, i) => i == 0 ? null : assign({}, xyo, o));
2389
function setDefault(o, i, xo, yo) {
2390
return assign({}, (i == 0 ? xo : yo), o);
2393
function snapNumX(self, dataMin, dataMax) {
2394
return dataMin == null ? nullNullTuple : [dataMin, dataMax];
2397
const snapTimeX = snapNumX;
2401
function snapNumY(self, dataMin, dataMax) {
2402
return dataMin == null ? nullNullTuple : rangeNum(dataMin, dataMax, rangePad, true);
2405
function snapLogY(self, dataMin, dataMax, scale) {
2406
return dataMin == null ? nullNullTuple : rangeLog(dataMin, dataMax, self.scales[scale].log, false);
2409
const snapLogX = snapLogY;
2411
function snapAsinhY(self, dataMin, dataMax, scale) {
2412
return dataMin == null ? nullNullTuple : rangeAsinh(dataMin, dataMax, self.scales[scale].log, false);
2415
const snapAsinhX = snapAsinhY;
2418
function findIncr(minVal, maxVal, incrs, dim, minSpace) {
2419
let intDigits = max(numIntDigits(minVal), numIntDigits(maxVal));
2421
let delta = maxVal - minVal;
2423
let incrIdx = closestIdx((minSpace / dim) * delta, incrs);
2426
let foundIncr = incrs[incrIdx];
2427
let foundSpace = dim * foundIncr / delta;
2429
if (foundSpace >= minSpace && intDigits + (foundIncr < 5 ? fixedDec.get(foundIncr) : 0) <= 17)
2430
return [foundIncr, foundSpace];
2431
} while (++incrIdx < incrs.length);
2436
function pxRatioFont(font) {
2437
let fontSize, fontSizeCss;
2438
font = font.replace(/(\d+)px/, (m, p1) => (fontSize = round((fontSizeCss = +p1) * pxRatio)) + 'px');
2439
return [font, fontSize, fontSizeCss];
2442
function syncFontSize(axis) {
2444
[axis.font, axis.labelFont].forEach(f => {
2445
let size = roundDec(f[2] * pxRatio, 1);
2446
f[0] = f[0].replace(/[0-9.]+px/, size + 'px');
2452
function uPlot(opts, data, then) {
2454
mode: ifNull(opts.mode, 1),
2457
const mode = self.mode;
2460
function getValPct(val, scale) {
2462
scale.distr == 3 ? log10(val > 0 ? val : scale.clamp(self, val, scale.min, scale.max, scale.key)) :
2463
scale.distr == 4 ? asinh(val, scale.asinh) :
2467
return (_val - scale._min) / (scale._max - scale._min);
2470
function getHPos(val, scale, dim, off) {
2471
let pct = getValPct(val, scale);
2472
return off + dim * (scale.dir == -1 ? (1 - pct) : pct);
2475
function getVPos(val, scale, dim, off) {
2476
let pct = getValPct(val, scale);
2477
return off + dim * (scale.dir == -1 ? pct : (1 - pct));
2480
function getPos(val, scale, dim, off) {
2481
return scale.ori == 0 ? getHPos(val, scale, dim, off) : getVPos(val, scale, dim, off);
2484
self.valToPosH = getHPos;
2485
self.valToPosV = getVPos;
2490
const root = self.root = placeDiv(UPLOT);
2492
if (opts.id != null)
2495
addClass(root, opts.class);
2498
let title = placeDiv(TITLE, root);
2499
title.textContent = opts.title;
2502
const can = placeTag("canvas");
2503
const ctx = self.ctx = can.getContext("2d");
2505
const wrap = placeDiv(WRAP, root);
2506
const under = self.under = placeDiv(UNDER, wrap);
2507
wrap.appendChild(can);
2508
const over = self.over = placeDiv(OVER, wrap);
2512
const pxAlign = +ifNull(opts.pxAlign, 1);
2514
const pxRound = pxRoundGen(pxAlign);
2516
(opts.plugins || []).forEach(p => {
2518
opts = p.opts(self, opts) || opts;
2521
const ms = opts.ms || 1e-3;
2523
const series = self.series = mode == 1 ?
2524
setDefaults(opts.series || [], xSeriesOpts, ySeriesOpts, false) :
2525
setDefaults2(opts.series || [null], xySeriesOpts);
2526
const axes = self.axes = setDefaults(opts.axes || [], xAxisOpts, yAxisOpts, true);
2527
const scales = self.scales = {};
2528
const bands = self.bands = opts.bands || [];
2530
bands.forEach(b => {
2531
b.fill = fnOrSelf(b.fill || null);
2534
const xScaleKey = mode == 2 ? series[1].facets[0].scale : series[0].scale;
2536
const drawOrderMap = {
2541
const drawOrder = (opts.drawOrder || ["axes", "series"]).map(key => drawOrderMap[key]);
2543
function initScale(scaleKey) {
2544
let sc = scales[scaleKey];
2547
let scaleOpts = (opts.scales || EMPTY_OBJ)[scaleKey] || EMPTY_OBJ;
2549
if (scaleOpts.from != null) {
2551
initScale(scaleOpts.from);
2553
scales[scaleKey] = assign({}, scales[scaleOpts.from], scaleOpts, {key: scaleKey});
2556
sc = scales[scaleKey] = assign({}, (scaleKey == xScaleKey ? xScaleOpts : yScaleOpts), scaleOpts);
2563
let isTime = sc.time;
2567
let rangeIsArr = isArr(rn);
2569
if (scaleKey != xScaleKey || mode == 2) {
2571
if (rangeIsArr && (rn[0] == null || rn[1] == null)) {
2573
min: rn[0] == null ? autoRangePart : {
2578
max: rn[1] == null ? autoRangePart : {
2587
if (!rangeIsArr && isObj(rn)) {
2590
rn = (self, dataMin, dataMax) => dataMin == null ? nullNullTuple : rangeNum(dataMin, dataMax, cfg);
2594
sc.range = fnOrSelf(rn || (isTime ? snapTimeX : scaleKey == xScaleKey ?
2595
(sc.distr == 3 ? snapLogX : sc.distr == 4 ? snapAsinhX : snapNumX) :
2596
(sc.distr == 3 ? snapLogY : sc.distr == 4 ? snapAsinhY : snapNumY)
2599
sc.auto = fnOrSelf(rangeIsArr ? false : sc.auto);
2601
sc.clamp = fnOrSelf(sc.clamp || clampScale);
2604
sc._min = sc._max = null;
2614
series.forEach(s => {
2623
for (let k in opts.scales)
2626
const scaleX = scales[xScaleKey];
2628
const xScaleDistr = scaleX.distr;
2630
let valToPosX, valToPosY;
2632
if (scaleX.ori == 0) {
2633
addClass(root, ORI_HZ);
2634
valToPosX = getHPos;
2635
valToPosY = getVPos;
2651
addClass(root, ORI_VT);
2652
valToPosX = getVPos;
2653
valToPosY = getHPos;
2669
const pendScales = {};
2672
for (let k in scales) {
2675
if (sc.min != null || sc.max != null) {
2676
pendScales[k] = {min: sc.min, max: sc.max};
2677
sc.min = sc.max = null;
2682
const _tzDate = (opts.tzDate || (ts => new Date(round(ts / ms))));
2683
const _fmtDate = (opts.fmtDate || fmtDate);
2685
const _timeAxisSplits = (ms == 1 ? timeAxisSplitsMs(_tzDate) : timeAxisSplitsS(_tzDate));
2686
const _timeAxisVals = timeAxisVals(_tzDate, timeAxisStamps((ms == 1 ? _timeAxisStampsMs : _timeAxisStampsS), _fmtDate));
2687
const _timeSeriesVal = timeSeriesVal(_tzDate, timeSeriesStamp(_timeSeriesStamp, _fmtDate));
2689
const activeIdxs = [];
2691
const legend = (self.legend = assign({}, legendOpts, opts.legend));
2692
const showLegend = legend.show;
2693
const markers = legend.markers;
2696
legend.idxs = activeIdxs;
2698
markers.width = fnOrSelf(markers.width);
2699
markers.dash = fnOrSelf(markers.dash);
2700
markers.stroke = fnOrSelf(markers.stroke);
2701
markers.fill = fnOrSelf(markers.fill);
2705
let legendRows = [];
2706
let legendCells = [];
2708
let multiValLegend = false;
2709
let NULL_LEGEND_VALUES = {};
2712
const getMultiVals = series[1] ? series[1].values : null;
2713
multiValLegend = getMultiVals != null;
2714
legendCols = multiValLegend ? getMultiVals(self, 1, 0) : {_: 0};
2716
for (let k in legendCols)
2717
NULL_LEGEND_VALUES[k] = "--";
2721
legendEl = placeTag("table", LEGEND, root);
2723
if (multiValLegend) {
2724
let head = placeTag("tr", LEGEND_THEAD, legendEl);
2725
placeTag("th", null, head);
2727
for (var key in legendCols)
2728
placeTag("th", LEGEND_LABEL, head).textContent = key;
2731
addClass(legendEl, LEGEND_INLINE);
2732
legend.live && addClass(legendEl, LEGEND_LIVE);
2736
const son = {show: true};
2737
const soff = {show: false};
2739
function initLegendRow(s, i) {
2740
if (i == 0 && (multiValLegend || !legend.live || mode == 2))
2741
return nullNullTuple;
2745
let row = placeTag("tr", LEGEND_SERIES, legendEl, legendEl.childNodes[i]);
2747
addClass(row, s.class);
2752
let label = placeTag("th", null, row);
2755
let indic = placeDiv(LEGEND_MARKER, label);
2758
let width = markers.width(self, i);
2761
indic.style.border = width + "px " + markers.dash(self, i) + " " + markers.stroke(self, i);
2763
indic.style.background = markers.fill(self, i);
2767
let text = placeDiv(LEGEND_LABEL, label);
2768
text.textContent = s.label;
2772
text.style.color = s.width > 0 ? markers.stroke(self, i) : markers.fill(self, i);
2774
onMouse("click", label, e => {
2778
let seriesIdx = series.indexOf(s);
2780
if ((e.ctrlKey || e.metaKey) != legend.isolate) {
2782
let isolate = series.some((s, i) => i > 0 && i != seriesIdx && s.show);
2784
series.forEach((s, i) => {
2785
i > 0 && setSeries(i, isolate ? (i == seriesIdx ? son : soff) : son, true, syncOpts.setSeries);
2789
setSeries(seriesIdx, {show: !s.show}, true, syncOpts.setSeries);
2793
onMouse(mouseenter, label, e => {
2797
setSeries(series.indexOf(s), FOCUS_TRUE, true, syncOpts.setSeries);
2802
for (var key in legendCols) {
2803
let v = placeTag("td", LEGEND_VALUE, row);
2804
v.textContent = "--";
2808
return [row, cells];
2811
const mouseListeners = new Map();
2813
function onMouse(ev, targ, fn) {
2814
const targListeners = mouseListeners.get(targ) || {};
2815
const listener = cursor.bind[ev](self, targ, fn);
2818
on(ev, targ, targListeners[ev] = listener);
2819
mouseListeners.set(targ, targListeners);
2823
function offMouse(ev, targ, fn) {
2824
const targListeners = mouseListeners.get(targ) || {};
2826
for (let k in targListeners) {
2827
if (ev == null || k == ev) {
2828
off(k, targ, targListeners[k]);
2829
delete targListeners[k];
2834
mouseListeners.delete(targ);
2854
let shouldSetScales = false;
2855
let shouldSetSize = false;
2856
let shouldConvergeSize = false;
2857
let shouldSetCursor = false;
2858
let shouldSetLegend = false;
2860
function _setSize(width, height, force) {
2861
if (force || (width != self.width || height != self.height))
2862
calcSize(width, height);
2864
resetYSeries(false);
2866
shouldConvergeSize = true;
2867
shouldSetSize = true;
2868
shouldSetCursor = shouldSetLegend = cursor.left >= 0;
2872
function calcSize(width, height) {
2875
self.width = fullWidCss = plotWidCss = width;
2876
self.height = fullHgtCss = plotHgtCss = height;
2877
plotLftCss = plotTopCss = 0;
2884
plotLft = bb.left = incrRound(plotLftCss * pxRatio, 0.5);
2885
plotTop = bb.top = incrRound(plotTopCss * pxRatio, 0.5);
2886
plotWid = bb.width = incrRound(plotWidCss * pxRatio, 0.5);
2887
plotHgt = bb.height = incrRound(plotHgtCss * pxRatio, 0.5);
2893
const CYCLE_LIMIT = 3;
2895
function convergeSize() {
2896
let converged = false;
2900
while (!converged) {
2903
let axesConverged = axesCalc(cycleNum);
2904
let paddingConverged = paddingCalc(cycleNum);
2906
converged = cycleNum == CYCLE_LIMIT || (axesConverged && paddingConverged);
2909
calcSize(self.width, self.height);
2910
shouldSetSize = true;
2915
function setSize({width, height}) {
2916
_setSize(width, height);
2919
self.setSize = setSize;
2922
function calcPlotRect() {
2924
let hasTopAxis = false;
2925
let hasBtmAxis = false;
2926
let hasRgtAxis = false;
2927
let hasLftAxis = false;
2929
axes.forEach((axis, i) => {
2930
if (axis.show && axis._show) {
2931
let {side, _size} = axis;
2932
let isVt = side % 2;
2933
let labelSize = axis.label != null ? axis.labelSize : 0;
2935
let fullSize = _size + labelSize;
2939
plotWidCss -= fullSize;
2942
plotLftCss += fullSize;
2949
plotHgtCss -= fullSize;
2952
plotTopCss += fullSize;
2962
sidesWithAxes[0] = hasTopAxis;
2963
sidesWithAxes[1] = hasRgtAxis;
2964
sidesWithAxes[2] = hasBtmAxis;
2965
sidesWithAxes[3] = hasLftAxis;
2968
plotWidCss -= _padding[1] + _padding[3];
2969
plotLftCss += _padding[3];
2972
plotHgtCss -= _padding[2] + _padding[0];
2973
plotTopCss += _padding[0];
2976
function calcAxesRects() {
2978
let off1 = plotLftCss + plotWidCss;
2979
let off2 = plotTopCss + plotHgtCss;
2981
let off3 = plotLftCss;
2982
let off0 = plotTopCss;
2984
function incrOffset(side, size) {
2986
case 1: off1 += size; return off1 - size;
2987
case 2: off2 += size; return off2 - size;
2988
case 3: off3 -= size; return off3 + size;
2989
case 0: off0 -= size; return off0 + size;
2993
axes.forEach((axis, i) => {
2994
if (axis.show && axis._show) {
2995
let side = axis.side;
2997
axis._pos = incrOffset(side, axis._size);
2999
if (axis.label != null)
3000
axis._lpos = incrOffset(side, axis.labelSize);
3005
const cursor = (self.cursor = assign({}, cursorOpts, {drag: {y: mode == 2}}, opts.cursor));
3008
cursor.idxs = activeIdxs;
3010
cursor._lock = false;
3012
let points = cursor.points;
3014
points.show = fnOrSelf(points.show);
3015
points.size = fnOrSelf(points.size);
3016
points.stroke = fnOrSelf(points.stroke);
3017
points.width = fnOrSelf(points.width);
3018
points.fill = fnOrSelf(points.fill);
3021
const focus = self.focus = assign({}, opts.focus || {alpha: 0.3}, cursor.focus);
3022
const cursorFocus = focus.prox >= 0;
3025
let cursorPts = [null];
3027
function initCursorPt(s, si) {
3029
let pt = cursor.points.show(self, si);
3032
addClass(pt, CURSOR_PT);
3033
addClass(pt, s.class);
3034
elTrans(pt, -10, -10, plotWidCss, plotHgtCss);
3035
over.insertBefore(pt, cursorPts[si]);
3042
function initSeries(s, i) {
3043
if (mode == 1 || i > 0) {
3044
let isTime = mode == 1 && scales[s.scale].time;
3047
s.value = isTime ? (isStr(sv) ? timeSeriesVal(_tzDate, timeSeriesStamp(sv, _fmtDate)) : sv || _timeSeriesVal) : sv || numSeriesVal;
3048
s.label = s.label || (isTime ? timeSeriesLabel : numSeriesLabel);
3052
s.width = s.width == null ? 1 : s.width;
3053
s.paths = s.paths || linearPath || retNull;
3054
s.fillTo = fnOrSelf(s.fillTo || seriesFillTo);
3055
s.pxAlign = +ifNull(s.pxAlign, pxAlign);
3056
s.pxRound = pxRoundGen(s.pxAlign);
3058
s.stroke = fnOrSelf(s.stroke || null);
3059
s.fill = fnOrSelf(s.fill || null);
3060
s._stroke = s._fill = s._paths = s._focus = null;
3062
let _ptDia = ptDia(s.width, 1);
3063
let points = s.points = assign({}, {
3065
width: max(1, _ptDia * .2),
3072
points.show = fnOrSelf(points.show);
3073
points.filter = fnOrSelf(points.filter);
3074
points.fill = fnOrSelf(points.fill);
3075
points.stroke = fnOrSelf(points.stroke);
3076
points.paths = fnOrSelf(points.paths);
3077
points.pxAlign = s.pxAlign;
3081
let rowCells = initLegendRow(s, i);
3082
legendRows.splice(i, 0, rowCells[0]);
3083
legendCells.splice(i, 0, rowCells[1]);
3084
legend.values.push(null);
3088
activeIdxs.splice(i, 0, null);
3090
let pt = initCursorPt(s, i);
3091
pt && cursorPts.splice(i, 0, pt);
3095
function addSeries(opts, si) {
3096
si = si == null ? series.length : si;
3098
opts = setDefault(opts, si, xSeriesOpts, ySeriesOpts);
3099
series.splice(si, 0, opts);
3100
initSeries(series[si], si);
3103
self.addSeries = addSeries;
3105
function delSeries(i) {
3106
series.splice(i, 1);
3109
legend.values.splice(i, 1);
3111
legendCells.splice(i, 1);
3112
let tr = legendRows.splice(i, 1)[0];
3113
offMouse(null, tr.firstChild);
3118
activeIdxs.splice(i, 1);
3120
cursorPts.length > 1 && cursorPts.splice(i, 1)[0].remove();
3126
self.delSeries = delSeries;
3128
const sidesWithAxes = [false, false, false, false];
3130
function initAxis(axis, i) {
3131
axis._show = axis.show;
3134
let isVt = axis.side % 2;
3136
let sc = scales[axis.scale];
3140
axis.scale = isVt ? series[1].scale : xScaleKey;
3141
sc = scales[axis.scale];
3145
let isTime = sc.time;
3147
axis.size = fnOrSelf(axis.size);
3148
axis.space = fnOrSelf(axis.space);
3149
axis.rotate = fnOrSelf(axis.rotate);
3150
axis.incrs = fnOrSelf(axis.incrs || ( sc.distr == 2 ? wholeIncrs : (isTime ? (ms == 1 ? timeIncrsMs : timeIncrsS) : numIncrs)));
3151
axis.splits = fnOrSelf(axis.splits || (isTime && sc.distr == 1 ? _timeAxisSplits : sc.distr == 3 ? logAxisSplits : sc.distr == 4 ? asinhAxisSplits : numAxisSplits));
3153
axis.stroke = fnOrSelf(axis.stroke);
3154
axis.grid.stroke = fnOrSelf(axis.grid.stroke);
3155
axis.ticks.stroke = fnOrSelf(axis.ticks.stroke);
3157
let av = axis.values;
3161
isArr(av) && !isArr(av[0]) ? fnOrSelf(av) :
3166
timeAxisVals(_tzDate, timeAxisStamps(av, _fmtDate)) :
3169
timeAxisVal(_tzDate, av) :
3171
) : av || numAxisVals
3174
axis.filter = fnOrSelf(axis.filter || ( sc.distr >= 3 ? logAxisValsFilt : retArg1));
3176
axis.font = pxRatioFont(axis.font);
3177
axis.labelFont = pxRatioFont(axis.labelFont);
3179
axis._size = axis.size(self, null, i, 0);
3186
axis._values = null;
3189
sidesWithAxes[i] = true;
3191
axis._el = placeDiv(AXIS, wrap);
3198
function autoPadSide(self, side, sidesWithAxes, cycleNum) {
3199
let [hasTopAxis, hasRgtAxis, hasBtmAxis, hasLftAxis] = sidesWithAxes;
3204
if (ori == 0 && (hasLftAxis || hasRgtAxis))
3205
size = (side == 0 && !hasTopAxis || side == 2 && !hasBtmAxis ? round(xAxisOpts.size / 3) : 0);
3206
if (ori == 1 && (hasTopAxis || hasBtmAxis))
3207
size = (side == 1 && !hasRgtAxis || side == 3 && !hasLftAxis ? round(yAxisOpts.size / 2) : 0);
3212
const padding = self.padding = (opts.padding || [autoPadSide,autoPadSide,autoPadSide,autoPadSide]).map(p => fnOrSelf(ifNull(p, autoPadSide)));
3213
const _padding = self._padding = padding.map((p, i) => p(self, i, sidesWithAxes, 0));
3220
const idxs = mode == 1 ? series[0].idxs : null;
3224
let viaAutoScaleX = false;
3226
function setData(_data, _resetScales) {
3229
for (let i = 1; i < series.length; i++)
3230
dataLen += data[i][0].length;
3231
self.data = data = _data;
3234
data = (_data || []).slice();
3235
data[0] = data[0] || [];
3237
self.data = data.slice();
3239
dataLen = data0.length;
3241
if (xScaleDistr == 2)
3242
data[0] = data0.map((v, i) => i);
3251
if (_resetScales !== false) {
3254
if (xsc.auto(self, viaAutoScaleX))
3257
_setScale(xScaleKey, xsc.min, xsc.max);
3259
shouldSetCursor = cursor.left >= 0;
3260
shouldSetLegend = true;
3265
self.setData = setData;
3267
function autoScaleX() {
3268
viaAutoScaleX = true;
3275
i1 = idxs[1] = dataLen - 1;
3280
if (xScaleDistr == 2) {
3284
else if (dataLen == 1) {
3285
if (xScaleDistr == 3)
3286
[_min, _max] = rangeLog(_min, _min, scaleX.log, false);
3287
else if (xScaleDistr == 4)
3288
[_min, _max] = rangeAsinh(_min, _min, scaleX.log, false);
3289
else if (scaleX.time)
3290
_max = _min + round(86400 / ms);
3292
[_min, _max] = rangeNum(_min, _max, rangePad, true);
3296
i0 = idxs[0] = _min = null;
3297
i1 = idxs[1] = _max = null;
3301
_setScale(xScaleKey, _min, _max);
3304
let ctxStroke, ctxFill, ctxWidth, ctxDash, ctxJoin, ctxCap, ctxFont, ctxAlign, ctxBaseline;
3307
function setCtxStyle(stroke = transparent, width, dash = EMPTY_ARR, cap = "butt", fill = transparent, join = "round") {
3308
if (stroke != ctxStroke)
3309
ctx.strokeStyle = ctxStroke = stroke;
3310
if (fill != ctxFill)
3311
ctx.fillStyle = ctxFill = fill;
3312
if (width != ctxWidth)
3313
ctx.lineWidth = ctxWidth = width;
3314
if (join != ctxJoin)
3315
ctx.lineJoin = ctxJoin = join;
3317
ctx.lineCap = ctxCap = cap;
3318
if (dash != ctxDash)
3319
ctx.setLineDash(ctxDash = dash);
3322
function setFontStyle(font, fill, align, baseline) {
3323
if (fill != ctxFill)
3324
ctx.fillStyle = ctxFill = fill;
3325
if (font != ctxFont)
3326
ctx.font = ctxFont = font;
3327
if (align != ctxAlign)
3328
ctx.textAlign = ctxAlign = align;
3329
if (baseline != ctxBaseline)
3330
ctx.textBaseline = ctxBaseline = baseline;
3333
function accScale(wsc, psc, facet, data) {
3334
if (wsc.auto(self, viaAutoScaleX) && (psc == null || psc.min == null)) {
3335
let _i0 = ifNull(i0, 0);
3336
let _i1 = ifNull(i1, data.length - 1);
3339
let minMax = facet.min == null ? (wsc.distr == 3 ? getMinMaxLog(data, _i0, _i1) : getMinMax(data, _i0, _i1)) : [facet.min, facet.max];
3342
wsc.min = min(wsc.min, facet.min = minMax[0]);
3343
wsc.max = max(wsc.max, facet.max = minMax[1]);
3347
function setScales() {
3351
let wipScales = copy(scales, fastIsObj);
3353
for (let k in wipScales) {
3354
let wsc = wipScales[k];
3355
let psc = pendScales[k];
3357
if (psc != null && psc.min != null) {
3364
else if (k != xScaleKey || mode == 2) {
3365
if (dataLen == 0 && wsc.from == null) {
3366
let minMax = wsc.range(self, null, null, k);
3367
wsc.min = minMax[0];
3368
wsc.max = minMax[1];
3379
series.forEach((s, i) => {
3382
let wsc = wipScales[k];
3383
let psc = pendScales[k];
3386
let minMax = wsc.range(self, wsc.min, wsc.max, k);
3388
wsc.min = minMax[0];
3389
wsc.max = minMax[1];
3391
i0 = closestIdx(wsc.min, data[0]);
3392
i1 = closestIdx(wsc.max, data[0]);
3395
if (data[0][i0] < wsc.min)
3397
if (data[0][i1] > wsc.max)
3403
else if (s.show && s.auto)
3404
accScale(wsc, psc, s, data[i]);
3411
if (s.show && s.auto) {
3413
let [ xFacet, yFacet ] = s.facets;
3414
let xScaleKey = xFacet.scale;
3415
let yScaleKey = yFacet.scale;
3416
let [ xData, yData ] = data[i];
3418
accScale(wipScales[xScaleKey], pendScales[xScaleKey], xFacet, xData);
3419
accScale(wipScales[yScaleKey], pendScales[yScaleKey], yFacet, yData);
3430
for (let k in wipScales) {
3431
let wsc = wipScales[k];
3432
let psc = pendScales[k];
3434
if (wsc.from == null && (psc == null || psc.min == null)) {
3435
let minMax = wsc.range(
3437
wsc.min == inf ? null : wsc.min,
3438
wsc.max == -inf ? null : wsc.max,
3441
wsc.min = minMax[0];
3442
wsc.max = minMax[1];
3448
for (let k in wipScales) {
3449
let wsc = wipScales[k];
3451
if (wsc.from != null) {
3452
let base = wipScales[wsc.from];
3454
if (base.min == null)
3455
wsc.min = wsc.max = null;
3457
let minMax = wsc.range(self, base.min, base.max, k);
3458
wsc.min = minMax[0];
3459
wsc.max = minMax[1];
3465
let anyChanged = false;
3467
for (let k in wipScales) {
3468
let wsc = wipScales[k];
3471
if (sc.min != wsc.min || sc.max != wsc.max) {
3475
let distr = sc.distr;
3477
sc._min = distr == 3 ? log10(sc.min) : distr == 4 ? asinh(sc.min, sc.asinh) : sc.min;
3478
sc._max = distr == 3 ? log10(sc.max) : distr == 4 ? asinh(sc.max, sc.asinh) : sc.max;
3480
changed[k] = anyChanged = true;
3486
series.forEach((s, i) => {
3488
if (i > 0 && changed.y)
3492
if (changed[s.scale])
3497
for (let k in changed) {
3498
shouldConvergeSize = true;
3499
fire("setScale", k);
3503
shouldSetCursor = shouldSetLegend = cursor.left >= 0;
3506
for (let k in pendScales)
3507
pendScales[k] = null;
3511
function getOuterIdxs(ydata) {
3512
let _i0 = clamp(i0 - 1, 0, dataLen - 1);
3513
let _i1 = clamp(i1 + 1, 0, dataLen - 1);
3515
while (ydata[_i0] == null && _i0 > 0)
3518
while (ydata[_i1] == null && _i1 < dataLen - 1)
3524
function drawSeries() {
3526
series.forEach((s, i) => {
3527
if (i > 0 && s.show && s._paths == null) {
3528
let _idxs = getOuterIdxs(data[i]);
3529
s._paths = s.paths(self, i, _idxs[0], _idxs[1]);
3533
series.forEach((s, i) => {
3534
if (i > 0 && s.show) {
3535
if (ctxAlpha != s.alpha)
3536
ctx.globalAlpha = ctxAlpha = s.alpha;
3539
cacheStrokeFill(i, false);
3540
s._paths && drawPath(i, false);
3544
cacheStrokeFill(i, true);
3546
let show = s.points.show(self, i, i0, i1);
3547
let idxs = s.points.filter(self, i, show, s._paths ? s._paths.gaps : null);
3550
s.points._paths = s.points.paths(self, i, i0, i1, idxs);
3556
ctx.globalAlpha = ctxAlpha = 1;
3558
fire("drawSeries", i);
3564
function cacheStrokeFill(si, _points) {
3565
let s = _points ? series[si].points : series[si];
3567
s._stroke = s.stroke(self, si);
3568
s._fill = s.fill(self, si);
3571
function drawPath(si, _points) {
3572
let s = _points ? series[si].points : series[si];
3574
let strokeStyle = s._stroke;
3575
let fillStyle = s._fill;
3577
let { stroke, fill, clip: gapsClip, flags } = s._paths;
3578
let boundsClip = null;
3579
let width = roundDec(s.width * pxRatio, 3);
3580
let offset = (width % 2) / 2;
3582
if (_points && fillStyle == null)
3583
fillStyle = width > 0 ? "#fff" : strokeStyle;
3585
let _pxAlign = s.pxAlign == 1;
3587
_pxAlign && ctx.translate(offset, offset);
3595
let halfWid = width * pxRatio / 2;
3605
boundsClip = new Path2D();
3606
boundsClip.rect(lft, top, wid, hgt);
3611
strokeFill(strokeStyle, width, s.dash, s.cap, fillStyle, stroke, fill, flags, gapsClip);
3613
fillStroke(si, strokeStyle, width, s.dash, s.cap, fillStyle, stroke, fill, flags, boundsClip, gapsClip);
3615
_pxAlign && ctx.translate(-offset, -offset);
3618
function fillStroke(si, strokeStyle, lineWidth, lineDash, lineCap, fillStyle, strokePath, fillPath, flags, boundsClip, gapsClip) {
3619
let didStrokeFill = false;
3623
bands.forEach((b, bi) => {
3625
if (b.series[0] == si) {
3626
let lowerEdge = series[b.series[1]];
3627
let lowerData = data[b.series[1]];
3629
let bandClip = (lowerEdge._paths || EMPTY_OBJ).band;
3632
let _fillStyle = null;
3635
if (lowerEdge.show && bandClip && hasData(lowerData, i0, i1)) {
3636
_fillStyle = b.fill(self, bi) || fillStyle;
3637
gapsClip2 = lowerEdge._paths.clip;
3642
strokeFill(strokeStyle, lineWidth, lineDash, lineCap, _fillStyle, strokePath, fillPath, flags, boundsClip, gapsClip, gapsClip2, bandClip);
3644
didStrokeFill = true;
3649
strokeFill(strokeStyle, lineWidth, lineDash, lineCap, fillStyle, strokePath, fillPath, flags, boundsClip, gapsClip);
3652
const CLIP_FILL_STROKE = BAND_CLIP_FILL | BAND_CLIP_STROKE;
3654
function strokeFill(strokeStyle, lineWidth, lineDash, lineCap, fillStyle, strokePath, fillPath, flags, boundsClip, gapsClip, gapsClip2, bandClip) {
3655
setCtxStyle(strokeStyle, lineWidth, lineDash, lineCap, fillStyle);
3657
if (boundsClip || gapsClip || bandClip) {
3659
boundsClip && ctx.clip(boundsClip);
3660
gapsClip && ctx.clip(gapsClip);
3664
if ((flags & CLIP_FILL_STROKE) == CLIP_FILL_STROKE) {
3666
gapsClip2 && ctx.clip(gapsClip2);
3667
doFill(fillStyle, fillPath);
3668
doStroke(strokeStyle, strokePath, lineWidth);
3670
else if (flags & BAND_CLIP_STROKE) {
3671
doFill(fillStyle, fillPath);
3673
doStroke(strokeStyle, strokePath, lineWidth);
3675
else if (flags & BAND_CLIP_FILL) {
3678
gapsClip2 && ctx.clip(gapsClip2);
3679
doFill(fillStyle, fillPath);
3681
doStroke(strokeStyle, strokePath, lineWidth);
3685
doFill(fillStyle, fillPath);
3686
doStroke(strokeStyle, strokePath, lineWidth);
3689
if (boundsClip || gapsClip || bandClip)
3693
function doStroke(strokeStyle, strokePath, lineWidth) {
3694
if (lineWidth > 0) {
3695
if (strokePath instanceof Map) {
3696
strokePath.forEach((strokePath, strokeStyle) => {
3697
ctx.strokeStyle = ctxStroke = strokeStyle;
3698
ctx.stroke(strokePath);
3702
strokePath != null && strokeStyle && ctx.stroke(strokePath);
3706
function doFill(fillStyle, fillPath) {
3707
if (fillPath instanceof Map) {
3708
fillPath.forEach((fillPath, fillStyle) => {
3709
ctx.fillStyle = ctxFill = fillStyle;
3714
fillPath != null && fillStyle && ctx.fill(fillPath);
3717
function getIncrSpace(axisIdx, min, max, fullDim) {
3718
let axis = axes[axisIdx];
3725
let minSpace = axis._space = axis.space(self, axisIdx, min, max, fullDim);
3726
let incrs = axis._incrs = axis.incrs(self, axisIdx, min, max, fullDim, minSpace);
3727
incrSpace = findIncr(min, max, incrs, fullDim, minSpace);
3730
return (axis._found = incrSpace);
3733
function drawOrthoLines(offs, filts, ori, side, pos0, len, width, stroke, dash, cap) {
3734
let offset = (width % 2) / 2;
3736
pxAlign == 1 && ctx.translate(offset, offset);
3738
setCtxStyle(stroke, width, dash, cap, stroke);
3742
let x0, y0, x1, y1, pos1 = pos0 + (side == 0 || side == 3 ? -len : len);
3753
for (let i = 0; i < offs.length; i++) {
3754
if (filts[i] != null) {
3767
pxAlign == 1 && ctx.translate(-offset, -offset);
3770
function axesCalc(cycleNum) {
3773
let converged = true;
3775
axes.forEach((axis, i) => {
3779
let scale = scales[axis.scale];
3781
if (scale.min == null) {
3785
resetYSeries(false);
3793
resetYSeries(false);
3797
let side = axis.side;
3800
let {min, max} = scale;
3802
let [_incr, _space] = getIncrSpace(i, min, max, ori == 0 ? plotWidCss : plotHgtCss);
3808
let forceMin = scale.distr == 2;
3810
let _splits = axis._splits = axis.splits(self, i, min, max, _incr, _space, forceMin);
3814
let splits = scale.distr == 2 ? _splits.map(i => data0[i]) : _splits;
3815
let incr = scale.distr == 2 ? data0[_splits[1]] - data0[_splits[0]] : _incr;
3817
let values = axis._values = axis.values(self, axis.filter(self, splits, i, _space, incr), i, _space, incr);
3820
axis._rotate = side == 2 ? axis.rotate(self, values, i, _space) : 0;
3822
let oldSize = axis._size;
3824
axis._size = ceil(axis.size(self, values, i, cycleNum));
3826
if (oldSize != null && axis._size != oldSize)
3833
function paddingCalc(cycleNum) {
3834
let converged = true;
3836
padding.forEach((p, i) => {
3837
let _p = p(self, i, sidesWithAxes, cycleNum);
3839
if (_p != _padding[i])
3848
function drawAxesGrid() {
3849
for (let i = 0; i < axes.length; i++) {
3852
if (!axis.show || !axis._show)
3855
let side = axis.side;
3860
let fillStyle = axis.stroke(self, i);
3862
let shiftDir = side == 0 || side == 3 ? -1 : 1;
3866
let shiftAmt = axis.labelGap * shiftDir;
3867
let baseLpos = round((axis._lpos + shiftAmt) * pxRatio);
3869
setFontStyle(axis.labelFont[0], fillStyle, "center", side == 2 ? TOP : BOTTOM);
3878
round(plotTop + plotHgt / 2),
3880
ctx.rotate((side == 3 ? -PI : PI) / 2);
3884
x = round(plotLft + plotWid / 2);
3888
ctx.fillText(axis.label, x, y);
3893
let [_incr, _space] = axis._found;
3898
let scale = scales[axis.scale];
3900
let plotDim = ori == 0 ? plotWid : plotHgt;
3901
let plotOff = ori == 0 ? plotLft : plotTop;
3903
let axisGap = round(axis.gap * pxRatio);
3905
let _splits = axis._splits;
3909
let splits = scale.distr == 2 ? _splits.map(i => data0[i]) : _splits;
3910
let incr = scale.distr == 2 ? data0[_splits[1]] - data0[_splits[0]] : _incr;
3912
let ticks = axis.ticks;
3913
let tickSize = ticks.show ? round(ticks.size * pxRatio) : 0;
3916
let angle = axis._rotate * -PI/180;
3918
let basePos = pxRound(axis._pos * pxRatio);
3919
let shiftAmt = (tickSize + axisGap) * shiftDir;
3920
let finalPos = basePos + shiftAmt;
3921
y = ori == 0 ? finalPos : 0;
3922
x = ori == 1 ? finalPos : 0;
3924
let font = axis.font[0];
3925
let textAlign = axis.align == 1 ? LEFT :
3926
axis.align == 2 ? RIGHT :
3929
ori == 0 ? "center" : side == 3 ? RIGHT : LEFT;
3930
let textBaseline = angle ||
3931
ori == 1 ? "middle" : side == 2 ? TOP : BOTTOM;
3933
setFontStyle(font, fillStyle, textAlign, textBaseline);
3935
let lineHeight = axis.font[1] * lineMult;
3937
let canOffs = _splits.map(val => pxRound(getPos(val, scale, plotDim, plotOff)));
3939
let _values = axis._values;
3941
for (let i = 0; i < _values.length; i++) {
3942
let val = _values[i];
3952
let _parts = val.indexOf("\n") == -1 ? [val] : val.split(/\n/gm);
3954
for (let j = 0; j < _parts.length; j++) {
3955
let text = _parts[j];
3959
ctx.translate(x, y + j * lineHeight);
3961
ctx.fillText(text, 0, 0);
3965
ctx.fillText(text, x, y + j * lineHeight);
3974
ticks.filter(self, splits, i, _space, incr),
3979
roundDec(ticks.width * pxRatio, 3),
3980
ticks.stroke(self, i),
3987
let grid = axis.grid;
3992
grid.filter(self, splits, i, _space, incr),
3995
ori == 0 ? plotTop : plotLft,
3996
ori == 0 ? plotHgt : plotWid,
3997
roundDec(grid.width * pxRatio, 3),
3998
grid.stroke(self, i),
4008
function resetYSeries(minMax) {
4011
series.forEach((s, i) => {
4021
s.facets.forEach(f => {
4031
let queuedCommit = false;
4034
if (!queuedCommit) {
4036
queuedCommit = true;
4040
function _commit() {
4043
if (shouldSetScales) {
4045
shouldSetScales = false;
4048
if (shouldConvergeSize) {
4050
shouldConvergeSize = false;
4053
if (shouldSetSize) {
4054
setStylePx(under, LEFT, plotLftCss);
4055
setStylePx(under, TOP, plotTopCss);
4056
setStylePx(under, WIDTH, plotWidCss);
4057
setStylePx(under, HEIGHT, plotHgtCss);
4059
setStylePx(over, LEFT, plotLftCss);
4060
setStylePx(over, TOP, plotTopCss);
4061
setStylePx(over, WIDTH, plotWidCss);
4062
setStylePx(over, HEIGHT, plotHgtCss);
4064
setStylePx(wrap, WIDTH, fullWidCss);
4065
setStylePx(wrap, HEIGHT, fullHgtCss);
4069
can.width = round(fullWidCss * pxRatio);
4070
can.height = round(fullHgtCss * pxRatio);
4074
let { _show, _el, _size, _pos, side } = a;
4077
let posOffset = (side === 3 || side === 0 ? _size : 0);
4078
let isVt = side % 2 == 1;
4080
setStylePx(_el, isVt ? "left" : "top", _pos - posOffset);
4081
setStylePx(_el, isVt ? "width" : "height", _size);
4082
setStylePx(_el, isVt ? "top" : "left", isVt ? plotTopCss : plotLftCss);
4083
setStylePx(_el, isVt ? "height" : "width", isVt ? plotHgtCss : plotWidCss);
4085
_el && remClass(_el, OFF);
4088
_el && addClass(_el, OFF);
4092
ctxStroke = ctxFill = ctxWidth = ctxJoin = ctxCap = ctxFont = ctxAlign = ctxBaseline = ctxDash = null;
4099
shouldSetSize = false;
4102
if (fullWidCss > 0 && fullHgtCss > 0) {
4103
ctx.clearRect(0, 0, can.width, can.height);
4105
drawOrder.forEach(fn => fn());
4118
if (cursor.show && shouldSetCursor) {
4119
updateCursor(null, true, false);
4120
shouldSetCursor = false;
4132
viaAutoScaleX = false;
4134
queuedCommit = false;
4137
self.redraw = (rebuildPaths, recalcAxes) => {
4138
shouldConvergeSize = recalcAxes || false;
4140
if (rebuildPaths !== false)
4141
_setScale(xScaleKey, scaleX.min, scaleX.max);
4149
function setScale(key, opts) {
4150
let sc = scales[key];
4152
if (sc.from == null) {
4154
let minMax = sc.range(self, opts.min, opts.max, key);
4155
opts.min = minMax[0];
4156
opts.max = minMax[1];
4159
if (opts.min > opts.max) {
4160
let _min = opts.min;
4161
opts.min = opts.max;
4165
if (dataLen > 1 && opts.min != null && opts.max != null && opts.max - opts.min < 1e-16)
4168
if (key == xScaleKey) {
4169
if (sc.distr == 2 && dataLen > 0) {
4170
opts.min = closestIdx(opts.min, data[0]);
4171
opts.max = closestIdx(opts.max, data[0]);
4173
if (opts.min == opts.max)
4180
pendScales[key] = opts;
4182
shouldSetScales = true;
4187
self.setScale = setScale;
4212
let dragging = false;
4214
const drag = cursor.drag;
4221
xCursor = placeDiv(CURSOR_X, over);
4223
yCursor = placeDiv(CURSOR_Y, over);
4225
if (scaleX.ori == 0) {
4234
mouseLeft1 = cursor.left;
4235
mouseTop1 = cursor.top;
4238
const select = self.select = assign({
4247
const selectDiv = select.show ? placeDiv(SELECT, select.over ? over : under) : null;
4249
function setSelect(opts, _fire) {
4251
for (let prop in opts)
4252
setStylePx(selectDiv, prop, select[prop] = opts[prop]);
4254
_fire !== false && fire("setSelect");
4258
self.setSelect = setSelect;
4260
function toggleDOM(i, onOff) {
4262
let label = showLegend ? legendRows[i] : null;
4265
label && remClass(label, OFF);
4267
label && addClass(label, OFF);
4268
cursorPts.length > 1 && elTrans(cursorPts[i], -10, -10, plotWidCss, plotHgtCss);
4272
function _setScale(key, min, max) {
4273
setScale(key, {min, max});
4276
function setSeries(i, opts, _fire, _pub) {
4281
if (opts.focus != null)
4284
if (opts.show != null) {
4286
toggleDOM(i, opts.show);
4288
_setScale(mode == 2 ? s.facets[1].scale : s.scale, null, null);
4292
_fire !== false && fire("setSeries", i, opts);
4294
_pub && pubSync("setSeries", self, i, opts);
4297
self.setSeries = setSeries;
4299
function setBand(bi, opts) {
4300
assign(bands[bi], opts);
4303
function addBand(opts, bi) {
4304
opts.fill = fnOrSelf(opts.fill || null);
4305
bi = bi == null ? bands.length : bi;
4306
bands.splice(bi, 0, opts);
4309
function delBand(bi) {
4313
bands.splice(bi, 1);
4316
self.addBand = addBand;
4317
self.setBand = setBand;
4318
self.delBand = delBand;
4320
function setAlpha(i, value) {
4321
series[i].alpha = value;
4323
if (cursor.show && cursorPts[i])
4324
cursorPts[i].style.opacity = value;
4326
if (showLegend && legendRows[i])
4327
legendRows[i].style.opacity = value;
4334
const FOCUS_TRUE = {focus: true};
4335
const FOCUS_FALSE = {focus: false};
4337
function setFocus(i) {
4338
if (i != focusedSeries) {
4341
let allFocused = i == null;
4343
let _setAlpha = focus.alpha != 1;
4345
series.forEach((s, i2) => {
4346
let isFocused = allFocused || i2 == 0 || i2 == i;
4347
s._focus = allFocused ? null : isFocused;
4348
_setAlpha && setAlpha(i2, isFocused ? 1 : focus.alpha);
4352
_setAlpha && commit();
4356
if (showLegend && cursorFocus) {
4357
on(mouseleave, legendEl, e => {
4360
setSeries(null, FOCUS_FALSE, true, syncOpts.setSeries);
4361
updateCursor(null, true, false);
4365
function posToVal(pos, scale, can) {
4366
let sc = scales[scale];
4369
pos = pos / pxRatio - (sc.ori == 1 ? plotTopCss : plotLftCss);
4371
let dim = plotWidCss;
4385
let sv = _min + (_max - _min) * pct;
4387
let distr = sc.distr;
4390
distr == 3 ? pow(10, sv) :
4391
distr == 4 ? sinh(sv, sc.asinh) :
4396
function closestIdxFromXpos(pos, can) {
4397
let v = posToVal(pos, xScaleKey, can);
4398
return closestIdx(v, data[0], i0, i1);
4401
self.valToIdx = val => closestIdx(val, data[0]);
4402
self.posToIdx = closestIdxFromXpos;
4403
self.posToVal = posToVal;
4404
self.valToPos = (val, scale, can) => (
4405
scales[scale].ori == 0 ?
4406
getHPos(val, scales[scale],
4407
can ? plotWid : plotWidCss,
4410
getVPos(val, scales[scale],
4411
can ? plotHgt : plotHgtCss,
4417
function batch(fn) {
4424
(self.setCursor = (opts, _fire, _pub) => {
4425
mouseLeft1 = opts.left;
4426
mouseTop1 = opts.top;
4428
updateCursor(null, _fire, _pub);
4431
function setSelH(off, dim) {
4432
setStylePx(selectDiv, LEFT, select.left = off);
4433
setStylePx(selectDiv, WIDTH, select.width = dim);
4436
function setSelV(off, dim) {
4437
setStylePx(selectDiv, TOP, select.top = off);
4438
setStylePx(selectDiv, HEIGHT, select.height = dim);
4441
let setSelX = scaleX.ori == 0 ? setSelH : setSelV;
4442
let setSelY = scaleX.ori == 1 ? setSelH : setSelV;
4444
function syncLegend() {
4445
if (showLegend && legend.live) {
4446
for (let i = mode == 2 ? 1 : 0; i < series.length; i++) {
4447
if (i == 0 && multiValLegend)
4450
let vals = legend.values[i];
4455
legendCells[i][j++].firstChild.nodeValue = vals[k];
4460
function setLegend(opts, _fire) {
4465
series.forEach((s, sidx) => {
4466
(sidx > 0 || !multiValLegend) && setLegendValues(sidx, idx);
4470
if (showLegend && legend.live)
4473
shouldSetLegend = false;
4475
_fire !== false && fire("setLegend");
4478
self.setLegend = setLegend;
4480
function setLegendValues(sidx, idx) {
4484
val = NULL_LEGEND_VALUES;
4486
let s = series[sidx];
4487
let src = sidx == 0 && xScaleDistr == 2 ? data0 : data[sidx];
4488
val = multiValLegend ? s.values(self, sidx, idx) : {_: s.value(self, src[idx], sidx, idx)};
4491
legend.values[sidx] = val;
4494
function updateCursor(src, _fire, _pub) {
4497
rawMouseLeft1 = mouseLeft1;
4498
rawMouseTop1 = mouseTop1;
4500
[mouseLeft1, mouseTop1] = cursor.move(self, mouseLeft1, mouseTop1);
4503
vCursor && elTrans(vCursor, round(mouseLeft1), 0, plotWidCss, plotHgtCss);
4504
hCursor && elTrans(hCursor, 0, round(mouseTop1), plotWidCss, plotHgtCss);
4511
let noDataInRange = i0 > i1;
4516
let xDim = scaleX.ori == 0 ? plotWidCss : plotHgtCss;
4517
let yDim = scaleX.ori == 1 ? plotWidCss : plotHgtCss;
4520
if (mouseLeft1 < 0 || dataLen == 0 || noDataInRange) {
4523
for (let i = 0; i < series.length; i++) {
4525
cursorPts.length > 1 && elTrans(cursorPts[i], -10, -10, plotWidCss, plotHgtCss);
4530
setSeries(null, FOCUS_TRUE, true, src == null && syncOpts.setSeries);
4533
activeIdxs.fill(null);
4534
shouldSetLegend = true;
4536
for (let i = 0; i < series.length; i++)
4537
legend.values[i] = NULL_LEGEND_VALUES;
4543
let mouseXPos, valAtPosX, xPos;
4546
mouseXPos = scaleX.ori == 0 ? mouseLeft1 : mouseTop1;
4547
valAtPosX = posToVal(mouseXPos, xScaleKey);
4548
idx = closestIdx(valAtPosX, data[0], i0, i1);
4549
xPos = incrRoundUp(valToPosX(data[0][idx], scaleX, xDim, 0), 0.5);
4552
for (let i = mode == 2 ? 1 : 0; i < series.length; i++) {
4555
let idx1 = activeIdxs[i];
4556
let yVal1 = mode == 1 ? data[i][idx1] : data[i][1][idx1];
4558
let idx2 = cursor.dataIdx(self, i, idx, valAtPosX);
4559
let yVal2 = mode == 1 ? data[i][idx2] : data[i][1][idx2];
4561
shouldSetLegend = shouldSetLegend || yVal2 != yVal1 || idx2 != idx1;
4563
activeIdxs[i] = idx2;
4565
let xPos2 = idx2 == idx ? xPos : incrRoundUp(valToPosX(mode == 1 ? data[0][idx2] : data[i][0][idx2], scaleX, xDim, 0), 0.5);
4567
if (i > 0 && s.show) {
4568
let yPos = yVal2 == null ? -10 : incrRoundUp(valToPosY(yVal2, mode == 1 ? scales[s.scale] : scales[s.facets[1].scale], yDim, 0), 0.5);
4570
if (yPos > 0 && mode == 1) {
4571
let dist = abs(yPos - mouseTop1);
4573
if (dist <= closestDist) {
4581
if (scaleX.ori == 0) {
4590
if (shouldSetLegend && cursorPts.length > 1) {
4591
elColor(cursorPts[i], cursor.points.fill(self, i), cursor.points.stroke(self, i));
4593
let ptWid, ptHgt, ptLft, ptTop,
4595
getBBox = cursor.points.bbox;
4597
if (getBBox != null) {
4600
let bbox = getBBox(self, i);
4605
ptHgt = bbox.height;
4610
ptWid = ptHgt = cursor.points.size(self, i);
4613
elSize(cursorPts[i], ptWid, ptHgt, centered);
4614
elTrans(cursorPts[i], ptLft, ptTop, plotWidCss, plotHgtCss);
4619
if (!shouldSetLegend || i == 0 && multiValLegend)
4622
setLegendValues(i, idx2);
4628
cursor.left = mouseLeft1;
4629
cursor.top = mouseTop1;
4631
if (shouldSetLegend) {
4637
if (select.show && dragging) {
4639
let [xKey, yKey] = syncOpts.scales;
4640
let [matchXKeys, matchYKeys] = syncOpts.match;
4641
let [xKeySrc, yKeySrc] = src.cursor.sync.scales;
4644
let sdrag = src.cursor.drag;
4648
let { left, top, width, height } = src.select;
4650
let sori = src.scales[xKey].ori;
4651
let sPosToVal = src.posToVal;
4653
let sOff, sDim, sc, a, b;
4655
let matchingX = xKey != null && matchXKeys(xKey, xKeySrc);
4656
let matchingY = yKey != null && matchYKeys(yKey, yKeySrc);
4671
a = valToPosX(sPosToVal(sOff, xKeySrc), sc, xDim, 0);
4672
b = valToPosX(sPosToVal(sOff + sDim, xKeySrc), sc, xDim, 0);
4674
setSelX(min(a,b), abs(b-a));
4696
a = valToPosY(sPosToVal(sOff, yKeySrc), sc, yDim, 0);
4697
b = valToPosY(sPosToVal(sOff + sDim, yKeySrc), sc, yDim, 0);
4699
setSelY(min(a,b), abs(b-a));
4709
let rawDX = abs(rawMouseLeft1 - rawMouseLeft0);
4710
let rawDY = abs(rawMouseTop1 - rawMouseTop0);
4712
if (scaleX.ori == 1) {
4718
dragX = drag.x && rawDX >= drag.dist;
4719
dragY = drag.y && rawDY >= drag.dist;
4725
if (dragX && dragY) {
4726
dragX = rawDX >= uni;
4727
dragY = rawDY >= uni;
4730
if (!dragX && !dragY) {
4738
else if (drag.x && drag.y && (dragX || dragY))
4740
dragX = dragY = true;
4745
if (scaleX.ori == 0) {
4754
setSelX(min(p0, p1), abs(p1 - p0));
4761
if (scaleX.ori == 1) {
4770
setSelY(min(p0, p1), abs(p1 - p0));
4777
if (!dragX && !dragY) {
4789
if (syncKey != null) {
4790
let [xSyncKey, ySyncKey] = syncOpts.scales;
4792
syncOpts.values[0] = xSyncKey != null ? posToVal(scaleX.ori == 0 ? mouseLeft1 : mouseTop1, xSyncKey) : null;
4793
syncOpts.values[1] = ySyncKey != null ? posToVal(scaleX.ori == 1 ? mouseLeft1 : mouseTop1, ySyncKey) : null;
4796
pubSync(mousemove, self, mouseLeft1, mouseTop1, plotWidCss, plotHgtCss, idx);
4800
let shouldPub = _pub && syncOpts.setSeries;
4803
if (focusedSeries == null) {
4804
if (closestDist <= p)
4805
setSeries(closestSeries, FOCUS_TRUE, true, shouldPub);
4808
if (closestDist > p)
4809
setSeries(null, FOCUS_TRUE, true, shouldPub);
4810
else if (closestSeries != focusedSeries)
4811
setSeries(closestSeries, FOCUS_TRUE, true, shouldPub);
4816
ready && _fire !== false && fire("setCursor");
4821
function syncRect(defer) {
4825
rect = over.getBoundingClientRect();
4826
fire("syncRect", rect);
4830
function mouseMove(e, src, _l, _t, _w, _h, _i) {
4834
cacheMouse(e, src, _l, _t, _w, _h, _i, false, e != null);
4837
updateCursor(null, true, true);
4839
updateCursor(src, true, false);
4842
function cacheMouse(e, src, _l, _t, _w, _h, _i, initial, snap) {
4847
_l = e.clientX - rect.left;
4848
_t = e.clientY - rect.top;
4851
if (_l < 0 || _t < 0) {
4857
let [xKey, yKey] = syncOpts.scales;
4859
let syncOptsSrc = src.cursor.sync;
4860
let [xValSrc, yValSrc] = syncOptsSrc.values;
4861
let [xKeySrc, yKeySrc] = syncOptsSrc.scales;
4862
let [matchXKeys, matchYKeys] = syncOpts.match;
4864
let rotSrc = src.scales[xKeySrc].ori == 1;
4866
let xDim = scaleX.ori == 0 ? plotWidCss : plotHgtCss,
4867
yDim = scaleX.ori == 1 ? plotWidCss : plotHgtCss,
4868
_xDim = rotSrc ? _h : _w,
4869
_yDim = rotSrc ? _w : _h,
4870
_xPos = rotSrc ? _t : _l,
4871
_yPos = rotSrc ? _l : _t;
4873
if (xKeySrc != null)
4874
_l = matchXKeys(xKey, xKeySrc) ? getPos(xValSrc, scales[xKey], xDim, 0) : -10;
4876
_l = xDim * (_xPos/_xDim);
4878
if (yKeySrc != null)
4879
_t = matchYKeys(yKey, yKeySrc) ? getPos(yValSrc, scales[yKey], yDim, 0) : -10;
4881
_t = yDim * (_yPos/_yDim);
4883
if (scaleX.ori == 1) {
4891
if (_l <= 1 || _l >= plotWidCss - 1)
4892
_l = incrRound(_l, plotWidCss);
4894
if (_t <= 1 || _t >= plotHgtCss - 1)
4895
_t = incrRound(_t, plotHgtCss);
4902
[mouseLeft0, mouseTop0] = cursor.move(self, _l, _t);
4910
function hideSelect() {
4917
function mouseDown(e, src, _l, _t, _w, _h, _i) {
4919
dragX = dragY = drag._x = drag._y = false;
4921
cacheMouse(e, src, _l, _t, _w, _h, _i, true, false);
4924
onMouse(mouseup, doc, mouseUp);
4925
pubSync(mousedown, self, mouseLeft0, mouseTop0, plotWidCss, plotHgtCss, null);
4929
function mouseUp(e, src, _l, _t, _w, _h, _i) {
4930
dragging = drag._x = drag._y = false;
4932
cacheMouse(e, src, _l, _t, _w, _h, _i, false, true);
4934
let { left, top, width, height } = select;
4936
let hasSelect = width > 0 || height > 0;
4938
hasSelect && setSelect(select);
4940
if (drag.setScale && hasSelect) {
4951
if (scaleX.ori == 1) {
4959
_setScale(xScaleKey,
4960
posToVal(xOff, xScaleKey),
4961
posToVal(xOff + xDim, xScaleKey)
4966
for (let k in scales) {
4969
if (k != xScaleKey && sc.from == null && sc.min != inf) {
4971
posToVal(yOff + yDim, k),
4980
else if (cursor.lock) {
4981
cursor._lock = !cursor._lock;
4984
updateCursor(null, true, false);
4988
offMouse(mouseup, doc);
4989
pubSync(mouseup, self, mouseLeft1, mouseTop1, plotWidCss, plotHgtCss, null);
4993
function mouseLeave(e, src, _l, _t, _w, _h, _i) {
4994
if (!cursor._lock) {
4995
let _dragging = dragging;
5005
if (scaleX.ori == 0) {
5014
if (dragH && dragV) {
5016
snapH = mouseLeft1 <= snapProx || mouseLeft1 >= plotWidCss - snapProx;
5017
snapV = mouseTop1 <= snapProx || mouseTop1 >= plotHgtCss - snapProx;
5021
mouseLeft1 = mouseLeft1 < mouseLeft0 ? 0 : plotWidCss;
5024
mouseTop1 = mouseTop1 < mouseTop0 ? 0 : plotHgtCss;
5026
updateCursor(null, true, true);
5035
updateCursor(null, true, true);
5038
dragging = _dragging;
5042
function dblClick(e, src, _l, _t, _w, _h, _i) {
5048
pubSync(dblclick, self, mouseLeft1, mouseTop1, plotWidCss, plotHgtCss, null);
5051
function syncPxRatio() {
5052
axes.forEach(syncFontSize);
5053
_setSize(self.width, self.height, true);
5056
on(dppxchange, win, syncPxRatio);
5061
events.mousedown = mouseDown;
5062
events.mousemove = mouseMove;
5063
events.mouseup = mouseUp;
5064
events.dblclick = dblClick;
5065
events["setSeries"] = (e, src, idx, opts) => {
5066
setSeries(idx, opts, true, false);
5070
onMouse(mousedown, over, mouseDown);
5071
onMouse(mousemove, over, mouseMove);
5072
onMouse(mouseenter, over, syncRect);
5073
onMouse(mouseleave, over, mouseLeave);
5075
onMouse(dblclick, over, dblClick);
5077
cursorPlots.add(self);
5079
self.syncRect = syncRect;
5083
const hooks = self.hooks = opts.hooks || {};
5085
function fire(evName, a1, a2) {
5086
if (evName in hooks) {
5087
hooks[evName].forEach(fn => {
5088
fn.call(null, self, a1, a2);
5093
(opts.plugins || []).forEach(p => {
5094
for (let evName in p.hooks)
5095
hooks[evName] = (hooks[evName] || []).concat(p.hooks[evName]);
5098
const syncOpts = assign({
5105
scales: [xScaleKey, series[1] ? series[1].scale : null],
5106
match: [retEq, retEq],
5107
values: [null, null],
5110
(cursor.sync = syncOpts);
5112
const syncKey = syncOpts.key;
5114
const sync = _sync(syncKey);
5116
function pubSync(type, src, x, y, w, h, i) {
5117
if (syncOpts.filters.pub(type, src, x, y, w, h, i))
5118
sync.pub(type, src, x, y, w, h, i);
5123
function pub(type, src, x, y, w, h, i) {
5124
if (syncOpts.filters.sub(type, src, x, y, w, h, i))
5125
events[type](null, src, x, y, w, h, i);
5130
function destroy() {
5132
cursorPlots.delete(self);
5133
mouseListeners.clear();
5134
off(dppxchange, win, syncPxRatio);
5139
self.destroy = destroy;
5142
fire("init", opts, data);
5144
setData(data || opts.data, false);
5146
if (pendScales[xScaleKey])
5147
setScale(xScaleKey, pendScales[xScaleKey]);
5151
_setSize(opts.width, opts.height);
5153
updateCursor(null, true, false);
5155
setSelect(select, false);
5158
series.forEach(initSeries);
5160
axes.forEach(initAxis);
5163
if (then instanceof HTMLElement) {
5164
then.appendChild(root);
5176
uPlot.assign = assign;
5177
uPlot.fmtNum = fmtNum;
5178
uPlot.rangeNum = rangeNum;
5179
uPlot.rangeLog = rangeLog;
5180
uPlot.rangeAsinh = rangeAsinh;
5181
uPlot.orient = orient;
5188
uPlot.fmtDate = fmtDate;
5189
uPlot.tzDate = tzDate;
5197
uPlot.addGap = addGap;
5198
uPlot.clipGaps = clipGaps;
5200
let paths = uPlot.paths = {
5204
(paths.linear = linear);
5205
(paths.stepped = stepped);
5206
(paths.bars = bars);
5207
(paths.spline = monotoneCubic);
5210
export { uPlot as default };