如何为Core Plot图表的轴提供标签?

sen*_*thu 17 iphone core-plot

我正在使用Core Plot框架,并尝试通过示例应用程序.有没有使用此框架的教程?

具体来说,如何在图表中为X轴和Y轴提供标签?

Bra*_*son 35

遗憾的是,鉴于框架的API尚未稳定,因此没有那么多教程,而且确实存在的一些教程已经过时了.示例应用程序确实是了解如何使用框架的最佳来源.

例如,要提供自定义轴标签,请查看CPTestApp-iPhone的CPTestAppBarChartController.m源文件.该示例中的条形图具有由以下代码定义的自定义X轴标签:

// Define some custom labels for the data elements
x.labelRotation = M_PI/4;
x.labelingPolicy = CPAxisLabelingPolicyNone;
NSArray *customTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:1], [NSDecimalNumber numberWithInt:5], [NSDecimalNumber numberWithInt:10], [NSDecimalNumber numberWithInt:15], nil];
NSArray *xAxisLabels = [NSArray arrayWithObjects:@"Label A", @"Label B", @"Label C", @"Label D", @"Label E", nil];
NSUInteger labelLocation = 0;
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[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];
Run Code Online (Sandbox Code Playgroud)

首先,将轴标签策略设置CPAxisLabelingPolicyNone为让框架知道您将提供自定义标签,然后创建带有相应位置的标签数组,最后将该标签数组分配给axisLabelsX轴上的属性.