Kernel string: axis rotation BandFills with custom axis labels

I use the Core Plot library to draw graphs on iOS. I want to add colors between yAxis grid lines. I managed to do this by setting the alternatingBandFills property of the axis. However, I have to use custom shortcuts on yAxis, and when I provide custom shortcuts, the alternatingBandFills property does not work for some reason.

Any help on adding colors to the spaces between the grid lines on yAxis, as well as using custom shortcuts, would be greatly appreciated.

The code I'm using now looks like this:

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]; 
0
source share
1 answer

I understood:

The axis marking policy should be: CPTAxisLabelingPolicyLocationsProvided , for which the documentation states: "The user ticks, the axis makes marks."

Now we only need to indicate the location of the ticks. This is done by creating an NSSet object with locations. Then we must set the majorTickLocations property of the axis.

So now my code looks like this:

  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]; 
+1
source

Source: https://habr.com/ru/post/1397589/


All Articles