Using Core Plot in iPhone/iPad app

Using Core Plot in iPhone/iPad app

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:

You made it all the way down here so you must have enjoyed this post! You may also like:

How We Built Sheeel iOS Application Dino Balen
Dino Balen, | 4

How We Built Sheeel iOS Application

How to add a property via class category? Ivan Kalaica
Ivan Kalaica, | 4

How to add a property via class category?

Consuming SOAP web services in iOS Ivan Kalaica
Ivan Kalaica, | 8

Consuming SOAP web services in iOS

1 comment

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.