how to display the texts inside the piechat each slices

Go To


I have implemented a pie chart using the core plot framework, and I need to know how to display text inside each of the slices.

Here is the .m source code:

@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], 

    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);
2012-04-04 06:46
by user1298307


Use the labelOffset property. A positive value will position the labels outside the plot and a negative value will position them inside.

2012-04-04 23:31
by Eric Skroch