DelphiFAQ Home Search:
General :: Programming :: perl
General questions about perl.

Articles:

This list is sorted by recent document popularity (not total page views).
New documents will first appear at the bottom.
Recommended links on this topic:
Featured Article

Calculate the optimal maximum value for a y axis in a chart

Question:

I am generating a chart graph, and need to programatically generate the axis labels. My vertical (y) data range is not fixed = not predictable.

Answer:

Use the function below to round your biggest value up to the nearest number that divides by 100 or 25.

E.g. 145 would be rounded up to 150 and you would have 6 intervals with a step of 25. If your maximal number is 385678 then it would round up to 400000 and you would have a step of 100000.

Use it like this:

my ($display_max_data, $vertical_step) = roundup(int($max_data);

max_data is the biggest value, the returned display_max_data is greater or equal to it.

sub roundup {
   my ($nr) = @_;
 
   # find the next smallest number that divides by 100 or 25
   my $nr2 = $nr;
   if (length($nr2)>2) {
     $nr2 = substr($nr2,0,2) . substr('00000000', 0, length($nr2)-2);
   }
   my $diff = $nr-$nr2;
   $diff = '1' . substr('000000000000000', 0, length($diff));
   $nr2 = $nr2 + $diff;
 
   while ($nr2 / $diff < 4) { $diff = $diff / 2; }
   while ($nr2 / $diff > 12) { $diff = $diff * 10; }
 
   return ($nr2,$diff);
 }
 

Generated 4:02:29 on Dec 18, 2018