|
| 1 | +import { postJson } from "../core/api.js"; |
| 2 | +import { createDisposer } from "../utils/dispose.js"; |
| 3 | +export function createHistogramOverlay(containerElement, options) { |
| 4 | + const { title = "Value Distribution", sources = [], apiBase = "", buildRequestBody, defaultBins = 64, } = options; |
| 5 | + if (!buildRequestBody) { |
| 6 | + throw new Error("buildRequestBody is required for histogram overlay"); |
| 7 | + } |
| 8 | + const button = document.createElement("button"); |
| 9 | + button.textContent = "Value Histogram"; |
| 10 | + const overlay = document.createElement("div"); |
| 11 | + Object.assign(overlay.style, { |
| 12 | + position: "fixed", |
| 13 | + top: "50px", |
| 14 | + right: "50px", |
| 15 | + width: "480px", |
| 16 | + padding: "12px", |
| 17 | + background: "rgba(0, 0, 0, 0.85)", |
| 18 | + color: "#fff", |
| 19 | + borderRadius: "8px", |
| 20 | + border: "1px solid #555", |
| 21 | + zIndex: 3000, |
| 22 | + display: "none", |
| 23 | + }); |
| 24 | + const header = document.createElement("div"); |
| 25 | + header.textContent = title; |
| 26 | + header.style.fontSize = "16px"; |
| 27 | + header.style.marginBottom = "8px"; |
| 28 | + header.style.fontWeight = "bold"; |
| 29 | + overlay.appendChild(header); |
| 30 | + const controls = document.createElement("div"); |
| 31 | + controls.style.display = "flex"; |
| 32 | + controls.style.gap = "12px"; |
| 33 | + controls.style.flexWrap = "wrap"; |
| 34 | + controls.style.alignItems = "flex-end"; |
| 35 | + const sourceGroup = document.createElement("div"); |
| 36 | + sourceGroup.style.display = "flex"; |
| 37 | + sourceGroup.style.flexDirection = "column"; |
| 38 | + sourceGroup.style.gap = "4px"; |
| 39 | + const sourceLabel = document.createElement("label"); |
| 40 | + sourceLabel.textContent = "Activation"; |
| 41 | + sourceLabel.style.fontSize = "12px"; |
| 42 | + sourceLabel.style.opacity = "0.8"; |
| 43 | + sourceGroup.appendChild(sourceLabel); |
| 44 | + const select = document.createElement("select"); |
| 45 | + select.id = "histogram-source"; |
| 46 | + sources.forEach((src) => { |
| 47 | + const opt = document.createElement("option"); |
| 48 | + opt.value = src.value; |
| 49 | + opt.textContent = src.label; |
| 50 | + select.appendChild(opt); |
| 51 | + }); |
| 52 | + if (!select.value && select.options.length) { |
| 53 | + select.selectedIndex = 0; |
| 54 | + } |
| 55 | + sourceLabel.htmlFor = select.id; |
| 56 | + sourceGroup.appendChild(select); |
| 57 | + controls.appendChild(sourceGroup); |
| 58 | + const binsGroup = document.createElement("div"); |
| 59 | + binsGroup.style.display = "flex"; |
| 60 | + binsGroup.style.flexDirection = "column"; |
| 61 | + binsGroup.style.gap = "4px"; |
| 62 | + const binsLabel = document.createElement("label"); |
| 63 | + binsLabel.textContent = "Bins"; |
| 64 | + binsLabel.style.fontSize = "12px"; |
| 65 | + binsLabel.style.opacity = "0.8"; |
| 66 | + binsGroup.appendChild(binsLabel); |
| 67 | + const binInput = document.createElement("input"); |
| 68 | + binInput.type = "number"; |
| 69 | + binInput.value = String(defaultBins); |
| 70 | + binInput.min = "4"; |
| 71 | + binInput.max = "512"; |
| 72 | + binInput.step = "2"; |
| 73 | + binInput.style.width = "80px"; |
| 74 | + binInput.title = "Number of bins"; |
| 75 | + binInput.id = "histogram-bins"; |
| 76 | + binsLabel.htmlFor = binInput.id; |
| 77 | + binsGroup.appendChild(binInput); |
| 78 | + controls.appendChild(binsGroup); |
| 79 | + overlay.appendChild(controls); |
| 80 | + const info = document.createElement("div"); |
| 81 | + info.style.margin = "6px 0"; |
| 82 | + info.style.fontSize = "12px"; |
| 83 | + overlay.appendChild(info); |
| 84 | + const canvas = document.createElement("canvas"); |
| 85 | + canvas.width = 440; |
| 86 | + canvas.height = 240; |
| 87 | + canvas.style.background = "#111"; |
| 88 | + canvas.style.border = "1px solid #444"; |
| 89 | + overlay.appendChild(canvas); |
| 90 | + const status = document.createElement("div"); |
| 91 | + status.style.fontSize = "12px"; |
| 92 | + status.style.marginTop = "4px"; |
| 93 | + overlay.appendChild(status); |
| 94 | + function show() { |
| 95 | + overlay.style.display = "block"; |
| 96 | + updateHistogram(); |
| 97 | + } |
| 98 | + function hide() { |
| 99 | + overlay.style.display = "none"; |
| 100 | + } |
| 101 | + const disposer = createDisposer(); |
| 102 | + disposer.listen(button, "click", show); |
| 103 | + disposer.listen(select, "change", () => { |
| 104 | + updateHistogram(); |
| 105 | + }); |
| 106 | + disposer.listen(binInput, "input", () => { |
| 107 | + updateHistogram(); |
| 108 | + }); |
| 109 | + async function updateHistogram() { |
| 110 | + status.textContent = "Loading histogram..."; |
| 111 | + info.textContent = ""; |
| 112 | + const bins = parseInt(binInput.value, 10) || defaultBins; |
| 113 | + if (!select.value && select.options.length) { |
| 114 | + select.selectedIndex = 0; |
| 115 | + } |
| 116 | + try { |
| 117 | + const body = buildRequestBody(select.value, bins); |
| 118 | + body.bins = bins; |
| 119 | + body.max_samples = body.max_samples || 200000; |
| 120 | + const data = await postJson("/api/histogram", body, { base: apiBase }); |
| 121 | + drawHistogram(canvas, data.counts, data.edges); |
| 122 | + info.textContent = `Min: ${data.min.toFixed(6)} | Max: ${data.max.toFixed(6)} | Total values: ${data.n} | Sampled: ${data.sampled}`; |
| 123 | + status.textContent = ""; |
| 124 | + } |
| 125 | + catch (err) { |
| 126 | + const message = err instanceof Error ? err.message : String(err); |
| 127 | + status.textContent = `Histogram error: ${message}`; |
| 128 | + const ctx = canvas.getContext("2d"); |
| 129 | + if (ctx) |
| 130 | + ctx.clearRect(0, 0, canvas.width, canvas.height); |
| 131 | + } |
| 132 | + } |
| 133 | + function drawHistogram(canvasEl, counts, edges) { |
| 134 | + const ctx = canvasEl.getContext("2d"); |
| 135 | + if (!ctx) |
| 136 | + return; |
| 137 | + ctx.clearRect(0, 0, canvasEl.width, canvasEl.height); |
| 138 | + if (!counts || !counts.length || !edges || edges.length < 2) { |
| 139 | + ctx.fillStyle = "#888"; |
| 140 | + ctx.fillText("No data", 20, 30); |
| 141 | + return; |
| 142 | + } |
| 143 | + const width = canvasEl.width - 40; |
| 144 | + const height = canvasEl.height - 40; |
| 145 | + const originX = 30; |
| 146 | + const originY = canvasEl.height - 20; |
| 147 | + const maxCount = Math.max(...counts); |
| 148 | + const barWidth = width / counts.length; |
| 149 | + ctx.strokeStyle = "#555"; |
| 150 | + ctx.beginPath(); |
| 151 | + ctx.moveTo(originX, originY); |
| 152 | + ctx.lineTo(originX + width, originY); |
| 153 | + ctx.stroke(); |
| 154 | + counts.forEach((count, idx) => { |
| 155 | + const barHeight = maxCount ? (count / maxCount) * height : 0; |
| 156 | + const x = originX + idx * barWidth; |
| 157 | + const y = originY - barHeight; |
| 158 | + ctx.fillStyle = "#ffa500"; |
| 159 | + ctx.fillRect(x, y, Math.max(1, barWidth - 2), barHeight); |
| 160 | + }); |
| 161 | + ctx.fillStyle = "#ccc"; |
| 162 | + ctx.font = "10px monospace"; |
| 163 | + const firstEdge = edges[0]; |
| 164 | + const lastEdge = edges[edges.length - 1]; |
| 165 | + if (firstEdge === undefined || lastEdge === undefined) |
| 166 | + return; |
| 167 | + ctx.fillText(`${firstEdge.toFixed(4)}`, originX, originY + 12); |
| 168 | + ctx.fillText(`${lastEdge.toFixed(4)}`, originX + width - 40, originY + 12); |
| 169 | + } |
| 170 | + (containerElement || document.body).appendChild(overlay); |
| 171 | + return { |
| 172 | + button, |
| 173 | + overlay, |
| 174 | + show, |
| 175 | + hide, |
| 176 | + destroy() { |
| 177 | + disposer.dispose(); |
| 178 | + overlay.remove(); |
| 179 | + }, |
| 180 | + }; |
| 181 | +} |
0 commit comments