Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

fix(dashboard): reserve legend rows the grid actually lays out (#12951)

Abhi kumar370b278f289a
diff --git a/frontend/src/lib/uPlotV2/components/Legend/constants.ts b/frontend/src/lib/uPlotV2/components/Legend/constants.ts
index 644eccb16e..8a5a0a8dc1 100644
--- a/frontend/src/lib/uPlotV2/components/Legend/constants.ts
+++ b/frontend/src/lib/uPlotV2/components/Legend/constants.ts
@@ -10,6 +10,11 @@ export const MIN_LEGEND_ITEM_WIDTH = 110;
/** Marker + row padding, on top of the estimated label width. */
export const LEGEND_ITEM_EXTRA_WIDTH = 16;
+/** Must match `.gridList`'s column gap and `.scroller`'s padding-right, or the
+ * reserved row count disagrees with the grid that gets laid out. */
+export const LEGEND_COLUMN_GAP = 8;
+export const LEGEND_SCROLLER_PADDING_RIGHT = 4;
+
/** Must match `.row`'s height and the grid's row gap, or the reserved
* rectangle clips a row. */
export const LEGEND_ROW_HEIGHT = 28;
diff --git a/frontend/src/lib/visualization/charts/__tests__/utils.test.ts b/frontend/src/lib/visualization/charts/__tests__/utils.test.ts
index 0002c42d8e..cc6cb3a58a 100644
--- a/frontend/src/lib/visualization/charts/__tests__/utils.test.ts
+++ b/frontend/src/lib/visualization/charts/__tests__/utils.test.ts
@@ -113,7 +113,7 @@ describe('calculateChartDimensions', () => {
});
it('BOTTOM: items one past a row still reserve two rows', () => {
- // 1000px wide fits 5 of these per row, so 6 items need a second row.
+ // 1000px wide fits 4 of these per row, so 6 items need a second row.
const dims = calculateChartDimensions({
containerWidth: 1000,
containerHeight: 500,
@@ -123,6 +123,19 @@ describe('calculateChartDimensions', () => {
expect(dims.legendHeight).toBe(70);
});
+ it('BOTTOM: reserves the rows the grid actually lays out, not the rows a bare width estimate allows', () => {
+ // The item width alone suggests three fit on one row; the grid's per-item
+ // padding and column gap leave room for two.
+ const dims = calculateChartDimensions({
+ containerWidth: 412,
+ containerHeight: 310,
+ legendConfig: { position: LegendPosition.BOTTOM },
+ seriesLabels: ['P99', 'P95', 'P50'],
+ });
+ expect(dims.legendHeight).toBe(70);
+ expect(dims.height).toBe(240);
+ });
+
it('BOTTOM: drops to a single row rather than take half a short panel', () => {
const dims = calculateChartDimensions({
containerWidth: 1000,
diff --git a/frontend/src/lib/visualization/charts/utils.ts b/frontend/src/lib/visualization/charts/utils.ts
index e30a0eb4a2..198820de66 100644
--- a/frontend/src/lib/visualization/charts/utils.ts
+++ b/frontend/src/lib/visualization/charts/utils.ts
@@ -1,8 +1,11 @@
import {
LEGEND_MAX_BOTTOM_ROWS,
MIN_LEGEND_ITEM_WIDTH,
+ LEGEND_COLUMN_GAP,
+ LEGEND_ITEM_EXTRA_WIDTH,
LEGEND_ROW_GAP,
LEGEND_ROW_HEIGHT,
+ LEGEND_SCROLLER_PADDING_RIGHT,
MAX_LEGEND_WIDTH,
} from 'lib/uPlotV2/components/Legend/constants';
import { LegendConfig, LegendPosition } from 'lib/uPlotV2/components/types';
@@ -143,9 +146,16 @@ export function calculateChartDimensions({
const legendItemWidth = Math.ceil(
Math.min(approxLegendItemWidth, MAX_LEGEND_WIDTH),
);
+ // Must resolve to the same track count as `.gridList`'s `auto-fill`; a more
+ // generous one under-reserves rows and the grid's last row is clipped away.
+ const gridWidth =
+ containerWidth - LEGEND_PADDING * 2 - LEGEND_SCROLLER_PADDING_RIGHT;
const legendItemsPerRow = Math.max(
1,
- Math.floor((containerWidth - LEGEND_PADDING * 2) / legendItemWidth),
+ Math.floor(
+ (gridWidth + LEGEND_COLUMN_GAP) /
+ (legendItemWidth + LEGEND_ITEM_EXTRA_WIDTH + LEGEND_COLUMN_GAP),
+ ),
);
// The wrapper's bottom padding is inside this height (border-box).
@@ -163,8 +173,8 @@ export function calculateChartDimensions({
);
// Without this, short grid panels hand most of their area to the legend and
- // the chart — the pie donut especially — collapses to a sliver. Dropping a
- // whole row beats clipping one.
+ // the chart — the pie donut especially — collapses to a sliver. The dropped
+ // row's items are clipped rather than removed, so they are scroll-only here.
const legendRowCount =
neededRowCount > 1 &&
heightForRows(neededRowCount) > containerHeight * MAX_SHORT_PANEL_LEGEND_RATIO