commit 0844574d85440c2f3f849087fb594612b98d8296
parent ebd94bd7e825acf8a861c151d6aafc26a21d75d9
Author: Bharatvaj Hemanth <bharatvaj@yahoo.com>
Date: Wed, 7 Oct 2026 17:19:51 +0530
parent ebd94bd7e825acf8a861c151d6aafc26a21d75d9
Author: Bharatvaj Hemanth <bharatvaj@yahoo.com>
Date: Wed, 7 Oct 2026 17:19:51 +0530
Split TVA timeline config into small helpers
1 file changed, 58 insertions(+), 1 deletion(-)
diff --git a/tva-control.js b/tva-control.js @@ -194,7 +194,64 @@ const labelPlugin = { id: 'tvaLabels', afterDatasetsDraw(c) { if (mode !== 'timeline') return; const meta = c.getDatasetMeta(metricDatasetIndex); if (!meta) return; const vs = values(); meta.data.forEach((pt, i) => { const p = parameters[i], stale = isMetricStale(i), lvl = !stale && breachedLevel(i, vs[i]), critical = lvl && lvl.blink !== false && lvl.key !== levels[0].key; const { x, y } = pt.getProps(['x', 'y'], true), gy = y - c.chartArea.top < 52 ? y + 18 : y - 43, text = stale ? 'STALE' : formatPoint(p), ctx = c.ctx; ctx.save(); ctx.globalAlpha = stale ? .72 : critical ? (criticalBlink ? 1 : .38) : 1; ctx.font = 'bold 9px monospace'; const w = Math.max(28, ctx.measureText(text).width + 10), gx = Math.min(Math.max(x - w / 2, c.chartArea.left + 2), c.chartArea.right - w - 2); ctx.fillStyle = stale ? state.uiColors.staleLabelFill : lvl ? colorWithAlpha(levelColor(lvl), .18) : state.uiColors.normalLabelFill; ctx.strokeStyle = stale ? staleColor(false) : lvl ? levelColor(lvl) : state.uiColors.normalLabelStroke; ctx.beginPath(); ctx.rect(gx, gy, w, 31); ctx.fill(); ctx.stroke(); drawIcon(ctx, p.icon || 'warning', gx + w / 2, gy + 3, stale ? state.uiColors.staleText : state.uiColors.plotIcon); ctx.fillStyle = stale ? state.uiColors.staleText : state.uiColors.plotIcon; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.fillText(text, gx + w / 2, gy + 24); ctx.restore(); }); } }; function thresholdDataset(level, side) { return parameters.map(p => limit(p, level, side, side === 'Upper' ? Infinity : -Infinity)); } - function timelineConfig() { const vs = values(); const datasets = []; levels.filter(l => l.showAxis !== false).forEach(l => { datasets.push({ label: `${l.label || l.key} Upper`, data: thresholdDataset(l, 'Upper'), borderColor: colorWithAlpha(levelColor(l), number(l.axisAlpha, .35)), borderWidth: number(l.axisWidth, 1.6), borderDash: l.axisDash || [4, 5], pointRadius: 0, tension: .4 }); datasets.push({ label: `${l.label || l.key} Lower`, data: thresholdDataset(l, 'Lower'), borderColor: colorWithAlpha(levelColor(l), number(l.axisAlpha, .35)), borderWidth: number(l.axisWidth, 1.6), borderDash: l.axisDash || [4, 5], pointRadius: 0, tension: .4 }); }); (init.extraAxes || []).forEach(axis => datasets.push({ label: axis.label, data: parameters.map(p => p.key === axis.parameterKey ? number(axis.value) : null), borderColor: axis.color || state.colors.normal, borderWidth: number(axis.width, 1.4), borderDash: axis.dash || [3, 5], pointRadius: 0, spanGaps: false })); datasets.push({ label: 'Timeline Metric', data: vs, borderWidth: 3.5, tension: .4, fill: false, borderColor: state.colors.normal, segment: { borderColor: gradient, borderDash: c => isMetricStale(c.p0DataIndex) || isMetricStale(c.p1DataIndex) ? [3, 5] : [] }, pointBorderColor: c => metricColor(c.dataIndex, vs[c.dataIndex]), pointBackgroundColor: c => metricColor(c.dataIndex, vs[c.dataIndex]), pointStyle: c => isMetricStale(c.dataIndex) ? 'circle' : trend(c.dataIndex) === 'flat' ? 'circle' : 'triangle', rotation: c => trend(c.dataIndex) === 'down' ? 180 : 0, pointRadius: c => isMetricStale(c.dataIndex) ? 3 : trend(c.dataIndex) === 'flat' ? 4 : 5.5, pointHoverRadius: 8 }); return { type: 'line', data: { labels, datasets }, options: { responsive: true, maintainAspectRatio: false, layout: { padding: { left: 2, right: 6, top: number(init.topPadding, 34), bottom: 4 } }, onClick: (_e, els) => { const hit = els[0]; if (!hit || hit.datasetIndex !== metricDatasetIndex) return; if (parameters[hit.index].detailMode) switchMode(parameters[hit.index].detailMode); }, plugins: { title: { display: false }, legend: { display: false }, tooltip: { callbacks: { title: items => items.length ? labels[items[0].dataIndex] : '', label: c => c.datasetIndex === metricDatasetIndex ? formatPoint(parameters[c.dataIndex]) : null } } }, scales: { x: { offset: false, border: { display: false }, grid: { color: state.uiColors.grid }, ticks: { color: state.uiColors.tick, font: { weight: 'bold', size: 10 }, callback: (_v, i) => container.classList.contains('tva-chart-fullscreen-mode') ? labels[i] : '' } }, y: { ...zoomBounds(vs), border: { display: false }, grid: { color: c => c.tick.value === 0 ? state.uiColors.transparent : state.uiColors.grid }, ticks: { display: false } } } }, plugins: [ambientGlowPlugin, zeroAxisPlugin, labelPlugin] }; } + function thresholdDatasets() { + return levels.filter(l => l.showAxis !== false).flatMap(l => ['Upper', 'Lower'].map(side => ({ + label: `${l.label || l.key} ${side}`, + data: thresholdDataset(l, side), + borderColor: colorWithAlpha(levelColor(l), number(l.axisAlpha, .35)), + borderWidth: number(l.axisWidth, 1.6), + borderDash: l.axisDash || [4, 5], + pointRadius: 0, + tension: .4 + }))); + } + function extraAxisDatasets() { + return (init.extraAxes || []).map(axis => ({ + label: axis.label, + data: parameters.map(p => p.key === axis.parameterKey ? number(axis.value) : null), + borderColor: axis.color || state.colors.normal, + borderWidth: number(axis.width, 1.4), + borderDash: axis.dash || [3, 5], + pointRadius: 0, + spanGaps: false + })); + } + function metricDataset(vs) { + return { + label: 'Timeline Metric', + data: vs, + borderWidth: 3.5, + tension: .4, + fill: false, + borderColor: state.colors.normal, + segment: { borderColor: gradient, borderDash: c => isMetricStale(c.p0DataIndex) || isMetricStale(c.p1DataIndex) ? [3, 5] : [] }, + pointBorderColor: c => metricColor(c.dataIndex, vs[c.dataIndex]), + pointBackgroundColor: c => metricColor(c.dataIndex, vs[c.dataIndex]), + pointStyle: c => isMetricStale(c.dataIndex) ? 'circle' : trend(c.dataIndex) === 'flat' ? 'circle' : 'triangle', + rotation: c => trend(c.dataIndex) === 'down' ? 180 : 0, + pointRadius: c => isMetricStale(c.dataIndex) ? 3 : trend(c.dataIndex) === 'flat' ? 4 : 5.5, + pointHoverRadius: 8 + }; + } + function timelineConfig() { + const vs = values(); + return { + type: 'line', + data: { labels, datasets: [...thresholdDatasets(), ...extraAxisDatasets(), metricDataset(vs)] }, + options: { + responsive: true, + maintainAspectRatio: false, + layout: { padding: { left: 2, right: 6, top: number(init.topPadding, 34), bottom: 4 } }, + onClick: (_e, els) => { const hit = els[0]; if (hit && hit.datasetIndex === metricDatasetIndex && parameters[hit.index].detailMode) switchMode(parameters[hit.index].detailMode); }, + plugins: { title: { display: false }, legend: { display: false }, tooltip: { callbacks: { title: items => items.length ? labels[items[0].dataIndex] : '', label: c => c.datasetIndex === metricDatasetIndex ? formatPoint(parameters[c.dataIndex]) : null } } }, + scales: { + x: { offset: false, border: { display: false }, grid: { color: state.uiColors.grid }, ticks: { color: state.uiColors.tick, font: { weight: 'bold', size: 10 }, callback: (_v, i) => container.classList.contains('tva-chart-fullscreen-mode') ? labels[i] : '' } }, + y: { ...zoomBounds(vs), border: { display: false }, grid: { color: c => c.tick.value === 0 ? state.uiColors.transparent : state.uiColors.grid }, ticks: { display: false } } + } + }, + plugins: [ambientGlowPlugin, zeroAxisPlugin, labelPlugin] + }; + } function detailParam() { return parameters.find(p => p.detailMode === mode); } function barConfig() { const p = detailParam(), rows = normalizeAreas(state[p.areaKey]), days = rows.map(r => r.days), dl = p.detailLevels || []; return { type: 'bar', data: { labels: rows.map(r => r.name), datasets: [{ data: days, borderWidth: 1.2, borderRadius: 8, backgroundColor: c => { const lvl = dl.findLast(l => (c.raw || 0) > number(l.above, Infinity)); return lvl ? colorWithAlpha(colorForDetail(lvl), .38) : colorWithAlpha(state.colors.normal, .3); }, borderColor: c => { const lvl = dl.findLast(l => (c.raw || 0) > number(l.above, Infinity)); return lvl ? colorForDetail(lvl) : state.colors.normal; } }] }, options: { responsive: true, maintainAspectRatio: false, plugins: { title: { display: false }, subtitle: { display: true, text: p.detailSubtitle || '', color: state.colors[dl[0] && dl[0].level] || state.colors.normal, font: { size: 10, weight: 'bold' } }, legend: { display: false } }, scales: { x: { grid: { color: state.uiColors.detailGrid }, ticks: { color: state.uiColors.tick, font: { weight: 'bold', size: 10 } } }, y: { min: 0, max: Math.max(...days, ...dl.map(l => number(l.above, 0))) + 1.5, ticks: { color: state.uiColors.detailTick, callback: v => `${v}d` } } } } }; } function colorForDetail(l) { return state.colors[l.level] || state.colors[camel(l.level)] || state.colors.normal; }
: asapien/superpulse