Ever wanted to have a plot inside your iOS application?
Core Plot Example Screenshot
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.

Preparation

  1. 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.
  2. Once you have finished the install, open the project you want to include a plot to
  3. Add to your project’s .PCH file: #import <CorePlot/CorePlot.h>
  4. Open Project -> Edit Project Settings and for All Configurations:
    1. Add to Additional SDKS: /Library/SDKs/CorePlotSDK/${PLATFORM_NAME}.sdkNote that’s the default location where the SDK is installed, if you installed somewhere else, you need to adjust that to match.
    2. Under Compiler Version, change your C/C++ compiler to LLVM GCC 4.2.
    3. Add to Other Linker Flags:-ObjC -all_load -lCorePlot
  5. 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.
  6. 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:

  1. CPXYGraph: That’s the main graph class where the plots will be written to.
  2. 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.
  3. 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.
  4. CPScatterPlot: That class is to create a scatter plot.
  5. CPBarPlot: That class is to create a bar plot.
  6. 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.
  7. CPPlotSymbol: That class defines the style/color for the dots on the plot

Basic Concepts:
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>
{
CPXYGraph *graph;

}
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.

[siteorigin_widget class=”SiteOrigin_Widget_Image_Widget”][/siteorigin_widget]
[siteorigin_widget class=”SiteOrigin_Widget_Image_Widget”][/siteorigin_widget]
How to Use Core Plot for iOS