开发者

How do I add labels to CPBarPlot barplots?

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 th开发者_StackOverflow社区ere 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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜