iPhone CorePlot proper formatting of y-axis
Asked Answered
C

3

7

I am using CorePlot to graph a set of data. Data for my x-axis consists of dates, while data for my y-axis consists of floats (which I then turn into NSNumber). I am getting data from a feed, and the feed returns numbers with a large number of decimals, eg: 0.46673718852844, 4.59392222219, 353.1293012045. I'm using the following piece of code to properly format y-axis:

NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setMaximumFractionDigits:3];
[numberFormatter setPositiveFormat:@"###0.000"];    
CPTXYAxis *y = axisSet.yAxis;
y.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
y.minorTicksPerInterval = 1;
y.preferredNumberOfMajorTicks = 5;
y.labelFormatter = numberFormatter;

In most cases, everything works fine. However, sometimes it displays the same value at multiple positions along the axis. See image below:

enter image description here

The values are presented correctly, I would just like to avoid unnecessary (0.466) labels. I've even tried to round the numbers to 3 decimals in numberForPlot method:

if(fieldEnum == CPTScatterPlotFieldY)
{ 
        float floatNum = r.value;
        floatNum *= 1000;
        int intValue = (int) floatNum;
        float decimal = floatNum - intValue;
        if (decimal > 0.5) {
            intValue++;
        }
        floatNum = intValue /1000.0;
        return [NSNumber numberWithFloat:floatNum];

}

but there is no difference in labels.

Cadel answered 7/11, 2011 at 20:34 Comment(0)
C
1

The preferredNumberOfMajorTicks property tells the labeling algorithm how many tick marks to use. If you want three ticks, set it to 3. You could also increase the maximum number of fraction digits for the number formatter to avoid the rounding issue altogether.

Condolent answered 8/11, 2011 at 1:48 Comment(2)
the problem is that this is not the only graph in my application, and 5 ticks usually look the best. the same thing with digits - three is usually enough, no need to show 4 digits. ah, guess I will have to do some manual setup of the graph. thanks anywayCadel
My solution was to dynamically change the number of ticks depending on the axis range.Standley
W
0

I believe that because you have said that you want 5 major ticks, that is what you are going to get, regardless of how close the y axis values are. If you sense that the values are too close together, you will probably have to adjust your preferredNumberOfMajorTicks property.

In my app that I am using Core Plot, I turned off the automatic axis labeling and I am putting together my own x and y axis labels. It is a bit more work, but i like the flexibility of doing it myself.

Wraith answered 7/11, 2011 at 21:6 Comment(1)
@maggie a little late here, but that would be: CPTAxisLabelingPolicyNoneVancouver
S
0

What I did here was dynamically set my preferredNumberOfMajorTicks property while the graph is plotted. Since my formatter shows 3 decimal places, I used the following code to change the number of ticks depending on the Y axis range so that the formatter will show a unique value on the axis at each tick. Too many ticks means the decimal places get cut off and there are duplicate values on the Y axis.

            CPTXYAxis *y          = axisSet.yAxis;
            {
                y.preferredNumberOfMajorTicks = round([newYRange.length floatValue]/0.001);
            }

I hope this helps.

Standley answered 25/1, 2017 at 6:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.