Bar and Pie Charts :-
BarGraphAppDelegate:----
#import <UIKit/UIKit.h>
#import "PieChart.h"
@class BarGraphViewController;
@interface BarGraphAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
PieChart *viewController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet PieChart *viewController;
@end
BarGraphAppDelegate.m:----
#import "BarGraphAppDelegate.h"
#import "BarGraphViewController.h"
#import "PieChart.h"
@implementation BarGraphAppDelegate
@synthesize window;
@synthesize viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after app launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
@end
BarGraphViewController.h :----
#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"
@interface BarGraphViewController : UIViewController<CPBarPlotDelegate,CPBarPlotDataSource,CPPlotDataSource> {
CPXYGraph *xyGraph;
//IBOutlet UIView *graph;
NSArray *xValues,*yValues;
}
@end
BarGraphViewController.m:---
#import "BarGraphViewController.h"
@implementation BarGraphViewController
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
xValues = [[NSArray alloc]initWithObjects:@"3",@"4",@"5",@"7",@"8",nil];
yValues = [[NSArray alloc]initWithObjects:@"3",@"4",@"5",@"7",@"8",nil];
xyGraph = [CPXYGraph alloc];
[xyGraph initWithFrame:CGRectMake(0, 0, 320, 480)];
CPLayerHostingView *graph = (CPLayerHostingView*)self.view;
graph.hostedLayer = xyGraph;
xyGraph.paddingTop = 20;
xyGraph.paddingBottom = 20;
xyGraph.paddingRight = 20;
xyGraph.paddingLeft = 5;
CPXYPlotSpace *plotSpace = (CPXYPlotSpace*)xyGraph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-2) length:CPDecimalFromInt(8)];
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-2) length:CPDecimalFromInt(10)];
CPXYAxisSet *xyAxis = (CPXYAxisSet*)xyGraph.axisSet;
xyAxis.xAxis.majorIntervalLength = CPDecimalFromInt(2);
xyAxis.xAxis.minorTicksPerInterval = 1;
xyAxis.xAxis.majorTickLength = 10;
xyAxis.xAxis.minorTickLength= 5;
xyAxis.yAxis.majorIntervalLength = CPDecimalFromInt(2);
xyAxis.yAxis.minorTicksPerInterval = 1;
xyAxis.yAxis.majorTickLength = 10;
xyAxis.yAxis.minorTickLength= 5;
CPBarPlot *barPlot = [CPBarPlot tubularBarPlotWithColor:[CPColor redColor] horizontalBars:NO];
barPlot.dataSource = self;
barPlot.barWidth = 10;
barPlot.barOffset = 0.25;
barPlot.baseValue = CPDecimalFromString(@"0");
barPlot.cornerRadius = 2.0f;
[xyGraph addPlot:barPlot];
}
-(CPFill *)barFillForBarPlot:(CPBarPlot *)barPlot recordIndex:(NSUInteger)index{
return nil;
}
/** @brief Gets a bar label for the given bar plot. This method is no longer used.
* @param barPlot The bar plot.
* @param index The data index of interest.
* @return The bar label for the point with the given index.
* If you return nil, the default bar label will be used. If you return an instance of NSNull,
* no label will be shown for the index in question.
* @deprecated This method has been replaced by the CPPlotDataSource <code>-dataLabelForPlot:recordIndex:</code> method and is no longer used.
**/
-(CPTextLayer *)barLabelForBarPlot:(CPBarPlot *)barPlot recordIndex:(NSUInteger)index{
CPTextLayer *textLayer = [CPTextLayer layer];
textLayer.text = [NSString stringWithFormat:@"%d",index];
CPTextStyle *style = [[CPTextStyle alloc]init];
style.fontSize = 10;
style.color = [CPColor blueColor];
textLayer.textStyle = style;
return textLayer;
}
-(NSUInteger)numberOfRecordsForPlot:(CPPlot *)plot{
return [xValues count];
}
-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index{
NSNumber *number = [NSNumber numberWithInt: [(NSString*)[xValues objectAtIndex:index] intValue]];
return number;
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
PieChart.h:---
#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"
@interface PieChart : UIViewController<CPPlotDataSource,CPPieChartDataSource> {
CPXYGraph *xyGraph;
CPPieChart *chart;
NSArray *xValues,*yValues;
}
@end
PieChart.m:---
#import "PieChart.h"
@implementation PieChart
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
xValues = [[NSArray alloc]initWithObjects:@"15",@"4",@"5",@"7",@"8",@"15",@"4",@"5",@"7",@"8",nil];
yValues = [[NSArray alloc]initWithObjects:@"3",@"4",@"5",@"7",@"8",nil];
xyGraph = [[CPXYGraph alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
CPLayerHostingView *hostingView = (CPLayerHostingView*)self.view;
hostingView.hostedLayer = xyGraph;
CPPieChart *piePlot = [[CPPieChart alloc] init];
piePlot.dataSource = self;
piePlot.pieRadius = 130.0;
piePlot.identifier = @"Pie Chart 1";
piePlot.startAngle = M_PI_4;
piePlot.sliceDirection = CPPieDirectionClockwise;
piePlot.borderLineStyle = [CPLineStyle lineStyle];
piePlot.sliceLabelOffset = -15.0;
[xyGraph addPlot:piePlot];
[piePlot release];
}
/** @brief Gets a label for the given pie chart slice. This method is no longer used.
* @param pieChart The pie chart.
* @param index The data index of interest.
* @return The pie slice label for the slice with the given index.
* If you return nil, the default pie slice label will be used. If you return an instance of NSNull,
* no label will be shown for the index in question.
* @deprecated This method has been replaced by the CPPlotDataSource <code>-dataLabelForPlot:recordIndex:</code> method and is no longer used.
**/
-(CPTextLayer *)sliceLabelForPieChart:(CPPieChart *)pieChart recordIndex:(NSUInteger)index{
return nil;
}
-(NSUInteger)numberOfRecordsForPlot:(CPPlot *)plot{
NSLog(@"numberOfRecordsForPlot");
return [xValues count];
}
-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index{
NSLog(@"numberForPlot %d",index);
NSNumber *number = [NSNumber numberWithInt: [(NSString*)[xValues objectAtIndex:index] intValue]];
return number;
}
-(CPFill *)sliceFillForPieChart:(CPPieChart *)pieChart recordIndex:(NSUInteger)index{
CPFill *fill;
switch (index) {
case 0:
fill = [[CPFill alloc]initWithColor:[CPColor redColor]];
break;
case 1:
fill = [[CPFill alloc]initWithColor:[CPColor blackColor]];
break;
case 2:
fill = [[CPFill alloc]initWithColor:[CPColor greenColor]];
break;
case 3:
fill = [[CPFill alloc]initWithColor:[CPColor blueColor]];
break;
case 4:
fill = [[CPFill alloc]initWithColor:[CPColor grayColor]];
break;
default:
fill = [[CPFill alloc]initWithColor:[CPColor grayColor]];
break;
}
return fill;
}
-(CPLayer *)dataLabelForPlot:(CPPlot *)plot recordIndex:(NSUInteger)index
{
CPTextLayer *label = [[CPTextLayer alloc] initWithText:[NSString stringWithFormat:@"%@ %lu", [xValues objectAtIndex:index]]];
label.textStyle.color = [CPColor lightGrayColor];
return [label autorelease];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
0 comments:
Post a Comment