CPTestAppBarChartController.h:--
#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"
@interface CPTestAppBarChartController : UIViewController <CPPlotDataSource>
{
CPXYGraph *barChart;
}
@end
CPTestAppBarChartController.m:-- #import "CPTestAppBarChartController.h"
@implementation CPTestAppBarChartController
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}
#pragma mark -
#pragma mark Initialization and teardown
- (void)viewDidLoad
{
[super viewDidLoad];
// Create barChart from theme
barChart = [[CPXYGraph alloc] initWithFrame:CGRectZero];
CPTheme *theme = [CPTheme themeNamed:kCPDarkGradientTheme];
[barChart applyTheme:theme];
CPLayerHostingView *hostingView = (CPLayerHostingView *)self.view;
hostingView.hostedLayer = barChart;
// Border
barChart.plotAreaFrame.borderLineStyle = nil;
barChart.plotAreaFrame.cornerRadius = 0.0f;
// Paddings
barChart.paddingLeft = 0.0f;
barChart.paddingRight = 0.0f;
barChart.paddingTop = 0.0f;
barChart.paddingBottom = 0.0f;
barChart.plotAreaFrame.paddingLeft = 70.0;
barChart.plotAreaFrame.paddingTop = 20.0;
barChart.plotAreaFrame.paddingRight = 20.0;
barChart.plotAreaFrame.paddingBottom = 80.0;
// Graph title
barChart.title = @"Graph Title";
CPTextStyle *textStyle = [CPTextStyle textStyle];
textStyle.color = [CPColor grayColor];
textStyle.fontSize = 16.0f;
barChart.titleTextStyle = textStyle;
barChart.titleDisplacement = CGPointMake(0.0f, -20.0f);
barChart.titlePlotAreaFrameAnchor = CPRectAnchorTop;
// Add plot space for horizontal bar charts
CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)barChart.defaultPlotSpace;
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0f) length:CPDecimalFromFloat(300.0f)];
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0f) length:CPDecimalFromFloat(16.0f)];
CPXYAxisSet *axisSet = (CPXYAxisSet *)barChart.axisSet;
CPXYAxis *x = axisSet.xAxis;
x.axisLineStyle = nil;
x.majorTickLineStyle = nil;
x.minorTickLineStyle = nil;
x.majorIntervalLength = CPDecimalFromString(@"5");
x.orthogonalCoordinateDecimal = CPDecimalFromString(@"0");
x.title = @"X Axis";
x.titleLocation = CPDecimalFromFloat(7.5f);
x.titleOffset = 55.0f;
// Define some custom labels for the data elements
x.labelRotation = M_PI/4;
x.labelingPolicy = CPAxisLabelingPolicyNone;
NSArray *customTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:1], [NSDecimalNumber numberWithInt:5], [NSDecimalNumber numberWithInt:10], [NSDecimalNumber numberWithInt:15], nil];
NSArray *xAxisLabels = [NSArray arrayWithObjects:@"Label A", @"Label B", @"Label C", @"Label D", @"Label E", nil];
NSUInteger labelLocation = 0;
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]];
for (NSNumber *tickLocation in customTickLocations) {
CPAxisLabel *newLabel = [[CPAxisLabel alloc] initWithText: [xAxisLabels objectAtIndex:labelLocation++] textStyle:x.labelTextStyle];
newLabel.tickLocation = [tickLocation decimalValue];
newLabel.offset = x.labelOffset + x.majorTickLength;
newLabel.rotation = M_PI/4;
[customLabels addObject:newLabel];
[newLabel release];
}
x.axisLabels = [NSSet setWithArray:customLabels];
CPXYAxis *y = axisSet.yAxis;
y.axisLineStyle = nil;
y.majorTickLineStyle = nil;
y.minorTickLineStyle = nil;
y.majorIntervalLength = CPDecimalFromString(@"50");
y.orthogonalCoordinateDecimal = CPDecimalFromString(@"0");
y.title = @"Y Axis";
y.titleOffset = 45.0f;
y.titleLocation = CPDecimalFromFloat(150.0f);
// First bar plot
CPBarPlot *barPlot = [CPBarPlot tubularBarPlotWithColor:[CPColor darkGrayColor] horizontalBars:NO];
barPlot.baseValue = CPDecimalFromString(@"0");
barPlot.dataSource = self;
barPlot.barOffset = -0.25f;
barPlot.identifier = @"Bar Plot 1";
[barChart addPlot:barPlot toPlotSpace:plotSpace];
// Second bar plot
barPlot = [CPBarPlot tubularBarPlotWithColor:[CPColor blueColor] horizontalBars:NO];
barPlot.dataSource = self;
barPlot.baseValue = CPDecimalFromString(@"0");
barPlot.barOffset = 0.25f;
barPlot.cornerRadius = 2.0f;
barPlot.identifier = @"Bar Plot 2";
[barChart addPlot:barPlot toPlotSpace:plotSpace];
#ifdef PERFORMANCE_TEST
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(changePlotRange) userInfo:nil repeats:YES];
#endif
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
#pragma mark -
#pragma mark Plot Data Source Methods
-(NSUInteger)numberOfRecordsForPlot:(CPPlot *)plot {
return 16;
}
-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
NSDecimalNumber *num = nil;
if ( [plot isKindOfClass:[CPBarPlot class]] ) {
switch ( fieldEnum ) {
case CPBarPlotFieldBarLocation:
num = (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index];
break;
case CPBarPlotFieldBarLength:
num = (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:(index+1)*(index+1)];
if ( [plot.identifier isEqual:@"Bar Plot 2"] )
num = [num decimalNumberBySubtracting:[NSDecimalNumber decimalNumberWithString:@"10"]];
break;
}
}
return num;
}
-(CPFill *) barFillForBarPlot:(CPBarPlot *)barPlot recordIndex:(NSNumber *)index;
{
return nil;
}
@end
CPTestApp_iPhoneAppDelegate.h:---
#import <UIKit/UIKit.h>
@interface CPTestApp_iPhoneAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@end
CPTestApp_iPhoneAppDelegate.m:
#import "CPTestApp_iPhoneAppDelegate.h"
@implementation CPTestApp_iPhoneAppDelegate
@synthesize window;
@synthesize tabBarController;
-(void)applicationDidFinishLaunching:(UIApplication *)application {
// Add the tab bar controller's current view as a subview of the window
[window addSubview:tabBarController.view];
}
/*
// Optional UITabBarControllerDelegate method
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
}
*/
/*
// Optional UITabBarControllerDelegate method
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed {
}
*/
-(void)dealloc {
[tabBarController release];
[window release];
[super dealloc];
}
@end
CPTestAppPieChartController.h:--
#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"
@interface CPTestAppPieChartController : UIViewController <CPPieChartDataSource, CPPieChartDelegate>
{
@private
CPXYGraph *pieChart;
NSMutableArray *dataForChart;
NSTimer *timer;
}
@property(readwrite, retain, nonatomic) NSMutableArray *dataForChart;
@property(readwrite, retain, nonatomic) NSTimer *timer;
-(void)timerFired;
@end
CPTestAppPieChartController.m:---
#import "CPTestAppPieChartController.h"
@implementation CPTestAppPieChartController
@synthesize dataForChart;
@synthesize timer;
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
CGFloat margin = pieChart.plotAreaFrame.borderLineStyle.lineWidth + 5.0;
CPPlot *piePlot = [pieChart plotWithIdentifier:@"Pie Chart 1"];
CGRect plotBounds = pieChart.plotAreaFrame.bounds;
CGFloat newRadius = MIN(plotBounds.size.width, plotBounds.size.height) / 2.0 - margin;
((CPPieChart *)piePlot).pieRadius = newRadius;
CGFloat y = 0.0;
if ( plotBounds.size.width > plotBounds.size.height ) {
y = 0.5;
}
else {
y = (newRadius + margin) / plotBounds.size.height;
}
((CPPieChart *)piePlot).centerAnchor = CGPointMake(0.5, y);
}
#pragma mark -
#pragma mark Initialization and teardown
-(void)dealloc
{
[dataForChart release];
[timer release];
[super dealloc];
}
-(void)viewDidAppear:(BOOL)animated
{
// Add some initial data
NSMutableArray *contentArray = [NSMutableArray arrayWithObjects:[NSNumber numberWithDouble:20.0], [NSNumber numberWithDouble:30.0], [NSNumber numberWithDouble:60.0], nil];
self.dataForChart = contentArray;
[self timerFired];
#ifdef MEMORY_TEST
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self
selector:@selector(timerFired) userInfo:nil repeats:YES];
#endif
}
-(void)timerFired
{
#ifdef MEMORY_TEST
static NSUInteger counter = 0;
NSLog(@"\n----------------------------\ntimerFired: %lu", counter++);
#endif
[pieChart release];
// Create pieChart from theme
pieChart = [[CPXYGraph alloc] initWithFrame:CGRectZero];
CPTheme *theme = [CPTheme themeNamed:kCPDarkGradientTheme];
[pieChart applyTheme:theme];
CPLayerHostingView *hostingView = (CPLayerHostingView *)self.view;
hostingView.hostedLayer = pieChart;
pieChart.paddingLeft = 20.0;
pieChart.paddingTop = 20.0;
pieChart.paddingRight = 20.0;
pieChart.paddingBottom = 20.0;
pieChart.axisSet = nil;
pieChart.titleTextStyle.color = [CPColor whiteColor];
pieChart.title = @"Graph Title";
// Add pie chart
CPPieChart *piePlot = [[CPPieChart alloc] init];
piePlot.dataSource = self;
piePlot.pieRadius = 131.0;
piePlot.identifier = @"Pie Chart 1";
piePlot.startAngle = M_PI_4;
piePlot.sliceDirection = CPPieDirectionCounterClockwise;
piePlot.centerAnchor = CGPointMake(0.5, 0.38);
piePlot.borderLineStyle = [CPLineStyle lineStyle];
piePlot.delegate = self;
[pieChart addPlot:piePlot];
[piePlot release];
#ifdef PERFORMANCE_TEST
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(changePlotRange) userInfo:nil repeats:YES];
#endif
}
-(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
#pragma mark -
#pragma mark Plot Data Source Methods
-(NSUInteger)numberOfRecordsForPlot:(CPPlot *)plot
{
return [self.dataForChart count];
}
-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
if ( index >= [self.dataForChart count] ) return nil;
if ( fieldEnum == CPPieChartFieldSliceWidth ) {
return [self.dataForChart objectAtIndex:index];
}
else {
return [NSNumber numberWithInt:index];
}
}
-(CPLayer *)dataLabelForPlot:(CPPlot *)plot recordIndex:(NSUInteger)index
{
CPTextLayer *label = [[CPTextLayer alloc] initWithText:[NSString stringWithFormat:@"%lu", index]];
label.textStyle.color = [CPColor lightGrayColor];
return [label autorelease];
}
/*-(CPFill *)sliceFillForPieChart:(CPPieChart *)pieChart recordIndex:(NSUInteger)index;
{
return nil;
}*/
#pragma mark -
#pragma mark Delegate Methods
-(void)pieChart:(CPPieChart *)plot sliceWasSelectedAtRecordIndex:(NSUInteger)index
{
pieChart.title = [NSString stringWithFormat:@"Selected index: %lu", index];
}
@end
CPTestAppScatterPlotController.h:---
#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"
@interface CPTestAppScatterPlotController : UIViewController <CPPlotDataSource>
{
CPXYGraph *graph;
NSMutableArray *dataForPlot;
}
@property(readwrite, retain, nonatomic) NSMutableArray *dataForPlot;
@end
CPTestAppScatterPlotController.m:----
#import "CPTestAppScatterPlotController.h"
@implementation CPTestAppScatterPlotController
@synthesize dataForPlot;
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}
#pragma mark -
#pragma mark Initialization and teardown
-(void)dealloc
{
[dataForPlot release];
[super dealloc];
}
-(void)viewDidLoad
{
[super viewDidLoad];
// Create graph from theme
graph = [[CPXYGraph alloc] initWithFrame:CGRectZero];
CPTheme *theme = [CPTheme themeNamed:kCPDarkGradientTheme];
[graph applyTheme:theme];
CPLayerHostingView *hostingView = (CPLayerHostingView *)self.view;
hostingView.hostedLayer = graph;
graph.paddingLeft = 10.0;
graph.paddingTop = 10.0;
graph.paddingRight = 10.0;
graph.paddingBottom = 10.0;
// Setup plot space
CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(1.0) length:CPDecimalFromFloat(2.5)];
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(1.0) length:CPDecimalFromFloat(3.0)];
// Axes
CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet;
CPXYAxis *x = axisSet.xAxis;
x.majorIntervalLength = CPDecimalFromString(@"0.5");
x.orthogonalCoordinateDecimal = CPDecimalFromString(@"2");
x.minorTicksPerInterval = 2;
NSArray *exclusionRanges = [NSArray arrayWithObjects:
[CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(1.99) length:CPDecimalFromFloat(0.02)],
[CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.99) length:CPDecimalFromFloat(0.02)],
[CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(2.99) length:CPDecimalFromFloat(0.02)],
nil];
x.labelExclusionRanges = exclusionRanges;
CPXYAxis *y = axisSet.yAxis;
y.majorIntervalLength = CPDecimalFromString(@"0.5");
y.minorTicksPerInterval = 5;
y.orthogonalCoordinateDecimal = CPDecimalFromString(@"2");
exclusionRanges = [NSArray arrayWithObjects:
[CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(1.99) length:CPDecimalFromFloat(0.02)],
[CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.99) length:CPDecimalFromFloat(0.02)],
[CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(3.99) length:CPDecimalFromFloat(0.02)],
nil];
y.labelExclusionRanges = exclusionRanges;
// Create a blue plot area
CPScatterPlot *boundLinePlot = [[[CPScatterPlot alloc] init] autorelease];
boundLinePlot.identifier = @"Blue Plot";
boundLinePlot.dataLineStyle.miterLimit = 1.0f;
boundLinePlot.dataLineStyle.lineWidth = 3.0f;
boundLinePlot.dataLineStyle.lineColor = [CPColor blueColor];
boundLinePlot.dataSource = self;
[graph addPlot:boundLinePlot];
// Do a blue gradient
CPColor *areaColor1 = [CPColor colorWithComponentRed:0.3 green:0.3 blue:1.0 alpha:0.8];
CPGradient *areaGradient1 = [CPGradient gradientWithBeginningColor:areaColor1 endingColor:[CPColor clearColor]];
areaGradient1.angle = -90.0f;
CPFill *areaGradientFill = [CPFill fillWithGradient:areaGradient1];
boundLinePlot.areaFill = areaGradientFill;
boundLinePlot.areaBaseValue = [[NSDecimalNumber zero] decimalValue];
// Add plot symbols
CPLineStyle *symbolLineStyle = [CPLineStyle lineStyle];
symbolLineStyle.lineColor = [CPColor blackColor];
CPPlotSymbol *plotSymbol = [CPPlotSymbol ellipsePlotSymbol];
plotSymbol.fill = [CPFill fillWithColor:[CPColor blueColor]];
plotSymbol.lineStyle = symbolLineStyle;
plotSymbol.size = CGSizeMake(10.0, 10.0);
boundLinePlot.plotSymbol = plotSymbol;
// Create a green plot area
CPScatterPlot *dataSourceLinePlot = [[[CPScatterPlot alloc] init] autorelease];
dataSourceLinePlot.identifier = @"Green Plot";
dataSourceLinePlot.dataLineStyle.lineWidth = 3.f;
dataSourceLinePlot.dataLineStyle.lineColor = [CPColor greenColor];
dataSourceLinePlot.dataLineStyle.dashPattern = [NSArray arrayWithObjects:[NSNumber numberWithFloat:5.0f], [NSNumber numberWithFloat:5.0f], nil];
dataSourceLinePlot.dataSource = self;
// Put an area gradient under the plot above
CPColor *areaColor = [CPColor colorWithComponentRed:0.3 green:1.0 blue:0.3 alpha:0.8];
CPGradient *areaGradient = [CPGradient gradientWithBeginningColor:areaColor endingColor:[CPColor clearColor]];
areaGradient.angle = -90.0f;
areaGradientFill = [CPFill fillWithGradient:areaGradient];
dataSourceLinePlot.areaFill = areaGradientFill;
dataSourceLinePlot.areaBaseValue = CPDecimalFromString(@"1.75");
// Animate in the new plot, as an example
dataSourceLinePlot.opacity = 0.0f;
[graph addPlot:dataSourceLinePlot];
CABasicAnimation *fadeInAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeInAnimation.duration = 1.0f;
fadeInAnimation.removedOnCompletion = NO;
fadeInAnimation.fillMode = kCAFillModeForwards;
fadeInAnimation.toValue = [NSNumber numberWithFloat:1.0];
[dataSourceLinePlot addAnimation:fadeInAnimation forKey:@"animateOpacity"];
// Add some initial data
NSMutableArray *contentArray = [NSMutableArray arrayWithCapacity:100];
NSUInteger i;
for ( i = 0; i < 60; i++ ) {
id x = [NSNumber numberWithFloat:1+i*0.05];
id y = [NSNumber numberWithFloat:1.2*rand()/(float)RAND_MAX + 1.2];
[contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x, @"x", y, @"y", nil]];
}
self.dataForPlot = contentArray;
#ifdef PERFORMANCE_TEST
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(changePlotRange) userInfo:nil repeats:YES];
#endif
}
-(void)changePlotRange
{
// Setup plot space
CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0) length:CPDecimalFromFloat(3.0 + 2.0*rand()/RAND_MAX)];
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0) length:CPDecimalFromFloat(3.0 + 2.0*rand()/RAND_MAX)];
}
#pragma mark -
#pragma mark Plot Data Source Methods
-(NSUInteger)numberOfRecordsForPlot:(CPPlot *)plot {
return [dataForPlot count];
}
-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
NSNumber *num = [[dataForPlot objectAtIndex:index] valueForKey:(fieldEnum == CPScatterPlotFieldX ? @"x" : @"y")];
// Green plot gets shifted above the blue
if ([(NSString *)plot.identifier isEqualToString:@"Green Plot"])
{
if ( fieldEnum == CPScatterPlotFieldY )
num = [NSNumber numberWithDouble:[num doubleValue] + 1.0];
}
return num;
}
@end
MyController.h:---
#import <Foundation/Foundation.h>
@interface MyController : UIViewController {
IBOutlet UIButton *btn;
}
@end
MyController.m:---
#import "MyController.h"
@implementation MyController
@end
0 comments:
Post a Comment