Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP given a series of arbitrary numbers, how can I choose a logical max value on a line graph?

Tags:

php

math

rounding

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;
    } 
like image 558
stormist Avatar asked May 11 '10 18:05

stormist


2 Answers

 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
like image 85
Boris Guéry Avatar answered Oct 20 '22 23:10

Boris Guéry


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.

like image 27
Alex Mcp Avatar answered Oct 20 '22 22:10

Alex Mcp