You'll have to forgive the phrasing of this question, I'm sure there's a better, more succinct way to ask it, but I don't know it.
Let's say I have a graph, and all the y-axis values are
[0,4,5,3,2,5,6]
The maximum value is six. So I would like the Y-Scale to be labeled from 0 to 10.
Given the following values
[33,26,54,23,86,23]
The maximum value is 86, so I would like the Y-Scale to go from 0 to 90.
Now let's say I have the following values
[98,253,87, 876,263]
The max is 876,so the Y-scale should go from 0 to 900
Now I have created the following function that should give me all the max y-scale values I need so far.
function padMaxValue(value){ for(var i = 1; i < 1000000000000000000; i = i * 10){ var decimalValue = value / i; if(value === i){ return i; } if(decimalValue < 1 && decimalValue > 0.09){ return i; } } }
However, given the following values
[99,123,82,189,45]
My function would set the y-scale max to 1000
. But the max should really be 200. I realise that what I really need is a smarter way to increase the value of i
instead of just multiplying it by 10. I need to be able to increase the value of i by 10, all the way up to 100. Then increase it by 100, all the way up to 1000. Then increase it by 1000, all the way up to 10,000 and so on.
I feel like there should be some neat and tidy mathematical way to do this. And I also feel that the 1000000000000000000
number I have in the for loop betrays my ignorance of mathematics.
Anyhoot, that's the problem. Any ideas?
A multiple is the result of multiplying a number by an integer. In our case, the number we're multiplying by is six. The integers we've been multiplying by are 11 and 12. That means that the next integer we'll multiply by is 13.
The multiples of 10 are 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, etc.
There is no need to go into the land of strings, which could be awkward if you ever had a decimal value.
function RoundedMax(a) { var mx = Math.max.apply(Math, a); if (mx == 0) {return 0}; var size = Math.floor(Math.log(Math.abs(mx)) / Math.LN10); var magnitude = Math.pow(10, size); var yMax = Math.ceil(mx / magnitude) * magnitude; return yMax; } function RoundedMin(a) { var mn = Math.min.apply(Math, a); if (mn == 0) {return 0}; var size = Math.floor(Math.log(Math.abs(mn)) / Math.LN10); var magnitude = Math.pow(10, size); var yMin = Math.floor(mn / magnitude) * magnitude; return yMin; } var arr = [-9.9,-1.23,-8.2,-2.01,-4.5,0]; document.write(RoundedMax(arr) + " " + RoundedMin(arr));
Outputs: 0 -10
.
EDIT Updated in view of the comments. Now works even in IE8.
I don't like math, so here is a pretty simple/hilarious string-manipulation solution:
Find the maximum value:
var max = Math.max.apply(Math, [98,253,87, 876,263]); // 876
Take its first character
var c = max.toString()[0] // "8"
Make it an integer and add 1
c = (c | 0) + 1 // 9
Convert it back to a string:
c = c.toString() // "9"
Add N - 1 zeros to it, where N is the length of your original number:
c += Array(max.toString().length).join("0") // "900"
Convert it back to an integer:
c = (c | 0) // 900
Done!
Seriously though, use math.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With