diff --git a/sidecars/panel/app.js b/sidecars/panel/app.js index 6e3b297..bc337fd 100644 --- a/sidecars/panel/app.js +++ b/sidecars/panel/app.js @@ -14303,21 +14303,15 @@ function alignCubicChainsForMorph(fromSegs, toSegs) { const tSpan = tEnd - tStart; const rawTau = [0, 1]; - const pushChainKnots = (segs, start, span, cap) => { + const pushChainKnots = (segs, start, span) => { if (!(span > 1e-6)) return; - if (segs.length <= cap) { - for (let i = 0; i < segs.length; i++) { - rawTau.push(Math.max(0, Math.min(1, (segs[i].u3 - start) / span))); - } - } else { - const stride = Math.ceil(segs.length / cap); - for (let i = 0; i < segs.length; i += stride) { - rawTau.push(Math.max(0, Math.min(1, (segs[i].u3 - start) / span))); - } + for (let i = 0; i < segs.length; i++) { + rawTau.push(Math.max(0, Math.min(1, (segs[i].u0 - start) / span))); + rawTau.push(Math.max(0, Math.min(1, (segs[i].u3 - start) / span))); } }; - pushChainKnots(toSegs, tStart, tSpan, 96); - pushChainKnots(fromSegs, fStart, fSpan, 48); + pushChainKnots(toSegs, tStart, tSpan); + pushChainKnots(fromSegs, fStart, fSpan); rawTau.sort((a, b) => a - b); const tauKnots = [0]; @@ -14402,6 +14396,20 @@ function createCubicCurveMorphController({ durationMs = CHART_CURVE_MORPH_MS } = hasRenderedOnce = false; } + function getCurrentSegs() { + const src = liveMorphSegs || committedSegs; + return hasRenderedOnce && src.length > 0 ? src.map((s) => ({ ...s })) : []; + } + + function seedSegs(segs) { + cancel(); + if (Array.isArray(segs) && segs.length > 0) { + committedSegs = segs.map((s) => ({ ...s })); + liveMorphSegs = null; + hasRenderedOnce = true; + } + } + function morphTo({ nextSegs, leftX, @@ -14459,6 +14467,17 @@ function createCubicCurveMorphController({ durationMs = CHART_CURVE_MORPH_MS } = const activeLive = alignedFrom.map((s) => ({ ...s })); liveMorphSegs = activeLive; + const initialLineD = buildPathFromNormalizedSegs(activeLive, leftX, spanX); + patchAttr(linePath, 'd', initialLineD); + if (areaPath) { + if (finalAreaD) { + const firstX = (leftX + activeLive[0].u0 * spanX).toFixed(1); + const lastX = (leftX + activeLive[segCount - 1].u3 * spanX).toFixed(1); + patchAttr(areaPath, 'd', `${initialLineD} L ${lastX} ${bottomY} L ${firstX} ${bottomY} Z`); + } else { + patchAttr(areaPath, 'd', ''); + } + } const startTs = performance.now(); const step = (nowTs) => { @@ -14498,7 +14517,7 @@ function createCubicCurveMorphController({ durationMs = CHART_CURVE_MORPH_MS } = morphRafId = window.requestAnimationFrame(step); } - return { morphTo, cancel, reset }; + return { morphTo, cancel, reset, getCurrentSegs, seedSegs }; } /** @@ -14507,6 +14526,7 @@ function createCubicCurveMorphController({ durationMs = CHART_CURVE_MORPH_MS } = */ function createMultiSeriesCurveMorphController({ durationMs = CHART_CURVE_MORPH_MS } = {}) { let morphRafId = 0; + let hasRenderedOnce = false; const stateByKey = new Map(); function cancel() { @@ -14522,18 +14542,47 @@ function createMultiSeriesCurveMorphController({ durationMs = CHART_CURVE_MORPH_ function reset() { cancel(); + hasRenderedOnce = false; stateByKey.clear(); } + function getSeriesSegs(key) { + const rec = stateByKey.get(key); + const src = rec ? rec.liveSegs || rec.committedSegs : null; + return src && src.length > 0 ? src.map((s) => ({ ...s })) : null; + } + + function getFirstSegs() { + for (const rec of stateByKey.values()) { + const src = rec.liveSegs || rec.committedSegs; + if (src && src.length > 0) return src.map((s) => ({ ...s })); + } + return null; + } + + function seedSeries(key, segs) { + if (!key || !Array.isArray(segs) || segs.length === 0) return; + stateByKey.set(key, { + committedSegs: segs.map((s) => ({ ...s })), + liveSegs: null, + endX: NaN, + endY: NaN, + }); + hasRenderedOnce = true; + } + function morphSeries({ items, leftX, spanX, bottomY, skipMorph = false, + allowBaselineEntry = false, }) { cancel(); const canAnim = !skipMorph && canAnimateChartInBrowser(); + const wasRenderedBefore = hasRenderedOnce; + hasRenderedOnce = items.length > 0; const activeSet = new Set(); const jobs = []; @@ -14541,7 +14590,26 @@ function createMultiSeriesCurveMorphController({ durationMs = CHART_CURVE_MORPH_ const it = items[idx]; activeSet.add(it.key); const prevEntry = stateByKey.get(it.key); - const prevSegs = prevEntry ? prevEntry.liveSegs || prevEntry.committedSegs : null; + let prevSegs = prevEntry ? prevEntry.liveSegs || prevEntry.committedSegs : null; + if ( + (!prevSegs || prevSegs.length === 0) && + canAnim && + allowBaselineEntry && + wasRenderedBefore && + it.nextSegs.length > 0 && + Number.isFinite(bottomY) + ) { + prevSegs = it.nextSegs.map((s) => ({ + u0: s.u0, + y0: bottomY, + cu1: s.cu1, + cy1: bottomY, + cu2: s.cu2, + cy2: bottomY, + u3: s.u3, + y3: bottomY, + })); + } if (!canAnim || !prevSegs || prevSegs.length === 0 || it.nextSegs.length === 0) { stateByKey.set(it.key, { @@ -14606,6 +14674,17 @@ function createMultiSeriesCurveMorphController({ durationMs = CHART_CURVE_MORPH_ endY: fromEndY, }; stateByKey.set(it.key, stateRec); + const initLineD = buildPathFromNormalizedSegs(activeLive, leftX, spanX); + patchAttr(it.lineEl, 'd', initLineD); + if (it.areaEl) { + if (it.finalAreaD) { + const firstX = (leftX + activeLive[0].u0 * spanX).toFixed(1); + const lastX = (leftX + activeLive[segCount - 1].u3 * spanX).toFixed(1); + patchAttr(it.areaEl, 'd', `${initLineD} L ${lastX} ${bottomY} L ${firstX} ${bottomY} Z`); + } else { + patchAttr(it.areaEl, 'd', ''); + } + } jobs.push({ it, alignedFrom, @@ -14684,7 +14763,7 @@ function createMultiSeriesCurveMorphController({ durationMs = CHART_CURVE_MORPH_ morphRafId = window.requestAnimationFrame(step); } - return { morphSeries, cancel, reset }; + return { morphSeries, cancel, reset, getSeriesSegs, getFirstSegs, seedSeries }; } function buildGridFitPolylineD(grid) { @@ -14708,12 +14787,41 @@ function buildGridCiBandD(grid) { return `${d} Z`; } +function resampleFitGridToTargetXs(sourceGrid, targetGrid) { + if (!Array.isArray(sourceGrid) || sourceGrid.length === 0) return null; + const sLen = sourceGrid.length; + const sFirst = sourceGrid[0]; + const sLast = sourceGrid[sLen - 1]; + let j = 0; + return targetGrid.map((tg) => { + const x = tg.x; + if (sLen === 1 || x <= sFirst.x) { + return { x, fitY: sFirst.fitY, lowY: sFirst.lowY, highY: sFirst.highY }; + } + if (x >= sLast.x) { + return { x, fitY: sLast.fitY, lowY: sLast.lowY, highY: sLast.highY }; + } + while (j < sLen - 2 && sourceGrid[j + 1].x < x) j += 1; + const a = sourceGrid[j]; + const b = sourceGrid[j + 1]; + const span = b.x - a.x; + const r = span > 1e-6 ? Math.max(0, Math.min(1, (x - a.x) / span)) : 0; + return { + x, + fitY: a.fitY + (b.fitY - a.fitY) * r, + lowY: a.lowY + (b.lowY - a.lowY) * r, + highY: a.highY + (b.highY - a.highY) * r, + }; + }); +} + /** * Smoothly morphs LOESS fit polylines, `95% CI` confidence bands, and optional OLS lines * across metric/model switches in `createModelFitChart`. */ function createFitGridMorphController({ durationMs = CHART_CURVE_MORPH_MS } = {}) { let morphRafId = 0; + let hasRenderedOnce = false; const stateByKey = new Map(); function cancel() { @@ -14729,12 +14837,39 @@ function createFitGridMorphController({ durationMs = CHART_CURVE_MORPH_MS } = {} function reset() { cancel(); + hasRenderedOnce = false; stateByKey.clear(); } - function morphFits({ items, skipMorph = false }) { + function getGrid(key) { + const rec = stateByKey.get(key); + const src = rec ? rec.liveGrid || rec.committedGrid : null; + return src && src.length > 0 ? src.map((g) => ({ ...g })) : null; + } + + function getFirstGrid() { + for (const rec of stateByKey.values()) { + const src = rec.liveGrid || rec.committedGrid; + if (src && src.length > 0) return src.map((g) => ({ ...g })); + } + return null; + } + + function seedGrid(key, gridPts) { + if (!key || !Array.isArray(gridPts) || gridPts.length === 0) return; + stateByKey.set(key, { + committedGrid: gridPts.map((g) => ({ ...g })), + liveGrid: null, + ols: null, + }); + hasRenderedOnce = true; + } + + function morphFits({ items, skipMorph = false, allowBaselineEntry = false, bottomY = NaN }) { cancel(); const canAnim = !skipMorph && canAnimateChartInBrowser(); + const wasRenderedBefore = hasRenderedOnce; + hasRenderedOnce = items.length > 0; const activeSet = new Set(); const jobs = []; @@ -14742,7 +14877,32 @@ function createFitGridMorphController({ durationMs = CHART_CURVE_MORPH_MS } = {} const it = items[idx]; activeSet.add(it.key); const prevEntry = stateByKey.get(it.key); - const prevGrid = prevEntry ? prevEntry.liveGrid || prevEntry.committedGrid : null; + const rawPrevGrid = prevEntry ? prevEntry.liveGrid || prevEntry.committedGrid : null; + let prevGrid = + rawPrevGrid && rawPrevGrid.length > 0 && it.gridPts.length >= 2 + ? rawPrevGrid.length === it.gridPts.length && + Math.abs(rawPrevGrid[0].x - it.gridPts[0].x) < 0.5 && + Math.abs( + rawPrevGrid[rawPrevGrid.length - 1].x - it.gridPts[it.gridPts.length - 1].x + ) < 0.5 + ? rawPrevGrid + : resampleFitGridToTargetXs(rawPrevGrid, it.gridPts) + : null; + if ( + !prevGrid && + canAnim && + allowBaselineEntry && + wasRenderedBefore && + it.gridPts.length >= 2 && + Number.isFinite(bottomY) + ) { + prevGrid = it.gridPts.map((g) => ({ + x: g.x, + fitY: bottomY, + lowY: bottomY, + highY: bottomY, + })); + } if ( !canAnim || @@ -14803,6 +14963,16 @@ function createFitGridMorphController({ durationMs = CHART_CURVE_MORPH_MS } = {} ols: fromOls ? { ...fromOls } : null, }; stateByKey.set(it.key, stateRec); + patchAttr(it.curveEl, 'd', buildGridFitPolylineD(activeLive)); + if (it.bandEl) { + patchAttr(it.bandEl, 'd', it.finalBandD ? buildGridCiBandD(activeLive) : ''); + } + if (it.olsEl && fromOls) { + patchAttr(it.olsEl, 'x1', fromOls.x1.toFixed(1)); + patchAttr(it.olsEl, 'y1', fromOls.y1.toFixed(1)); + patchAttr(it.olsEl, 'x2', fromOls.x2.toFixed(1)); + patchAttr(it.olsEl, 'y2', fromOls.y2.toFixed(1)); + } jobs.push({ it, fromGrid: prevGrid, activeLive, fromOls, stateRec }); } @@ -14869,7 +15039,7 @@ function createFitGridMorphController({ durationMs = CHART_CURVE_MORPH_MS } = {} morphRafId = window.requestAnimationFrame(step); } - return { morphFits, cancel, reset }; + return { morphFits, cancel, reset, getGrid, getFirstGrid, seedGrid }; } /** @@ -16884,7 +17054,7 @@ return { HEATMAP_DAYS_THRESHOLD, createTrendAreaChart }; })(); // --- src/components/charts/stepChartMath.js --- -const { MODEL_COLOR_VARS, getStepModelGroupKey, toChronologicalSteps, clampValue, computeLinearFit, computePublicationFit, niceStepNumber, computeNiceYScale, computeNiceXStepTicks, formatYAxisTick, buildPolylinePath, buildSplinePath, computeStepTickIndices, formatStepAxisTag, formatChartMetricValue, formatChartMetricCompact } = (() => { +const { MODEL_COLOR_VARS, getStepModelGroupKey, toChronologicalSteps, clampValue, computeLinearFit, computePublicationFit, niceStepNumber, computeNiceYScale, computeNiceXStepTicks, formatYAxisTick, buildPolylinePath, buildSplinePath, computeStepTickIndices, formatStepAxisTag, formatChartMetricValue, formatChartMetricCompact, downsampleStepPointsForPlot } = (() => { /** * @fileoverview Pure regression math (OLS + IRLS-LOESS), nice axis tick generators, * SVG spline/polyline path builders, and step metric formatters extracted from SessionStepCharts.js. @@ -17540,7 +17710,54 @@ function formatChartMetricCompact(metric, val) { return String(val); } } -return { MODEL_COLOR_VARS, getStepModelGroupKey, toChronologicalSteps, clampValue, computeLinearFit, computePublicationFit, niceStepNumber, computeNiceYScale, computeNiceXStepTicks, formatYAxisTick, buildPolylinePath, buildSplinePath, computeStepTickIndices, formatStepAxisTag, formatChartMetricValue, formatChartMetricCompact }; + +/** + * Downsamples dense chronological step series (`> maxPoints`, e.g. 1,500–5,000 steps) to an + * adaptive visual vertex budget (`~2.1px` per segment) so cubic splines never overplot into an + * opaque solid wall while strictly preserving the start step, end step, and exact global peak step. + * + * @template {{ value?: number }} T + * @param {T[]} points + * @param {number} [maxPoints=208] + * @returns {T[]} + */ +function downsampleStepPointsForPlot(points, maxPoints = 208) { + if (!Array.isArray(points) || points.length <= maxPoints || maxPoints < 8) { + return points; + } + const n = points.length; + let globalPeakIdx = -1; + let globalPeakVal = -Infinity; + for (let i = 0; i < n; i += 1) { + const v = Number(points[i]?.value); + if (Number.isFinite(v) && v >= globalPeakVal) { + globalPeakVal = v; + globalPeakIdx = i; + } + } + + const out = [points[0]]; + const bucketCount = maxPoints - 2; + const interiorLen = n - 2; + for (let b = 0; b < bucketCount; b += 1) { + const bStart = 1 + Math.floor((b * interiorLen) / bucketCount); + const bEnd = Math.max(bStart + 1, 1 + Math.floor(((b + 1) * interiorLen) / bucketCount)); + const pickIdx = + globalPeakIdx >= bStart && globalPeakIdx < bEnd + ? globalPeakIdx + : bStart + Math.floor((bEnd - bStart) / 2); + const picked = points[pickIdx]; + if (picked && picked !== out[out.length - 1]) { + out.push(picked); + } + } + const lastPt = points[n - 1]; + if (lastPt && lastPt !== out[out.length - 1]) { + out.push(lastPt); + } + return out; +} +return { MODEL_COLOR_VARS, getStepModelGroupKey, toChronologicalSteps, clampValue, computeLinearFit, computePublicationFit, niceStepNumber, computeNiceYScale, computeNiceXStepTicks, formatYAxisTick, buildPolylinePath, buildSplinePath, computeStepTickIndices, formatStepAxisTag, formatChartMetricValue, formatChartMetricCompact, downsampleStepPointsForPlot }; })(); // --- src/components/charts/SessionStepCharts.js --- @@ -17713,6 +17930,8 @@ function createSessionLineChart() { let lastSummaryBadge = ''; let lastUpdateParams = null; let cachedPlotSvgRect = null; + let lastLineMode = ''; + let lastSingleLineKey = ''; const multiSeriesNodesByKey = new Map(); const singleCurveMorphCtrl = createCubicCurveMorphController(); @@ -17966,8 +18185,13 @@ function createSessionLineChart() { patchText(emptyHintEl, t('chart.step.empty')); if (!matchedSteps.length) { currentPoints = []; + patchClasses(rootEl, { 'is-dense': false }); lastSig = 'empty'; lastPreSig = ''; + lastLineMode = ''; + lastSingleLineKey = ''; + singleCurveMorphCtrl.reset(); + multiCurveMorphCtrl.reset(); lastSummaryBadge = t('chart.step.zeroSteps'); invalidatePlotSvgRect(); patchAttr(areaPath, 'd', ''); @@ -18191,6 +18415,7 @@ function createSessionLineChart() { currentMaxVal = maxVal; const { leftX, rightX, spanX } = resolvePlotBounds(viewW); + const maxPlotPoints = Math.max(128, Math.min(256, Math.floor(spanX / 2.1))); const denom = maxVal > 0 ? maxVal : 1; const isSingle = rawItems.length === 1; const points = rawItems.map((item, i) => { @@ -18201,9 +18426,28 @@ function createSessionLineChart() { const y = item.valid ? BOTTOM_Y - ratio * SPAN_Y : BOTTOM_Y; return { ...item, x, y }; }); - currentPoints = points; const validPoints = points.filter((p) => p.valid); + let hoverPoints = points; + if (isMultiCompare) { + if (points.length > maxPlotPoints * targetKeys.length) { + const merged = []; + for (const gk of targetKeys) { + const rawSeries = points.filter((p) => p.modelGroupKey === gk && p.valid); + merged.push(...downsampleStepPointsForPlot(rawSeries, maxPlotPoints)); + } + merged.sort((a, b) => a.idx - b.idx); + if (merged.length > 0) hoverPoints = merged; + } + } else if (validPoints.length > maxPlotPoints) { + hoverPoints = downsampleStepPointsForPlot(validPoints, maxPlotPoints); + } + currentPoints = hoverPoints; + patchClasses(rootEl, { + 'is-dense': isMultiCompare + ? hoverPoints.length > 120 * targetKeys.length + : hoverPoints.length > 120, + }); patchAttr(emptyHintEl, 'hidden', validPoints.length > 0); const colorSig = Array.from(modelColorMap.entries()) @@ -18224,7 +18468,21 @@ function createSessionLineChart() { while (anomalyDotsGroup.firstChild) anomalyDotsGroup.removeChild(anomalyDotsGroup.firstChild); if (isMultiCompare) { + if (lastLineMode === 'single') { + const prevSingleSegs = singleCurveMorphCtrl.getCurrentSegs(); + if (prevSingleSegs.length > 0) { + if (lastSingleLineKey && lastSingleLineKey !== '__all__') { + multiCurveMorphCtrl.seedSeries(lastSingleLineKey, prevSingleSegs); + } else { + for (const gk of targetKeys) { + multiCurveMorphCtrl.seedSeries(gk, prevSingleSegs); + } + } + } + } singleCurveMorphCtrl.reset(); + lastLineMode = 'multi'; + lastSingleLineKey = ''; patchAttr(linePath, 'd', ''); patchAttr(areaPath, 'd', ''); for (let ci = multiLineGroup.children.length - 1; ci >= 0; ci--) { @@ -18239,7 +18497,8 @@ function createSessionLineChart() { for (const gk of targetKeys) { const seriesColor = modelColorMap.get(gk) || accentColor; - const seriesPts = points.filter((p) => p.modelGroupKey === gk && p.valid); + const rawSeriesPts = points.filter((p) => p.modelGroupKey === gk && p.valid); + const seriesPts = downsampleStepPointsForPlot(rawSeriesPts, maxPlotPoints); if (seriesPts.length === 0) continue; if (seriesPts.length === 1) { @@ -18277,7 +18536,6 @@ function createSessionLineChart() { nextSegs: pointsToNormalizedCubicSegs(seriesPts, BOTTOM_Y, { leftX, spanX, - tension: 0.42, }), lineEl: nodes.mLine, areaEl: nodes.mArea, @@ -18303,19 +18561,32 @@ function createSessionLineChart() { spanX, bottomY: BOTTOM_Y, skipMorph: _skipMorph, + allowBaselineEntry: true, }); } else { + const nextSingleKey = isPooledAll ? '__all__' : targetKeys[0] || '__all__'; + if (lastLineMode === 'multi') { + const seedSegs = + multiCurveMorphCtrl.getSeriesSegs(nextSingleKey) || + multiCurveMorphCtrl.getFirstSegs(); + if (seedSegs && seedSegs.length > 0) { + singleCurveMorphCtrl.seedSegs(seedSegs); + } + } multiCurveMorphCtrl.reset(); + lastLineMode = 'single'; + lastSingleLineKey = nextSingleKey; for (const nodes of multiSeriesNodesByKey.values()) { nodes.mArea.remove(); nodes.mLine.remove(); } - if (validPoints.length === 0) { + const plotValidPoints = downsampleStepPointsForPlot(validPoints, maxPlotPoints); + if (plotValidPoints.length === 0) { singleCurveMorphCtrl.reset(); patchAttr(linePath, 'd', ''); patchAttr(areaPath, 'd', ''); - } else if (validPoints.length === 1) { - const yVal = validPoints[0].y; + } else if (plotValidPoints.length === 1) { + const yVal = plotValidPoints[0].y; const yStr = yVal.toFixed(1); const lineD = `M ${leftX.toFixed(1)} ${yStr} L ${rightX.toFixed(1)} ${yStr}`; const areaD = `${lineD} L ${rightX.toFixed(1)} ${BOTTOM_Y} L ${leftX.toFixed(1)} ${BOTTOM_Y} Z`; @@ -18335,12 +18606,12 @@ function createSessionLineChart() { skipMorph: _skipMorph, }); } else { - const lineD = buildSplinePath(validPoints); - const firstX = validPoints[0].x.toFixed(1); - const lastX = validPoints[validPoints.length - 1].x.toFixed(1); + const lineD = buildSplinePath(plotValidPoints); + const firstX = plotValidPoints[0].x.toFixed(1); + const lastX = plotValidPoints[plotValidPoints.length - 1].x.toFixed(1); const areaD = `${lineD} L ${lastX} ${BOTTOM_Y} L ${firstX} ${BOTTOM_Y} Z`; singleCurveMorphCtrl.morphTo({ - nextSegs: pointsToNormalizedCubicSegs(validPoints, BOTTOM_Y, { leftX, spanX }), + nextSegs: pointsToNormalizedCubicSegs(plotValidPoints, BOTTOM_Y, { leftX, spanX }), leftX, spanX, bottomY: BOTTOM_Y, @@ -18377,12 +18648,12 @@ function createSessionLineChart() { anomalyDotsGroup.appendChild(aTick); } - const tickIndices = computeStepTickIndices(points.length); + const tickIndices = computeStepTickIndices(currentPoints.length); while (xAxisEl.firstChild) xAxisEl.removeChild(xAxisEl.firstChild); tickElsByIdx = new Map(); for (let i = 0; i < tickIndices.length; i++) { const tIdx = tickIndices[i]; - const pt = points[tIdx]; + const pt = currentPoints[tIdx]; if (!pt) continue; const isFirst = !isSingle && i === 0; const isLast = !isSingle && i === tickIndices.length - 1; @@ -18467,7 +18738,7 @@ function createSessionLineChart() { patchAttr(peakPill, 'hidden', true); } - if (hoveredIdx >= points.length) { + if (hoveredIdx >= currentPoints.length) { clearHover(); } else if (hoveredIdx >= 0) { showHoverAt(hoveredIdx, lastPointer, { forceRefresh: true }); @@ -18657,6 +18928,8 @@ function createModelFitChart() { const multiFitNodesByKey = new Map(); const fitMorphCtrl = createFitGridMorphController(); + let lastFitMode = ''; + let lastSingleFitKey = ''; const fitTooltipCtrl = createFloatingChartTooltipController({ containerEl: rootEl, tooltipEl, @@ -19091,6 +19364,8 @@ function createModelFitChart() { if (matchedItems.length === 0) { fitCache.clear(); fitMorphCtrl.reset(); + lastFitMode = ''; + lastSingleFitKey = ''; currentSamples = []; lastSig = 'empty'; patchAttr(ciBandPath, 'd', ''); @@ -19367,7 +19642,26 @@ function createModelFitChart() { nodes.curveEl.remove(); } } - fitMorphCtrl.morphFits({ items: fitMorphItems, skipMorph: _skipMorph }); + if (lastFitMode === 'single') { + const prevSingleGrid = fitMorphCtrl.getGrid('single'); + if (prevSingleGrid && prevSingleGrid.length > 0) { + if (lastSingleFitKey && lastSingleFitKey !== '__all__') { + fitMorphCtrl.seedGrid(`multi:${lastSingleFitKey}`, prevSingleGrid); + } else { + for (const s of seriesList) { + fitMorphCtrl.seedGrid(`multi:${s.key}`, prevSingleGrid); + } + } + } + } + lastFitMode = 'multi'; + lastSingleFitKey = ''; + fitMorphCtrl.morphFits({ + items: fitMorphItems, + skipMorph: _skipMorph, + allowBaselineEntry: true, + bottomY: BOTTOM_Y, + }); anomalyOverlayDots.sort((a, b) => a.anomalySeverity === 'danger' ? 1 : b.anomalySeverity === 'danger' ? -1 : 0 ); @@ -19431,6 +19725,16 @@ function createModelFitChart() { } // 分支 B:单模型或整体合并单曲线模式 + const nextSingleFitKey = isPooledAll ? '__all__' : targetKeys[0] || '__all__'; + if (lastFitMode === 'multi') { + const seedGrid = + fitMorphCtrl.getGrid(`multi:${nextSingleFitKey}`) || fitMorphCtrl.getFirstGrid(); + if (seedGrid && seedGrid.length > 0) { + fitMorphCtrl.seedGrid('single', seedGrid); + } + } + lastFitMode = 'single'; + lastSingleFitKey = nextSingleFitKey; for (const nodes of multiFitNodesByKey.values()) { nodes.bandEl.remove(); nodes.curveEl.remove(); diff --git a/sidecars/panel/index.html b/sidecars/panel/index.html index 817c021..abb985d 100644 --- a/sidecars/panel/index.html +++ b/sidecars/panel/index.html @@ -4,11 +4,11 @@ 用量监控 - +
- + diff --git a/sidecars/panel/src/components/charts/SessionStepCharts.js b/sidecars/panel/src/components/charts/SessionStepCharts.js index 2267045..c4fccac 100644 --- a/sidecars/panel/src/components/charts/SessionStepCharts.js +++ b/sidecars/panel/src/components/charts/SessionStepCharts.js @@ -34,6 +34,7 @@ import { computeNiceYScale, computePublicationFit, computeStepTickIndices, + downsampleStepPointsForPlot, formatChartMetricCompact, formatChartMetricValue, formatStepAxisTag, @@ -213,6 +214,8 @@ export function createSessionLineChart() { let lastSummaryBadge = ''; let lastUpdateParams = null; let cachedPlotSvgRect = null; + let lastLineMode = ''; + let lastSingleLineKey = ''; const multiSeriesNodesByKey = new Map(); const singleCurveMorphCtrl = createCubicCurveMorphController(); @@ -466,8 +469,13 @@ export function createSessionLineChart() { patchText(emptyHintEl, t('chart.step.empty')); if (!matchedSteps.length) { currentPoints = []; + patchClasses(rootEl, { 'is-dense': false }); lastSig = 'empty'; lastPreSig = ''; + lastLineMode = ''; + lastSingleLineKey = ''; + singleCurveMorphCtrl.reset(); + multiCurveMorphCtrl.reset(); lastSummaryBadge = t('chart.step.zeroSteps'); invalidatePlotSvgRect(); patchAttr(areaPath, 'd', ''); @@ -691,6 +699,7 @@ export function createSessionLineChart() { currentMaxVal = maxVal; const { leftX, rightX, spanX } = resolvePlotBounds(viewW); + const maxPlotPoints = Math.max(128, Math.min(256, Math.floor(spanX / 2.1))); const denom = maxVal > 0 ? maxVal : 1; const isSingle = rawItems.length === 1; const points = rawItems.map((item, i) => { @@ -701,9 +710,28 @@ export function createSessionLineChart() { const y = item.valid ? BOTTOM_Y - ratio * SPAN_Y : BOTTOM_Y; return { ...item, x, y }; }); - currentPoints = points; const validPoints = points.filter((p) => p.valid); + let hoverPoints = points; + if (isMultiCompare) { + if (points.length > maxPlotPoints * targetKeys.length) { + const merged = []; + for (const gk of targetKeys) { + const rawSeries = points.filter((p) => p.modelGroupKey === gk && p.valid); + merged.push(...downsampleStepPointsForPlot(rawSeries, maxPlotPoints)); + } + merged.sort((a, b) => a.idx - b.idx); + if (merged.length > 0) hoverPoints = merged; + } + } else if (validPoints.length > maxPlotPoints) { + hoverPoints = downsampleStepPointsForPlot(validPoints, maxPlotPoints); + } + currentPoints = hoverPoints; + patchClasses(rootEl, { + 'is-dense': isMultiCompare + ? hoverPoints.length > 120 * targetKeys.length + : hoverPoints.length > 120, + }); patchAttr(emptyHintEl, 'hidden', validPoints.length > 0); const colorSig = Array.from(modelColorMap.entries()) @@ -724,7 +752,21 @@ export function createSessionLineChart() { while (anomalyDotsGroup.firstChild) anomalyDotsGroup.removeChild(anomalyDotsGroup.firstChild); if (isMultiCompare) { + if (lastLineMode === 'single') { + const prevSingleSegs = singleCurveMorphCtrl.getCurrentSegs(); + if (prevSingleSegs.length > 0) { + if (lastSingleLineKey && lastSingleLineKey !== '__all__') { + multiCurveMorphCtrl.seedSeries(lastSingleLineKey, prevSingleSegs); + } else { + for (const gk of targetKeys) { + multiCurveMorphCtrl.seedSeries(gk, prevSingleSegs); + } + } + } + } singleCurveMorphCtrl.reset(); + lastLineMode = 'multi'; + lastSingleLineKey = ''; patchAttr(linePath, 'd', ''); patchAttr(areaPath, 'd', ''); for (let ci = multiLineGroup.children.length - 1; ci >= 0; ci--) { @@ -739,7 +781,8 @@ export function createSessionLineChart() { for (const gk of targetKeys) { const seriesColor = modelColorMap.get(gk) || accentColor; - const seriesPts = points.filter((p) => p.modelGroupKey === gk && p.valid); + const rawSeriesPts = points.filter((p) => p.modelGroupKey === gk && p.valid); + const seriesPts = downsampleStepPointsForPlot(rawSeriesPts, maxPlotPoints); if (seriesPts.length === 0) continue; if (seriesPts.length === 1) { @@ -777,7 +820,6 @@ export function createSessionLineChart() { nextSegs: pointsToNormalizedCubicSegs(seriesPts, BOTTOM_Y, { leftX, spanX, - tension: 0.42, }), lineEl: nodes.mLine, areaEl: nodes.mArea, @@ -803,19 +845,32 @@ export function createSessionLineChart() { spanX, bottomY: BOTTOM_Y, skipMorph: _skipMorph, + allowBaselineEntry: true, }); } else { + const nextSingleKey = isPooledAll ? '__all__' : targetKeys[0] || '__all__'; + if (lastLineMode === 'multi') { + const seedSegs = + multiCurveMorphCtrl.getSeriesSegs(nextSingleKey) || + multiCurveMorphCtrl.getFirstSegs(); + if (seedSegs && seedSegs.length > 0) { + singleCurveMorphCtrl.seedSegs(seedSegs); + } + } multiCurveMorphCtrl.reset(); + lastLineMode = 'single'; + lastSingleLineKey = nextSingleKey; for (const nodes of multiSeriesNodesByKey.values()) { nodes.mArea.remove(); nodes.mLine.remove(); } - if (validPoints.length === 0) { + const plotValidPoints = downsampleStepPointsForPlot(validPoints, maxPlotPoints); + if (plotValidPoints.length === 0) { singleCurveMorphCtrl.reset(); patchAttr(linePath, 'd', ''); patchAttr(areaPath, 'd', ''); - } else if (validPoints.length === 1) { - const yVal = validPoints[0].y; + } else if (plotValidPoints.length === 1) { + const yVal = plotValidPoints[0].y; const yStr = yVal.toFixed(1); const lineD = `M ${leftX.toFixed(1)} ${yStr} L ${rightX.toFixed(1)} ${yStr}`; const areaD = `${lineD} L ${rightX.toFixed(1)} ${BOTTOM_Y} L ${leftX.toFixed(1)} ${BOTTOM_Y} Z`; @@ -835,12 +890,12 @@ export function createSessionLineChart() { skipMorph: _skipMorph, }); } else { - const lineD = buildSplinePath(validPoints); - const firstX = validPoints[0].x.toFixed(1); - const lastX = validPoints[validPoints.length - 1].x.toFixed(1); + const lineD = buildSplinePath(plotValidPoints); + const firstX = plotValidPoints[0].x.toFixed(1); + const lastX = plotValidPoints[plotValidPoints.length - 1].x.toFixed(1); const areaD = `${lineD} L ${lastX} ${BOTTOM_Y} L ${firstX} ${BOTTOM_Y} Z`; singleCurveMorphCtrl.morphTo({ - nextSegs: pointsToNormalizedCubicSegs(validPoints, BOTTOM_Y, { leftX, spanX }), + nextSegs: pointsToNormalizedCubicSegs(plotValidPoints, BOTTOM_Y, { leftX, spanX }), leftX, spanX, bottomY: BOTTOM_Y, @@ -877,12 +932,12 @@ export function createSessionLineChart() { anomalyDotsGroup.appendChild(aTick); } - const tickIndices = computeStepTickIndices(points.length); + const tickIndices = computeStepTickIndices(currentPoints.length); while (xAxisEl.firstChild) xAxisEl.removeChild(xAxisEl.firstChild); tickElsByIdx = new Map(); for (let i = 0; i < tickIndices.length; i++) { const tIdx = tickIndices[i]; - const pt = points[tIdx]; + const pt = currentPoints[tIdx]; if (!pt) continue; const isFirst = !isSingle && i === 0; const isLast = !isSingle && i === tickIndices.length - 1; @@ -967,7 +1022,7 @@ export function createSessionLineChart() { patchAttr(peakPill, 'hidden', true); } - if (hoveredIdx >= points.length) { + if (hoveredIdx >= currentPoints.length) { clearHover(); } else if (hoveredIdx >= 0) { showHoverAt(hoveredIdx, lastPointer, { forceRefresh: true }); @@ -1157,6 +1212,8 @@ export function createModelFitChart() { const multiFitNodesByKey = new Map(); const fitMorphCtrl = createFitGridMorphController(); + let lastFitMode = ''; + let lastSingleFitKey = ''; const fitTooltipCtrl = createFloatingChartTooltipController({ containerEl: rootEl, tooltipEl, @@ -1591,6 +1648,8 @@ export function createModelFitChart() { if (matchedItems.length === 0) { fitCache.clear(); fitMorphCtrl.reset(); + lastFitMode = ''; + lastSingleFitKey = ''; currentSamples = []; lastSig = 'empty'; patchAttr(ciBandPath, 'd', ''); @@ -1867,7 +1926,26 @@ export function createModelFitChart() { nodes.curveEl.remove(); } } - fitMorphCtrl.morphFits({ items: fitMorphItems, skipMorph: _skipMorph }); + if (lastFitMode === 'single') { + const prevSingleGrid = fitMorphCtrl.getGrid('single'); + if (prevSingleGrid && prevSingleGrid.length > 0) { + if (lastSingleFitKey && lastSingleFitKey !== '__all__') { + fitMorphCtrl.seedGrid(`multi:${lastSingleFitKey}`, prevSingleGrid); + } else { + for (const s of seriesList) { + fitMorphCtrl.seedGrid(`multi:${s.key}`, prevSingleGrid); + } + } + } + } + lastFitMode = 'multi'; + lastSingleFitKey = ''; + fitMorphCtrl.morphFits({ + items: fitMorphItems, + skipMorph: _skipMorph, + allowBaselineEntry: true, + bottomY: BOTTOM_Y, + }); anomalyOverlayDots.sort((a, b) => a.anomalySeverity === 'danger' ? 1 : b.anomalySeverity === 'danger' ? -1 : 0 ); @@ -1931,6 +2009,16 @@ export function createModelFitChart() { } // 分支 B:单模型或整体合并单曲线模式 + const nextSingleFitKey = isPooledAll ? '__all__' : targetKeys[0] || '__all__'; + if (lastFitMode === 'multi') { + const seedGrid = + fitMorphCtrl.getGrid(`multi:${nextSingleFitKey}`) || fitMorphCtrl.getFirstGrid(); + if (seedGrid && seedGrid.length > 0) { + fitMorphCtrl.seedGrid('single', seedGrid); + } + } + lastFitMode = 'single'; + lastSingleFitKey = nextSingleFitKey; for (const nodes of multiFitNodesByKey.values()) { nodes.bandEl.remove(); nodes.curveEl.remove(); diff --git a/sidecars/panel/src/components/charts/chartMotion.js b/sidecars/panel/src/components/charts/chartMotion.js index a7b0d32..f1fffea 100644 --- a/sidecars/panel/src/components/charts/chartMotion.js +++ b/sidecars/panel/src/components/charts/chartMotion.js @@ -282,21 +282,15 @@ function alignCubicChainsForMorph(fromSegs, toSegs) { const tSpan = tEnd - tStart; const rawTau = [0, 1]; - const pushChainKnots = (segs, start, span, cap) => { + const pushChainKnots = (segs, start, span) => { if (!(span > 1e-6)) return; - if (segs.length <= cap) { - for (let i = 0; i < segs.length; i++) { - rawTau.push(Math.max(0, Math.min(1, (segs[i].u3 - start) / span))); - } - } else { - const stride = Math.ceil(segs.length / cap); - for (let i = 0; i < segs.length; i += stride) { - rawTau.push(Math.max(0, Math.min(1, (segs[i].u3 - start) / span))); - } + for (let i = 0; i < segs.length; i++) { + rawTau.push(Math.max(0, Math.min(1, (segs[i].u0 - start) / span))); + rawTau.push(Math.max(0, Math.min(1, (segs[i].u3 - start) / span))); } }; - pushChainKnots(toSegs, tStart, tSpan, 96); - pushChainKnots(fromSegs, fStart, fSpan, 48); + pushChainKnots(toSegs, tStart, tSpan); + pushChainKnots(fromSegs, fStart, fSpan); rawTau.sort((a, b) => a - b); const tauKnots = [0]; @@ -381,6 +375,20 @@ export function createCubicCurveMorphController({ durationMs = CHART_CURVE_MORPH hasRenderedOnce = false; } + function getCurrentSegs() { + const src = liveMorphSegs || committedSegs; + return hasRenderedOnce && src.length > 0 ? src.map((s) => ({ ...s })) : []; + } + + function seedSegs(segs) { + cancel(); + if (Array.isArray(segs) && segs.length > 0) { + committedSegs = segs.map((s) => ({ ...s })); + liveMorphSegs = null; + hasRenderedOnce = true; + } + } + function morphTo({ nextSegs, leftX, @@ -438,6 +446,17 @@ export function createCubicCurveMorphController({ durationMs = CHART_CURVE_MORPH const activeLive = alignedFrom.map((s) => ({ ...s })); liveMorphSegs = activeLive; + const initialLineD = buildPathFromNormalizedSegs(activeLive, leftX, spanX); + patchAttr(linePath, 'd', initialLineD); + if (areaPath) { + if (finalAreaD) { + const firstX = (leftX + activeLive[0].u0 * spanX).toFixed(1); + const lastX = (leftX + activeLive[segCount - 1].u3 * spanX).toFixed(1); + patchAttr(areaPath, 'd', `${initialLineD} L ${lastX} ${bottomY} L ${firstX} ${bottomY} Z`); + } else { + patchAttr(areaPath, 'd', ''); + } + } const startTs = performance.now(); const step = (nowTs) => { @@ -477,7 +496,7 @@ export function createCubicCurveMorphController({ durationMs = CHART_CURVE_MORPH morphRafId = window.requestAnimationFrame(step); } - return { morphTo, cancel, reset }; + return { morphTo, cancel, reset, getCurrentSegs, seedSegs }; } /** @@ -486,6 +505,7 @@ export function createCubicCurveMorphController({ durationMs = CHART_CURVE_MORPH */ export function createMultiSeriesCurveMorphController({ durationMs = CHART_CURVE_MORPH_MS } = {}) { let morphRafId = 0; + let hasRenderedOnce = false; const stateByKey = new Map(); function cancel() { @@ -501,18 +521,47 @@ export function createMultiSeriesCurveMorphController({ durationMs = CHART_CURVE function reset() { cancel(); + hasRenderedOnce = false; stateByKey.clear(); } + function getSeriesSegs(key) { + const rec = stateByKey.get(key); + const src = rec ? rec.liveSegs || rec.committedSegs : null; + return src && src.length > 0 ? src.map((s) => ({ ...s })) : null; + } + + function getFirstSegs() { + for (const rec of stateByKey.values()) { + const src = rec.liveSegs || rec.committedSegs; + if (src && src.length > 0) return src.map((s) => ({ ...s })); + } + return null; + } + + function seedSeries(key, segs) { + if (!key || !Array.isArray(segs) || segs.length === 0) return; + stateByKey.set(key, { + committedSegs: segs.map((s) => ({ ...s })), + liveSegs: null, + endX: NaN, + endY: NaN, + }); + hasRenderedOnce = true; + } + function morphSeries({ items, leftX, spanX, bottomY, skipMorph = false, + allowBaselineEntry = false, }) { cancel(); const canAnim = !skipMorph && canAnimateChartInBrowser(); + const wasRenderedBefore = hasRenderedOnce; + hasRenderedOnce = items.length > 0; const activeSet = new Set(); const jobs = []; @@ -520,7 +569,26 @@ export function createMultiSeriesCurveMorphController({ durationMs = CHART_CURVE const it = items[idx]; activeSet.add(it.key); const prevEntry = stateByKey.get(it.key); - const prevSegs = prevEntry ? prevEntry.liveSegs || prevEntry.committedSegs : null; + let prevSegs = prevEntry ? prevEntry.liveSegs || prevEntry.committedSegs : null; + if ( + (!prevSegs || prevSegs.length === 0) && + canAnim && + allowBaselineEntry && + wasRenderedBefore && + it.nextSegs.length > 0 && + Number.isFinite(bottomY) + ) { + prevSegs = it.nextSegs.map((s) => ({ + u0: s.u0, + y0: bottomY, + cu1: s.cu1, + cy1: bottomY, + cu2: s.cu2, + cy2: bottomY, + u3: s.u3, + y3: bottomY, + })); + } if (!canAnim || !prevSegs || prevSegs.length === 0 || it.nextSegs.length === 0) { stateByKey.set(it.key, { @@ -585,6 +653,17 @@ export function createMultiSeriesCurveMorphController({ durationMs = CHART_CURVE endY: fromEndY, }; stateByKey.set(it.key, stateRec); + const initLineD = buildPathFromNormalizedSegs(activeLive, leftX, spanX); + patchAttr(it.lineEl, 'd', initLineD); + if (it.areaEl) { + if (it.finalAreaD) { + const firstX = (leftX + activeLive[0].u0 * spanX).toFixed(1); + const lastX = (leftX + activeLive[segCount - 1].u3 * spanX).toFixed(1); + patchAttr(it.areaEl, 'd', `${initLineD} L ${lastX} ${bottomY} L ${firstX} ${bottomY} Z`); + } else { + patchAttr(it.areaEl, 'd', ''); + } + } jobs.push({ it, alignedFrom, @@ -663,7 +742,7 @@ export function createMultiSeriesCurveMorphController({ durationMs = CHART_CURVE morphRafId = window.requestAnimationFrame(step); } - return { morphSeries, cancel, reset }; + return { morphSeries, cancel, reset, getSeriesSegs, getFirstSegs, seedSeries }; } function buildGridFitPolylineD(grid) { @@ -687,12 +766,41 @@ function buildGridCiBandD(grid) { return `${d} Z`; } +function resampleFitGridToTargetXs(sourceGrid, targetGrid) { + if (!Array.isArray(sourceGrid) || sourceGrid.length === 0) return null; + const sLen = sourceGrid.length; + const sFirst = sourceGrid[0]; + const sLast = sourceGrid[sLen - 1]; + let j = 0; + return targetGrid.map((tg) => { + const x = tg.x; + if (sLen === 1 || x <= sFirst.x) { + return { x, fitY: sFirst.fitY, lowY: sFirst.lowY, highY: sFirst.highY }; + } + if (x >= sLast.x) { + return { x, fitY: sLast.fitY, lowY: sLast.lowY, highY: sLast.highY }; + } + while (j < sLen - 2 && sourceGrid[j + 1].x < x) j += 1; + const a = sourceGrid[j]; + const b = sourceGrid[j + 1]; + const span = b.x - a.x; + const r = span > 1e-6 ? Math.max(0, Math.min(1, (x - a.x) / span)) : 0; + return { + x, + fitY: a.fitY + (b.fitY - a.fitY) * r, + lowY: a.lowY + (b.lowY - a.lowY) * r, + highY: a.highY + (b.highY - a.highY) * r, + }; + }); +} + /** * Smoothly morphs LOESS fit polylines, `95% CI` confidence bands, and optional OLS lines * across metric/model switches in `createModelFitChart`. */ export function createFitGridMorphController({ durationMs = CHART_CURVE_MORPH_MS } = {}) { let morphRafId = 0; + let hasRenderedOnce = false; const stateByKey = new Map(); function cancel() { @@ -708,12 +816,39 @@ export function createFitGridMorphController({ durationMs = CHART_CURVE_MORPH_MS function reset() { cancel(); + hasRenderedOnce = false; stateByKey.clear(); } - function morphFits({ items, skipMorph = false }) { + function getGrid(key) { + const rec = stateByKey.get(key); + const src = rec ? rec.liveGrid || rec.committedGrid : null; + return src && src.length > 0 ? src.map((g) => ({ ...g })) : null; + } + + function getFirstGrid() { + for (const rec of stateByKey.values()) { + const src = rec.liveGrid || rec.committedGrid; + if (src && src.length > 0) return src.map((g) => ({ ...g })); + } + return null; + } + + function seedGrid(key, gridPts) { + if (!key || !Array.isArray(gridPts) || gridPts.length === 0) return; + stateByKey.set(key, { + committedGrid: gridPts.map((g) => ({ ...g })), + liveGrid: null, + ols: null, + }); + hasRenderedOnce = true; + } + + function morphFits({ items, skipMorph = false, allowBaselineEntry = false, bottomY = NaN }) { cancel(); const canAnim = !skipMorph && canAnimateChartInBrowser(); + const wasRenderedBefore = hasRenderedOnce; + hasRenderedOnce = items.length > 0; const activeSet = new Set(); const jobs = []; @@ -721,7 +856,32 @@ export function createFitGridMorphController({ durationMs = CHART_CURVE_MORPH_MS const it = items[idx]; activeSet.add(it.key); const prevEntry = stateByKey.get(it.key); - const prevGrid = prevEntry ? prevEntry.liveGrid || prevEntry.committedGrid : null; + const rawPrevGrid = prevEntry ? prevEntry.liveGrid || prevEntry.committedGrid : null; + let prevGrid = + rawPrevGrid && rawPrevGrid.length > 0 && it.gridPts.length >= 2 + ? rawPrevGrid.length === it.gridPts.length && + Math.abs(rawPrevGrid[0].x - it.gridPts[0].x) < 0.5 && + Math.abs( + rawPrevGrid[rawPrevGrid.length - 1].x - it.gridPts[it.gridPts.length - 1].x + ) < 0.5 + ? rawPrevGrid + : resampleFitGridToTargetXs(rawPrevGrid, it.gridPts) + : null; + if ( + !prevGrid && + canAnim && + allowBaselineEntry && + wasRenderedBefore && + it.gridPts.length >= 2 && + Number.isFinite(bottomY) + ) { + prevGrid = it.gridPts.map((g) => ({ + x: g.x, + fitY: bottomY, + lowY: bottomY, + highY: bottomY, + })); + } if ( !canAnim || @@ -782,6 +942,16 @@ export function createFitGridMorphController({ durationMs = CHART_CURVE_MORPH_MS ols: fromOls ? { ...fromOls } : null, }; stateByKey.set(it.key, stateRec); + patchAttr(it.curveEl, 'd', buildGridFitPolylineD(activeLive)); + if (it.bandEl) { + patchAttr(it.bandEl, 'd', it.finalBandD ? buildGridCiBandD(activeLive) : ''); + } + if (it.olsEl && fromOls) { + patchAttr(it.olsEl, 'x1', fromOls.x1.toFixed(1)); + patchAttr(it.olsEl, 'y1', fromOls.y1.toFixed(1)); + patchAttr(it.olsEl, 'x2', fromOls.x2.toFixed(1)); + patchAttr(it.olsEl, 'y2', fromOls.y2.toFixed(1)); + } jobs.push({ it, fromGrid: prevGrid, activeLive, fromOls, stateRec }); } @@ -848,7 +1018,7 @@ export function createFitGridMorphController({ durationMs = CHART_CURVE_MORPH_MS morphRafId = window.requestAnimationFrame(step); } - return { morphFits, cancel, reset }; + return { morphFits, cancel, reset, getGrid, getFirstGrid, seedGrid }; } /** diff --git a/sidecars/panel/src/components/charts/stepChartMath.js b/sidecars/panel/src/components/charts/stepChartMath.js index c2afd2f..6d2134f 100644 --- a/sidecars/panel/src/components/charts/stepChartMath.js +++ b/sidecars/panel/src/components/charts/stepChartMath.js @@ -663,3 +663,50 @@ export function formatChartMetricCompact(metric, val) { return String(val); } } + +/** + * Downsamples dense chronological step series (`> maxPoints`, e.g. 1,500–5,000 steps) to an + * adaptive visual vertex budget (`~2.1px` per segment) so cubic splines never overplot into an + * opaque solid wall while strictly preserving the start step, end step, and exact global peak step. + * + * @template {{ value?: number }} T + * @param {T[]} points + * @param {number} [maxPoints=208] + * @returns {T[]} + */ +export function downsampleStepPointsForPlot(points, maxPoints = 208) { + if (!Array.isArray(points) || points.length <= maxPoints || maxPoints < 8) { + return points; + } + const n = points.length; + let globalPeakIdx = -1; + let globalPeakVal = -Infinity; + for (let i = 0; i < n; i += 1) { + const v = Number(points[i]?.value); + if (Number.isFinite(v) && v >= globalPeakVal) { + globalPeakVal = v; + globalPeakIdx = i; + } + } + + const out = [points[0]]; + const bucketCount = maxPoints - 2; + const interiorLen = n - 2; + for (let b = 0; b < bucketCount; b += 1) { + const bStart = 1 + Math.floor((b * interiorLen) / bucketCount); + const bEnd = Math.max(bStart + 1, 1 + Math.floor(((b + 1) * interiorLen) / bucketCount)); + const pickIdx = + globalPeakIdx >= bStart && globalPeakIdx < bEnd + ? globalPeakIdx + : bStart + Math.floor((bEnd - bStart) / 2); + const picked = points[pickIdx]; + if (picked && picked !== out[out.length - 1]) { + out.push(picked); + } + } + const lastPt = points[n - 1]; + if (lastPt && lastPt !== out[out.length - 1]) { + out.push(lastPt); + } + return out; +} diff --git a/sidecars/panel/src/styles/views-tables.css b/sidecars/panel/src/styles/views-tables.css index d1bbdc7..6602c98 100644 --- a/sidecars/panel/src/styles/views-tables.css +++ b/sidecars/panel/src/styles/views-tables.css @@ -820,6 +820,11 @@ stroke-width: 1.9; } +.sstep-line-card .sstep-chart-wrap.is-dense .trend-line, +.sstep-line-card .sstep-chart-wrap.is-dense .sstep-fit-line { + stroke-width: 1.45; +} + .sstep-multi-area { fill: var(--sstep-series-color, var(--ctx-cached)); fill-opacity: 0.08; diff --git a/sidecars/panel/styles.css b/sidecars/panel/styles.css index 0c58015..09f29f5 100644 --- a/sidecars/panel/styles.css +++ b/sidecars/panel/styles.css @@ -5135,6 +5135,11 @@ html[data-standalone="true"] .app-shell { stroke-width: 1.9; } +.sstep-line-card .sstep-chart-wrap.is-dense .trend-line, +.sstep-line-card .sstep-chart-wrap.is-dense .sstep-fit-line { + stroke-width: 1.45; +} + .sstep-multi-area { fill: var(--sstep-series-color, var(--ctx-cached)); fill-opacity: 0.08; diff --git a/sidecars/panel/test/modern-ui.test.mjs b/sidecars/panel/test/modern-ui.test.mjs index 8e9f02c..a940fdd 100644 --- a/sidecars/panel/test/modern-ui.test.mjs +++ b/sidecars/panel/test/modern-ui.test.mjs @@ -630,6 +630,109 @@ test('SegmentedControl variable-width glider snaps cleanly on reveal and glides queuedRaf?.(); assert.equal(stepTipEl.classList.contains('is-tracking'), true); assert.equal(stepHoverDot.classList.contains('is-tracking'), true); + + // High-density 2606-step session downsamples spline cleanly without turning into a 2600-segment wall, while keeping exact peak + const denseSteps = Array.from({ length: 2606 }, (_, i) => ({ + stepIndex: 5500 + i, + createdAtMs: 1000 + i * 100, + modelLabel: i % 2 === 0 ? 'neon-4-xhigh' : 'carbon-4-high', + modelShort: i % 2 === 0 ? 'neon-4-xhigh' : 'carbon-4-high', + tokPerSec: i === 1963 ? 151 : 25 + ((i * 17) % 90), + inputTokens: 1000, + cacheReadTokens: 2000, + outputTokens: 100, + })); + stepLineChart.update({ + steps: denseSteps, + modelGroupKey: 'neon-4-xhigh', + modelGroupKeys: ['neon-4-xhigh'], + metric: 'tps', + }); + // Flush any morph animation to final path + if (queuedRaf) queuedRaf(performance.now() + 400); + const denseLineD = stepLineChart.el.querySelector('.trend-line').getAttribute('d') || ''; + const cubicSegCount = (denseLineD.match(/ C /g) || []).length; + assert.ok( + cubicSegCount >= 120 && cubicSegCount <= 256, + `Expected adaptive downsampled cubic segment count in [120..256], got ${cubicSegCount}` + ); + stepLineChart.update({ + steps: denseSteps, + modelGroupKey: '__all__', + modelGroupKeys: ['__all__'], + metric: 'tps', + }); + if (queuedRaf) queuedRaf(performance.now() + 400); + assert.match(stepLineChart.el.querySelector('.trend-peak-pill').textContent, /#7463.*151/); + + // Switching from 1 model -> 2 models animates newly added model smoothly from baseline instead of snapping + globalThis.window.getComputedStyle = () => ({}); + stepLineChart.update({ + steps: denseSteps, + modelGroupKey: 'neon-4-xhigh', + modelGroupKeys: ['neon-4-xhigh'], + metric: 'tps', + }); + if (queuedRaf) queuedRaf(performance.now() + 400); + queuedRaf = null; + stepLineChart.update({ + steps: denseSteps, + modelGroupKey: 'neon-4-xhigh', + modelGroupKeys: ['neon-4-xhigh', 'carbon-4-high'], + metric: 'tps', + }); + assert.equal( + typeof queuedRaf, + 'function', + 'Checking a 2nd model in SessionLineChart schedules a 240ms morph animation' + ); + const multiPaths = stepLineChart.el.querySelectorAll('.sstep-multi-line-group .sstep-fit-line'); + assert.equal(multiPaths.length, 2); + const carbonStartD = multiPaths[1].getAttribute('d') || ''; + queuedRaf(performance.now() + 400); + const carbonEndD = multiPaths[1].getAttribute('d') || ''; + assert.notEqual(carbonStartD, carbonEndD, 'Newly checked 2nd model morphs from baseline to target curve'); + + // High-density (>96 segments at 460px width) morph must NOT skip knots during animation or pop peaks up at completion + stepStage.clientWidth = 460; + stepLineChart.update({ + steps: denseSteps, + modelGroupKey: '__all__', + modelGroupKeys: ['__all__'], + metric: 'totalTokens', + }); + if (queuedRaf) queuedRaf(performance.now() + 400); + queuedRaf = null; + const morphStartTs = performance.now(); + stepLineChart.update({ + steps: denseSteps, + modelGroupKey: '__all__', + modelGroupKeys: ['__all__'], + metric: 'tps', + }); + assert.equal(typeof queuedRaf, 'function'); + queuedRaf(morphStartTs + 239.95); + const nearEndD = stepLineChart.el.querySelector('.trend-line').getAttribute('d') || ''; + queuedRaf(morphStartTs + 300); + const exactEndD = stepLineChart.el.querySelector('.trend-line').getAttribute('d') || ''; + const nearSegCount = (nearEndD.match(/ C /g) || []).length; + const exactSegCount = (exactEndD.match(/ C /g) || []).length; + assert.ok( + exactSegCount > 96 && nearSegCount >= exactSegCount, + '460px plot preserves >=100% of target knots during >96-segment morph without stride-2 skipping' + ); + const parseEndpoints = (d) => + [...d.matchAll(/([\d.]+)\s+([\d.]+)(?=\s*C|$)/g)].map((m) => ({ x: +m[1], y: +m[2] })); + const nearPts = parseEndpoints(nearEndD); + const exactPts = parseEndpoints(exactEndD); + for (const ep of exactPts) { + const minDy = nearPts.reduce( + (best, np) => (Math.abs(np.x - ep.x) <= 0.2 ? Math.min(best, Math.abs(np.y - ep.y)) : best), + Infinity + ); + assert.ok(minDy <= 0.2, `Target vertex (${ep.x}, ${ep.y}) must not pop at completion (dy=${minDy})`); + } + delete globalThis.window.getComputedStyle; stepLineChart.destroy(); const fitChart = createModelFitChart();