Iphone 核心图-动态刷新图形+;iPad

Iphone 核心图-动态刷新图形+;iPad,iphone,ipad,core-plot,Iphone,Ipad,Core Plot,我有一个graphview.m,它以这种方式创建coreplotviewcontroller.view CorePlotViewController *aCorePlotViewController = [[CorePlotViewController alloc] initWithNibName:@"CorePlotViewController" bundle:nil]; aCorePlotViewController.view.bounds = CGRectMake(0,

我有一个graphview.m,它以这种方式创建coreplotviewcontroller.view

      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];
现在我正在通过删除并再次添加来刷新图表

 [[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];
但这将占用大量内存,最终我将无法为CoreAnimation分配更多内存(我正在记录此错误)

那么,如何重新创建图形呢?通常使用在coreplotviewcontroller.m中创建方法来重新绘制图形。CorePlotViewController.h和.m的当前代码如下

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
#导入
#导入“CorePlot CoCoCoTouch.h”
#导入“GraphView.h”
@接口CorePlotViewController:UIViewController
{
CPXYGraph*图;
NSMUTABLEARRY*dataForPlot;
NSMutableArray*dataForPlot2;
NSMutableArray*最终数据;
NSMutableArray*numofdata;
NSMutableArray*numofdata2;
}
@属性(读写、保留、非原子)NSMutableArray*dataForPlot;
@属性(读写、保留、非原子)NSMutableArray*dataForPlot2;
@属性(读写、保留、非原子)NSMUTABLEARRY*FINALDATA;
@属性(读写、保留、非原子)NSMUTABLEARRY*numofdata;
@属性(读写、保留、非原子)NSMutableArray*numofdata2;
@结束
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
#导入“MedicalBedAppDelegate.h”
#导入“CorePlotViewController.h”
#导入“UsageData.h”
#导入“GraphView.h”
@CorePlotViewController的实现
@综合数据进行绘图;
@为Plot2合成数据;
@合成最终数据;
@合成numofdata,numofdata2;
-(NSUInteger)numberOfRecordsForPlot:(Cplot*)绘图{
MedicalBedAppDelegate*appDelegate=(MedicalBedAppDelegate*)[[UIApplication sharedApplication]委托];
如果([appDelegate.TimeToDisplayObjectAtIndex:0]==@“4小时”){
返回appDelegate.usagedatas.count;
}
else if([appDelegate.TimeToDisplayObjectAtIndex:0]==@“1小时”){
返回appDelegate.usagedatas.count;
}
否则如果([appDelegate.TimeToDisplayObjectAtIndex:0]==@“8分钟”)
{
返回appDelegate.usagedatas.count;
}
否则如果([appDelegate.TimeToDisplayObjectAtIndex:0]==@“1分钟”){
返回appDelegate.usagedatas.count;
}   
}
-(无效)viewDidLoad{
[超级视图下载];
MedicalBedAppDelegate*appDelegate=(MedicalBedAppDelegate*)[[UIApplication sharedApplication]委托];
graph=[[CPXYGraph alloc]initWithFrame:self.view.bounds];
self.view=[[CPLayerHostingView alloc]initWithFrame:[UIScreen mainScreen].bounds];
CPLayerHostingView*hostingView=(CPLayerHostingView*)self.view;
hostingView.hostedLayer=图形;
graph.paddingLeft=0.0;
graph.paddingTop=0.0;
graph.paddingRight=0.0;
graph.paddingBottom=0.0;
//这会将“背景色”应用于可用主题的图形
//kCPDarkGradientTheme,kCPPlainWhiteTheme,KCPPlainBlackTheme,KCPStockTheme,kCPSlateTheme
CPTheme*Theme=[CPTheme themeNamed:kCPSlateTheme];
[图形应用主题:主题];
CPXYPlotSpace*plotSpace=(CPXYPlotSpace*)graph.defaultPlotSpace;
plotSpace.xRange=[CPPlotRange plotRangeWithLocation:cpdecimalfloat(-0.015)长度:cpdecimalfloat(14.2)];
//plotSpace.xRange=[CPPlotRange plotRangeWithLocation:cpdecimalfloat(-0.5)长度:cpdecimalfloat(14.4)];
plotSpace.yRange=[CPPlotRange plotRangeWithLocation:cpdecimalfloat(-0.015)长度:cpdecimalfloat(19.85)];
//plotSpace.yRange=[CPPlotRange plotRangeWithLocation:cpdecimalfloat(-1)长度:cpdecimalfloat(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=线条样式;
axisSet.xAxis.minorTickLineStyle=线条样式;
axisSet.xAxis.axisLineStyle=线型;
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=线条样式;
axisSet.yAxis.minorTickLineStyle=线条样式;
axisSet.yAxis.axisLineStyle=线型;
axisSet.yAxis.minorTickLength=0.0f;
axisSet.yAxis.majorTickLength=0.0f;
axisSet.yAxis.labelOffset=5.0f;
//Alloc和init散点图
CPScatterPlot*呼吸速率图=[[CPScatterPlot alloc]initWithFrame:CGRectMake(90,12,200,25)];
呼吸速率图。标识符=@“呼吸速率图”;
BreatratePlot.dataLineStyle.lineWidth=3.3f;
BreatRatePlot.dataLineStyle.lineColor=[CPColor yellowColor];
BreatratePlot.dataSource=self;
[图形添加图:呼吸速率图];
//向点添加打印符号
CPPlotSymbol*greenCirclePlotSymbol=[CPPlotSymbol ellipsePlotSymbol];
greenCirclePlotSymbol.fill=[CPFill fillWithColor:[CPColor greenColor]];
greenCirclePlotSymbol.size=CGSizeMake(1.0,1.0);
BreatratePlot.plotSymbol=绿色圆圈plotSymbol;
//Alloc和init散点图
CPScatterPlot*noOfMovementRegisteredPlot=[[CPScatterPlot alloc]initWithFrame:CGRectMake(90,12,200,25)];
noOfMovementRegisteredPlot.identifier=@“移动已注册的绘图”;
noOfMovementRegisteredPlot.dataLineStyle.lineWidth=2.3f;
noOfMovementRegisteredPlot.dataLineStyle.lineColor=[CPColor redColor];
noOfMovementRegisteredPlot.dataSource=self;
[图形添加绘图:noOfMovementRegisteredPlot];
//向点添加打印符号
CPPlotSymbol*greenCirclePlotSymbol2=[CPPlotSymbol ellipsePlotSymbol];
greenCirclePlotSymbol2.fill=[CPFill fillWithColo