# Why Grafana table gauge bars didn't match their values

A gauge makes one simple promise: a bigger number gets a longer bar. In a Grafana table, that promise was broken. A column holding the values 1, 8.88, 9 and 9.22 drew the longest bar for 9.

Grafana is an open source tool for building dashboards of charts and tables from your data. seriych reported the bug in [issue #113304](https://github.com/grafana/grafana/issues/113304), and my fix was merged in [grafana/grafana#136236](https://github.com/grafana/grafana/pull/136236). The cause was one comparison written the wrong way round, and review taught me something about code that runs for every cell.

## What users saw

The setup in the issue was small: a Table panel, a column width of 150 pixels, the cell type set to Gauge, and four values, `1`, `8.88`, `9` and `9.22`. A gauge cell shows a horizontal bar with the number next to it. `9.22` is the biggest number, but `9` had the biggest bar.

seriych then added a stranger case. Two columns held the same two values, `58` and `62`, sorted in opposite orders. The bars came out differently in each column, and wrong in both: `58` looked bigger than `62` each time. Later, another user said they saw something similar in Grafana v12.3.3.

## How a gauge cell makes room for its text

Each gauge cell has a fixed width, and it has to fit two things: the bar and the number's text. For the bars to be comparable, every cell in the column has to set aside the same room for text, enough for the longest value. The bar gets what's left.

Think of a race. If every runner starts from the same line, the finish tells you who was fastest. Give each runner a different start line and the finish tells you nothing. Here, the room for text is the start line.

Grafana calls the value behind this the alignment factor: the longest formatted value in the column. Formatted means the text as it's shown, including any prefix or suffix such as ` ms`. A function called `getAlignmentFactor`, in the newer table code known as TableNG, works it out. When a column has no factor yet, it looks at the next 1000 rows for the longest one. When it already has one, it only checks whether the current cell's value is longer.

## Why it happened

Here's the "look at the next 1000 rows" part before the fix, trimmed to the loop:

```ts
    // look at the next 1000 rows
    alignmentFactor = { ...displayValue };
    const maxIndex = Math.min(field.values.length, rowIndex + 1000);

    for (let i = rowIndex + 1; i < maxIndex; i++) {
      const nextDisplayValue = field.display?.(field.values[i]) ?? field.values[i];
      if (formattedValueToString(alignmentFactor).length > formattedValueToString(nextDisplayValue).length) {
        alignmentFactor.text = displayValue.text;
      }
    }
```

It starts from the current row's value, then walks the rows after it, comparing the length of each one's display text.

Two things are off. The `>` is the wrong way round, so the inside of the `if` runs when the next value is shorter, not longer. And when it runs, it writes the current row's own text back into the factor, which is the text that was already there. So a longer value further down never gets picked up, and whichever row is drawn first decides the room for the whole column.

In the issue's example the first row is `1`, so the room was sized for one character. Rows drawn later could still widen it through the other branch, so how much room a cell got depended on when it was drawn. That's also why sorting changed the picture in the two column example. Once the room for text differs from cell to cell, the bars aren't measured against the same space anymore, and their lengths stop matching their values.

The tests had the bug written into them, too. Two of them expected `'1'` as the factor for the values `[1, 22, 333, 4444]`, and a placeholder test (`it.todo`) was named after that exact wrong line.

## My first fix

The first version was small: compare the right way, and when a longer value turns up, take the whole thing.

```ts
      const nextDisplayValue = field.display?.(field.values[i]) ?? { text: String(field.values[i] ?? '') };
      if (formattedValueToString(alignmentFactor).length < formattedValueToString(nextDisplayValue).length) {
        alignmentFactor = { ...nextDisplayValue };
      }
```

Taking the whole value, not only its text, matters because the length counts the prefix and suffix too. And when a column has no `display` function to format its values, the raw value now gets wrapped as `{ text: ... }`, so it has the same shape as everything else.

For tests, I changed the two old ones to expect `'4444'`, removed the placeholder, and added a case with `[1, 8.88, 9, 9.22]` and a ` ms` suffix. The factor must be `8.88` with its suffix (`9.22` is just as long, so the first one found stays), and the shorter rows after it must not shrink it. On the unchanged code, three of the `getAlignmentFactor` tests failed. With the fix, all five passed.

## What review changed

fastfrwrd requested changes. The review said the fix looked correct, but it created more objects than it needed to. Every time a new longest value turned up, my version made a fresh copy, up to 999 extra copies in one scan, and all but the last were thrown away. Thrown away objects become garbage that the JavaScript engine has to clean up later, and fastfrwrd pointed out that avoiding that in this part of the table is critical for keeping it responsive. The review also noted that the current longest value was formatted again on every step, which caching its length would avoid.

It's like photocopying a document every time you find a better draft, instead of putting a sticky note on the best one and copying it once at the end. The suggestion was the sticky note: track the longest value and its length during the scan, then copy the winner once.

That made a lot of sense, so I reworked it that way. The merged version, without its comment:

```ts
    let longest: DisplayValueAlignmentFactors = displayValue;
    let longestLength = formattedValueToString(displayValue).length;
    const maxIndex = Math.min(field.values.length, rowIndex + 1000);

    for (let i = rowIndex + 1; i < maxIndex; i++) {
      const nextDisplayValue = field.display?.(field.values[i]) ?? { text: String(field.values[i] ?? '') };
      const nextLength = formattedValueToString(nextDisplayValue).length;
      if (nextLength > longestLength) {
        longest = nextDisplayValue;
        longestLength = nextLength;
      }
    }

    alignmentFactor = { ...longest };
```

Each row is formatted once, nothing is copied inside the loop, and one shallow copy (a new object with the same top level fields) is made at the end. I added a test for that as well: with four values starting at row 0, the format function is called three times, the result is `4444`, and the stored factor is its own object, not one of the rows' values. The tests passed, and eslint, prettier and the type check were clean.

fastfrwrd approved with "works well" and merged it on 3 November 2026. Thanks, fastfrwrd, for a review that taught me something.

## What I'm keeping from this one

A test that expects a wrong value is easy to miss. When a fix breaks it, don't work around it: update the test and say why in the PR.

Code that runs for every cell needs a different eye. My first fix was correct, but the reviewer was thinking about every row, and that's the habit I want.

Have you ever found a test that locked in a bug? How did you handle it in your PR?

