开发者

Real time core plot IOS

I am using the core plot framework to make a real time chart. The problem is that after a while it freezes my iphone . I think is because of the large number of data and that i reload it every time i get new data. I need a solution to this. I though of setting a timer and reloading every 5 sec or so or maybe using a parallel thread.What should i do.Thanks.

I am talking about maybe 2-3000.

Here is my code:

//graph1 init
CGRect graphRect = CGRectMake(0, 0, 320, 183);
graph=[[CPXYGraph alloc] initWithFrame:graphRect];

CPGraphHostingView *graphView = (CPGraphHostingView*)graphViewScroll;
graphView.hostedGraph = graph;

graph.paddingLeft = 20.0;
graph.paddingTop = 20.0;
graph.paddingRight = 20.0;
graph.paddingBottom = 20.0;

CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0)
                                               length:CPDecimalFromFloat(17)];
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0)
                                               length:CPDecimalFromFloat(25)];


CPMutableLineStyle *lineStyle = [CPMutableLineStyle lineStyle];
lineStyle.lineWidth = 1.5f;
lineStyle.lineColor = [CPColor whiteColor];



CGRect speedframe=CGRectMake(305,70, 50, 15);


UILabel *speedlabel=[[UILabel alloc]initWithFrame:speedframe];
speedlabel.text=@"Speed";
speedlabel.backgroundColor=[UIColor clearColor];
speedlabel.textColor=[UIColor whiteColor];
speedlabel.transform = CGAffineTransformMakeRotation( M_PI/2 );


CGRect metersframe=CGRectMake(615,80, 70, 15);


UILabel *meterslabel=[[UILabel alloc]initWithFrame:metersframe];
meterslabel.text=@"Meters";
if([distance1 isEqualToString:@"miles"]) {
    meterslabel.text=@"Feet";
}

meterslabel.backgroundColor=[UIColor clearColor];
meterslabel.textColor=[UIColor whiteColor];
meterslabel.transform = CGAffineTransformMakeRotation( M_PI/2 );

CGRect speedframe1=CGRectMake(450,165, 90, 15);


UILabel *timelabel=[[UILabel alloc]initWithFrame:speedframe1];

timelabel.text=@"Time";
timelabel.backgroundColor=[UIColor clearColor];
timelabel.textColor=[UIColor whiteColor];
//timelabel.transform = CGAffineTransformMakeRotation( M_PI/2 );

CGRect kmframe=CGRectMake(770,165, 90, 15);


UILabel *kmlabel=[[UILabel alloc]initWithFrame:kmframe];
kmlabel.text=@"Km";
if([distance1 isEqualToString:@"miles"]) {
    kmlabel.text=@"Miles";
}
kmlabel.backgroundColor=[UIColor clearColor];
kmlabel.textColor=[UIColor whiteColor];

CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet;
axisSet.xAxis.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:@"1"] decimalValue];
axisSet.xAxis.minorTicksPerInterval = 4;
axisSet.xAxis.majorTickLineStyle = lineStyle;
axisSet.xAxis.minorTickLineStyle = lineStyle;
axisSet.xAxis.axisLineStyle = lineStyle;
axisSet.xAxis.minorTickLength = 5.0f;
axisSet.xAxis.majorTickLength = 7.0f;

//axisSet.xAxis.axisTitle=@"Test";
//axisSet.xAxis.axisLabelOffset = 3.0f;

axisSet.yAxis.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:@"3"] decimalValue];
axisSet.yAxis.minorTicksPerInterval = 4;
axisSet.yAxis.majorTickLineStyle = lineStyle;
axisSet.yAxis.minorTickLineStyle = lineStyle;
axisSet.yAxis.axisLineStyle = lineStyle;
axisSet.yAxis.minorTickLength = 5.0f;
axisSet.yAxis.majorTickLength = 7.0f;
//axisSet.yAxis.axisLabelOffset = 3.0f;

CPScatterPlot *xInversePlot = [[[CPScatterPlot alloc] init] autorelease];
xInversePlot.identifier = @"X Inverse Plot 1";
lineStyle = [[xInversePlot.dataLineStyle mutableCopy] autorelease];
lineStyle.lineWidth = 1.0f;
lineStyle.lineColor = [CPColor whiteColor];
xInversePlot.dataLineStyle = lineStyle;
xInversePlot.dataSource = self;
[graph addPlot:xInversePlot];

//graph1 end

I have two charts. Here i put data in the chart:

-(NSUInteger)numberOfRecordsForPlot:(CPPlot *)plot{
if(plot.identifier == @"X Inverse Plot 2") {
    return [contentArray count];
}
else {
    return [dataForPlot count];
}
 }

 -(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{

//NSLog(@"numar:%@",num);
if(plot.identifier == @"X Inverse Plot 2") {
    NSL开发者_运维技巧og(@"X Inverse Plot 2");
     NSNumber *num = [[self.contentArray objectAtIndex:index] valueForKey:(fieldEnum == CPScatterPlotFieldX ? @"x" : @"y")];
    return num;
}
else {
      NSNumber *num = [[self.dataForPlot objectAtIndex:index] valueForKey:(fieldEnum == CPScatterPlotFieldX ? @"x" : @"y")];
    return num;
}
}

And i reload the data every time i get a new data from the gps.


See this thread on the Core Plot discussion board for some ideas to improve your data loading performance.

I would recommend against trying to load data on a separate thread. Core Plot isn't designed to be multi-threaded and other users have reported problems doing so.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜