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

2

17

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?

Dominique answered 25/5, 2010 at 12:32 Comment(0)
A
34

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.

Alsatia answered 25/5, 2010 at 13:11 Comment(2)
customTickLocations contain 5,10...its related to x-axis range? as i have x-axis containg values in seconds then what whould be values in customTickLocations in my case ..thank youCasuistry
@Casuistry Custom Labels can be added for pie charts in the exact same way as in the answer...you're welcome. You can work on custom tick locations as mentioned in this SO answer...you're welcome!Fillmore
Z
1

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.

Zalucki answered 16/2, 2013 at 12:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.