views:

72

answers:

3

Hi all, I am building a custom chart library for my company. I'm having troubles finding / inventing a good algorithm to place the chart labels so that the following requirements are met:

  • Labels should not overlap
  • Labels should remain as close as possible to the related slice
  • If a label must be moved away from its slice, it should preferably go in radial, outwards direction.

To be clear, this is rather good in my book:

http://betterdashboards.files.wordpress.com/2009/02/piechartpercentagelabel1a.png

Thanks in advance!

A: 

In the sample image, there is no collision test (ie. labels may overlap). Thus, the algorithm is quite easy:

  • There is a first line which starts from the center of a part of a pie in the direction opposite to the pie center. This first line has a fixed length.
  • The second line is horizontal (fixed length too), and goes left or right according to the angle of the first line,
  • Follows the text which is aligned left or right, according to the angle of the first line too.

You may use the same algorithm in your application, since it seems the most harmonious one. But text may overlap if you have several pie parts which are too close.

To avoid this, you may test for collisions, ie. see if two horizontal lines are too close, and then change the angle of the first line (or probably the angles of the first lines in two nearest pie parts), until the horizontal lines are far enough.

MainMa
+2  A: 

I assume you are using the MS .net stack. If thats the case rather than reinventing the wheel, you might use the MS Charts library provided by the Microsoft. As such there is nothing wrong in creating the library but with the edge case like this it will take some time to make it perfect, while the ms chart library is more or less stable and works pretty well.

http://www.microsoft.com/downloads/details.aspx?FamilyID=130f7986-bf49-4fe5-9ca8-910ae6ea442c&displaylang=en

The pie charts generated by it are pretty good. For your labeling issue it has some option called smart label, which arranges the label pretty neatly , with out any overlapping.

P.S. In the downloads you will find all the samples.

Biswanath
A: 

Biswanath, thanks for the suggestion but we had some very specific requirements which the MS charts couldn't solve, so we went ahead and built our own. This is why I am asking for an algorithm. Re. the picture, well I didn't choose a good one, I guess. I can already deal with simple cases, the problem arises when there are too many slices, too near to each other. For that, I'll need something more sophisticated than MainMa's solution :)

Caffenero