Skip to content

Commit

Permalink
store and use direct references to legend <tr>s. close #496.
Browse files Browse the repository at this point in the history
  • Loading branch information
leeoniya committed Apr 15, 2021
1 parent c315b3b commit 095e103
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 74 deletions.
39 changes: 21 additions & 18 deletions dist/uPlot.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ function genIncrs(base, minExp, maxExp, mults) {

const EMPTY_OBJ = {};

const nullNullTuple = [null, null];

const isArr = Array.isArray;

function isStr(v) {
Expand Down Expand Up @@ -2060,28 +2062,26 @@ function setDefault(o, i, xo, yo) {
return assign({}, (i == 0 ? xo : yo), o);
}

const nullMinMax = [null, null];

function snapNumX(self, dataMin, dataMax) {
return dataMin == null ? nullMinMax : [dataMin, dataMax];
return dataMin == null ? nullNullTuple : [dataMin, dataMax];
}

const snapTimeX = snapNumX;

// this ensures that non-temporal/numeric y-axes get multiple-snapped padding added above/below
// TODO: also account for incrs when snapping to ensure top of axis gets a tick & value
function snapNumY(self, dataMin, dataMax) {
return dataMin == null ? nullMinMax : rangeNum(dataMin, dataMax, 0.1, true);
return dataMin == null ? nullNullTuple : rangeNum(dataMin, dataMax, 0.1, true);
}

function snapLogY(self, dataMin, dataMax, scale) {
return dataMin == null ? nullMinMax : rangeLog(dataMin, dataMax, self.scales[scale].log, false);
return dataMin == null ? nullNullTuple : rangeLog(dataMin, dataMax, self.scales[scale].log, false);
}

const snapLogX = snapLogY;

function snapAsinhY(self, dataMin, dataMax, scale) {
return dataMin == null ? nullMinMax : rangeAsinh(dataMin, dataMax, self.scales[scale].log, false);
return dataMin == null ? nullNullTuple : rangeAsinh(dataMin, dataMax, self.scales[scale].log, false);
}

const snapAsinhX = snapAsinhY;
Expand Down Expand Up @@ -2221,7 +2221,7 @@ function uPlot(opts, data, then) {
if (scaleKey != xScaleKey && !rangeIsArr && isObj(rn)) {
let cfg = rn;
// this is similar to snapNumY
rn = (self, dataMin, dataMax) => dataMin == null ? nullMinMax : rangeNum(dataMin, dataMax, cfg);
rn = (self, dataMin, dataMax) => dataMin == null ? nullNullTuple : rangeNum(dataMin, dataMax, cfg);
}

sc.range = fnOrSelf(rn || (isTime ? snapTimeX : scaleKey == xScaleKey ?
Expand Down Expand Up @@ -2332,6 +2332,7 @@ function uPlot(opts, data, then) {

let legendEl;
let legendRows = [];
let legendCells = [];
let legendCols;
let multiValLegend = false;
let NULL_LEGEND_VALUES = {};
Expand Down Expand Up @@ -2366,9 +2367,9 @@ function uPlot(opts, data, then) {

function initLegendRow(s, i) {
if (i == 0 && (multiValLegend || !legend.live))
return null;
return nullNullTuple;

let _row = [];
let cells = [];

let row = placeTag("tr", LEGEND_SERIES, legendEl, legendEl.childNodes[i]);

Expand Down Expand Up @@ -2425,10 +2426,10 @@ function uPlot(opts, data, then) {
for (var key in legendCols) {
let v = placeTag("td", LEGEND_VALUE, row);
v.textContent = "--";
_row.push(v);
cells.push(v);
}

return _row;
return [row, cells];
}

const mouseListeners = new Map();
Expand Down Expand Up @@ -2692,7 +2693,9 @@ function uPlot(opts, data, then) {
}

if (showLegend) {
legendRows.splice(i, 0, initLegendRow(s, i));
let rowCells = initLegendRow(s, i);
legendRows.splice(i, 0, rowCells[0]);
legendCells.splice(i, 0, rowCells[1]);
legend.values.push(null); // NULL_LEGEND_VALS not yet avil here :(
}

Expand All @@ -2718,9 +2721,9 @@ function uPlot(opts, data, then) {
if (showLegend) {
legend.values.splice(i, 1);

let tr = legendRows.splice(i, 1)[0][0].parentNode;
let label = tr.firstChild;
offMouse(null, label);
legendCells.splice(i, 1);
let tr = legendRows.splice(i, 1)[0];
offMouse(null, tr.firstChild);
tr.remove();
}

Expand Down Expand Up @@ -3741,7 +3744,7 @@ function uPlot(opts, data, then) {

function toggleDOM(i, onOff) {
let s = series[i];
let label = showLegend ? legendRows[i][0].parentNode : null;
let label = showLegend ? legendRows[i] : null;

if (s.show)
label && remClass(label, OFF);
Expand Down Expand Up @@ -3785,7 +3788,7 @@ function uPlot(opts, data, then) {
cursorPts[i].style.opacity = value;

if (showLegend && legendRows[i])
legendRows[i][0].parentNode.style.opacity = value;
legendRows[i].style.opacity = value;
}

// y-distance
Expand Down Expand Up @@ -3910,7 +3913,7 @@ function uPlot(opts, data, then) {
let j = 0;

for (let k in vals)
legendRows[i][j++].firstChild.nodeValue = vals[k];
legendCells[i][j++].firstChild.nodeValue = vals[k];
}
}
}
Expand Down
39 changes: 21 additions & 18 deletions dist/uPlot.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ function genIncrs(base, minExp, maxExp, mults) {

const EMPTY_OBJ = {};

const nullNullTuple = [null, null];

const isArr = Array.isArray;

function isStr(v) {
Expand Down Expand Up @@ -2058,28 +2060,26 @@ function setDefault(o, i, xo, yo) {
return assign({}, (i == 0 ? xo : yo), o);
}

const nullMinMax = [null, null];

function snapNumX(self, dataMin, dataMax) {
return dataMin == null ? nullMinMax : [dataMin, dataMax];
return dataMin == null ? nullNullTuple : [dataMin, dataMax];
}

const snapTimeX = snapNumX;

// this ensures that non-temporal/numeric y-axes get multiple-snapped padding added above/below
// TODO: also account for incrs when snapping to ensure top of axis gets a tick & value
function snapNumY(self, dataMin, dataMax) {
return dataMin == null ? nullMinMax : rangeNum(dataMin, dataMax, 0.1, true);
return dataMin == null ? nullNullTuple : rangeNum(dataMin, dataMax, 0.1, true);
}

function snapLogY(self, dataMin, dataMax, scale) {
return dataMin == null ? nullMinMax : rangeLog(dataMin, dataMax, self.scales[scale].log, false);
return dataMin == null ? nullNullTuple : rangeLog(dataMin, dataMax, self.scales[scale].log, false);
}

const snapLogX = snapLogY;

function snapAsinhY(self, dataMin, dataMax, scale) {
return dataMin == null ? nullMinMax : rangeAsinh(dataMin, dataMax, self.scales[scale].log, false);
return dataMin == null ? nullNullTuple : rangeAsinh(dataMin, dataMax, self.scales[scale].log, false);
}

const snapAsinhX = snapAsinhY;
Expand Down Expand Up @@ -2219,7 +2219,7 @@ function uPlot(opts, data, then) {
if (scaleKey != xScaleKey && !rangeIsArr && isObj(rn)) {
let cfg = rn;
// this is similar to snapNumY
rn = (self, dataMin, dataMax) => dataMin == null ? nullMinMax : rangeNum(dataMin, dataMax, cfg);
rn = (self, dataMin, dataMax) => dataMin == null ? nullNullTuple : rangeNum(dataMin, dataMax, cfg);
}

sc.range = fnOrSelf(rn || (isTime ? snapTimeX : scaleKey == xScaleKey ?
Expand Down Expand Up @@ -2330,6 +2330,7 @@ function uPlot(opts, data, then) {

let legendEl;
let legendRows = [];
let legendCells = [];
let legendCols;
let multiValLegend = false;
let NULL_LEGEND_VALUES = {};
Expand Down Expand Up @@ -2364,9 +2365,9 @@ function uPlot(opts, data, then) {

function initLegendRow(s, i) {
if (i == 0 && (multiValLegend || !legend.live))
return null;
return nullNullTuple;

let _row = [];
let cells = [];

let row = placeTag("tr", LEGEND_SERIES, legendEl, legendEl.childNodes[i]);

Expand Down Expand Up @@ -2423,10 +2424,10 @@ function uPlot(opts, data, then) {
for (var key in legendCols) {
let v = placeTag("td", LEGEND_VALUE, row);
v.textContent = "--";
_row.push(v);
cells.push(v);
}

return _row;
return [row, cells];
}

const mouseListeners = new Map();
Expand Down Expand Up @@ -2690,7 +2691,9 @@ function uPlot(opts, data, then) {
}

if (showLegend) {
legendRows.splice(i, 0, initLegendRow(s, i));
let rowCells = initLegendRow(s, i);
legendRows.splice(i, 0, rowCells[0]);
legendCells.splice(i, 0, rowCells[1]);
legend.values.push(null); // NULL_LEGEND_VALS not yet avil here :(
}

Expand All @@ -2716,9 +2719,9 @@ function uPlot(opts, data, then) {
if (showLegend) {
legend.values.splice(i, 1);

let tr = legendRows.splice(i, 1)[0][0].parentNode;
let label = tr.firstChild;
offMouse(null, label);
legendCells.splice(i, 1);
let tr = legendRows.splice(i, 1)[0];
offMouse(null, tr.firstChild);
tr.remove();
}

Expand Down Expand Up @@ -3739,7 +3742,7 @@ function uPlot(opts, data, then) {

function toggleDOM(i, onOff) {
let s = series[i];
let label = showLegend ? legendRows[i][0].parentNode : null;
let label = showLegend ? legendRows[i] : null;

if (s.show)
label && remClass(label, OFF);
Expand Down Expand Up @@ -3783,7 +3786,7 @@ function uPlot(opts, data, then) {
cursorPts[i].style.opacity = value;

if (showLegend && legendRows[i])
legendRows[i][0].parentNode.style.opacity = value;
legendRows[i].style.opacity = value;
}

// y-distance
Expand Down Expand Up @@ -3908,7 +3911,7 @@ function uPlot(opts, data, then) {
let j = 0;

for (let k in vals)
legendRows[i][j++].firstChild.nodeValue = vals[k];
legendCells[i][j++].firstChild.nodeValue = vals[k];
}
}
}
Expand Down
Loading

0 comments on commit 095e103

Please sign in to comment.