fix: stable power curve colors — buttons and chart lines always match

This commit is contained in:
Davide Scaini
2026-05-21 21:15:44 +02:00
parent d4e5b11f71
commit 793b719983
+18 -13
View File
@@ -32,8 +32,18 @@
'#22d3ee', // cyan-400 '#22d3ee', // cyan-400
]; ];
function curveColor(key: RangeKey, index: number): string { const allRangeKeys = [
return PALETTE[index % PALETTE.length]; ...Object.keys(PRESET_LABELS),
...seasons.map(s => s.name),
];
// Stable color per key — based on position in allRangeKeys, not in the active selection
const RANGE_COLOR: Record<string, string> = Object.fromEntries(
allRangeKeys.map((k, i) => [k, PALETTE[i % PALETTE.length]])
);
function curveColor(key: RangeKey): string {
return RANGE_COLOR[key] ?? PALETTE[0];
} }
// ── Record-holder lookup ─────────────────────────────────────────────────── // ── Record-holder lookup ───────────────────────────────────────────────────
@@ -121,7 +131,7 @@
}); });
}); });
$: colorMap = Object.fromEntries(selectedKeys.map((k, i) => [k, curveColor(k, i)])); $: colorMap = Object.fromEntries(selectedKeys.map(k => [k, curveColor(k)]));
function getAxisColor() { function getAxisColor() {
return document.documentElement.getAttribute('data-theme') === 'light' ? '#52525b' : '#a1a1aa'; return document.documentElement.getAttribute('data-theme') === 'light' ? '#52525b' : '#a1a1aa';
@@ -155,7 +165,7 @@
}, },
color: { color: {
domain: selectedKeys, domain: selectedKeys,
range: selectedKeys.map((k, i) => curveColor(k, i)), range: selectedKeys.map(k => curveColor(k)),
legend: selectedKeys.length > 1, legend: selectedKeys.length > 1,
}, },
marks: [ marks: [
@@ -222,11 +232,6 @@
selectedRanges = next; selectedRanges = next;
} }
const allRangeKeys = [
...Object.keys(PRESET_LABELS),
...seasons.map(s => s.name),
];
// ── Records table ────────────────────────────────────────────────────────── // ── Records table ──────────────────────────────────────────────────────────
let tableRange: RangeKey = 'all_time'; let tableRange: RangeKey = 'all_time';
@@ -254,9 +259,9 @@
<!-- Range selector pills --> <!-- Range selector pills -->
<div class="flex flex-wrap gap-2 mb-4"> <div class="flex flex-wrap gap-2 mb-4">
{#each allRangeKeys as key, i} {#each allRangeKeys as key}
{@const active = selectedRanges.has(key)} {@const active = selectedRanges.has(key)}
{@const color = curveColor(key, i)} {@const color = curveColor(key)}
<button <button
on:click={() => toggleRange(key)} on:click={() => toggleRange(key)}
class="px-3 py-1 rounded-full text-sm font-medium border transition-colors" class="px-3 py-1 rounded-full text-sm font-medium border transition-colors"
@@ -282,12 +287,12 @@
<div class="flex items-center justify-between mb-3"> <div class="flex items-center justify-between mb-3">
<h4 class="text-xs font-semibold text-zinc-400 uppercase tracking-wide">Power records</h4> <h4 class="text-xs font-semibold text-zinc-400 uppercase tracking-wide">Power records</h4>
<div class="flex gap-1"> <div class="flex gap-1">
{#each allRangeKeys as key, i} {#each allRangeKeys as key}
<button <button
on:click={() => tableRange = key} on:click={() => tableRange = key}
class="px-2.5 py-1 rounded-full text-xs font-medium border transition-colors" class="px-2.5 py-1 rounded-full text-xs font-medium border transition-colors"
style={tableRange === key style={tableRange === key
? `background:${curveColor(key, i)}22; border-color:${curveColor(key, i)}; color:${curveColor(key, i)}` ? `background:${curveColor(key)}22; border-color:${curveColor(key)}; color:${curveColor(key)}`
: 'background:transparent; border-color:#3f3f46; color:#71717a'} : 'background:transparent; border-color:#3f3f46; color:#71717a'}
>{PRESET_LABELS[key] ?? key}</button> >{PRESET_LABELS[key] ?? key}</button>
{/each} {/each}