user969043
user969043

Reputation: 766

Core Plot and displaying y label

I've run into some issues with displaying my y axis labels properly with iOS and Core Plot...

I'll show you the two different scenarios I currently have, and hopefully someone can help me get one of them working properly...

Okay, so first scenario: The code:

// Configure y-axis
CPTAxis *y = axisSet.yAxis;
y.axisLineStyle = axisLineStyle;
y.majorGridLineStyle = customGridStyle;
y.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
y.labelTextStyle = axisTextStyle;
y.majorTickLineStyle = axisLineStyle;
y.majorTickLength = 0.0f;
y.minorTickLength = 0.0f;
y.tickDirection = CPTSignNegative;
NSInteger majorIncrement = [self findStep:(maxValue - minValue)/7];
CGFloat yMax = [self findMaximumValue:maxValue];
NSMutableSet *yLabels = [NSMutableSet set];
NSMutableSet *yMajorLocations = [NSMutableSet set];

int maxLocation = 0;

for (NSInteger j = 0; j <= yMax + majorIncrement; j += majorIncrement) {
    CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:@"%i", j] textStyle:y.labelTextStyle];
    NSDecimal location = CPTDecimalFromInteger(j);
    label.tickLocation = location;
    label.offset = 125;

    if (label) {
        [yLabels addObject:label];
    }

    [yMajorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:location]];
    if (maxLocation < [[NSDecimalNumber decimalNumberWithDecimal:location] intValue]) {
        maxLocation = [[NSDecimalNumber decimalNumberWithDecimal:location] intValue];
    }
}

y.gridLinesRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(0) length:CPTDecimalFromInteger(maxLocation)];
y.axisLabels = yLabels;
y.majorTickLocations = yMajorLocations;

Now this code works to a certain extent... See my screenshots below: Core Plot Core Plot 2

Here the part I would love to change is the decimal... I want to change the location decimal to now show a .0...

  1. scenario (the one I would really liked fixed) To save you the trouble of reading the below code, I can point out that the big change here is

    y.labelingPolicy = CPTAxisLabelingPolicyNone;

Code:

// Configure y-axis
CPTAxis *y = axisSet.yAxis;
y.axisLineStyle = axisLineStyle;
y.majorGridLineStyle = customGridStyle;
y.labelingPolicy = CPTAxisLabelingPolicyNone;
y.labelTextStyle = axisTextStyle;
y.majorTickLineStyle = axisLineStyle;
y.majorTickLength = 0.0f;
y.minorTickLength = 0.0f;
y.tickDirection = CPTSignPositive;
NSInteger majorIncrement = [self findStep:(maxValue - minValue)/7];
CGFloat yMax = [self findMaximumValue:maxValue];
NSMutableSet *yLabels = [NSMutableSet set];
NSMutableSet *yMajorLocations = [NSMutableSet set];

int maxLocation = 0;

for (NSInteger j = 0; j <= yMax + majorIncrement; j += majorIncrement) {
    CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:@"%i", j] textStyle:y.labelTextStyle];
    NSDecimal location = CPTDecimalFromInteger(j);
    label.tickLocation = location;
    label.offset = -25;

    if (label) {
        [yLabels addObject:label];
    }

    [yMajorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:location]];
    if (maxLocation < [[NSDecimalNumber decimalNumberWithDecimal:location] intValue]) {
        maxLocation = [[NSDecimalNumber decimalNumberWithDecimal:location] intValue];
    }
}

y.gridLinesRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(0) length:CPTDecimalFromInteger(maxLocation)];
y.axisLabels = yLabels;
y.majorTickLocations = yMajorLocations;
x.gridLinesRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(0) length:CPTDecimalFromInteger(maxLocation)];

and the plot come out as:

Core Plot 3Core Plot 4Core Plot 5

Now here a lot of y axises are missing...

Any help would be greatly appreciated!

Upvotes: 0

Views: 1870

Answers (1)

Eric Skroch
Eric Skroch

Reputation: 27381

With the CPTAxisLabelingPolicyNone labeling policy, you are in complete control of the tick locations and labels. It's up to you to decide how many ticks and labels to make and where to put them.

If all you want to do is change the number format on the axis, keep the CPTAxisLabelingPolicyAutomatic labeling policy (or any one of the others except CPTAxisLabelingPolicyNone), remove the code that creates tick locations and labels, and change the labelFormatter. You can use any NSFormatter. Create an NSNumberFormatter, configure it to display the desired number format, and set it to the labelFormatter property.

Upvotes: 2

Related Questions