BNPieChart

This post introduces a new open-source iOS pie chart class, BNPieChart.  It’s designed to draw beautiful, scalable pie charts with a dead-simple interface.

To get the class, just click the Downloads button from the moriarty github page.

This chart:

is produced with this code:

BNPieChart *chart = [[[BNPieChart alloc] initWithFrame:frame] autorelease];
[chart addSlicePortion:0.1 withName:@"Orange"];
[chart addSlicePortion:0.2 withName:@"Fandango"];
[chart addSlicePortion:0.1 withName:@"Blue"];
[chart addSlicePortion:0.1 withName:@"Cerulean"];
[chart addSlicePortion:0.3 withName:@"Green"];
[chart addSlicePortion:0.1 withName:@"Yellow"];
[chart addSlicePortion:0.1 withName:@"Pink"];

BNPieChart is a UIView subclass, and you include it in your view hierarchy just like any other view. Note that the actual colors are chosen for you – in the example I’ve named the colors only for the sake of drawing labels. If you exclude labels, the chart knows to use the extra space and fills up most of its frame.

BNPieChart is also designed to display well at much smaller sizes. For example, the exact same code, within a frame size of 140×100, appears like this:

The chart is also drawn in a transparency-friendly way, so that you can display it above images or other views. For example:

If you want to avoid transparency, just set

myPieChart.backgroundColor = [UIColor whiteColor];

similar to any other UIView.

As with all code posted here and in the moriarty library, this class is free to use under the Apache 2 license, and I welcome contributions on the github repo.

(PS Fandango is the name of a dance, but also of a color in the fuchsia range, in case you were curious about that.)

22 Comments