2012-02-23 154 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];