First published on Homingo Learn .
A label that fits today can land on its neighbor tomorrow when someone renames a metric or adds a series, and you may not notice until the chart is on screen. You can flag likely collisions without rendering by estimating a box for each label and checking the boxes against each other.
The technique
Each label's box comes from arithmetic on its text:
Width is len(text) * size * 0.6 , which assumes an average character is 0.6 em wide.
Height is size * 1.2 , which assumes a line height of 1.2 em.
Both constants are assumptions, and the program never compares them with rendered text.
To find collisions, sort the labels by left edge and walk through them, keeping an active list of boxes that may still overlap something.…