We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to make the tooltip stick to points
<!doctype html>
<link rel="stylesheet" href="../dist/uPlot.min.css">
function init(u, opts) { legendEl = u.root.querySelector(".u-legend"); legendEl.classList.remove("u-inline"); className && legendEl.classList.add(className); uPlot.assign(legendEl.style, { textAlign: "left", pointerEvents: "none", display: "none", position: "absolute", left: 0, top: 0, zIndex: 100, boxShadow: "2px 2px 10px rgba(0,0,0,0.5)", ...style }); const idents = legendEl.querySelectorAll(".u-marker"); for (let i = 0; i < idents.length; i++) idents[i].style.height = 0; const overEl = u.over; overEl.style.overflow = "visible"; overEl.appendChild(legendEl); overEl.addEventListener("mouseenter", () => { legendEl.style.display = null; }); overEl.addEventListener("mouseleave", () => { legendEl.style.display = "none"; }); } function update(u) { const { left, top } = u.cursor; legendEl.style.transform = "translate(" + left + "px, " + top + "px)"; } return { hooks: { init: init, setCursor: update, } }; } function datumsPlugin() { let x1; let x2; let y1; let y2; const drawDatum = (u, x, y, color) => { let cx = u.valToPos(x, "x", true); let cy = u.valToPos(y, "y", true); let rad = 10; u.ctx.strokeStyle = color; u.ctx.beginPath(); u.ctx.arc(cx, cy, rad, 0, 2 * Math.PI); u.ctx.moveTo(cx - rad - 5, cy); u.ctx.lineTo(cx + rad + 5, cy); u.ctx.moveTo(cx, cy - rad - 5); u.ctx.lineTo(cx, cy + rad + 5); u.ctx.stroke(); }; const clearDatums = (u) => { x1 = x2 = y1 = y2 = null; u.redraw(); }; const drawDelta = (u) => { let dxLabel = (x2 - x1).toPrecision(3); let dyLabel = (y2 - y1).toPrecision(3); let xPos = u.valToPos((x1 + x2) / 2, "x", true); let yPos = u.valToPos((y1 + y2) / 2, "y", true); u.ctx.textAlign = "center"; u.ctx.textBaseline = "middle"; u.ctx.fillStyle = "black"; u.ctx.fillText(`dx: ${dxLabel}, dy: ${dyLabel}`, xPos, yPos); }; return { hooks: { init: (u) => { u.over.tabIndex = -1; // required for key handlers u.over.style.outlineWidth = "0"; // prevents yellow input box outline when in focus u.over.addEventListener("wheel", (e) => { clearDatums(u); }); u.over.addEventListener("dblclick", (e) => { clearDatums(u); }); u.over.addEventListener( "keydown", (e) => { if (e.key == "Escape") { clearDatums(u); } else { const { left, top } = u.cursor; if (left >= 0 && top >= 0) { if (e.key == "1") { x1 = u.posToVal(left, "x"); y1 = u.posToVal(top, "y"); u.redraw(); } else if (e.key == "2") { x2 = u.posToVal(left, "x"); y2 = u.posToVal(top, "y"); u.redraw(); } } } }, true ); }, draw: (u) => { if (x1 != null || x2 != null) { u.ctx.save(); u.ctx.lineWidth = 2; if (x1 != null) { drawDatum(u, x1, y1, "blue"); } if (x2 != null) { drawDatum(u, x2, y2, "orange"); drawDelta(u); } u.ctx.restore(); } }, }, }; } let data = [ [0, 1, 2, 3, 4], [0, 100, 30, 25, 7], [0, 20, 20, 15, 8], [0, 20, 40, 15, 8], ]; const opts = { width: 800, height: 400, plugins: [datumsPlugin(), legendAsTooltipPlugin()], scales: { x: { time: false, }, }, series: [ {}, { label: "Data 1", stroke: "green", fill: "rgba(255,0,0,0.1)", }, { label: "Data 2", stroke: "red", fill: "rgba(255,0,0,0.1)", }, ], toolbar }; let u = new uPlot(opts, data, document.body); </script>
The text was updated successfully, but these errors were encountered:
No branches or pull requests
How to make the tooltip stick to points
<!doctype html>
<title>Measure / Datums</title>The text was updated successfully, but these errors were encountered: