Inspiration
Grafana is an open source dashboard tool, and its tables can show numbers as gauge cells: a bar plus the value's text in a fixed width cell. In issue #113304, seriych showed a 150 pixel wide gauge column with 1, 8.88, 9 and 9.22 where 9 had the widest bar, and a second case where the same values sorted two ways gave different, wrong bars. A later comment reported a similar mismatch in v12.3.3.
What it does
The merged change, grafana/grafana#136236, fixes how TableNG, Grafana's newer table, finds a column's alignment factor: the longest formatted value, which sets how much room every gauge cell keeps for its text. The scan over the next 1000 rows now keeps the longest value, prefix and suffix included, so every cell reserves the same room and the bars line up with their values.
How I built it
Reading getAlignmentFactor showed two problems in the scan: the length comparison was inverted (> instead of <), and the loop wrote the current row's own text back instead of adopting the longer value. So the first row drawn decided the room for the whole column. Rows drawn later could still widen it through a second branch, which explains why sort order changed the bars.
Commit one: < instead of >, and the longer display value is taken whole. Fields without a display function now pass { text: ... } instead of the bare value. On the test side, two existing tests that expected '1' for [1, 22, 333, 4444] now expect '4444', a placeholder it.todo for the wrong line was removed, and a new case covers [1, 8.88, 9, 9.22] with a ms suffix. Three getAlignmentFactor tests failed on the old code; all five passed with the fix.
Challenges I ran into
Performance in code that runs for every cell. fastfrwrd requested changes: the fix looked correct, but it copied every new longest value, up to 999 objects per scan that would become garbage, and formatted the current leader again on each step. In the second commit, the loop compares cached lengths and only remembers the leader; one copy is made after the loop ends:
alignmentFactor = { ...longest };
A new test confirms each row is displayed once and the stored factor is its own object. Lint, formatting and the grafana-ui type check all came back clean.
Accomplishments that I'm proud of
A two commit fix that corrects the gauges and does less work than the first version. Merged on 3 November 2026 by fastfrwrd, whose approval said it "works well".
What I learned
Existing tests can encode a bug, so a fix that breaks them deserves a careful look rather than a workaround. And code that runs per cell should be reviewed for what each iteration allocates and repeats, not only for its result.
What's next
The PR left the other branch of getAlignmentFactor, the one used once a column already has a factor, as it was. If gauge bars ever look off again, that branch is where I'd look first.
The full story, with all the code, is on my blog: https://ashokramsi.hashnode.dev/grafana-tableng-alignment
Built With
- github
- grafana
- react
- typescript