Objective c 核心绘图,如何更改负轴刻度标签的颜色

Objective c 核心绘图,如何更改负轴刻度标签的颜色,objective-c,core-plot,Objective C,Core Plot,我正在使用core plot library在objective-c中绘制图形,是否要更改负轴标签的颜色?我怎样才能做到这一点?根据您需要的灵活性,至少有三种不同的方法 使用两个y轴。设置它们的方式相同,只是在一个上设置可见范围以覆盖正值,另一个覆盖负值。根据需要为每个标签设置labelTextStyle和/或labelFormatter 使用axis委托并实现-axis:shouldUpdateAxisLabelsLocations:委托方法。返回NO并在每个提供的位置制作自定义标签。这适用于

我正在使用core plot library在objective-c中绘制图形,是否要更改负轴标签的颜色?我怎样才能做到这一点?

根据您需要的灵活性,至少有三种不同的方法

  • 使用两个y轴。设置它们的方式相同,只是在一个上设置可见范围以覆盖正值,另一个覆盖负值。根据需要为每个标签设置
    labelTextStyle
    和/或
    labelFormatter

  • 使用axis委托并实现
    -axis:shouldUpdateAxisLabelsLocations:
    委托方法。返回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;
    }
    
  • 使用
    cptaxislabelingpolicyne
    标签策略。这是最灵活的,但也是最有效的,因为除了制作自定义标签外,还必须计算记号位置


  • 根据您需要的灵活性,至少有三种不同的方法可以做到这一点

  • 使用两个y轴。设置它们的方式相同,只是在一个上设置可见范围以覆盖正值,另一个覆盖负值。根据需要为每个标签设置
    labelTextStyle
    和/或
    labelFormatter

  • 使用axis委托并实现
    -axis:shouldUpdateAxisLabelsLocations:
    委托方法。返回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;
    }
    
  • 使用
    cptaxislabelingpolicyne
    标签策略。这是最灵活的,但也是最有效的,因为除了制作自定义标签外,还必须计算记号位置


  • 感谢这些解决方案,在第1种方式中,我必须自己定义+ve/-ve范围,但我正在使用core plot为给定数据定义的自动范围。对于第二种方式,您可以列出axis的代理名称和-axis:shouldUpdateAxisLabelsLocations:this以及在此代理方法中如何分离+ve axis和-ve?制作标签时请查看位置。Eric,我使用了CPTAxisDelegate并将-axis:shouldUpdateAxisLabelsLocations:this实现到我的.m文件中,但该方法不会在那里触发。你能举出它的任何例子吗?你是否设置了委托对象的
    axis.delegate
    ?是的axis.delegate=self我的界面有这个CPTAxisDelegate委托。感谢这些解决方案,在第一种方式中,我必须自己定义+ve/-ve范围,但我使用的是由core plot为我给定的数据定义的自动范围。对于第二种方式,您可以列出axis的代理名称和-axis:shouldUpdateAxisLabelsLocations:this以及在此代理方法中如何分离+ve axis和-ve?制作标签时请查看位置。Eric,我使用了CPTAxisDelegate并将-axis:shouldUpdateAxisLabelsLocations:this实现到我的.m文件中,但该方法不会在那里触发。您可以列出它的任何示例吗?您是否已将
    axis.delegate
    设置为委托对象?yup axis.delegate=self我的界面具有此CPTAxisDelegate委托。