2012-02-23 94 views
0

我正在使用Core Plot库在iOS上绘制图形。我想在yAxis的网格线之间添加颜色。我设法通过设置axis的alternatingBandFills属性来完成它。但是,我也必须在yAxis上使用自定义标签,并且当我提供自定义标签时,alternatingBandFills属性由于某种原因而不起作用。核心图:使用自定义轴标签交替填充轴

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

,我现在使用的代码看起来像:

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]; 

回答

1

我已经想通了:

轴的标签策略应该是: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];