コアプロットフレームワークを使用して円グラフを実装しました。各スライス内にテキストを表示する方法を知る必要があります。
.mソースコードは次のとおりです。
@implementation ViewController
@synthesize graph,pieData;
// Implement loadView to create a view hierarchy programmatically
// without using a nib.
- (void)loadView {
CPTGraphHostingView * newView = [[CPTGraphHostingView alloc]initWithFrame:
[[UIScreen mainScreen] applicationFrame]];
self.view = newView;
[newView release];
}
// Implement viewDidLoad to do additional setup after loading the view,
// typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
graph = [[CPTXYGraph alloc] initWithFrame: self.view.bounds];
CPTGraphHostingView *hostingView = (CPTGraphHostingView *)self.view;
hostingView.hostedGraph = graph;
CPTPieChart *pieChart = [[CPTPieChart alloc] init];
pieChart.dataSource = self;
pieChart.delegate = self;
pieChart.pieRadius = 100.0;
pieChart.identifier = @"PieChart1";
pieChart.startAngle = M_PI_4;
//pieChart.sliceDirection = CPTPieDirectionCounterClockwise;
pieChart.sliceDirection = CPTPieDirectionClockwise;
self.pieData= [NSMutableArray arrayWithObjects:
[NSNumber numberWithDouble:90.0],
[NSNumber numberWithDouble:20.0],
[NSNumber numberWithDouble:30.0],
[NSNumber numberWithDouble:40.0],
[NSNumber numberWithDouble:50.0],
[NSNumber numberWithDouble:60.0],
nil];
CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme];
[graph applyTheme:theme];
[graph addPlot:pieChart];
[pieChart release];
}
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
return [self.pieData count];
}
-(NSNumber *)numberForPlot:(CPTPlot *)plot
field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
return [self.pieData objectAtIndex:index];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
[pieData release];
[graph release];
[super dealloc];
}
- (void)pieChart:(CPTPieChart *)plot
sliceWasSelectedAtRecordIndex:(NSUInteger)index {
NSLog(@"Slices are selected: index = %d", index);
}
@end