Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
HoverState,
TimeSeriesPoint,
} from "./types";
import { dateToIndex, snapIndex } from "./utils";
import { dateToIndex, formatUniqueTickLabels, snapIndex } from "./utils";

const chartId = Math.random().toString(36).slice(2, 11);
const CLICK_THRESHOLD_PX = 4;
Expand Down Expand Up @@ -185,6 +185,12 @@
return measureFormatter(value);
};
$: axisFormatter = createMeasureValueFormatter(measure, "axis");
$: defaultFormatter = createMeasureValueFormatter(measure, "table");
$: yTickLabels = formatUniqueTickLabels(
yTicks,
axisFormatter,
defaultFormatter,
);

// Annotations
$: annotationGroups = groupAnnotations(
Expand Down Expand Up @@ -440,7 +446,7 @@
plotWidth={pb.width}
plotTop={pb.top}
plotHeight={pb.height}
{axisFormatter}
{yTickLabels}
/>

<!-- Chart body -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@
export let plotWidth: number;
export let plotTop: number;
export let plotHeight: number;
export let axisFormatter: (value: number) => string;
export let yTickLabels: string[];

const DASH = "1,1.5";
</script>

<g class="y-axis">
{#each yTicks as tick (tick)}
{#each yTicks as tick, i (tick)}
<text
class="fill-fg-muted text-[11px]"
text-anchor="start"
x={plotLeft + plotWidth + 4}
y={yScale(tick) + 4}
>
{axisFormatter(tick)}
{yTickLabels[i]}
</text>
<line
class="stroke-gray-300"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,23 @@ export function barCenterX(
singleBarWidth / 2
);
}

/**
* Formats tick values ensuring all labels are visually distinct.
*
* Uses the primary formatter (axis) as the first pass. If any labels
* are duplicated, falls back to the secondary formatter (table),
* which uses higher precision from the same formatting system.
*/
export function formatUniqueTickLabels(
ticks: number[],
primaryFormatter: (n: number) => string,
fallbackFormatter: (n: number) => string,
): string[] {
if (ticks.length <= 1) return ticks.map(primaryFormatter);

const labels = ticks.map(primaryFormatter);
if (new Set(labels).size === labels.length) return labels;

return ticks.map(fallbackFormatter);
}
Loading