Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm generating charts and I'm letting the user specify the date range. The problem is that there might be anywhere from 5-1000 data points and the values might be from 0 to 1,000,000.

I'm primarily using Open Flash Charts2 and Google Charts API.

I need to generate the Y axis step value and X step value and X labels. I think the most X labels I can fit really is about 30. So for a 30 day range that's really easy. Just show every label. But if they have 500 data points what do I do? 5000 / 30 = 166 x step value? Same with Y step value?

Does every single person that comes across this kind of charting problem just re-invent the wheel, or are there good libraries for this stuff, or is there some simple formulas for making this trivial to do manually?


Thanks in advance.
  • Comment on Dynamically Charting with Variable Dates and Data

Replies are listed 'Best First'.
Re: Dynamically Charting with Variable Dates and Data
by moritz (Cardinal) on Jan 12, 2009 at 08:36 UTC
    I didn't like GD::Graph's algorithm to determine the maximal value, so I invented my own:
    use POSIX qw(log10); my $round_to = 10 ** int(log10 $max) / 5; $max = $round_to * (1 + int($max / $round_to)); $p->set(y_max_value => $max );

    In this case $max is the maximum value that appears in the data set. $round_to is first set to the next lower power of ten (so for example 35 is set to 10) and then divided by 5 (so that would be 2 here).

    Then the maximum value is set to the next higher step of $round_to.

    You can simply use $round_to, or a multiple thereof, as step value, or be inspired by my usage of logarithm+exponentiation+rounding to find your own formula.

      I like the numbers your algorithm is giving me. I'm going to use it. Thanks.
Re: Dynamically Charting with Variable Dates and Data
by Anonymous Monk on Jan 12, 2009 at 22:23 UTC
    Poster here. I remembered that Chart::Strip jumps through all the necessary hoops to handle highly variable date ranges and data points. I'm going to take a crack and re-using that code and instead of generating its (relatively ugly) GD charts output using Open Flash Charts/Google Charts.
    Seems like it's going to be a little rough though.