🏠 back to Observable

placement of diagram labels

One of the things holding me back most in making interactive diagrams is labeling points, curves, line segments, filled shapes, etc. (with labels consisting of either words or phrases, letters/symbols in the font used for mathematics in the text, mathematical expressions, etc. depending on context).

I’m probably perfectionistically overthinking it and should just try to get the simplest possible thing working and not worry whether it looks right in every possible configuration as a reader drags stuff around. But it would be great to use some kind of automatic label placement tool.

I’ve tried looking at the research literature about label placement a few times in the past, but it’s quite extensive and a bit intimidating. I haven’t seen any clear overview anywhere of what the trade-offs are involved with different techniques (e.g. speed, quality, flexibility, …).

Does anyone have any recommendation of places to look? Favorite techniques? Existing Javascript implementations? …

I’ve noticed that like myself e.g. @mike often makes interactive or animated diagrams of geometric algorithms without labels – is that a stylistic preference or is it just the most convenient choice when there’s no drop-in label library?

Maybe folks who have worked on cartography have some ideas?


If there aren’t good existing Javascript solutions I will probably spend at least a bit of effort on this at some point in the next year, as I need labeled diagrams for many of my projects and ideas. Maybe I’ll just resort to punting on doing anything fancy and just let the labels overlap if users drag labeled elements to an inconvenient configuration, we’ll see.

3 Likes

Here’s a notebook that does something similar:

1 Like

It’s a hard problem in general, but there are a few heuristic approaches that sometimes work well in practice. One is using a force layout for decollision with a bounding box constraint, such as the one implemented here. Another approach is to label some of the points using a Voronoi diagram like in the notebook in the reply above.

Automatic label placement is a relevant article on Wikipedia.

I’d be very curious to learn what other approaches people have used successfully.

3 Likes

My coworker used a greedy approach implemented here for placing labels on a contour plot. I was stuck on how to do it “right”, but he just picked a cost function out of the sky and then did a bit of bisection searching to place labels one after another. It’s not sophisticated but allowed balancing a number of things (proximity to edges, rotation from horizontal, proximity to other labels).

The main downside is preventing things from jumping around discontinuously when things are dragged, though if the function actually has a minimum and bisection finds it, maybe this would be manageable. Nevertheless, it seemed like a good example to me of a fairly ugly solution that worked perfectly well in practice.

edit: oh, also, this was a 1d problem since the labels were along the contours. that’s not so simple in 2d, though maybe a black box 2d minimizer and an arbitrary cost function could still work. the other thing we had going for us is that we didn’t have to place many labels so that the minimization didn’t have to be blazing fast.

1 Like

I don’t care too much about having things jump around occasionally, as long as they don’t glitch out and jump back and forth repeatedly.

At least some kind of real-time result (in response to dragged points) would be good, but if an algorithm starts with an instant guess based on the previous state and takes a little while to converge to its final optimized set of positions after elements are dragged around that’s okay with me.

I’ve been experimenting as well on this topic:

3 Likes

Dead links abound for research from the 90s.

Here’s a site with a bunch of hosted relevant papers, https://web.cs.du.edu/~snarayan/sada/maplabel/classes/ref.shtml

e.g. (a better copy of one of those papers) http://www.eecs.harvard.edu/shieber/Biblio/Papers/tog-final.pdf which found that simulated annealing worked best of the several algorithms they implemented.

Another meta-reference https://i11www.iti.kit.edu/~awolff/map-labeling/bibliography/sites.html and a long bibliography
https://i11www.iti.kit.edu/~awolff/map-labeling/bibliography/maplab_date.html

https://dash.harvard.edu/handle/1/2051370
http://web.archive.org/web/20110726091656/https://www.ads.tuwien.ac.at/research/labeling/
http://web.archive.org/web/20050308090004/http://wwwisg.cs.uni-magdeburg.de/~hartmann/Projects/labelling.html
http://www.dissertations.wsu.edu/Thesis/Fall2007/k_mote_111307.pdf
http://innovis.cpsc.ucalgary.ca/innovis/uploads/Courses/InformationVisualizationDetails2009/Luboschik2008.pdf
http://www.in.tum.de/fileadmin/w00bws/cg/Research/Publications/2012/Force_Labeling/2012_Vaaraniemi_Temporally_Coherent_Real-Time_Labeling_of_Dynamic_Scenes.pdf
http://www.shixialiu.com/publications/Clutter/paper.pdf
http://www.szoraster.com/Cartography/PracticalExperience.htm
https://web.cs.du.edu/~wbae/wan/research/maplabel/classes/docs/linear.pdf

5 Likes