简体   繁体   中英

iOS CorePlot numeric formatting of y-axis

I'm working with Core Plot 1.1 to draw a simple scatter plot in iOS6. I am using the following code to properly format my y-axis which then dynamically scales to the plot data.

CPTXYAxis *y = axisSet.yAxis;
y.labelingPolicy              = CPTAxisLabelingPolicyAutomatic;
y.minorTicksPerInterval       = 3;
y.preferredNumberOfMajorTicks = 6;
y.majorGridLineStyle          = majorGridLineStyle;
y.minorGridLineStyle          = minorGridLineStyle;

...

NSNumberFormatter * yformatter =  [[NSNumberFormatter alloc] init];
[yformatter setUsesSignificantDigits:YES];
[yformatter setMaximumSignificantDigits:4];
[yformatter setMaximumFractionDigits:1];
[yformatter setRoundingMode:NSNumberFormatterRoundCeiling];
y.labelFormatter = yformatter;

Then I dynamically change the range based on the data to be plotted using maxPlotValue but bound it to a minimum.

plotSpace.xRange = [CPTPlotRange
                    plotRangeWithLocation:CPTDecimalFromFloat(0)
                    length:CPTDecimalFromFloat(5)];
plotSpace.yRange = [CPTPlotRange
                    plotRangeWithLocation:CPTDecimalFromFloat(0)
                    length:CPTDecimalFromFloat(maxPlotValue)];

This works great in most cases but sometimes I get a strange formatting error like in the below fig 1 where 0.6001 is displayed in stead of 0.6. If I manually change the minimum range to 2 the error disappears.

The reason I'm using 4 significant digits is that I can have numbers up to 8000 and then they are displayed without the fraction. If I change the setMaximumSignificantDigits to 3 I get 0.601 which I guess indicates that the problem is with the CPTAxisLabelingPolicyAutomatic.

Any help on this would be greatly appreciated.

Fig 1, Error in formatting: https://dl.dropbox.com/u/8083213/fig_1.png

Fig 2, No error in formatting: https://dl.dropbox.com/u/8083213/fig_2.png

This sounds like a rounding error in the labeling calculations. Please report it on the Core Plot issue tracker .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM