Hi All,
I have become somewhat frustrated in understanding how to place XAxis Labels on a Chart I am in the process of creating.
I hope that I have supplied enough detail for you to steer me in the right direction. I have spent countless hours searching the internet but end up banging my head up against the wall as there appears to be so many variations.
The first graphic is a screen dump of the type of graph I want to create – so far all is well.
The included code gives you an idea how I got there, be it incorrect or not.
- (void)setXAxis {
// Collect Dates from fuelDetailsForSelectedBike Which Will Be Used In X-Axis Labels
NSMutableArray *xAxisLabels = [@[] mutableCopy];
for (NSArray *details in self.fuelDetailsForSelectedBike){
[xAxisLabels addObject:details[1]];
}
// Fuel X-Axis Set
self.fuelAxisSet = (CPTXYAxisSet *)self.graph.axisSet;
self.fuelAxisSet.xAxis.plotSpace = self.plotSpace;
self.fuelAxisSet.xAxis.visibleAxisRange = [CPTPlotRange plotRangeWithLocation:@0.0 length:@([self.fuelDetailsForSelectedBike count])];
self.fuelAxisSet.xAxis.majorIntervalLength = [NSNumber numberWithFloat:2.0f];
self.fuelAxisSet.xAxis.labelTextStyle = self.axixLabelTextStyle;
self.fuelAxisSet.xAxis.minorTicksPerInterval = 5;
self.fuelAxisSet.xAxis.majorTickLineStyle = self.axisLineStyle;
self.fuelAxisSet.xAxis.minorTickLineStyle = self.axisLineStyle;
self.fuelAxisSet.xAxis.axisLineStyle = self.axisLineStyle;
self.fuelAxisSet.xAxis.minorTickLength = 5.0f;
self.fuelAxisSet.xAxis.majorTickLength = 7.0f;
self.fuelAxisSet.xAxis.labelOffset = 5.0f;
self.fuelAxisSet.xAxis.title = @"Date";
self.fuelAxisSet.xAxis.titleTextStyle = self.graphXTitleTextStyle;
self.fuelAxisSet.xAxis.titleOffset = 20.0f;
// X-Axis Labels
// ?????????????
The second graphic is an Array that holds the Labels I wish to place on the X-Axis.
What would be my next steps from here??
Pete
CPTNumberArray *customTickLocations = @[@1, @5, @10, @15];
CPTStringArray *xAxisLabels = @[@"Label A", @"Label B", @"Label C", @"Label D"];
x.labelingPolicy = CPTAxisLabelingPolicyNone;
NSUInteger labelLocation = 0;
CPTMutableAxisLabelSet *customLabels = [NSMutableSet setWithCapacity:xAxisLabels.count];
for ( NSNumber *tickLocation in customTickLocations ) {
CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:xAxisLabels[labelLocation++] textStyle:x.labelTextStyle];
newLabel.tickLocation = tickLocation;
newLabel.offset = x.labelOffset;
newLabel.rotation = CPTFloat(M_PI_4);
[customLabels addObject:newLabel];
}
x.axisLabels = customLabels;
x.majorTickLocations = [NSSet setWithArray:customTickLocations];