diff --git a/src/dom.js b/src/dom.js index bc794fa4..6af1fcf2 100644 --- a/src/dom.js +++ b/src/dom.js @@ -49,14 +49,17 @@ export function setStylePx(el, name, value) { el.style[name] = value + "px"; } -export function placeTag(tag, cls, targ, refEl) { +export function placeTag(tag, cls, targ, refEl, frag) { let el = doc.createElement(tag); if (cls != null) addClass(el, cls); - if (targ != null) + if (frag != null) { + frag.appendChild(el); + } else if (targ != null) { targ.insertBefore(el, refEl); + } return el; } @@ -121,4 +124,4 @@ export function off(ev, el, cb, capt) { el.removeEventListener(ev, cb, capt ? evOpts2 : evOpts); } -domEnv && setPxRatio(); \ No newline at end of file +domEnv && setPxRatio(); diff --git a/src/uPlot.css b/src/uPlot.css index 5282773e..c13a3258 100644 --- a/src/uPlot.css +++ b/src/uPlot.css @@ -1,7 +1,5 @@ .uplot, -.uplot *, -.uplot *::before, -.uplot *::after { +.uplot * { box-sizing: border-box; } @@ -79,6 +77,7 @@ .u-inline.u-live th::after { content: ":"; vertical-align: middle; + box-sizing: border-box; } .u-inline:not(.u-live) .u-value { @@ -142,4 +141,4 @@ .u-cursor-y.u-off, .u-cursor-pt.u-off { display: none; -} \ No newline at end of file +} diff --git a/src/uPlot.js b/src/uPlot.js index f20d9545..c340e64f 100644 --- a/src/uPlot.js +++ b/src/uPlot.js @@ -589,6 +589,8 @@ export default function uPlot(opts, data, then) { NULL_LEGEND_VALUES[k] = LEGEND_DISP; } + let legendRowsFrag = null; + if (showLegend) { legendTable = placeTag("table", LEGEND, root); legendBody = placeTag("tbody", null, legendTable); @@ -600,15 +602,20 @@ export default function uPlot(opts, data, then) { legendHead = placeTag("thead", null, legendTable, legendBody); let head = placeTag("tr", null, legendHead); - placeTag("th", null, head); + let headFrag = doc.createDocumentFragment(); + placeTag("th", null, null, null, headFrag); for (var key in legendCols) - placeTag("th", LEGEND_LABEL, head).textContent = key; + placeTag("th", LEGEND_LABEL, null, null, headFrag).textContent = key; + + head.appendChild(headFrag); } else { addClass(legendTable, LEGEND_INLINE); legend.live && addClass(legendTable, LEGEND_LIVE); } + + legendRowsFrag = doc.createDocumentFragment(); } const son = {show: true}; @@ -620,7 +627,8 @@ export default function uPlot(opts, data, then) { let cells = []; - let row = placeTag("tr", LEGEND_SERIES, legendBody, legendBody.childNodes[i]); + const rowFrag = doc.createDocumentFragment(); + let row = placeTag("tr", LEGEND_SERIES, rowFrag, null); addClass(row, s.class); @@ -691,6 +699,8 @@ export default function uPlot(opts, data, then) { cells.push(v); } + legendRowsFrag.appendChild(rowFrag); + return [row, cells]; } @@ -1008,7 +1018,6 @@ export default function uPlot(opts, data, then) { addClass(pt, CURSOR_PT); addClass(pt, s.class); elTrans(pt, -10, -10, plotWidCss, plotHgtCss); - over.insertBefore(pt, cursorPts[si]); return pt; } @@ -3485,6 +3494,27 @@ export default function uPlot(opts, data, then) { axes.forEach(initAxis); + requestAnimationFrame(() => { + if (cursor.show && cursorPts.length > 0) { + let cursorFrag = doc.createDocumentFragment(); + for (let i = 0; i < cursorPts.length; i++) { + if (cursorPts[i] != null) { + cursorFrag.appendChild(cursorPts[i]); + } + } + if (cursorFrag.hasChildNodes()) { + let refEl = over.firstChild; + over.insertBefore(cursorFrag, refEl); + } + } + + requestAnimationFrame(() => { + if (showLegend && legendRowsFrag != null && legendRowsFrag.hasChildNodes()) { + legendBody.appendChild(legendRowsFrag); + } + }); + }); + if (then) { if (then instanceof HTMLElement) { then.appendChild(root);