0

Let's say I have a simple line chart with 5 values (a = 155, b = 200, c = 250, d = 300, e 0 345)

I need a way to calculate which values go on the Y-axis, in such a way that the values look nice. I also want to see the minor steps.

If I use a simple formula I would do this:

MaxValue - Minvalue = difference 
300- 900 = 600

For 5 steps: 600/5 = 120 per step

That would lead to these values for the Y-axis:

Y0 = 200.0 (Rounding off to 200)    
Y1 = 360.0 (Rounding off to 400)    
Y2 = 520.0 (Rounding off to 600)    
Y3 = 680.0 (Rounding off to 700)    
Y4 = 840.0 (Rounding off to 900)    
Y4 = 1000.0 (Rounding off to 1000)

What I actually would like is the values to be:

Y0 = 200    
Y1 = 400    
Y2 = 600    
Y3 = 800    
Y4 = 1000

But how do I calculate this?

Before calculation I don't know the magnitude of the values, it could be also like thousands, or tens.

Denim Datta
  • 3,740
  • 3
  • 27
  • 53
NikhilReddy
  • 6,904
  • 11
  • 38
  • 58

1 Answers1

0

Not exactly what you expect, but may give you an idea:

const int N = 6;
double vals[N] = {200.0, 360.0, 520.0, 680.0, 840.0, 1000.0};
for (int i = 0; i < N; i++) {
    double factor = pow(10.0, floor(log10(vals[i])));
    double v =  floor(vals[i] / factor + 0.5) * factor;
    std::cout << vals[i] << " " << v << std::endl;
}

P.S. Sorry, it's in C++ but you can easily translate it to Java.

Archie
  • 2,644
  • 21
  • 21