在核心图的条形图中为每个条添加注释

时间:2015-05-08 11:41:54

标签: ios annotations core-plot

我使用了barPlot:barWasSelectedAtRecordIndex方法为每个条形添加注释但它没有用,我在我的方法中使用了相同的代码并手动创建了绘图对象并为其分配了实际绘图的值,如下所示

 - (NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot{
    return [[[CPDStockPriceStore sharedInstance] datesInWeek] count];
    _getplot = plot;
}

我用于注释的方法如下:

- (void)SetAnnotations {
    static CPTMutableTextStyle *style = nil;
    if (!style) {
        style = [CPTMutableTextStyle textStyle];
        style.color= [CPTColor yellowColor];
        style.fontSize = 16.0f;
        style.fontName = @"Helvetica-Bold";
    }

    // 3 - Create annotation, if necessary
    for (int index=0; index<3; index++) {

    NSNumber *price = [self numberForPlot:_getplot field:CPTBarPlotFieldBarTip recordIndex:index];
    if (!self.priceAnnotation) {
        NSNumber *x = [NSNumber numberWithInt:0];
        NSNumber *y = [NSNumber numberWithInt:0];
        NSArray *anchorPoint = [NSArray arrayWithObjects:x, y, nil];
        self.priceAnnotation = [[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:_getplot.plotSpace anchorPlotPoint:anchorPoint];
    }
    // 4 - Create number formatter, if needed
    static NSNumberFormatter *formatter = nil;
    if (!formatter) {
        formatter = [[NSNumberFormatter alloc] init];
        [formatter setMaximumFractionDigits:2];
    }
    // 5 - Create text layer for annotation
    NSString *priceValue = [formatter stringFromNumber:price];
    CPTTextLayer *textLayer = [[CPTTextLayer alloc] initWithText:priceValue style:style];
    self.priceAnnotation.contentLayer = textLayer;
    // 6 - Get plot index based on identifier
    NSInteger plotIndex = 0;
    if ([_getplot.identifier isEqual:CPDTickerSymbolAAPL] == YES) {
        plotIndex = 0;
    } else if ([_getplot.identifier isEqual:CPDTickerSymbolGOOG] == YES) {
        plotIndex = 1;
    } else if ([_getplot.identifier isEqual:CPDTickerSymbolMSFT] == YES) {
        plotIndex = 2;
    }
    // 7 - Get the anchor point for annotation
    CGFloat x = index + CPDBarInitialX + (plotIndex * CPDBarWidth);
    NSNumber *anchorX = [NSNumber numberWithFloat:x];
    CGFloat y = [price floatValue] + 40.0f;
    NSNumber *anchorY = [NSNumber numberWithFloat:y];
    self.priceAnnotation.anchorPlotPoint = [NSArray arrayWithObjects:anchorX, anchorY, nil];
    // 8 - Add the annotation
    [_getplot.graph.plotAreaFrame.plotArea addAnnotation:self.priceAnnotation];
    }
}

但它显示错误为

Assertion failure in -[CPTPlotSpaceAnnotation initWithPlotSpace:anchorPlotPoint:], /Users/eskroch/Projects/Core Plot/framework/Source/CPTPlotSpaceAnnotation.m:42
2015-05-08 12:16:19.195 CorePlotDemo[871:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: newPlotSpace'
*** First throw call stack:
(
    0   CoreFoundation                      0x01c8d1e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x018758e5 objc_exception_throw + 44
    2   CoreFoundation                      0x01c8d048 +[NSException raise:format:arguments:] + 136
    3   Foundation                          0x012484de -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116

2 个答案:

答案 0 :(得分:0)

异常告诉您CPTPlotSpaceAnnotation在创建时需要有效的绘图空间。将图表添加到图表时,会将图表分配给图表空间。

答案 1 :(得分:-1)

  

任何正文都可以告诉我如何在核心图1.4

的条形图中为每个条添加注释

此代码将为所有栏添加注释。

        - (void)setAnnotations
        {
            for (CPTBarPlot *theBarPlot in theArrayOfBarPlots)
            {
                for (int i = 0; i < [self numberOfRecordsForPlot:theBarPlot]; i++)
                {
                    NSNumber *theNumber = [self numberForPlot:theBarPlot field:CPTBarPlotFieldBarTip recordIndex:i];

                    CPTMutableTextStyle *style = [[CPTMutableTextStyle alloc] init];
                    style.color = [CPTColor yellowColor];
                    style.fontSize = 10;

                    CPTPlotSpaceAnnotation *thePriceAnnotation = [[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:theBarPlot.plotSpace anchorPlotPoint:@[@(i), @(theNumber.doubleValue + 30)]];
                    thePriceAnnotation.contentLayer = [[CPTTextLayer alloc] initWithText:[NSString stringWithFormat:@"%@", theNumber] style:style];
                    [theBarPlot.graph.plotAreaFrame.plotArea addAnnotation:thePriceAnnotation];
                }
            }
        }