1

我有以下 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");
}
4

2 回答 2

0

这是 coreplot 1.2 引入的东西。以下几行会导致应用程序长时间挂起:

axisSet.xAxis.labelRotation = M_PI/4;
axisSet.xAxis.titleOffset = 55.5f;

恢复到 1.1 立即解决了这个问题。从上面@Eric Skroch 的评论来看,这可能会在 1.3 中修复,但在那之前我想使用一个稳定的版本,尤其是因为我没有使用 1.2 中的任何新功能。

顺便说一句,我认为旧库无法与我的 iPhone 5 配合使用的原因是没有以下内容:

  1. 在目标构建设置中添加到其他链接器标志:

    -ObjC -all_load

于 2013-06-04T04:49:18.720 回答
0

将标签策略设置为无很重要,以明确您将要自定义 x 轴和 y 轴。

CPTAxis *x = axisSet.xAxis;

CPTAxis *y = axisSet.yAxis;

x.labelingPolicy = CPTAxisLabelingPolicyNone;
y.labelingPolicy = CPTAxisLabelingPolicyNone;
于 2013-06-09T03:23:09.963 回答