Menu

Catch Overlapping Labels Before Rendering With a Sweep Over X
📰

Catch Overlapping Labels Before Rendering With a Sweep Over X

DEV Community: python·Mohd Yusuf Khan·4 days ago
#Tlu5AQvx
#devtopython#devto#dev#label#labels
Reading 0:00
15s threshold

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.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.