如何向CPBarPlot条形图添加标签?

时间:2010-01-03 03:53:49

标签: iphone core-plot

我对Core Plot完全不熟悉并且有一个工作条形图,但视觉对我来说没用,除非我知道每个条形图中都有哪个对象。我已经看到有一个名为'fieldIdentifiers'的方法,但不知道如何实现它,也找不到任何文档(如果这是正确的方法)。你能引导我朝正确的方向前进吗?谢谢!

1 个答案:

答案 0 :(得分:9)

要在Core Plot图表中手动设置轴的标签,您需要将该轴的labeledPolicy设置为CPAxisLabelingPolicyNone并自行提供标签。例如,以下代码将在条形图中设置自定义标签(从我将在某个时刻添加到iPhone示例应用程序的代码中提取):

CPXYAxisSet *axisSet = (CPXYAxisSet *)barChart.axisSet;
CPXYAxis *x = axisSet.xAxis;
x.labelRotation = M_PI/4;
x.labelingPolicy = CPAxisLabelingPolicyNone;
NSArray *customTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:1], [NSDecimalNumber numberWithInt:5], [NSDecimalNumber numberWithInt:10], [NSDecimalNumber numberWithInt:15], [NSDecimalNumber numberWithInt:20], nil];
NSArray *xAxisLabels = [NSArray arrayWithObjects:@"Label A", @"Label B", @"Label C", @"Label D", @"Label E", nil];
NSUInteger labelLocation = 0;
NSMutableArray *customLabels = [[NSMutableArray alloc] initWithCapacity:[xAxisLabels count]];
for (NSNumber *tickLocation in customTickLocations)
{
    CPAxisLabel *newLabel = [[CPAxisLabel alloc] initWithText: [xAxisLabels objectAtIndex:labelLocation++] textStyle:x.labelTextStyle];
    newLabel.tickLocation = [tickLocation decimalValue];
    newLabel.offset = x.labelOffset + x.majorTickLength;
    newLabel.rotation = M_PI/4;
    [customLabels addObject:newLabel];
    [newLabel release];
}

x.axisLabels =  [NSSet setWithArray:customLabels];

在X轴上的位置1,5,10,15和20处设置五个自定义标签(标签A,B,C,D和E)。