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 では 2 倍の時間がかかります。これは正常ですか?

また、そのメソッドをメインスレッドから切り離して を使用することで、それを回避しようとしましたUIActivityIndicatorViewが、2 つ目の奇妙な動作があります。

スピナーが表示され、しばらくするとプロットがアニメーション化されることもあれば、スピナーが表示されず、プロットがアニメーションなしで表示されることもあります。

ご覧のとおり、私は実際には 2 つの問題に取り組んでいますが、2 つ目の問題は最初の問題と絡み合っている可能性があります。


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 に答える