Using Core Plot in iPhone/iPad app

Featured Image

Core Plot is a plotting framework for iOS and OS X. You can download the latest zip file from here. I’m using CorePlot_1.0. After unzipping it go to CorePlot_1.0/Binaries/iOS, there you can find CorePlotHeaders folder and libCorePlot-CocoaTouch.a, add them to your project. Add QuartzCore.framework, go to Build Settings and add -ObjC -all_load to Other Linker Flags.

Next step is to define view in which your graph will be presented and define data which will be shown. Your views class must be CPTGraphHostingView. In my case this is in MBReportViewController.nib. Now create MBBarPlot class. It’s a subclass of NSObject.


#import <Foundation/Foundation.h>
#import "CorePlot-CocoaTouch.h"
@interface MBBarPlot : NSObject <CPTPlotDataSource>
@property (nonatomic, retain) CPTGraphHostingView *hostingView;
@property (nonatomic, retain) CPTXYGraph *graph;
@property (nonatomic, retain) NSMutableArray *graphData;
@property (nonatomic, retain) INReport *currentReport;
-(id)initWithHostingView:(CPTGraphHostingView *)hostingView andReport:(INReport*)aReport;


#import "MBBarPlot.h"
#import "INReport.h"
@implementation MBSimplePlot
@synthesize hostingView;
@synthesize graph;
@synthesize graphData;
@synthesize currentReport;
- (id)initWithHostingView:(CPTGraphHostingView *)hostingView andReport:(INReport*)aReport
self = [super init];
if ( self != nil )
self.hostingView = hostingView;
self.graph = nil;
self.graphData = [aReport reportsXYItemEntity];
self.currentReport = aReport;
return self;
- (void)plotSetups
if ( (self.hostingView == nil) || (self.graphData == nil) || (self.graph == nil) )
self.graph = [[CPTXYGraph alloc] initWithFrame:[self.hostingView bounds]] ;
self.hostingView.hostedGraph = self.graph;
// here yo can set graph elements:
// paddings
// line styles
// text style
// min and max values on axis
// title
// axis labels ...
CPTBarPlot *plot = [[CPTBarPlot alloc] init] ;
plot.delegate = self;
plot.dataSource = self;
[self.graph addPlot:plot];
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
return [self.graphData count];
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
if ( fieldEnum == CPTBarPlotFieldBarLocation )
return [NSNumber numberWithFloat:index*10];
return [NSNumber numberWithFloat:[[[self.graphData objectAtIndex:index] y] floatValue]];

In MBReportViewController you need to create instance of your chart with initWithHostingView:andReport: after that call plotSetups where you can set plot frame, plot space, axis set, line style, text style, plot type, legend etc. With Core Plot you can choose various plot types and easily adjust them to fit your needs. Here are the result:


Care to rate this post?


Ana Gabric

Ex. Inchooer

Ana Gabric is an iPhone/iPad developer. She enjoys creating front-end side of iPhone applications.

Other posts from this author

Discussion 1 Comment

Add Comment
  1. julis

    where can i find INReport class file?

Add Your Comment

Please wrap all source codes with [code][/code] tags.