X-Axis Labels - Help !!

27 views
Skip to first unread message

PETE MOSCATT

unread,
Nov 5, 2016, 4:11:48 AM11/5/16
to coreplot-discuss

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

   

Eric

unread,
Nov 5, 2016, 9:27:15 AM11/5/16
to coreplot-discuss
To use custom axis labels, set the labelingPolicy to CPTAxisLabelingPolicyNone. This requires you to provide major and/or minor tick locations and labels. The label locations and the tick locations do not have to be the same. You won't have to set the majorIntervalLength or minorTicksPerInterval since those only apply to other labeling policies.

There are many examples of custom label code in the Core Plot examples and on the web. Here's one from the Plot Gallery example app:

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


Eric

PETE MOSCATT

unread,
Nov 9, 2016, 2:02:30 AM11/9/16
to coreplot-discuss
Thanks Eric,

One question, the first line in your example code:   CPTNumberArray *customTickLocations  = @[@1@5@10@15];
The way I am taking this is that you would use   customTickLocations @[@1@5@10@15]  if you had a constant number of items in the database.
In my case the number records in my database will change on a daily basis, so would I typically count the records and divide to create my *customTickLocations?

Pete

Eric

unread,
Nov 9, 2016, 7:09:06 AM11/9/16
to coreplot-discuss
The tick locations can be whatever you want. The labels can be at the same locations, or not. You don't have to label every tick mark and labels don't have to fall on a tick location at all.

Eric
Reply all
Reply to author
Forward
0 new messages