Am a newbie to iOS programming and to Core Plot. Am using XCode 4.2 and iOS 5, and testing the iPhone App using iOS Simulator.
I know am overlooking something here (breaking my head on this for more than a day & tried google’ing a lot & tried out various possible solutions, all in vain). Can someone please help or any pointers?
Am trying to do something very simple -> adding a pie chart (done using Core Plot) as a subview. Initially, I was displaying the pie chart as a modal view & it worked fine. Now, in the same view I want to add few buttons and so I created a view (which will get displayed on pressing a button) in which I added the 2 buttons and also tried to add this pie chart too. The buttons display fine but the pie chart is not to be seen!
This is the XIB file of the view called ‘GraphView’ which gets displayed when a button’s pressed:

In the above screenshot, the highlighted view ‘View’ is a UIView object within which I want to display the piechart. The entire view ‘GraphView’ gets displayed fine; the two buttons appear, but the piechart that I added to the subview doesn’t. The UIView object that I added as subview in the above figure also gets displayed fine (I checked it by setting a background color for it). This is the code:
GRAPHVIEW.H
#import <UIKit/UIKit.h>
#import "PieChartView.h"
@interface GraphView : UIViewController
@property (strong, nonatomic) PieChartView *pieChartView;
// gView is the view which is linked to the 'UIView' subview in IB
// in the above figure
@property (strong, nonatomic) IBOutlet UIView *gView;
@property (strong, nonatomic) IBOutlet UIButton *buttonBack;
@property (strong, nonatomic) IBOutlet UIButton *buttonMail;
-(void) setHistoryData:(NSArray *)history;
...
...
@end
GRAPHVIEW.M
...
...
@synthesize gView;
@synthesize buttonBack;
@synthesize buttonMail;
...
...
-(void) setHistoryData:(NSArray *)history {
NSLog(@"GraphView: setHistoryData");
[pieChartView setHistoryData:history];
[gView addSubview:pieChartView];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
pieChartView = [[PieChartView alloc] init];
return self;
}
...
...
#pragma mark – View lifecycle
- (void)viewDidLoad
{
NSLog(@"GraphView: viewDidLoad");
[super viewDidLoad];
[pieChartView initializeGraph];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
PIECHARTVIEW.H
This is the one that draws (that which is ‘supposed to’ draw) the pie chart!
#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"
// I tied subclassing 'UIViewController' or just 'UIView'...all in vain
@interface PieChartView : CPTGraphHostingView <CPTPlotDataSource> {
CPTXYGraph *graph;
CPTPieChart *pieChart;
}
@property (nonatomic, retain) CPTXYGraph *graph;
-(void) initializeGraph;
-(void) initializePieChart;
-(void) setHistoryData:(NSArray *)history;
@end
PIECHARTVIEW.m
...
@implementation PieChartView
@synthesize graph;
NSArray *historyData;
// I added the NSLog to see if these get called, but they don't seem to get called!
// So, this means that the pie chart is not being drawn actually!
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
NSLog(@"numberOfRecordsForPlot: History count: %d", (int)[historyData count]);
return [historyData count];
}
// This too isn't getting called
-(NSNumber *) numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
NSLog(@"historyView: numberForPlot");
return [historyData objectAtIndex:index];
}
// This too is not getting called! OMG!
-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index {
NSLog(@"HistoryView: dataLabelForPlot");
...
}
// This method is getting called. Am seeing the log messages
-(void) initializeGraph {
NSLog(@"HistoryView: initializeGraph");
graph = [[CPTXYGraph alloc] init];
CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme];
[graph applyTheme:theme];
CPTGraphHostingView *hostingView = (CPTGraphHostingView *) self;
hostingView.hostedGraph = graph;
//hostingView.bounds = CGRectMake(5, 5, 70, 70);
[self initializePieChart];
}
// This method is also getting called. I see the log messages from this method too
/**
* Initialize the pie chart for display
*/
-(void) initializePieChart {
NSLog(@"HistoryView: initializePieChart");
pieChart = [[CPTPieChart alloc] init];
pieChart.dataSource = self;
pieChart.pieRadius = 100.0;
pieChart.opaque = FALSE;
//pieChart.pieRadius = 60;
pieChart.shadowOffset = CGSizeMake(-1, 1);
pieChart.identifier = @"PieChart";
pieChart.startAngle = M_PI_4;
pieChart.sliceDirection = CPTPieDirectionCounterClockwise;
pieChart.labelOffset = -0.6;
[graph addPlot:pieChart];
NSLog(@"added pie chart to the graph view");
}
// This also is getting called
-(void) setHistoryData:(NSArray *)history {
NSLog(@"HistoryView: setHistoryData");
historyData = history;
}
...
...
@end
You don’t need the
gViewproperty. Remove it and change thepieChartViewdeclaration to:Link the custom view to this property instead of
gViewand change the class in IB toPieChartView. Also remove the initialization of this property from-initWithNibName:bundle:—the view will be initialized automatically when the nib is loaded.