Skip to content

The only tool you need to know what is happening and how to fix it.

chore: fix unit and value collision on charts (#412)

Vuk01dc14b35ddc
diff --git a/frontend/src/lib/components/dashboard/d3-line-chart.svelte b/frontend/src/lib/components/dashboard/d3-line-chart.svelte
index 33373cae..4b065992 100644
--- a/frontend/src/lib/components/dashboard/d3-line-chart.svelte
+++ b/frontend/src/lib/components/dashboard/d3-line-chart.svelte
@@ -127,6 +127,13 @@
const yAxisUnit = $derived(() => axisScale?.unit ?? '');
+ // The unit label sits just above the plot; when the top tick is that high too, the two overlap,
+ // so the unit moves onto the top tick's label instead.
+ const unitOnTopTick = $derived.by(() => {
+ const ticks = yTickItems();
+ return !!yAxisUnit() && ticks.length > 0 && yScale(ticks[ticks.length - 1]) < 12;
+ });
+
const yTickItems = $derived(() => {
const seen = new SvelteSet<string>();
return yTicks().filter((tick) => {
@@ -485,12 +492,14 @@
fill="#6b7280"
font-size="10"
>
- {formatYLabel(tick)}
+ {formatYLabel(tick)}{unitOnTopTick && __index === yTickItems().length - 1
+ ? ` ${yAxisUnit()}`
+ : ''}
</text>
{/each}
<!-- Y axis unit label -->
- {#if yAxisUnit()}
+ {#if yAxisUnit() && !unitOnTopTick}
<text x={-8} y={-4} text-anchor="end" fill="#9ca3af" font-size="10">
{yAxisUnit()}
</text>
diff --git a/frontend/src/lib/components/dashboard/d3-stacked-area-chart.svelte b/frontend/src/lib/components/dashboard/d3-stacked-area-chart.svelte
index 16dd7fe8..660421cf 100644
--- a/frontend/src/lib/components/dashboard/d3-stacked-area-chart.svelte
+++ b/frontend/src/lib/components/dashboard/d3-stacked-area-chart.svelte
@@ -166,6 +166,13 @@
const yAxisUnit = $derived(() => axisScale?.unit ?? '');
+ // The unit label sits just above the plot; when the top tick is that high too, the two overlap,
+ // so the unit moves onto the top tick's label instead.
+ const unitOnTopTick = $derived.by(() => {
+ const ticks = yTicks();
+ return !!yAxisUnit() && ticks.length > 0 && yScale(ticks[ticks.length - 1]) < 12;
+ });
+
function formatYLabel(value: number): string {
if (axisScale) {
return formatAxisTick(value, axisScale.divisor);
@@ -386,12 +393,14 @@
class="text-muted-foreground"
font-size="10"
>
- {formatYLabel(tick)}
+ {formatYLabel(tick)}{unitOnTopTick && __index === yTicks().length - 1
+ ? ` ${yAxisUnit()}`
+ : ''}
</text>
{/each}
<!-- Y axis unit label -->
- {#if yAxisUnit()}
+ {#if yAxisUnit() && !unitOnTopTick}
<text
x={-8}
y={-4}