+1  A: 

I'd probably place one label, then place the next, check if it overlaps the first, and if so flip it, if that doesn't work, nudge it upwards until it doesn't overlap anymore... as a starting point anyway. Maybe add a cost function as a distance from the ideal or default placement (if there were no other labels to get in the way), which is to be minimized. Then find the lowest-cost arrangement of labels. Can give flipping, and movement, and rotation, abbreviation, and dropping each a different cost.

Mark
That's a good idea, but what happens if the first choice isn't good and messes up the next several? For example, what if the first choice from left to right is to place a right-going label that covers up most of a valley and makes it impossible to place labels with long leaders coming up out of the valley for points that are down in the valley?
Doug McClean
Well, then your overall cost would be higher because each subsequent label would have to be moved a greater distance. So I guess you *could* try every possible ordering of labels and then take the lowest cost one... this of course is highly inefficient, but perhaps you can come up with some heuristics to minimize the number of possibilties that need to be examined.
Mark
+2  A: 

Here's my take:

  1. Mark the points where you want to put labels
  2. Split them into groups with the distance of at least 2*size between them
  3. For each group, go from the right an try to put a label.
  4. Try to put label to the right or to the left. See which results in lower length of the vertical line
  5. If doesn't matter, try to put to the right
  6. Unless it's the end of the group, then try to put it to the left

Now go again over the labels and see if any can be flipped from one side to another while shortening the length of vertical lines.

Should result in a decent output, in my opinion.

ilya n.
+1  A: 

This question reminds me of a graphics project I did not too long ago. It was to draw a mathematical graph (as in nodes and edges) in the most pleasing way possible. There are several approaches but my favorite by far was the physics approach. You treat each node as a charged particle that repels all others and each edge as a classical spring with some ideal length. You run a few hundred time steps and eventually get to a stable state with an appropriate damping effect.

I see many parallels with your problem. The text boxes are the nodes and the leader lines are the edges.

It will have to be modified. For example there should be a positive force upward so they don't go below the graph. You'd also have to incorporate the idea of flipping the text to the left or right. But it should give a reasonable result on most inputs.

The article I referenced for my project was here.

colithium
Another good plan, I had heard of this approach before but it never crossed my mind thinking about this project. Thanks!
Doug McClean