如何为Core Plot图表的轴提供标签? [英] How do you provide labels for the axis of a Core Plot chart?

查看:98
本文介绍了如何为Core Plot图表的轴提供标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using the Core Plot framework, and attempting to work through the example applications. Are there any tutorials for using this framework?

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

Specifically, how do you provide labels for X and Y axes in a chart?

推荐答案

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

Unfortunately, given the fact that the API of the framework has not yet stabilized, there aren't that many tutorials out there, and several of the ones that do exist are already outdated. The example applications really are the best sources to see how to work with the framework.

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

For example, to provide custom axis labels, take a look at the CPTestApp-iPhone's CPTestAppBarChartController.m source file. The bar chart in that example has custom X axis labels defined by the following code:

// 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 让框架知道您将提供自定义标签,然后创建一个带有相应位置的标签数组,最后将该标签数组分配给 axisLabels X轴上的属性。

First, you set the axis labeling policy to CPAxisLabelingPolicyNone to let the framework know you will be providing custom labels, then you create an array of labels with their corresponding locations, and finally you assign that array of labels to the axisLabels property on the X axis.

这篇关于如何为Core Plot图表的轴提供标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆