Showing ("00" .. "24") will be easy but I doubt it'll look very good. It also won't work as soon as you have more than 24 hours to display, or when you don't have enough space for all the numbers. I had a similar problem a few months back, here's how I solved it:

# format a set of dates for display on the X axis sub dates_to_header { my ($self, $dates) = @_; my @header = ("") x scalar @$dates; # choose a format based on the length of the range my $delta = $dates->[-1]->subtract_datetime($dates->[0]); my $months = $delta->delta_months; my $days = $delta->delta_days + ($months * 30); # rough guess is + ok here my ($format, $number); if ($months >= 5 and $months <= 11) { $format = "%b"; # month abrev $number = $months + 1; } elsif ($days <= 1) { $format = "%l %p"; # HH AM/PM $number = 5; } elsif ($days <= 6) { $format = "%a"; # weekdays, mark them all $number = @$dates; } elsif ($dates->[0]->year eq $dates->[-1]->year) { $format = "%m/%d"; # MM/DD $number = 5; } else { $format = "%D"; # MM/DD/YYx $number = 5; } if ($number == @$dates) { # marking all bars return [map { $_->strftime($format) } @$dates]; } # find the optimal spacing between labels my $spacing = @$dates / ("$number.0" - 1); # place a label at the edges my @spots = (0, $#$dates); # if there are odd numbers of dates to place, put one in the cente +r my $center = int(@$dates / 2); push(@spots, $center) if $number % 2; # place the remaining dates around the center, following the spaci +ng for (1 .. (($number - @spots) / 2)) { push(@spots, $center + int($spacing * $_)); push(@spots, $center - int($spacing * $_)); } # render the date at each chosen spot foreach my $spot (@spots) { $header[$spot] = $dates->[$spot]->strftime($format); } return \@header; }

That subroutine takes a reference to an array of DateTime objects representing the data-points on the graph. It then applies a bunch of heuristics to come up with some reasonable labels for the X axis, leaving enough space for them to render successfully.

Hope that helps.

-sam


In reply to Re: Help with GD::Graph by samtregar
in thread Help with GD::Graph by jeiku

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.