I'm completely new to Core Plot and have a working bar graph, but the visual is kind of useless for me unless I know which object is represented in each bar. I have seen that there is a method called 'fieldIdentifiers' but do not know how to implement it, nor can I find any documentation (if this even the correct method). Can you steer me in the right direction? Thanks!
How do I add labels to CPBarPlot barplots?
In order to manually set the labels for an axis in a Core Plot chart, you'll need to set the labelingPolicy of that axis to CPAxisLabelingPolicyNone
and provide the labels yourself. For example, the following code will set custom labels in a bar chart (drawn from code I'll add to the iPhone sample application at some point):
CPXYAxisSet *axisSet = (CPXYAxisSet *)barChart.axisSet;
CPXYAxis *x = axisSet.xAxis;
x.labelRotation = M_PI/4;
x.labelingPolicy = CPAxisLabelingPolicyNone;
NSArray *customTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:1], [NSDecimalNumber numberWithInt:5], [NSDecimalNumber numberWithInt:10], [NSDecimalNumber numberWithInt:15], [NSDecimalNumber numberWithInt:20], nil];
NSArray *xAxisLabels = [NSArray arrayWithObjects:@"Label A", @"Label B", @"Label C", @"Label D", @"Label E", nil];
NSUInteger labelLocation = 0;
NSMutableArray *customLabels = [[NSMutableArray alloc] initWithCapacity:[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];
This sets five custom labels (Label A, B, C, D, and E) at locations 1, 5, 10, 15, and 20 on the X axis.
You are my hero. I was able to take what you wrote and merge it into my code for a delicious label-y chart. Have you played with the new feature to set the labels at the tips of the bars? It's in the CPTestApp for the Mac, but it's opaque to me. –
Jamilla
Drew just added that a few days ago, and I can't quite figure out how it works. It has something to do with the -barLabelForBarPlot:recordIndex: delegate method, but I can't replicate the effect in my code. –
Maghutte
hi if you manually set labels for axis,when the values of barplots get changed,the position of axislabels are getting disturbed how to fix this issue? –
Toothache
© 2022 - 2024 — McMap. All rights reserved.