координаты аннотации основного графика, возвращающие ноль - PullRequest
1 голос
/ 09 августа 2011

У меня есть рабочий основной график, мой первый, и в настоящее время я пытаюсь реализовать аннотацию.Я записал аннотацию, координаты X и Y, и они нулевые.спасибо

-(void)scatterPlot:(CPTScatterPlot *)plot plotSymbolWasSelectedAtRecordIndex:(NSUInteger)index
{
    //CPTGraph* graph = [graphs objectAtIndex:0];

    NSLog(@"add annotation called");

    if ( symbolTextAnnotation ) {
        [graph.plotAreaFrame.plotArea removeAnnotation:symbolTextAnnotation];
        //[graph removeAnnotation:symbolTextAnnotation];
        symbolTextAnnotation = nil;
    }

    // Setup a style for the annotation
    CPTMutableTextStyle *hitAnnotationTextStyle = [CPTMutableTextStyle textStyle];
    hitAnnotationTextStyle.color = [CPTColor whiteColor];
    hitAnnotationTextStyle.fontSize = 16.0f;
    hitAnnotationTextStyle.fontName = @"Helvetica-Bold";

    // Determine point of symbol in plot coordinates
    NSNumber *x = [[plotData objectAtIndex:index] valueForKey:@"x"];
    NSNumber *y = [[plotData objectAtIndex:index] valueForKey:@"y"];
    NSArray *anchorPoint = [NSArray arrayWithObjects:x, y, nil];
    NSLog(@"x %@, y %@",[[plotData objectAtIndex:index] valueForKey:@"x"],y);

    // Add annotation
    // First make a string for the y value
    NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease];
    [formatter setMaximumFractionDigits:2];
    NSString *yString = [formatter stringFromNumber:y];

    // Now add the annotation to the plot area
    CPTTextLayer *textLayer = [[[CPTTextLayer alloc] initWithText:yString style:hitAnnotationTextStyle] autorelease];
    symbolTextAnnotation = [[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:graph.defaultPlotSpace anchorPlotPoint:anchorPoint];
    symbolTextAnnotation.contentLayer = textLayer;
    symbolTextAnnotation.displacement = CGPointMake(0.0f, 20.0f);
    [graph.plotAreaFrame.plotArea addAnnotation:symbolTextAnnotation];   
    //[graph addAnnotation:symbolTextAnnotation];

    CPTAnnotation *annot = [graph.plotAreaFrame.plotArea.annotations objectAtIndex:0];
    NSLog(@"annot: %@", annot);
}

1 Ответ

0 голосов
/ 10 августа 2011

Это код, который определяет точку привязки:

NSNumber *x = [[plotData objectAtIndex:index] valueForKey:@"x"];
NSNumber *y = [[plotData objectAtIndex:index] valueForKey:@"y"];
NSArray *anchorPoint = [NSArray arrayWithObjects:x, y, nil];

Предполагается, что данные вашего графика хранятся в виде объектов NSNumber в массиве словарей с ключами "x" и "y". Если ваши данные хранятся другим способом, вам придется извлечь значения по-другому. Важно упаковать два числа в NSArray с первым значением x и вторым значением y.

...