i have a have graphview.m which creates a coreplotviewcontroller.view in this way
CorePlotViewController *aCorePlotViewController = [[CorePlotViewController alloc] initWithNibName:@"CorePlotViewController" bundle:nil];
aCorePlotViewController.view.bounds = CGRectMake(0,0,896,605);
aCorePlotViewController.view.center = CGPointMake(576, 374.5);
[aCorePlotViewController.view setTag:99];
[self.view addSubview:aCorePlotViewController.view];
[aCorePlotViewController.view release];
right now im refreshing the graph by removing and adding it again.
[[self.view viewWithTag:99]removeFromSuperview];
CorePlotViewController *aCPView = [[CorePlotViewController alloc] initWithNibName:@"CorePlotViewController" bundle:nil];
aCPView.view.bounds = CGRectMake(0,0,896,605);
aCPView.view.center = CGPointMake(576, 374.5);
[aCPView.view setTag:99];
[self.view addSubview:aCPView.view];
[aCPView release];
But this would take up alot of memory and eventually i wouldn't be able to allocate anymore memory to CoreAnimation (im getting this error logged).
So how do i recreate the graph again? presumingly using creating a method in coreplotviewcontroller.m to redraw the graph. my current codes for the CorePlotViewController.h and .m are as follows
CorePlotViewController.h
#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"
#import "GraphView.h"
@interface CorePlotViewController : UIViewController <CPPlotDataSource>
{
CPXYGraph *graph;
NSMutableArray *dataForPlot;
NSMutableArray *dataForPlot2;
NSMutableArray *finalDatas;
NSMutableArray *numofdata;
NSMutableArray *numofdata2;
}
@property(readwrite, retain, nonatomic) NSMutableArray *dataForPlot;
@property(readwrite, retain, nonatomic) NSMutableArray *dataForPlot2;
@property(readwrite, retain, nonatomic) NSMutableArray *finalDatas;
@property(readwrite, retain, nonatomic) NSMutableArray *numofdata;
@property(readwrite, retain, nonatomic) NSMutableArray *numofdata2;
@end
CorePlotViewController.m
#import "MedicalBedAppDelegate.h"
#import "CorePlotViewController.h"
#import "UsageData.h"
#import "GraphView.h"
@implementation CorePlotViewController
@synthesize dataForPlot;
@synthesize dataForPlot2;
@synthesize finalDatas;
@synthesize numofdata,numofdata2;
-(NSUInteger)numberOfRecordsForPlot:(CPPlot *)plot{
MedicalBedAppDelegate *appDelegate = (MedicalBedAppDelegate *)[[UIApplication sharedApplication] delegate];
if ([appDelegate.timetodisplay objectAtIndex:0] == @"4 Hours") {
return appDelegate.usagedatas.count;
}
else if ([appDelegate.timetodisplay objectAtIndex:0] == @"1 Hour") {
return appDelegate.usagedatas.count;
}
else if ([appDelegate.timetodisplay objectAtIndex:0] == @"8 Minutes")
{
return appDelegate.usagedatas.count;
}
else if ([appDelegate.timetodisplay objectAtIndex:0] == @"1 Minute") {
return appDelegate.usagedatas.count;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
MedicalBedAppDelegate *appDelegate = (MedicalBedAppDelegate *)[[UIApplication sharedApplication] delegate];
graph = [[CPXYGraph alloc] initWithFrame: self.view.bounds];
self.view = [[CPLayerHostingView alloc]initWithFrame:[UIScreen mainScreen].bounds];
CPLayerHostingView *hostingView = (CPLayerHostingView *)self.view;
hostingView.hostedLayer = graph;
graph.paddingLeft = 0.0;
graph.paddingTop = 0.0;
graph.paddingRight = 0.0;
graph.paddingBottom = 0.0;
//This applies the 'background color to the graph available themes are
//kCPDarkGradientTheme,kCPPlainWhiteTheme,KCPPlainBlackTheme,kCPStocksTheme,kCPSlateTheme
CPTheme *Theme =[CPTheme themeNamed:kCPSlateTheme];
[graph applyTheme:Theme];
CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-0.015) length:CPDecimalFromFloat(14.2)];
//plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-0.5) length:CPDecimalFromFloat(14.4)];
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-0.015) length:CPDecimalFromFloat(19.85)];
//plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-1) length:CPDecimalFromFloat(19.85)];
CPLineStyle *lineStyle = [CPLineStyle lineStyle];
lineStyle.lineColor = [CPColor blackColor];
lineStyle.lineWidth = 0.0f;
CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet;
axisSet.xAxis.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:@"1"] decimalValue];
//axisSet.xAxis.majorIntervalLength = CPDecimalFromFloat(1.0);
axisSet.xAxis.minorTicksPerInterval = 9;
axisSet.xAxis.majorTickLineStyle = lineStyle;
axisSet.xAxis.minorTickLineStyle = lineStyle;
axisSet.xAxis.axisLineStyle = lineStyle;
axisSet.xAxis.minorTickLength = 5.0f;
axisSet.xAxis.majorTickLength = 10.0f;
axisSet.xAxis.labelOffset = 5.0f;
//axisSet.xAxis.majorTickLocations = 2;
axisSet.yAxis.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:@"1"] decimalValue];
axisSet.yAxis.minorTicksPerInterval = 9;
axisSet.yAxis.majorTickLineStyle = lineStyle;
axisSet.yAxis.minorTickLineStyle = lineStyle;
axisSet.yAxis.axisLineStyle = lineStyle;
axisSet.yAxis.minorTickLength = 0.0f;
axisSet.yAxis.majorTickLength = 0.0f;
axisSet.yAxis.labelOffset = 5.0f;
//Alloc and init a Scatter plot
CPScatterPlot *breathRatePlot = [[CPScatterPlot alloc]initWithFrame:CGRectMake(90, 12, 200, 25)] ;
breathRatePlot.identifier = @"Breath Rate Plot";
breathRatePlot.dataLineStyle.lineWidth = 3.3f;
breathRatePlot.dataLineStyle.lineColor = [CPColor yellowColor];
breathRatePlot.dataSource = self;
[graph addPlot:breathRatePlot];
//Add Plot symbols to the points
CPPlotSymbol *greenCirclePlotSymbol = [CPPlotSymbol ellipsePlotSymbol];
greenCirclePlotSymbol.fill = [CPFill fillWithColor:[CPColor greenColor]];
greenCirclePlotSymbol.size = CGSizeMake(1.0 , 1.0);
breathRatePlot.plotSymbol = greenCirclePlotSymbol;
//Alloc and init a Scatter plot
CPScatterPlot *noOfMovementRegisteredPlot = [[CPScatterPlot alloc]initWithFrame:CGRectMake(90, 12, 200, 25)];
noOfMovementRegisteredPlot.identifier = @"Move Registered Plot";
noOfMovementRegisteredPlot.dataLineStyle.lineWidth = 2.3f;
noOfMovementRegisteredPlot.dataLineStyle.lineColor = [CPColor redColor];
noOfMovementRegisteredPlot.dataSource = self;
[graph addPlot:noOfMovementRegisteredPlot];
//Add Plot symbols to the points
CPPlotSymbol *greenCirclePlotSymbol2 = [CPPlotSymbol ellipsePlotSymbol];
greenCirclePlotSymbol2.fill = [CPFill fillWithColor:[CPColor greenColor]];
greenCirclePlotSymbol2.size = CGSizeMake(1.0, 1.0);
noOfMovementRegisteredPlot.plotSymbol = greenCirclePlotSymbol2;
//Get an instance of the appDelegate and set some vars to be used
NSInteger indexPRowArrayInteger;
NSString *indexPRowArrayString;
indexPRowArrayString = [appDelegate.indexPathRowArray objectAtIndex:0 ] ;
indexPRowArrayInteger = [indexPRowArrayString integerValue ];
// Add some initial data
NSMutableArray *contentArray =[[NSMutableArray alloc]init];
NSMutableArray *contentArray2 =[[NSMutableArray alloc]init];
NSNumber *BRNumber;
NSString *BRString;
NSNumber *NOMRNumber;
NSString *NOMRString;
NSString *DString;
NSNumberFormatter *f = [[NSNumberFormatter alloc]init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
//Datetime section
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy"];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"HH:mm:ss"];
NSDate *now = [[NSDate alloc] initWithTimeIntervalSinceNow:-4*60*60];//time from 4 hrs ago
NSString *theTime = [timeFormat stringFromDate:now];
NSDate *cDate1 = [timeFormat dateFromString:theTime];
NSDate *now1Hr = [[NSDate alloc] initWithTimeIntervalSinceNow:-1*60*60];//time from 1hr ago
NSString *theTime1Hr = [timeFormat stringFromDate:now1Hr];
NSDate *cDate1Hr = [timeFormat dateFromString:theTime1Hr];
NSDate *now8Mins = [[NSDate alloc] initWithTimeIntervalSinceNow:-8*60];//time from 8Mins ago
NSString *theTime8Mins = [timeFormat stringFromDate:now8Mins];
NSDate *cDate8Mins = [timeFormat dateFromString:theTime8Mins];
NSDate *now1Min = [[NSDate alloc] initWithTimeIntervalSinceNow:-1*60];//time from 1Min ago
NSString *theTime1Min = [timeFormat stringFromDate:now1Min];
NSDate *cDate1Min = [timeFormat dateFromString:theTime1Min];
NSUInteger i;
NSUInteger i2;
numofdata = [[NSMutableArray alloc]init];
numofdata2 = [[NSMutableArray alloc]init];
for ( i = 0; i < appDelegate.usagedatas.count; i++ ) {
//NSLog(@"appdelcount= %d",appDelegate.usagedatas.count);
UsageData *aUsageDataInstance = (UsageData *)[appDelegate.usagedatas objectAtIndex:i];
BRString = aUsageDataInstance.breathrate;
BRNumber = [f numberFromString:BRString];
DString = aUsageDataInstance.time;
NSDate *cDate2 = [timeFormat dateFromString:DString];
NSTimeInterval time = [cDate2 timeIntervalSinceDate:cDate1];
NSTimeInterval time1Hr = [cDate2 timeIntervalSinceDate:cDate1Hr];
NSTimeInterval time8Mins = [cDate2 timeIntervalSinceDate:cDate8Mins];
NSTimeInterval time1Min = [cDate2 timeIntervalSinceDate:cDate1Min];
if ([appDelegate.timetodisplay objectAtIndex:0] == @"4 Hours") {
id x = [NSNumber numberWithFloat: ((time/132)/9)];
id y = [NSNumber numberWithFloat:([BRNumber intValue]/1.975) ];
[contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x, @"x", y, @"y", nil]];
}
//calculation = (time(interval from how many hrs/mins ago) / seconds of 1 minor-interval of x-axis(NOT MAJOR,1major has 10 minor intervals)/9
//time = the number of seconds from the time in the database to the current time
else if ([appDelegate.timetodisplay objectAtIndex:0] == @"1 Hour") {
id x = [NSNumber numberWithFloat:((time1Hr/33.3)/9)];
id y = [NSNumber numberWithFloat:([BRNumber intValue]/1.975) ];
[contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x, @"x", y, @"y", nil]];
}
else if ([appDelegate.timetodisplay objectAtIndex:0] == @"8 Minutes") {
id x = [NSNumber numberWithFloat: ((time8Mins/4.4)/9)];
id y = [NSNumber numberWithFloat:([BRNumber intValue]/1.975) ];
[contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x, @"x", y, @"y", nil]];
}
else if ([appDelegate.timetodisplay objectAtIndex:0] == @"1 Minute") {
id x = [NSNumber numberWithFloat: ((time1Min/0.53)/9)];
id y = [NSNumber numberWithFloat:([BRNumber intValue]/1.975) ];
[contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x, @"x", y, @"y", nil]];
}
}
self.dataForPlot = contentArray;
for ( i2 = 0; i2 < appDelegate.usagedatas.count; i2++ ) {
UsageData *aUsageDataInstance = (UsageData *)[appDelegate.usagedatas objectAtIndex:i2];
NOMRString = aUsageDataInstance.noofmovereg;
NOMRNumber = [f numberFromString:NOMRString];
DString = aUsageDataInstance.time;
NSDate *cDate2 = [timeFormat dateFromString:DString];
NSTimeInterval time = [cDate2 timeIntervalSinceDate:cDate1];
NSTimeInterval time1Hr = [cDate2 timeIntervalSinceDate:cDate1Hr];
NSTimeInterval time8Mins = [cDate2 timeIntervalSinceDate:cDate8Mins];
NSTimeInterval time1Min = [cDate2 timeIntervalSinceDate:cDate1Min];
if ([appDelegate.timetodisplay objectAtIndex:0] == @"4 Hours") {
id x2 = [NSNumber numberWithFloat: ((time/132)/9)];
id y2 = NOMRNumber;
[contentArray2 addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x2, @"x", y2, @"y", nil]];
}
else if ([appDelegate.timetodisplay objectAtIndex:0] == @"1 Hour") {
id x2 = [NSNumber numberWithFloat:((time1Hr/33.3)/9)];
id y2 = NOMRNumber;
[contentArray2 addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x2, @"x", y2, @"y", nil]];
}
else if ([appDelegate.timetodisplay objectAtIndex:0] == @"8 Minutes") {
id x2 = [NSNumber numberWithFloat: ((time8Mins/4.4)/9)];
id y2 = NOMRNumber;
[contentArray2 addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x2, @"x", y2, @"y", nil]];
}
else if ([appDelegate.timetodisplay objectAtIndex:0] == @"1 Minute") {
id x2 = [NSNumber numberWithFloat: ((time1Min/0.53)/9)];
id y2 = NOMRNumber;
[contentArray2 addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x2, @"x", y2, @"y", nil]];
}
}
self.dataForPlot2 = contentArray2;
//release for memory management.
[dateFormat release];
[timeFormat release];
[now release];
[now1Hr release];
[now8Mins release];
[now1Min release];
[f release];
[noOfMovementRegisteredPlot release];
[breathRatePlot release];
[contentArray release];
[contentArray2 release];
}
-(NSNumber *)numberForPlot:(CPPlot *)plot
field:(NSUInteger)fieldEnum
recordIndex:(NSUInteger)index
{
NSNumber *num = [[dataForPlot objectAtIndex:index] valueForKey:(fieldEnum == CPScatterPlotFieldX ? @"x" : @"y")];
NSNumber *num2 = [[dataForPlot2 objectAtIndex:index] valueForKey:(fieldEnum == CPScatterPlotFieldX ? @"x" : @"y")];
if ([(NSString *)plot.identifier isEqualToString:@"Breath Rate Plot"])
{
if ( fieldEnum == CPScatterPlotFieldY )
num = [NSNumber numberWithDouble:[num doubleValue] ];
return num;
}
else {
num2 = [NSNumber numberWithDouble:[num2 doubleValue] ];
return num2;
}
}
- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft );
}
- (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)dealloc {
[graph release];
[dataForPlot release];
[dataForPlot2 release];
[finalDatas release];
[numofdata release];
[numofdata2 release];
[super dealloc];
}
@end
Use [graph reloadData];
to update your data and redraw all of your plots. You can send the -reloadData
message to individual plots if you don't need to update all of them at the same time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With