我正在使用Core Plot框架,并尝试完成示例应用程序。有没有使用这个框架的教程?
具体地说,如何在图表中为X和Y轴提供标签?
发布于 2010-05-25 21:11:20
不幸的是,考虑到框架的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];首先,将轴标签策略设置为CPAxisLabelingPolicyNone,以让框架知道您将提供自定义标签,然后创建一个标签数组及其相应位置,最后将该标签数组分配给X轴上的axisLabels属性。
发布于 2013-02-16 20:44:01
若要提供自定义格式化标签,可以为轴的labelFormatter属性指定格式化程序。您可以使用根据您的首选项配置的NSNumberFormatter,例如:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
// ... configure formatter
CPTXYAxis *y = axisSet.yAxis;
y.labelFormatter = formatter;或者,如果需要更具体的格式设置,可以子类化NSNumberFormatter并覆盖stringForObjectValue:方法以执行所需的确切格式设置。
https://stackoverflow.com/questions/2904562
复制相似问题