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?
The proper form for a graph title is "y-axis variable vs. x-axis variable." For example, if you were comparing the the amount of fertilizer to how much a plant grew, the amount of fertilizer would be the independent, or x-axis variable and the growth would be the dependent, or y-axis variable.
Axis labels are words or numbers that mark the different portions of the axis. Value axis labels are computed based on the data displayed in the chart. Category axis labels are taken from the category headings entered in the chart's data range. Axis titles are words or phrases that describe the entire axis.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With