Core plot: axis alternatingBandFills with custom axis labels

爷,独闯天下 提交于 2019-12-25 00:46:11

问题


I am using the Core Plot library for drawing graphs on the iOS. I want to add colors between the grid lines of the yAxis. I have managed to do it with setting the alternatingBandFills property of the axis. However, I have to use custom labels on the yAxis as well, and when I am providing the custom labels, the alternatingBandFills property does not work for some reason.

Any help how to add colors to the spaces between the grid lines on the yAxis as well as using custom labels will be much appreciated.

The code that I am using now looks like:

    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:


I have figured it out:

The labeling policy of the axis should be: CPTAxisLabelingPolicyLocationsProvided, for which the documentation states: " User sets tick locations; axis makes labels.".

Now we only need to specify the locations of the ticks. This is done by creating a NSSetobject with the locations. Then we have to set the majorTickLocations property of the axis.

So my code now looks like:

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


来源:https://stackoverflow.com/questions/9416254/core-plot-axis-alternatingbandfills-with-custom-axis-labels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!