";
+ }
+}
+
+function metricRows(id) {
+ const src = METRIC_SOURCE[id];
+ if (!src) return null;
+ if (src === "money") return moneyData ? (moneyData.series || []) : null;
+ return trendsData;
+}
+
+function loadForMetric() {
+ const src = METRIC_SOURCE[trendMetric];
+ if (src === "money") { if (moneyData) renderTrends(); else loadMoneySeries(); return; }
+ if (trendsData) renderTrends(); else loadMetrics();
+}
+
async function loadMetrics() {
if (trendsData) { renderCoopTotals(); renderTrends(); return; }
const host = $("trend-body");
@@ -1362,12 +1437,8 @@ function renderCoopTotals() {
function setTrendMetric(m) {
trendMetric = m;
- $("tm-members").className = "mode-btn" + (m === "members" ? " active" : "");
- $("tm-requests").className = "mode-btn" + (m === "requests" ? " active" : "");
- $("tm-spend").className = "mode-btn" + (m === "spend" ? " active" : "");
- $("tm-energy").className = "mode-btn" + (m === "energy" ? " active" : "");
- $("tm-co2e").className = "mode-btn" + (m === "co2e" ? " active" : "");
- renderTrends();
+ syncMetricButtons();
+ loadForMetric();
}
function setTrendDays(d) {
@@ -1378,14 +1449,6 @@ function setTrendDays(d) {
renderTrends();
}
-function trendVal(row) {
- if (trendMetric === "members") return row.members_total;
- if (trendMetric === "spend") return row.spend_usd;
- if (trendMetric === "energy") return row.energy_wh;
- if (trendMetric === "co2e") return row.co2e_g;
- return row.requests;
-}
-
// ---- Shared chart module ----
// Chart rendering + the exact-value hover tooltips come from the shared
// CoOpChart module (canonical: ops-docs scripts/shared/coop_chart.js,
@@ -1396,58 +1459,46 @@ function trendVal(row) {
const {
registerChartTips, hideChartTip, showChartTip, moveChartTip, chartTipTarget,
chartTipData, niceStep, measuredRuns, chartFrame, dateMarks, drawBars,
- drawLineRuns, hoverTargets
+ drawLineRuns, hoverTargets,
+ CHART_METRICS, metricDef, fmtMetric, renderChart
} = CoOpChart;
+// (fmtMoney is NOT destructured: this page already defines its own, byte-identical
+// one. Two declarations of the same const is a page-blanking SyntaxError — the
+// exact failure check-page-js.sh exists to catch, and it did.)
-function trendLabel(v) {
- if (v === null || v === undefined) return "not measured";
- if (trendMetric === "spend") return fmtMoney(v);
- if (trendMetric === "energy") return Number(v).toLocaleString() + " Wh";
- if (trendMetric === "co2e") return Number(v).toLocaleString() + " g";
- return Number(v).toLocaleString();
-}
+let moneyData = null; // the money metric's rows (a different endpoint)
function renderTrends() {
const host = $("trend-body");
- if (!host || !trendsData) return;
+ if (!host) return;
hideChartTip();
- const conf = TREND_CONF[trendMetric];
- const rows = trendDays === 0 ? trendsData : trendsData.slice(-trendDays);
-
- const vals = rows.map(trendVal);
- // Exact-value hover tooltips (the shared module's contract): register one
- // point per row, straight from the data — same convention as the panel.
- registerChartTips("trend-body", rows.map((row, i) => ({
- label: row.day,
- series: [[conf.title, trendLabel(vals[i])]]
- })));
- const have = vals.filter(v => v !== null && v !== undefined);
- if (have.length < 2) {
- host.innerHTML = "
Not enough history yet.
";
+ const def = metricDef(trendMetric);
+ const legendEl = $("trend-legend");
+ $("trend-title").textContent = def.title;
+ const all = metricRows(trendMetric);
+ if (!all) {
+ // A metric this page has no source for, versus one it has not fetched yet.
+ host.innerHTML = METRIC_SOURCE[trendMetric]
+ ? "
Loading
"
+ : "
Not available here.
";
+ if (legendEl) legendEl.innerHTML = "";
return;
}
- const maxV = Math.max.apply(null, have);
- const step = niceStep((maxV <= 0 ? 1 : maxV * 1.08) / 4, conf.cont);
- const fr = chartFrame({
- n: vals.length,
- top: step * 4,
- step,
- tickLabel: trendLabel,
- aria: conf.title,
- gridStroke: "var(--border, #e4e0d8)"
- });
-
- if (conf.kind === "bar") {
- drawBars(fr, vals, { color: "var(--green-mid)", host: "trend-body" });
- } else {
- // Measured runs only: a day we never recorded stays a gap.
- drawLineRuns(fr, vals, { color: "var(--green-deep)", area: true });
- hoverTargets(fr, vals, "trend-body");
+ const rows = trendDays === 0 ? all : all.slice(-trendDays);
+ // The shared renderer: it draws the chart, registers the exact tooltip values
+ // and hands back the legend, so this chart is the same chart the panel draws.
+ const out = renderChart({ host: "trend-body", def, rows, gridStroke: "var(--border, #e4e0d8)" });
+ if (out.empty) {
+ host.innerHTML = "