To create pie chart with for example 3 sectors you need to:
- Create 3 objects of NChartPieSeries. Each one will represent a sector.
- Create data source that returns array with one point for each series. Actually you can return more points for each sector and this will be displayed as concentric circles.
To create doughnut you just need to set hole ratio to whatever you want.
Here is the code snippet to create simple doughnut chart with 3 sectors:
@interface ViewController : UIViewController
@property(nonatomic, retain) NSMutableArray *brushes;
@end
@implementation ViewController
{
NChartView *m_view;
}
- (void)dealloc
{
[m_view release];
[super dealloc];
}
- (void)loadView
{
// Create a chart view that will display the chart.
m_view = [[NChartView alloc] initWithFrame:CGRectZero];
// Paste your license key here.
m_view.chart.licenseKey = @"";
// Margin to ensure some free space for the iOS status bar.
m_view.chart.polarSystem.margin = NChartMarginMake(10.0f, 10.0f, 10.0f, 20.0f);
// Create brushes.
self.brushes = [NSMutableArray array];
[self.brushes addObject:[NChartSolidColorBrush solidColorBrushWithColor:[UIColor colorWithRed:0.38f green:0.8f blue:0.91f alpha:1.0f]]];
[self.brushes addObject:[NChartSolidColorBrush solidColorBrushWithColor:[UIColor colorWithRed:0.8f green:0.86f blue:0.22f alpha:1.0f]]];
[self.brushes addObject:[NChartSolidColorBrush solidColorBrushWithColor:[UIColor colorWithRed:0.9f green:0.29f blue:0.51f alpha:1.0f]]];
for (int i = 0; i < 3; ++i)
{
// Create series that will be displayed on the chart.
NChartPieSeries *series = [NChartPieSeries series];
// Set data source for the series.
series.dataSource = self;
// Set tag of the series.
series.tag = i;
// Set brush that will fill that series with color.
series.brush = [self.brushes objectAtIndex:i % self.brushes.count];
// Add series to the chart.
[m_view.chart addSeries:series];
}
// Set hole ratio.
NChartPieSeriesSettings *settings = [NChartPieSeriesSettings seriesSettings];
settings.holeRatio = 0.1f;
[m_view.chart addSeriesSettings:settings];
// Update data in the chart.
[m_view.chart updateData];
// Set chart view to the controller.
self.view = m_view;
}
#pragma mark - NChartSeriesDataSource
- (NSArray *)seriesDataSourcePointsForSeries:(NChartSeries *)series
{
// Create one point with some data for the series.
NSMutableArray *result = [NSMutableArray array];
[result addObject:[NChartPoint pointWithState:[NChartPointState pointStateWithCircle:0 value:(rand() % 30) + 1]
forSeries:series]];
return result;
}
- (NSString *)seriesDataSourceNameForSeries:(NChartSeries *)series
{
// Get name of the series.
return [NSString stringWithFormat:NSLocalizedString(@"Series %d", nil), series.tag + 1];
}
@end
Comments
No comments yet.
Please log in to place a comment.