9.22 is bigger than 9. A Grafana table gauge disagreed and gave 9 the longest bar in the column.
seriych filed it as issue #113304. My fix, grafana/grafana#136236, came down to one backwards comparison, plus a review lesson about code that runs for every single cell.
What a gauge cell is
Grafana is an open source dashboard tool. In its tables, a column can show each number as a gauge: a horizontal bar plus the number as text, squeezed into a cell of fixed width. The trick that keeps bars honest: each cell holds back the same width for text, sized to fit the longest value, and the bar takes whatever is left. Grafana calls that value the alignment factor.
If each cell keeps a different amount of text space, it's like comparing people's heights while each one stands on a box of a different size. The lengths stop meaning anything.
The bug
When a column has no factor yet, getAlignmentFactor scans up to the next 1000 rows for the longest formatted value (the text as shown, suffix included). The heart of the old loop, trimmed:
if (formattedValueToString(alignmentFactor).length > formattedValueToString(nextDisplayValue).length) {
alignmentFactor.text = displayValue.text;
}
Spot it? The > runs the body when the next value is shorter. And the body writes back the text that was already there. So the scan never adopts a longer value, and whichever row is drawn first sets the space for everyone. In the issue, that was 1: one character.
A second branch let rows drawn afterwards grow the factor, so each cell's text space depended on its draw timing. That's why sorting the same values differently changed the bars.
Bonus find: two existing tests expected '1' as the factor for [1, 22, 333, 4444]. The bug was written into the tests.
Fix, take one
Flip the comparison to < and adopt the longer value whole with { ...nextDisplayValue }, so a prefix or suffix like ms counts toward its length. I updated the two tests to expect '4444' and added one with [1, 8.88, 9, 9.22] plus a suffix. On the old code three of the factor tests went red, and with the fix all five were green.
Fix, take two
fastfrwrd requested changes: the fix looked correct, but every new leader got its own copied object. That's up to 999 copies per scan, all garbage except the last, in a part of the table where avoiding that is critical for responsiveness. The current leader was also being formatted again on every step.
The suggestion was to hold on to the longest value and its length, compare plain numbers in the loop, and copy once at the end. The merged loop, trimmed:
if (nextLength > longestLength) {
longest = nextDisplayValue;
longestLength = nextLength;
}
}
alignmentFactor = { ...longest };
I also added a test that counts the formatting calls, one per row, and makes sure the stored factor is a separate object. It went in on 3 November 2026, with a short "works well" from fastfrwrd.
The lesson
For code that runs once per cell, a correct answer is only half the review. The other half is counting what each trip through the loop creates and redoes.
Have you had a review where your fix was right, but the reviewer asked for a cheaper version? What did you change?
The full story, with all the code, is on my blog: https://ashokramsi.hashnode.dev/grafana-tableng-alignment
Top comments (0)