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.

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

Comments:

2014-05-26, 08:24:46
anonymous from Malaysia  
Malaysia
2014-05-26, 08:27:20
anonymous from Malaysia  
Malaysia
2014-05-26, 08:28:52
anonymous from Malaysia  
MELAKA, MY
2014-05-26, 08:31:12
anonymous from Malaysia  
MELAKA, MY
2014-05-26, 08:34:31
anonymous from Malaysia  
MELAKA, MY
2014-05-26, 08:35:57
anonymous from Malaysia  
MELAKA, MY
2015-01-31, 23:49:45
anonymous  
Cheers pal. I do apiaecprte the writing.

 

 

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: