实时核心情节IOS

时间:2011-05-06 12:17:57

标签: iphone ios charts core-plot

我正在使用核心情节框架来制作实时图表。问题是,过了一段时间它会冻结我的iPhone。我认为是因为大量的数据,每当我获得新数据时我都会重新加载它。我需要一个解决方案。我虽然设置了一个计时器并且每5秒左右重新加载一次,或者可能使用并行线程。我该怎么办。谢谢。

我说的可能是2-3000。

这是我的代码:

//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

我有两张图表。 在这里我把数据放在图表中:

-(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") {
    NSLog(@"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;
}
}

每次我从gps获取新数据时,我都会重新加载数据。

1 个答案:

答案 0 :(得分:0)

请参阅Core Plot讨论板上的this thread,了解改善数据加载性能的一些建议。

我建议不要尝试在单独的线程上加载数据。 Core Plot并非设计为多线程,而其他用户报告了这样做的问题。