Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rounding error on Y-axis when all Y values are zeroes #1123

Closed
duzenko opened this issue Jun 10, 2016 · 19 comments
Closed

Rounding error on Y-axis when all Y values are zeroes #1123

duzenko opened this issue Jun 10, 2016 · 19 comments

Comments

@duzenko
Copy link

duzenko commented Jun 10, 2016

I can't get the Y-axis labels right using the latest version of the library
By default they include trailing zeroes:
simulator screen shot jun 10 2016 7 08 13 pm
Now I try to pass a custom number formatter so as to get rid of the zeroes and it works for that data set

                NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
                formatter.maximumSignificantDigits = 3;
                [chart set_defaultValueFormatter:formatter];

But when I pass all zeroes to a line chart I get an ugly rounding error:
simulator screen shot jun 10 2016 6 46 35 pm
Is there another way to get rid of the trailing zeroes, or am I doing something wrong?

@liuxuan30
Copy link
Member

have you tried allowRepeatingValue?

@duzenko
Copy link
Author

duzenko commented Jun 15, 2016

What is that? Can't see it here: https://jitpack.io/com/github/PhilJay/MPAndroidChart/v2.2.5/javadoc/

@liuxuan30
Copy link
Member

it's in iOS portion, I am not sure if you can see it on android side

@duzenko
Copy link
Author

duzenko commented Jun 15, 2016

What class is this method in and what does it do?

@liuxuan30
Copy link
Member

liuxuan30 commented Jun 15, 2016

should be similar to #315

@duzenko
Copy link
Author

duzenko commented Jun 15, 2016

Sorry, which of my two problems is this supposed to help? Trailing zeroes or rounding error?
And I can't see this property anywhere

@liuxuan30
Copy link
Member

liuxuan30 commented Jun 15, 2016

oops, I thought you are having duplicated zeroes. Are you asking about the number formatter?You can take a look at maximumFractionDigits and minimumFractionDigits

@duzenko
Copy link
Author

duzenko commented Jun 15, 2016

I think I explained my problem in the first post.
There is an ugly rounding error when I use formatter

@liuxuan30
Copy link
Member

as I said, maximumFractionDigits and minimumFractionDigits seems solution to me

@duzenko
Copy link
Author

duzenko commented Jun 16, 2016

Enlighten me - what values do I need to assign to maximumFractionDigits and minimumFractionDigits?

@liuxuan30
Copy link
Member

hmm, if you only want max two, like 0.11, it will avoid situation like 0.111111111111.
if you need minimum digits like 2, "1" would be "1.00".
So in your case, I would guess minimumFractionDigits = 0 and maximumFractionDigits = 1 or 2. Does this works for you?

@duzenko
Copy link
Author

duzenko commented Jun 17, 2016

Regret it does not

Test code

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    LineChartView *chart = [[LineChartView alloc] init];
    chart._defaultValueFormatter.maximumFractionDigits = 2;
    chart._defaultValueFormatter.minimumFractionDigits = 0;
    [self.view addSubview:chart];

    int N = 5, M = 11;
    NSMutableArray *vals = [NSMutableArray arrayWithCapacity:N], *xVals = [NSMutableArray arrayWithCapacity:M];
    for (int i=0; i<M; i++) {
        if (i < N) {
            ChartDataEntry *entry = [[ChartDataEntry alloc] initWithValue:98+i xIndex:vals.count];
            [vals addObject:entry];
        }
        [xVals addObject:[NSNumber numberWithInt:i]];
    }
    LineChartDataSet *dataSet = [[LineChartDataSet alloc] initWithYVals:vals];
    LineChartData *data = [[LineChartData alloc] initWithXVals:xVals dataSet:dataSet];
    [chart setData:data];
    chart.frame = CGRectMake(0, 30, self.view.frame.size.width, 100);
}

Result:
simulator screen shot jun 17 2016 11 31 30 am

@liuxuan30
Copy link
Member

OK first of all, if you see something contains _ as prefix, that's what not usually you should use outside the library, it's private or internal, so nothing guaranteed if you directly use it..
What you should use at y axis is valueFormatter, not _defaultValueFormatter.
If you want the same formatter for value labels, use set.valueFormatter

@duzenko
Copy link
Author

duzenko commented Jun 17, 2016

Changed to

    chart.leftAxis.valueFormatter.maximumFractionDigits = 2;
    chart.leftAxis.valueFormatter.minimumFractionDigits = 0;

No effect. Same thing as on the picture from the previous post

@liuxuan30
Copy link
Member

liuxuan30 commented Jun 17, 2016

that's simply not true: I used ChartsDemo and you can see it has 23, 23.16, and 23.2 on the chart.

    ChartYAxis *leftAxis = _chartView.leftAxis;
    leftAxis.labelFont = [UIFont systemFontOfSize:10.f];
    leftAxis.labelCount = 8;
    leftAxis.valueFormatter = [[NSNumberFormatter alloc] init];
    leftAxis.valueFormatter.maximumFractionDigits = 2;
    leftAxis.valueFormatter.minimumFractionDigits = 0;
    leftAxis.valueFormatter.negativeSuffix = @" $";
    leftAxis.valueFormatter.positiveSuffix = @" $";
    leftAxis.labelPosition = YAxisLabelPositionOutsideChart;
    leftAxis.spaceTop = 0.15;
    leftAxis.axisMinValue = 0.0; // this replaces startAtZero = YES

value

@duzenko
Copy link
Author

duzenko commented Jun 17, 2016

What is not true? My data are integer numbers and I don't want decimals after dot on the Y axis

@liuxuan30
Copy link
Member

look I think it's all about the value formatter you are using... The library will take all values into double type, so even you mean integer, it's still double.

If you don't want decimals, simply set maximumFractionDigits = 0.

you need to take a look at the number formatter documentation, it's all there.

@duzenko
Copy link
Author

duzenko commented Jun 17, 2016

I need to show values as float if they're float and as integer if they are integer - the data comes from web and I don't know that beforehand

@iamabhiee
Copy link

How can I set integers for values label in Horizontal Bar chart?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants