How do you provide labels for the axis of a Core Plot chart?

别说谁变了你拦得住时间么 提交于 2019-12-17 22:34:18

问题


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

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


回答1:


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.

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];

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.




回答2:


To provide custom formatted labels, you may assign a formatter to the labelFormatter property of an axis. You can either use the NSNumberFormatter configured to your preferences, e.g.:

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
// ... configure formatter

CPTXYAxis *y = axisSet.yAxis;
y.labelFormatter = formatter;

Or, if you need more specific formatting, you can subclass NSNumberFormatter and override the stringForObjectValue: method to do the exact formatting you would like.



来源:https://stackoverflow.com/questions/2904562/how-do-you-provide-labels-for-the-axis-of-a-core-plot-chart

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