如何向CPBarPlot条形图添加标签? [英] How do I add labels to CPBarPlot barplots?

查看:210
本文介绍了如何向CPBarPlot条形图添加标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Core Plot完全不熟悉并且有一个工作条形图,但视觉对我来说没用,除非我知道每个条形图中都有哪个对象。我已经看到有一个名为'fieldIdentifiers'的方法,但不知道如何实现它,也找不到任何文档(如果这是正确的方法)。你能引导我朝着正确的方向前进吗?谢谢!

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!

推荐答案

为了在Core Plot图表中手动设置轴的标签,您需要设置将该轴的标签标记为 CPAxisLabelingPolicyNone 并自行提供标签。例如,以下代码将在条形图中设置自定义标签(从我将在某个时刻添加到iPhone示例应用程序的代码中提取):

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];

这会在位置1设置五个自定义标签(标签A,B,C,D和E) X轴上有5,10,15和20个。

This sets five custom labels (Label A, B, C, D, and E) at locations 1, 5, 10, 15, and 20 on the X axis.

这篇关于如何向CPBarPlot条形图添加标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆