Core-Plot CPTScatterPlot data labels

霸气de小男生 提交于 2019-12-04 16:53:16

"Data labels" (labels above each point) do not appear by default. The automatic labels require both a labelTextStyle and labelFormatter. Both of these properties default to nil which hides the labels.

You can also make it appear on the graph if you touch via:

    -(void)scatterPlot:(CPTScatterPlot *)plot
plotSymbolWasSelectedAtRecordIndex:(NSUInteger)index
{
    int plotNumber = 0;

    for (int i = 0; i < [mainDataForPlot count]; i++)
    {
        if ([mainDataForPlot objectAtIndex:i] != [NSNull null]
            &&
            [(NSString *)plot.identifier isEqualToString:
             [NSString stringWithFormat:@"%@-%@",[[mainDataForPlot objectAtIndex:i] objectAtIndex:0],[[mainDataForPlot objectAtIndex:i] objectAtIndex:1]]
             ]) 
        {
            plotNumber = i;
            break;
        }
    }

    if (self.symbolTextAnnotation != nil) 
    {
        self.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 = [NSNumber numberWithInt:index];
    NSNumber *y = [[[mainDataForPlot objectAtIndex:plotNumber] objectAtIndex:3] objectAtIndex:index];
    NSArray *anchorPoint = [NSArray arrayWithObjects:x, y, nil];

    // 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];
    self.symbolTextAnnotation = [[[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:(CPTXYPlotSpace *)self.graph.defaultPlotSpace  anchorPlotPoint:anchorPoint] autorelease];
    self.symbolTextAnnotation.contentLayer = textLayer;
    self.symbolTextAnnotation.displacement = CGPointMake(0.0f, 20.0f);
    [graph.plotAreaFrame.plotArea addAnnotation:symbolTextAnnotation];

}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!