iPhone CorePlot正确的y轴格式

时间:2022-10-19 13:47:17

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:

我正在使用CorePlot来绘制一组数据。x轴的数据由日期组成,y轴的数据由浮点数组成(然后将其转换为NSNumber)。我正在从提要中获取数据,提要返回带有大量小数的数字,例如:0.46673718852844、4.59392222219、353.1293012045。我正在使用以下代码来正确格式化y轴:

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:

在大多数情况下,一切正常。但是,有时它在轴的多个位置显示相同的值。见下图:

iPhone CorePlot正确的y轴格式

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:

这些值显示正确,我只想避免不必要的(0.466)标签。我甚至试着把数字四舍五入到3个小数在numberForPlot方法中:

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.

但是标签没有区别。

3 个解决方案

#1


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.

preferrednumberof马略蜱属性告诉标记算法要使用多少个标记。如果你想要三个节拍,把它设为3。您还可以增加numberformatter的最大小数位数,以避免完全的舍入问题。

#2


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.

我相信因为你说过你想要5个主要的节拍,这就是你要得到的,不管y轴的值有多接近。如果您觉得这些值太接近了,那么您可能需要调整preferrednumberofmajortick属性。

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.

在我的应用中,我使用Core Plot,我关闭了自动轴标,我将自己的x轴和y轴标放在一起。这是一个多一点的工作,但我喜欢自己做的灵活性。

#3


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.

我在这里所做的是在绘制图形时动态地设置我最喜欢的主要属性。由于formatter显示了3个小数点位置,所以我使用了以下代码,根据Y轴范围更改了滴答数,以便格式化程序在每次滴答时在轴上显示唯一的值。太多的刻度意味着小数点被切断,Y轴上有重复的值。

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

I hope this helps.

我希望这可以帮助。

#1


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.

preferrednumberof马略蜱属性告诉标记算法要使用多少个标记。如果你想要三个节拍,把它设为3。您还可以增加numberformatter的最大小数位数,以避免完全的舍入问题。

#2


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.

我相信因为你说过你想要5个主要的节拍,这就是你要得到的,不管y轴的值有多接近。如果您觉得这些值太接近了,那么您可能需要调整preferrednumberofmajortick属性。

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.

在我的应用中,我使用Core Plot,我关闭了自动轴标,我将自己的x轴和y轴标放在一起。这是一个多一点的工作,但我喜欢自己做的灵活性。

#3


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.

我在这里所做的是在绘制图形时动态地设置我最喜欢的主要属性。由于formatter显示了3个小数点位置,所以我使用了以下代码,根据Y轴范围更改了滴答数,以便格式化程序在每次滴答时在轴上显示唯一的值。太多的刻度意味着小数点被切断,Y轴上有重复的值。

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

I hope this helps.

我希望这可以帮助。