Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 878
senthilM Avatar asked May 25 '10 12:05

senthilM


People also ask

How do you label the axes?

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.

What is an axis label on a chart?

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.


2 Answers

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.

like image 191
Brad Larson Avatar answered Oct 04 '22 00:10

Brad Larson


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.

like image 37
Bjørn Egil Avatar answered Oct 03 '22 22:10

Bjørn Egil