Thomas
Thomas

Reputation: 59

formula to calculate axis values

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 345 - 155 = 190

For 5 steps: 190 / 4 = 47.50 per step

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

Y0 = 155

Y1 = 203

Y2 = 250

Y3 = 298

Y4 = 345

What I actually would like is the values to be:

Y0 = 150

Y1 = 200

Y2 = 250

Y3 = 300

Y4 = 350

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.

I hope I did explain it ok. English is not my main language, so please ask if things are not clear.

Upvotes: 2

Views: 1356

Answers (3)

Antonin Portelli
Antonin Portelli

Reputation: 698

If I understand well, you are speaking about fitting your points to a line. This a non trivial problem (but still simple) called linear regression. The traditional algorithm used to solve it is the least squares algorithm. I am quite sure that in all language you can find a library where it is implemented.

[Edit] Wolfram alpha can do the job :
http://www.wolframalpha.com/input/?i=linear+fit+155+200+250+300+345

Upvotes: 1

Troubadour
Troubadour

Reputation: 13431

One way to do it is to write a simple "coin-rounding" algorithm. This is an algorithm that rounds a number (eg. your proposed step) so that it begins with either 1,2 or 5. For example 47.50 would get coin-rounded to 50.

Upvotes: 0

ysap
ysap

Reputation: 8115

Rounding is the key here. You can round your extreme values to 1st digit, 2nd digit, etc. and use the rounded values as your axis limits.

Upvotes: 0

Related Questions