ios - 核心剧情: axis alternatingBandFills with custom axis labels

标签 ios objective-c colors core-plot

我正在使用 Core Plot 库在 iOS 上绘制图形。我想在 yAxis 的网格线之间添加颜色。我已经设法通过设置轴的 alternatingBandFills 属性来做到这一点。但是,我也必须在 yAxis 上使用自定义标签,并且当我提供自定义标签时,alternatingBandFills 属性由于某种原因不起作用。

任何有关如何为 y 轴上的网格线之间的空间添加颜色以及使用自定义标签的帮助将不胜感激。

我现在使用的代码如下所示:

    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.hostedGraph.axisSet;
    CPTXYAxis *yAxis = axisSet.yAxis; 

    yAxis.orthogonalCoordinateDecimal = CPTDecimalFromDouble(minValueX);        
    yAxis.labelingPolicy = CPTAxisLabelingPolicyNone;

    NSArray *yAxisTickLocations = [NSArray arrayWithObjects:
                                   [NSDecimalNumber numberWithDouble:lowerRedRangeFrom],
                                   [NSDecimalNumber numberWithDouble:lowerOrangeRangeFrom],
                                   [NSDecimalNumber numberWithDouble:greenRangeFrom],
                                   [NSDecimalNumber numberWithDouble:upperOrangeRangeFrom],
                                   [NSDecimalNumber numberWithDouble:upperRedRangeFrom],
                                   [NSDecimalNumber numberWithDouble:upperRedRangeTo],
                                   nil];
    NSArray *yAxisLabels = [NSArray arrayWithObjects:@"Label1",@"Label2", @"Label3",@"Label4",@"Label5",@"Label6", nil];

    NSUInteger labelLocationY = 0;
    NSMutableArray *customLabelsY = [NSMutableArray arrayWithCapacity:[yAxisLabels count]];
    for (NSNumber *tickLocation in yAxisTickLocations) {

        CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText: [yAxisLabels objectAtIndex:labelLocationY++] textStyle:axisSet.xAxis.labelTextStyle];
        newLabel.tickLocation = [tickLocation decimalValue];
        newLabel.offset = axisSet.xAxis.labelOffset + axisSet.xAxis.majorTickLength;
        newLabel.rotation = M_PI/4;
        [customLabelsY addObject:newLabel];
    }

    axisSet.yAxis.axisLabels =  [NSSet setWithArray:customLabelsY];
    yAxis.alternatingBandFills = [NSArray arrayWithObjects:
                                  [CPTColor redColor],
                                  [CPTColor orangeColor],
                                  [CPTColor greenColor],
                                  [CPTColor orangeColor],
                                  [CPTColor redColor], nil];

最佳答案

我想通了:

轴的标签策略应该是:CPTAxisLabelingPolicyLocationsProvided,文档指出:“用户设置刻度位置;轴制作标签。”。

现在我们只需要指定刻度的位置。这是通过创建一个包含位置的 NSSet 对象来完成的。然后我们必须设置轴的 majorTickLocations 属性。

所以我的代码现在看起来像:

    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.hostedGraph.axisSet;
    CPTXYAxis *yAxis = axisSet.yAxis;

    yAxis.orthogonalCoordinateDecimal = CPTDecimalFromDouble(minValueX);        
    yAxis.labelingPolicy = CPTAxisLabelingPolicyLocationsProvided;

    NSSet *majorTickLocations = [NSSet setWithObjects:
                                 [NSDecimalNumber numberWithDouble:lowerRedRangeFrom],
                                 [NSDecimalNumber numberWithDouble:lowerOrangeRangeFrom],
                                 [NSDecimalNumber numberWithDouble:greenRangeFrom],
                                 [NSDecimalNumber numberWithDouble:upperOrangeRangeFrom],
                                 [NSDecimalNumber numberWithDouble:upperRedRangeFrom],
                                 [NSDecimalNumber numberWithDouble:upperRedRangeTo],
                                 nil];
    yAxis.majorTickLocations = majorTickLocations;

    yAxis.alternatingBandFills = [NSArray arrayWithObjects:
                                  [CPTColor redColor],
                                  [CPTColor orangeColor],
                                  [CPTColor greenColor],
                                  [CPTColor orangeColor],
                                  [CPTColor redColor], nil];

关于ios - 核心剧情: axis alternatingBandFills with custom axis labels,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9416254/

相关文章:

ios - 如何取消 UIViews 基于 block 的动画?

ios - NSFileManager 创建目录错误 518 NSFileWriteUnsupportedSchemeError

c++ - 更改 cout 文本的颜色 C++

vim - 启动 vim 时是否可以将配色方案作为参数传递?

ios - alamofire 在模拟器中给出了准确的响应,但在设备中给出了错误

ios - 如何使用objective c在iOS中设置起始viewcontroller.xib

iphone - 迭代 NSMutableArray 时出现 "out of scope"错误

Javascript单独循环多个div的背景颜色

iOS 添加受信任的根证书 - 公共(public)还是私有(private)?

objective-c - 更改窗口按钮的控制色调