Displaying Text Inside Pie Chart Slices Using Core Plot in iOS.

Displaying Text Inside Pie Chart Slices

In this article, we’ll explore how to display text inside each slice of a pie chart created using Core Plot. We’ll delve into the details of the Core Plot framework and provide practical examples to help you achieve your goal.

Introduction to Core Plot

Core Plot is a powerful and flexible framework for creating high-quality charts and graphs on iOS devices. It provides a comprehensive set of tools and APIs for customizing plots, including pie charts.

For this example, we’ll focus on the CPTPieChart class, which represents a pie chart plot in Core Plot. We’ll explore how to display text inside each slice of the pie chart using the labelOffset property.

Understanding the Pie Chart Data Model

Before we dive into displaying text inside the pie chart slices, let’s take a closer look at the data model used by the CPTPieChart class.

The CPTPieChart class uses an array of NSNumber objects to represent the slice values. Each NSNumber object represents a slice in the pie chart and contains the following properties:

  • The value of the slice as a double-precision floating-point number.
  • A boolean indicating whether the slice is hollow or solid.

In our example, we’ve created an array of six NSNumber objects to represent the slice values:

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];

Displaying Text Inside Pie Chart Slices

To display text inside each slice of the pie chart, we need to implement a custom label class that will handle the text rendering.

Here’s an updated version of our ViewController class with a custom label class:

#import <CorePlot/CorePlot.h>

@interface ViewController : UIViewController <CPTPieChartDelegate>

@property (nonatomic, strong) CPTGraphHostingView *hostingView;
@property (nonatomic, strong) CPTXYGraph *graph;
@property (nonatomic, strong) CPTPieChart *pieChart;
@property (nonatomic, strong) NSMutableArray *pieData;

@end

@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;

    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];
}

// Custom label class to handle text rendering
@interface CPPieLabel : UILabel

@property (nonatomic, assign) CGFloat labelOffset;

@end

@implementation CPPieLabel

@synthesize labelOffset;

- (instancetype)initWithText:(NSString *)text
                       labelOffset:(CGFloat)labelOffset {
    self = [super init];
    if (self) {
        _labelOffset = labelOffset;
    }
    return self;
}

@end

// Implement numberOfRecordsForPlot to return the number of slices in the pie chart.
- (NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
    return [self.pieData count];
}

// Implement numberForPlot:field:recordIndex to return the text label for each slice.
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
    CPPieLabel *label = [[CPPieLabel alloc] initWithText:[NSString stringWithFormat:@"%f", [self.pieData objectAtIndex:index]]
                                             labelOffset:0];
    return nil;
}

// Implement pieChart:sliceWasSelectedAtRecordIndex to handle slice selection events.
- (void)pieChart:(CPTPieChart *)plot sliceWasSelectedAtRecordIndex:(NSUInteger)index {
    NSLog(@"Slices are selected: index = %d", index);
}
@end

In this updated code, we’ve created a custom CPPieLabel class that implements the UILabel protocol. The CPPieLabel class has an additional labelOffset property that will be used to position the text label inside each slice.

We’ve also overridden the numberForPlot:field:recordIndex: method to return an instance of the CPPieLabel class for each slice in the pie chart. This allows us to customize the text rendering for each slice by setting the labelOffset property.

Conclusion

In this example, we’ve demonstrated how to display text inside each slice of a pie chart using the CPTPieChart class and a custom label class. By overriding the numberForPlot:field:recordIndex: method, we can customize the text rendering for each slice by setting the labelOffset property.

We hope this helps you understand how to display text inside your pie charts using Core Plot!


Last modified on 2024-11-02