经过大量的投入,我无法弄清楚我错过了什么。我跟随了很多与自定义标签相关的例子,但我找不到答案。可能有什么不对?非常感谢您的帮助。
自定义标签定义:
NSMutableArray *customArray = [[NSMutableArray alloc] initWithCapacity:[xcLabels count]];
int idx = 0;
for (NSNumber *tickLocation in xcLabels) {
CPTAxisLabel *iLabel = [[CPTAxisLabel alloc] initWithText:[xcLabels objectAtIndex:idx++] textStyle:x.labelTextStyle];
iLabel.tickLocation = CPTDecimalFromInt(idx);
iLabel.offset = x.labelOffset + x.majorTickLength;
iLabel.rotation = M_PI/2;
[customArray addObject:iLabel];
[iLabel release];
}
x.axisLabels = [NSSet setWithArray:customArray];
NSLog(@"x.axisLabels%@", x.axisLabels);
[customArray release];
majorTicklocation定义:
NSMutableArray *ticks = [[[NSMutableArray alloc] initWithCapacity:[xcLabels count]] autorelease];
for ( NSUInteger loc = 0; loc < [xcLabels count]; ++loc ) {
[ticks addObject:[NSDecimalNumber numberWithUnsignedInteger:loc]];}
NSLog(@"tickLocations%@", ticks);
x.majorTickLocations =[NSSet setWithArray:ticks];
NSLog(@"x.majorTickLocations%@", x.majorTickLocations);
数据定义
- (void) xLabels {
xcLabels = [[NSMutableArray alloc] initWithCapacity:[self.dataGraph count]];
for (int i = 0; i < [self.dataGraph count]; ++i) {
NSNumber* nsValue = [self.dataGraph objectAtIndex:i];
CGPoint point = [nsValue CGPointValue];
NSLog(@"point.x%f", point.x);
NSDate *xFecha = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:point.x];
NSLog(@"xFecha%@", xFecha);
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd-MM"];
NSString *nstFecha = [df stringFromDate:xFecha];
[df release];
[xcLabels addObject:nstFecha];}}
x.axisLabels输出:
x.axisLabels{(
<<CPTAxisLabel: 0x6e7c250> {<<<CPTTextLayer: 0x6e7bf60> bounds: {{0, 0}, {35, 19}}> "14-01">}>,
<<CPTAxisLabel: 0x6e7c040> {<<<CPTTextLayer: 0x6e7da30> bounds: {{0, 0}, {35, 19}}> "28-01">}>,
<<CPTAxisLabel: 0x6e7dae0> {<<<CPTTextLayer: 0x6e7db10> bounds: {{0, 0}, {35, 19}}> "14-02">}>,
<<CPTAxisLabel: 0x6e7dbc0> {<<<CPTTextLayer: 0x6e7dbf0> bounds: {{0, 0}, {35, 19}}> "25-02">}>,
<<CPTAxisLabel: 0x6e7dca0> {<<<CPTTextLayer: 0x6e7dcd0> bounds: {{0, 0}, {35, 19}}> "14-03">}>,
<<CPTAxisLabel: 0x6e7dd80> {<<<CPTTextLayer: 0x6e7ddb0> bounds: {{0, 0}, {35, 19}}> "30-03">}>,
<<CPTAxisLabel: 0x6e7de60> {<<<CPTTextLayer: 0x6e7de90> bounds: {{0, 0}, {35, 19}}> "14-04">}>,
<<CPTAxisLabel: 0x6e7df40> {<<<CPTTextLayer: 0x6e7df70> bounds: {{0, 0}, {35, 19}}> "29-04">}>,
<<CPTAxisLabel: 0x6e7e020> {<<<CPTTextLayer: 0x6e7e050> bounds: {{0, 0}, {35, 19}}> "09-05">}>,
<<CPTAxisLabel: 0x6e7e100> {<<<CPTTextLayer: 0x6e7e190> bounds: {{0, 0}, {35, 19}}> "29-08">}>,
<<CPTAxisLabel: 0x6e7e200> {<<<CPTTextLayer: 0x6e7e230> bounds: {{0, 0}, {35, 19}}> "29-09">}>,
<<CPTAxisLabel: 0x6e7e2e0> {<<<CPTTextLayer: 0x6e7e370> bounds: {{0, 0}, {35, 19}}> "27-10">}>,
<<CPTAxisLabel: 0x6e7e3e0> {<<<CPTTextLayer: 0x6e7e410> bounds: {{0, 0}, {35, 19}}> "29-11">}>,
<<CPTAxisLabel: 0x6e7e4c0> {<<<CPTTextLayer: 0x6e7e550> bounds: {{0, 0}, {35, 19}}> "28-12">}>
)}
x.majorTickLocations输出
2012-05-29 22:21:46.960 AbcPrpk[8931:fb03] x.majorTickLocations{(
0,
13,
12,
11,
7,
6,
2,
1,
10,
9,
8,
5,
4,
3
)}
答案 0 :(得分:1)
您已将所有自定义标签的tickLocation
设置为零(0)。在循环内增加idx
。
答案 1 :(得分:0)
我没有看到上面的任何代码告诉我你的标签被添加到视图树中(例如,[self addSubview:someLabel])
您可以通过在任何标签上记录窗口属性的值来验证这是您的问题 - 如果它返回nil,那么您的视图尚未添加到视图树中。