I am constructing a line graph in PHP. I was setting the max value of the line graph to the max value of my collection of items, but this ended up making the graph less readable you are unable to view the highest line on the graph as it intersects with the top of it. So what I need is basically a formula to take a set of numbers and calculate what the logical max value of on the line graph should be.. so some examples
3500
250
10049
45394
434
312
Max value on line graph should probably be 50000
493
412
194
783
457
344
max value on line graph would ideally be 1000
545
649
6854
5485
11545
In this case, 12000 makes sense as max value
So something as simple as rounding upward to the nearest thousandth might work but I'd need it to progressively increase as the numbers got bigger. (50000 instead of 46,000 in first example) The maximum these numbers will ever be is about a million.
Any recommendations would be greatly appreciated, thank you.
Here is what I settled on, thank you all for your comments:
private function FigureMaxValue($array)
{
$highestNumber = max($array);
if ($highestNumber == 0) return 0;
$highestNumber = $highestNumber * 1.1;
(float)$highestNumber = round((float)$highestNumber, 0);
$maxValue = ceil( (integer)$highestNumber / 100 ) * 100;
return $maxValue;
}
1. $numbers = array(3500, 250, 10049, 45394, 434, 312)
2. $highestNumber = max($numbers)
3. $n = 10 ^ (strlen($highestNumber) - 1)
4. $highestNumber = $highestNumber / $n
5. $newMax = ceil($n)
6. $newMax = $newMax * $n
I would just do a magnitude above the highest value, like $highestValue * 1.05
or something. You'll still be faced with the issue of outliers IE -
4
5
12
2
1
4
4266
In this case you'd lose resolution on the lower numbers. You could test standard deviation on each element if you wanted to discard any outliers.
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