Skip to content

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

fix(charts): prevent compact axes from recursing across zero (#17365)

Nikita Kabardin857a1e359f14
diff --git a/web/src/features/widgets/chart-library/Chart.clienttest.tsx b/web/src/features/widgets/chart-library/Chart.clienttest.tsx
index cb099c0cb4..36f586b1f5 100644
--- a/web/src/features/widgets/chart-library/Chart.clienttest.tsx
+++ b/web/src/features/widgets/chart-library/Chart.clienttest.tsx
@@ -21,7 +21,11 @@ class ResizeObserverStub {
(global as typeof globalThis & { ResizeObserver: unknown }).ResizeObserver =
ResizeObserverStub;
-afterEach(cleanup);
+afterEach(() => {
+ cleanup();
+ vi.restoreAllMocks();
+ vi.unstubAllGlobals();
+});
const point = (metric: DataPoint["metric"], dimension?: string): DataPoint => ({
time_dimension: "2026-01-01T00:00:00Z",
@@ -74,3 +78,41 @@ describe("Chart dispatcher — empty-state guard (LFE-14333)", () => {
expect(screen.getByText("No data")).toBeInTheDocument();
});
});
+
+it("renders a compact bar chart whose values cross zero", () => {
+ const bounds = new DOMRect(0, 0, 500, 63);
+ vi.spyOn(HTMLElement.prototype, "getBoundingClientRect").mockImplementation(
+ function (this: HTMLElement) {
+ return this.id === "recharts_measurement_span"
+ ? new DOMRect(0, 0, 8, 12)
+ : bounds;
+ },
+ );
+ vi.stubGlobal(
+ "ResizeObserver",
+ class implements ResizeObserver {
+ constructor(private readonly callback: ResizeObserverCallback) {}
+ observe(target: Element) {
+ this.callback(
+ [{ target, contentRect: bounds } as ResizeObserverEntry],
+ this,
+ );
+ }
+ unobserve() {}
+ disconnect() {}
+ },
+ );
+
+ const { container } = render(
+ <Chart
+ chartType="VERTICAL_BAR"
+ data={[point(-1, "negative"), point(1, "positive")]}
+ rowLimit={100}
+ zeroBaseline
+ hideXAxisLabels
+ />,
+ );
+
+ expect(container.querySelectorAll(".recharts-bar-rectangle")).toHaveLength(2);
+ expect(screen.getByText("0")).toBeInTheDocument();
+});
diff --git a/web/src/features/widgets/chart-library/useChartTickBudget.ts b/web/src/features/widgets/chart-library/useChartTickBudget.ts
index be845457cb..b71e6f4edb 100644
--- a/web/src/features/widgets/chart-library/useChartTickBudget.ts
+++ b/web/src/features/widgets/chart-library/useChartTickBudget.ts
@@ -39,11 +39,12 @@ export function useChartTickBudget() {
? Math.max(2, Math.floor((size.width - AXIS_GUTTER_PX) / APPROX_LABEL_PX))
: 6;
+ // Recharts needs at least three ticks when an axis crosses zero.
const maxYTicks =
size.height > 0
? Math.min(
DEFAULT_Y_TICK_COUNT,
- Math.max(2, Math.floor(size.height / APPROX_Y_LABEL_PX)),
+ Math.max(3, Math.floor(size.height / APPROX_Y_LABEL_PX)),
)
: DEFAULT_Y_TICK_COUNT;