现在的位置: 首页 > 综合 > 正文

CorePlot学习三—自定义CorePlot label及majorGridLine莫名其妙消失的Bug

2014年09月05日 ⁄ 综合 ⁄ 共 2640字 ⁄ 字号 评论关闭

转载于:点击打开链接

今天在项目中为了实现一个可以显示自定义样式的x轴label,整了我一个下午,搞死我了。先上图:

    

    图中的表格可以左右拖动,不允许上下拖动。而且只允许显示当前三十天的数据,且今天的标签显示白色。

设置corePlot可拖动:

  1. plotSpace.allowsUserInteraction = YES;  

设置x显示的范围:

  1. plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(8 * oneDay)length:CPTDecimalFromInt(23 * oneDay)];  

设置x轴拖动范围:

  1. plotSpace.globalXRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInt(31 * oneDay)];  

禁止corePlot缩放(Self是CPTGraphHostingView):

  1. [self setAllowPinchScaling:NO];//禁止缩放  

自定义label:

  1. x.axisLabels = [self buildLabelTitle];  
  2. x.labelingPolicy = CPTAxisLabelingPolicyNone;  
  3.   
  4. //构建30天的label样式  
  5. - (NSMutableSet*)buildLabelTitle  
  6. {  
  7.     NSMutableSet *newAxisLabels = [NSMutableSet set];  
  8.       
  9.     CPTMutableTextStyle *textStyleB = [CPTMutableTextStyle textStyle];  
  10.     textStyleB.color = [CPTColor colorWithComponentRed:CPTFloat((float)0x09/0xFF) green:CPTFloat((float)0x31/0xFF) blue:CPTFloat((float)0x4A/0xFF) alpha:CPTFloat(1.0)];  
  11.     NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];  
  12.     [dateFormatter setDateFormat:@"MM.dd"];  
  13.   
  14.     int n = 1;  
  15.     for ( NSUInteger i = 29; i > 0; i--) //从今起前29天  
  16.     {  
  17.         NSTimeInterval secondsPerDay = 24 * 60 * 60 * i;  
  18.         NSDate *ago = [[NSDate alloc] initWithTimeIntervalSinceNow:-secondsPerDay];  
  19.         CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:[dateFormatter stringFromDate:ago]  
  20.                                                           textStyle:textStyleB];  
  21.         [ago release];  
  22.         newLabel.tickLocation = CPTDecimalFromUnsignedInteger(n++ * oneDay);  
  23.         newLabel.offset = 5;  
  24.         [self.locationLabels addObject:[NSNumber numberWithInt:(n-1) * oneDay]];  
  25.         [newAxisLabels addObject:newLabel];  
  26.         [newLabel release];  
  27.     }  
  28.       
  29.     //今天  
  30.     CPTMutableTextStyle *textStyleW = [CPTMutableTextStyle textStyle];  
  31.     textStyleW.color = [CPTColor whiteColor];  
  32.     CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:@"今日" textStyle:textStyleW];  
  33.     newLabel.tickLocation = CPTDecimalFromUnsignedInteger(oneDay * n);  
  34.     [self.locationLabels addObject:[NSNumber numberWithInt:(n) * oneDay]];  
  35.     newLabel.offset = 5;  
  36.     [newAxisLabels addObject:newLabel];  
  37.     [newLabel release];  
  38.       
  39.     return newAxisLabels;  
  40. }  

但是写好后发现majorGridLine不见了,之后各种折腾后终于找到原因是:

CPTAxisLabelingPolicyNone  
   
No labels provided; user sets labels and tick locations.

设置这个属性是要自己重新定义majorGridLine位置的

所以需要使用:

  1. x.majorTickLocations = [NSSet setWithArray:locationLabels];  

结果为:

抱歉!评论已关闭.