Ever wanted to have a plot inside your iOS application?
Well CorePlot could be what you need. It provides basic plotting capabilities like scatter plots or bar plots with ability to configure the style and color of the plot and also have multiple plots included in a graph. The project is open sourced and available on Google code and since the instructions / examples currently available are not up to date with the latest version (0.2.2) we thought it would be good to share our observations.
- First thing is that the latest version now includes a pure installer. Most of the instructions you will see are currently for the “manual” install version. We were able to install using the installer so I recommend you use that (the CorePlotInstaller_0.2.2.zip file). The zip file contains Core Plot Installer.pkg (standard package installer). You simply need to run that.
- Once you have finished the install, open the project you want to include a plot to
- Add to your project’s .PCH file:
- Open Project -> Edit Project Settings and for All Configurations:
- Add to Additional SDKS: /
Note that’s the default location where the SDK is installed, if you installed somewhere else, you need to adjust that to match.
- Under Compiler Version, change your C/C++ compiler to LLVM GCC 4.2.
- Add to Other Linker Flags:
-ObjC -all_load -lCorePlot
- Add to Additional SDKS: /
- Add the QuartzCore framework to the project. Right click on Frameworks | Add > Existing Frameworks. Under iOS 4.2 SDK, select QuartzCore.framework and add that.
- You are now ready to add a CPXYGraph to your application.
Note that we completed those steps with XCode 3.2.5 against 4.2 iOS SDK.
Using Core Plot
Here are the important classes to use:
- CPXYGraph: That’s the main graph class where the plots will be written to.
- CPGraphHostingView: That’s a special view design to host CPXYGraph. One trick you will need to do is that whichever UIView you will want to have the graph in will need to be changed to a CPGraphHostingView in Interface Builder using the Identity Inspector. The class won’t show up in the list so you have to type in manually. Note this class used to be called CPLayerHostingView so old instructions may reference this.
- CPXYPlotSpace: That defines the drawing area for the plots. You can define the start position (say -10) and the length (say 20) to give you how big your plot is going to show (say from -10 to 10) and you do that for X and Y using the xRange and yRange properties.
- CPScatterPlot: That class is to create a scatter plot.
- CPBarPlot: That class is to create a bar plot.
- CPXYAxisSet: That class represents the axis in the plot. You use the properties xAxis and yAxis to decide how they are going to look like. You can set major intervals, ticks per intervals, line style, and label offset amongst other things.
- CPPlotSymbol: That class defines the style/color for the dots on the plot
To have a graph shown, you need to include one CPXYGraph in your view controller. Also CPXYGraph makes use of the CPPlotDataSource protocol so include that in your interface definition. For example:
@interface TestCorePlotViewController : UIViewController <CPPlotDataSource>
and now in your code you can instantiate your graph like so:
graph = [[CPXYGraph alloc] initWithFrame:YOURFRAME];
Once you have a graph, you can start adding plots to it. Once your graph is completed, you can assign it to the CPGraphHostingview hostedGraph property and you are all set.
The methods from the CPPlotDataSource is what is going to return the data points. You can return the number of records for each plot using the
-(NSUInteger)numberOfRecordsForPlot:(CPPlot *)plot method and the actual values for each data point using the
-(NSNumber *)numberForPlot:(CPPlot *)plot field:NSUInteger)fieldEnum recordIndex:(NSUInteger)index method.
Now most of what was learned here was obtained from the initial example obtained here
Now unfortunately that app hasn’t been updated in a while so it won’t compile out of the box. Here is the updated code: CorePlotExample
If CorePlot is too basic for you, I suggest having a look at KeepEdge or at iPhone Chart since they provide commercial plotting libraries. We have no experience with them however so we cannot provide comments.
UPDATE:: We are doing an informal poll to see the iOS verstion distribution amongst our readers. Head over to our statistics post and let us know which version you are currently using.