Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(web): Improve y axis for small charts (#18172)

Ben Bachem8ffa2cfedd13
diff --git a/web/src/components/design-system/charts/BarChart/BarChart.stories.tsx b/web/src/components/design-system/charts/BarChart/BarChart.stories.tsx
index 8113625fc2..be7690278f 100644
--- a/web/src/components/design-system/charts/BarChart/BarChart.stories.tsx
+++ b/web/src/components/design-system/charts/BarChart/BarChart.stories.tsx
@@ -1,5 +1,5 @@
import preview from "../../../../../.storybook/preview";
-import { expect, spyOn, userEvent, within } from "storybook/test";
+import { expect, spyOn, userEvent, waitFor, within } from "storybook/test";
import { chartColors } from "../constants";
import { BarChart } from "./BarChart";
@@ -25,6 +25,47 @@ const meta = preview.meta({
export const Default = meta.story({});
+export const YAxisTickDensity = meta.story({
+ name: "(Test) Y-axis Tick Density",
+ render: (args) => (
+ <div className="flex w-[400px] flex-col gap-4">
+ {["h-[50px]", "h-[100px]", "h-[224px]"].map((heightClass) => (
+ <div key={heightClass} className={heightClass}>
+ <BarChart {...args} />
+ </div>
+ ))}
+ </div>
+ ),
+ play: async ({ canvasElement }) => {
+ await waitFor(async () => {
+ const charts = within(canvasElement).getAllByRole("group", {
+ name: "Bar chart",
+ });
+ await expect(charts).toHaveLength(3);
+ for (const chart of charts) {
+ const labels = Array.from(
+ chart.querySelectorAll('text[text-anchor="end"][dominant-baseline]'),
+ );
+ await expect(labels.length).toBeGreaterThan(0);
+ const bounds = labels
+ .map((label) => label.getBoundingClientRect())
+ .sort((left, right) => left.top - right.top);
+ for (const bound of bounds) {
+ await expect(bound.height).toBeGreaterThan(0);
+ }
+ for (let index = 1; index < bounds.length; index++) {
+ const previous = bounds[index - 1];
+ const current = bounds[index];
+ if (!previous || !current) throw new Error("Label bounds not found");
+ // Allow minor font-metric and subpixel differences between browsers.
+ await expect(current.top).toBeGreaterThanOrEqual(previous.bottom - 1);
+ }
+ await expect(labels[0]).toHaveTextContent("0");
+ }
+ });
+ },
+});
+
export const PositiveBaseline = meta.story({
name: "(Test) Positive Baseline",
play: async ({ canvasElement }) => {
diff --git a/web/src/components/design-system/internal/charts/CartesianChart.tsx b/web/src/components/design-system/internal/charts/CartesianChart.tsx
index a5a145cdb7..4f49133157 100644
--- a/web/src/components/design-system/internal/charts/CartesianChart.tsx
+++ b/web/src/components/design-system/internal/charts/CartesianChart.tsx
@@ -277,6 +277,39 @@ export function CartesianChart({
activeX?: { key: string; x: number; label: string };
children: ReactNode;
}) {
+ // D3's tick count is a hint, not a limit. Reserve the endpoint labels first;
+ // their inward-facing baselines need more room than centered labels.
+ const visibleYTicks = yTicks
+ .map((tick, index) => {
+ const position = y(tick);
+ let top = position - 6;
+ if (index === 0) top = position - 12;
+ else if (index === yTicks.length - 1) top = position;
+ return { tick, index, top, bottom: top + 12 };
+ })
+ .sort((left, right) => {
+ const leftEndpoint = left.index === 0 || left.index === yTicks.length - 1;
+ const rightEndpoint =
+ right.index === 0 || right.index === yTicks.length - 1;
+ return Number(rightEndpoint) - Number(leftEndpoint);
+ })
+ .reduce<
+ Array<{ tick: number; index: number; top: number; bottom: number }>
+ >((visible, label) => {
+ if (
+ visible.some(
+ (other) =>
+ label.top < other.bottom + 4 && label.bottom + 4 > other.top,
+ )
+ ) {
+ return visible;
+ }
+ visible.push(label);
+ return visible;
+ }, [])
+ .sort((left, right) => left.index - right.index)
+ .map(({ tick }) => tick);
+
return (
<svg
width={width}
@@ -290,7 +323,7 @@ export function CartesianChart({
}
>
<ChartGrid
- yTicks={yTicks}
+ yTicks={visibleYTicks}
y={y}
left={plot.left}
top={plot.top}
@@ -312,7 +345,7 @@ export function CartesianChart({
/>
) : null}
<ChartYAxis
- ticks={yTicks}
+ ticks={visibleYTicks}
y={y}
left={plot.left}
valueFormatter={valueFormatter}
diff --git a/web/src/components/design-system/internal/charts/CartesianLayout.tsx b/web/src/components/design-system/internal/charts/CartesianLayout.tsx
index e23e37cab3..5334ddc722 100644
--- a/web/src/components/design-system/internal/charts/CartesianLayout.tsx
+++ b/web/src/components/design-system/internal/charts/CartesianLayout.tsx
@@ -22,7 +22,7 @@ function getCartesianPlotHeight(height: number, showXAxisLabels: boolean) {
}
function getCartesianTickCount(plotHeight: number) {
- return Math.min(5, Math.max(3, Math.floor(plotHeight / Y_TICK_HEIGHT)));
+ return Math.min(5, Math.max(1, Math.floor(plotHeight / Y_TICK_HEIGHT)));
}
function getCartesianLayout({