Working with CorePlot – Tuts+ Premium
plus

Working with CorePlot – Tuts+ Premium

Tutorial Details
  • Technology: iOS SDK
  • Difficulty: Intermediate

When working with data intensive applications, a developer must often do more than just show lists of data records in a table view. The CorePlot library will allow you to add stunning data visualizations to your applications. Find out how in this Tuts+ Premium series!


Tutorial Teaser

Where We Left Off

Last time we got into how to create a bar chart, how to customize the bar colors and how to add custom labels to our axis if we want to display custom text instead of just numbers.

What We’ll Cover Today

This time we’ll cover how to abstract the logic out of our controller into a more reusable data source object. Once we’ve mastered this, we’ll go over how to display the same data as the bar chart, except this time displaying it as a pie chart!

Step 1: Setting Up

Before we get into abstracting the data logic we’re going to create our base classes for the pie chart. Just like last time, we’re going to need to create a new view controller for the graph. Let’s call it ‘STPieGraphViewController’.

Notice that we don’t need to create a view this time because we will be able to use ‘STGraphView’. Before we start setting things up let’s jump into STStudentListViewController.h and import STPieGraphViewController.h. We also need to conform to the protocol STPieGraphViewControllerDelegate (which we will create later):

@interface STStudentListViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, AddStudentViewControllerDelegate, UIActionSheetDelegate, STLineGraphViewControllerDelegate, STBarGraphViewControllerDelegate, STPieGraphViewControllerDelegate>

Switch over to the .m file. We need to add a button to the action sheet. Locate the graphButtonWasSelected: method. We’re going to edit the second button text and add a third one:

- (void)graphButtonWasSelected:(id)sender
{
    UIActionSheet *graphSelectionActionSheet = [[[UIActionSheet alloc] initWithTitle:@"Choose a graph" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Enrolment over time", @"Subject totals - Bar", @"Subject totals - Pie", nil] autorelease];
    [graphSelectionActionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
}

Now jump into the actionSheet:clickedButtonAtIndex: method and add a clause for buttonIndex == 2:

else if (buttonIndex == 2)
{
    STPieGraphViewController *graphVC = [[STPieGraphViewController alloc] init];
    [graphVC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [graphVC setModalPresentationStyle:UIModalPresentationFullScreen];
    [graphVC setDelegate:self];
    [graphVC setManagedObjectContext:[self managedObjectContext]];
    [self presentModalViewController:graphVC animated:YES];
    [graphVC release];
}

Just like last time, it will show some warnings because STPieGraphViewController doesn’t have a delegate or managedObjectContext property.


Get the Full Series!

This tutorial series is available to Tuts+ Premium members only. Read a preview of this tutorial on the Tuts+ Premium web site or login to Tuts+ Premium to access the full content.


Joining Tuts+ Premium. . .

For those unfamiliar, the family of Tuts+ sites runs a premium membership service called Tuts+ Premium. For $19 per month, you gain access to exclusive premium tutorials, screencasts, and freebies from Mobiletuts+, Nettuts+, Aetuts+, Audiotuts+, Vectortuts+, and CgTuts+. You’ll learn from some of the best minds in the business. Become a premium member to access this tutorial, as well as hundreds of other advanced tutorials and screencasts.

Tags: Premium
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more