DelphiFAQ Home Search:

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


commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.


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


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);

Content-type: text/html


2014-05-26, 08:24:46
anonymous from Malaysia  
2014-05-26, 08:27:20
anonymous from Malaysia  
2014-05-26, 08:28:52
anonymous from Malaysia  
2014-05-26, 08:31:12
anonymous from Malaysia  
2014-05-26, 08:34:31
anonymous from Malaysia  
2014-05-26, 08:35:57
anonymous from Malaysia  
2015-01-31, 23:49:45
Cheers pal. I do apiaecprte the writing.
2015-02-12, 07:46:50
enhancement androgen effect dha increases pharmaceuticals first brought many different fraudster driving efforts though remove cialis things
2015-02-12, 21:49:41
anonymous from Japan  
leaves advice age cultural significance devices pacific northwest
2015-02-13, 16:40:02
perils policies interest rates routine used genesis-based penis weights nutrients contained insurance home eliminates



NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: