我有以下 CorePlot (v1.2) 代码,它基于http://www.raywenderlich.com/13271/how-to-draw-graphs-with-core-plot-part-2中的教程,但扩展了少量。大约 30 个数据点在 iPhone 4S 上渲染图形大约需要 3 秒。该图确实加载正确,只是速度很慢。有没有办法提高性能?
这是代码中的 NSLog 输出:
2013-05-30 17:41:20.386 myApp[2796:907] A
2013-05-30 17:41:20.387 myApp[2796:907] B
2013-05-30 17:41:20.388 myApp[2796:907] C
2013-05-30 17:41:20.389 myApp[2796:907] D
2013-05-30 17:41:21.158 myApp[2796:907] E
2013-05-30 17:41:21.160 myApp[2796:907] F
2013-05-30 17:41:22.594 myApp[2796:907] G
NB 代码完成运行后,在呈现图形之前还有大约 0.5 秒的延迟。
和代码:
-(void)configureAxes {
NSLog(@"A");
// 1 - Create styles
CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle];
axisTitleStyle.color = [CPTColor whiteColor];
axisTitleStyle.fontName = @"Helvetica-Bold";
axisTitleStyle.fontSize = 12.0f;
CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineWidth = 2.0f;
axisLineStyle.lineColor = [CPTColor whiteColor];
CPTMutableTextStyle *axisTextStyle = [[CPTMutableTextStyle alloc] init];
axisTextStyle.color = [CPTColor whiteColor];
axisTextStyle.fontName = @"Helvetica-Bold";
axisTextStyle.fontSize = 11.0f;
CPTMutableLineStyle *tickLineStyle = [CPTMutableLineStyle lineStyle];
tickLineStyle.lineColor = [CPTColor whiteColor];
tickLineStyle.lineWidth = 2.0f;
NSLog(@"B");
// 2 - Get axis set
CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.hostView.hostedGraph.axisSet;
// 3 - Configure x-axis
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineColor = [CPTColor whiteColor];
lineStyle.lineWidth = 2.0f;
CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
textStyle.fontName = @"Helvetica";
textStyle.fontSize = 14;
textStyle.color = [CPTColor whiteColor];
axisSet.xAxis.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0.0");
axisSet.xAxis.minorTicksPerInterval = 0;
NSLog(@"C");
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
if ([person.unitPreference isEqualToString:@"US"]) {
[dateFormatter setDateFormat:@"MMM-dd"];
} else {
[dateFormatter setDateFormat:@"dd-MMM"];
}
NSLog(@"D");
CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter];
axisSet.xAxis.labelFormatter = timeFormatter;
axisSet.xAxis.labelRotation = M_PI/4;
axisSet.xAxis.titleOffset = 55.5f;
axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
NSLog(@"E");
if ([measurementField isEqualToString:@"mWeight"]) {
NSString *unit;
if ([person.unitPreference isEqualToString:@"METRIC"]) {
unit = @"Kgs";
} else if ([person.unitPreference isEqualToString:@"US"]) {
unit = @"lbs";
} else {
unit = @"st lbs";
}
axisSet.yAxis.title = [NSString stringWithFormat: @"Weight (%@)", unit];
WeightFormatter *weightFormatter = [[WeightFormatter alloc] init];
weightFormatter.person = person;
axisSet.yAxis.labelFormatter = weightFormatter;
} else if ([measurementField isEqualToString:@"mBodyFatPercentage"]) {
axisSet.yAxis.title = @"Body Fat %";
axisSet.yAxis.labelFormatter = [[BodyFatPercentageFormatter alloc] init];
} else {
NSString *unit;
if ([person.unitPreference isEqualToString:@"METRIC"]) {
unit = @"CMs";
} else {
unit = @"Inches";
}
axisSet.yAxis.title = [NSString stringWithFormat: @"Measurements (%@)", unit];
MeasurementFormatter *measurementFormatter = [[MeasurementFormatter alloc] init];
measurementFormatter.person = person;
axisSet.yAxis.labelFormatter = measurementFormatter;
}
NSLog(@"F");
axisSet.yAxis.titleTextStyle = textStyle;
axisSet.yAxis.titleOffset = 40.0f + marginOffset;
axisSet.yAxis.axisLineStyle = lineStyle;
axisSet.yAxis.majorTickLineStyle = lineStyle;
axisSet.yAxis.minorTickLineStyle = lineStyle;
axisSet.yAxis.labelTextStyle = textStyle;
axisSet.yAxis.labelOffset = 3.0f;
axisSet.yAxis.majorIntervalLength = CPTDecimalFromFloat((yAxisMax-yAxisMin)/10.0f);
axisSet.yAxis.minorTicksPerInterval = 1;
axisSet.yAxis.minorTickLength = 5.0f;
axisSet.yAxis.majorTickLength = 7.0f;
axisSet.yAxis.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
axisSet.xAxis.orthogonalCoordinateDecimal = CPTDecimalFromFloat(yAxisMin);
axisSet.xAxis.majorIntervalLength = CPTDecimalFromFloat(oneDay);
NSLog(@"G");
}