2

我在设置 CPTBarPlot 的方法中有以下代码:

CPTGraphHostingView *chartView = [[CPTGraphHostingView alloc] initWithFrame:CGRectMake(10, 100, 290, 330)];
    [self.view addSubview:chartView];

    CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:chartView.bounds];
    graph.plotAreaFrame.masksToBorder = NO;
    chartView.hostedGraph = graph;

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

    CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
    textStyle.color = [CPTColor grayColor];
    textStyle.fontName = @"Helvetica-Bold";
    textStyle.fontSize = 8.0f;;

    CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle];
    axisTitleStyle.color = [CPTColor grayColor];
    axisTitleStyle.fontName = @"Helvetica-Bold";
    axisTitleStyle.fontSize = 12.0f;
    CPTMutableLineStyle *axisLineStyleX = [CPTMutableLineStyle lineStyle];
    axisLineStyleX.lineWidth = 2.0f;
    axisLineStyleX.lineColor = [CPTColor grayColor];
    CPTMutableLineStyle *axisLineStyleY = [CPTMutableLineStyle lineStyle];
    axisLineStyleY.lineWidth = 2.0f;
    axisLineStyleY.lineColor = [CPTColor grayColor];

    CPTXYAxisSet *axisSet = (CPTXYAxisSet *) chartView.hostedGraph.axisSet;

    axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyFixedInterval;
    axisSet.xAxis.title = @"Ferientage pro Jahr";
    axisSet.xAxis.titleTextStyle = axisTitleStyle;
    axisSet.xAxis.titleOffset = 10.0f;
    axisSet.xAxis.axisLineStyle = axisLineStyleX;
    axisSet.xAxis.labelTextStyle = textStyle;
    axisSet.xAxis.labelOffset = -7.0f;
    axisSet.xAxis.majorIntervalLength = CPTDecimalFromFloat(10.0f);
    axisSet.xAxis.labelFormatter.maximumFractionDigits = 0;

    axisSet.yAxis.labelingPolicy = CPTAxisLabelingPolicyNone;
    axisSet.yAxis.title = @"Bundesländer";
    axisSet.yAxis.titleTextStyle = axisTitleStyle;
    axisSet.yAxis.titleOffset = 5.0f;
    axisSet.yAxis.axisLineStyle = axisLineStyleY;

    CPTXYPlotSpace *plotspace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
            plotspace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) length:CPTDecimalFromFloat(110)];
    plotspace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) length:CPTDecimalFromFloat(17)];
    [graph addPlotSpace:plotspace];

    CPTBarPlot *plot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor redColor] horizontalBars:YES];
    plot.plotRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.5) length:CPTDecimalFromFloat(16)];
    plot.labelTextStyle = textStyle;
    plot.labelFormatter.maximumFractionDigits = 0;
    plot.labelOffset = -3.0f;
    plot.identifier = @"bl";
    plot.dataSource = self;
    plot.delegate = self;
    [graph addPlot:plot];

    plot.anchorPoint = CGPointMake(0.0, 0.0);
    CABasicAnimation *scaling = [CABasicAnimation
                                         animationWithKeyPath:@"transform.scale.x"];
    scaling.fromValue = [NSNumber numberWithFloat:0.0];
    scaling.toValue = [NSNumber numberWithFloat:1.0];
    scaling.duration = 1.0f;
    scaling.removedOnCompletion = NO;
    scaling.fillMode = kCAFillModeForwards;
    [plot addAnimation:scaling forKey:@"scaling"];

加载它在 iPhone 5 上大约需要 4 秒,在 iPhone 4 上是两倍。这正常吗?

我还试图通过从主线程中分离该方法并使用 a 来规避这一点UIActivityIndicatorView,但是还有第二个奇怪的行为:

有时它会显示微调器并且情节会在一段时间后变为动画,有时微调器不会出现并且情节显示时没有动画。

如您所见,我实际上正在努力解决两个问题,但也许第二个问题与第一个问题交织在一起。


也许我会向您展示与 Core Plot 工作相关的其他方法:

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
return 16;
}

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
Ferien *ferien = [[Ferien alloc] init];

NSNumber *num = nil;
if ([plot isKindOfClass:[CPTBarPlot class]]) {
    NSArray *reversedArrayCount = [[ferien.getToplist reverseObjectEnumerator] allObjects];
    num = (NSNumber *)[NSNumber numberWithFloat:[[reversedArrayCount objectAtIndex:index] floatValue]];
    }
return num;

}

-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index {
if ( [plot.identifier isEqual: @"bl"] ) {
    Ferien *ferien = [[Ferien alloc] init];
    CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
    textStyle.fontName = @"Helvetica-Bold";
    textStyle.fontSize = 8.0f;
    textStyle.color = [CPTColor blackColor];

    NSArray *reversedArrayBl = [[ferien.bundeslandListWithManagedObjects reverseObjectEnumerator] allObjects];
    NSArray *reversedArrayCount = [[ferien.getToplist reverseObjectEnumerator] allObjects];

    CPTTextLayer *label = [[CPTTextLayer alloc] initWithText:[NSString stringWithFormat:@"%@ (%@)", [reversedArrayBl objectAtIndex:index], [reversedArrayCount objectAtIndex:index]]];
    label.textStyle = textStyle;

    return label;
}

CPTTextLayer *defaultLabel = [[CPTTextLayer alloc] initWithText:@"Label"];
return defaultLabel;

}

-(CPTFill *)barFillForBarPlot:(CPTBarPlot *)barPlot recordIndex:(NSUInteger)index {
if ([barPlot.identifier isEqual:@"bl"] ) {
    Ferien *ferien = [[Ferien alloc] init];

    NSMutableArray *colors = [[NSMutableArray alloc] initWithObjects:[UIColor lightGrayColor], [UIColor lightGrayColor], [UIColor lightGrayColor], [UIColor lightGrayColor], [UIColor lightGrayColor], [UIColor lightGrayColor], [UIColor lightGrayColor], [UIColor lightGrayColor], [UIColor lightGrayColor], [UIColor lightGrayColor], [UIColor lightGrayColor], [UIColor lightGrayColor], [UIColor lightGrayColor], [UIColor lightGrayColor], [UIColor lightGrayColor], [UIColor lightGrayColor], nil];

    [colors replaceObjectAtIndex:15-ferien.currentBundesland withObject:[UIColor orangeColor]];

    for (int i = 0; i < 16; i++) {
        CPTGradient *gradient = [CPTGradient gradientWithBeginningColor:[colors objectAtIndex:index]
                                                        endingColor:[CPTColor whiteColor]
                                                  beginningPosition:0.0 endingPosition:1.5 ];
        [gradient setGradientType:CPTGradientTypeAxial];
        [gradient setAngle:320.0];

        CPTFill *fill = [CPTFill fillWithGradient:gradient];

        return fill;
    }
}
return [CPTFill fillWithColor:[CPTColor colorWithComponentRed:1.0 green:1.0 blue:1.0 alpha:1.0]];

}

- (void)initView {
self.labelFerienName.text = self.ferienName;
self.labelFeriendauer.text = self.feriendauer;

}

- (void)mergeChanges:(NSNotification *)notification {
AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *mainContext = [delegate managedObjectContext];

// Merge changes into the main context on the main thread
[mainContext performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:)
                              withObject:notification
                           waitUntilDone:YES];

}

4

1 回答 1

1

您可以在任何地方进行数据计算,包括其他线程,但图形设置和所有其他 Core Plot 交互必须在主线程上。当您准备好显示图表时调用-initGraph,即使数据尚未准备好。在后台线程上进行数据计算,并将结果缓存在数据源可以快速访问的结构中。当数据准备好时调用-reloadData绘图(从主线程)以强制绘图从数据源读取新数据。

于 2013-02-20T02:35:16.527 回答