2012-01-29 67 views

回答

4

至少有三种不同的方法可以做到这一点,具体取决于您需要多大的灵活性。

  1. 使用两个y轴。将它们设置为相同,除了将可见范围设置为覆盖正值和负值。根据需要为每一个设置labelTextStyle和/或labelFormatter

  2. 使用一个轴代表与实施-axis:shouldUpdateAxisLabelsAtLocations:委托方法。返回NO并在每个提供的位置制作自定义标签。这适用于任何标签政策。

    -(BOOL)axis:(CPTAxis *)axis shouldUpdateAxisLabelsAtLocations:(NSSet *)locations 
    { 
        static CPTTextStyle *positiveStyle = nil; 
        static CPTTextStyle *negativeStyle = nil; 
    
        NSNumberFormatter *formatter = axis.labelFormatter; 
        CGFloat labelOffset   = axis.labelOffset; 
        NSDecimalNumber *zero  = [NSDecimalNumber zero]; 
    
        NSMutableSet *newLabels = [NSMutableSet set]; 
    
        for (NSDecimalNumber *tickLocation in locations) { 
         CPTTextStyle *theLabelTextStyle; 
    
         if ([tickLocation isGreaterThanOrEqualTo:zero]) { 
          if (!positiveStyle) { 
           CPTMutableTextStyle *newStyle = [axis.labelTextStyle mutableCopy]; 
           newStyle.color = [CPTColor greenColor]; 
           positiveStyle = newStyle; 
          } 
          theLabelTextStyle = positiveStyle; 
         } 
         else { 
          if (!negativeStyle) { 
           CPTMutableTextStyle *newStyle = [axis.labelTextStyle mutableCopy]; 
           newStyle.color = [CPTColor redColor]; 
           negativeStyle = newStyle; 
          } 
          theLabelTextStyle = negativeStyle; 
         } 
    
         NSString *labelString  = [formatter stringForObjectValue:tickLocation]; 
         CPTTextLayer *newLabelLayer = [[CPTTextLayer alloc] initWithText:labelString style:theLabelTextStyle]; 
    
         CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithContentLayer:newLabelLayer]; 
         newLabel.tickLocation = tickLocation.decimalValue; 
         newLabel.offset  = labelOffset; 
    
         [newLabels addObject:newLabel]; 
    
         [newLabel release]; 
         [newLabelLayer release]; 
        } 
    
        axis.axisLabels = newLabels; 
    
        return NO; 
    } 
    
  3. 使用CPTAxisLabelingPolicyNone标签策略。这是最灵活的,也是最重要的工作,因为除了制作自定义标签之外,您还必须计算刻度位置。

+0

感谢这些解决方案,在第一种方法中,我必须自己定义+ ve/-ve范围,但我使用由核心图定义的自动范围来存储给定的数据。第二种方法可以列出axis的委托名和-axis的定义:shouldUpdateAxisLabelsAtLocations:this,并且在这个委托方法中,我将如何分离+ ve轴和-ve? – utariq 2012-01-30 11:37:06

+0

查看制作标签时的位置。用它来决定使用哪种文本样式。 – 2012-01-30 11:55:28

+0

埃里克,我用CPTAxisDelegate和实现-axis:shouldUpdateAxisLabelsAtLocations:这到我的.m文件,但是这种方法没有触发那里。你能列出它的任何例子吗? – utariq 2012-01-31 10:38:01

相关问题