Image
Code

Experimental Label Placement

Curran Kelleher

Last edited May 07, 2024
Created on Apr 28, 2021

An experiment in label placement based on Mike Bostock's US Population by State viz.

Unfortunately this technique doesn't really pan out. The technique here finds the point at which the distance between the top and the bottom of the area is greatest and uses that as the center point for the text label. However, this is not the ideal place for the center of the label. The ideal place for the center of the label is the point at which the area is "widest" along the direction of its center line. This is not a straightforward calculation of top - bottom at the same X coordinate, but rather is a more nuanced calculation that needs to be based on the angle of the center line.

Ideally we could borrow some techniques from computer graphics and do a "ray tracing" of sorts. We could compute, for each point along the center line, a vector that is orthogonal to the center line (90 degree angle), and see where it intersects the edge of the area. We could do this for both the top of the center line and bottom, then use the distance between the two intersection points as the "gap", and choose the largest gap as the place for the center of the label.

I'm not even sure the center line of (top + bottom) / 2 is the "correct" line along which to place the text. A medial axis might be the better shape, as it optimizes for distance to the edges of the area in all directions, rather than just vertically.

Also, the fixed Y offset does not play out well here. The Y offset should, again, be relative to the angle of the center line, not just vertical.

Additionally, this technique does not at all take into account the length of the text. An ideal technique would use the length of the text as well, such that the text would be optimized to fit in the largest part of the area that accommodates all of it. Otherwise we end up with parts of the text that may flow into narrow parts of the areas and not be contained within the bounds of the area.

This feels like an uncharted space in computational geometry. Maybe there is a solution out there yet to be discovered!

ISC Licensed