2011-08-09 8 views
1

私は最初の1つであり、現在注釈を実装しようとしています。私はアノテーションを記録しており、x座標とy座標はnullです。おかげcore-plot注釈座標がnullを返す

-(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); 
} 

答えて

0

これは、アンカーポイントを決定するコードである:

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

それはあなたのプロットデータを「X」と「Y」キーで辞書の配列内のNSNumberオブジェクトとして格納されている前提としています。データが別の方法で保存されている場合は、値を別々に抽出する必要があります。重要なことは、最初のx値と2番目のy値でNSArrayの2つの数値をパッケージ化することです。

+0

問題はこれらのxとyの値を取得していますが、正しい方向に私を誘導している可能性があります。私はキー名が違うと思う。あなたが私のコードで気づいたら、nullを返す私のxとyを記録するlog文があります。情報をありがとう – zambono

関連する問題