#!/usr/local/bin/perl -w # # Example 4-5. biorhythm.cgi use GD::Graph::bars; use Date::Calc; use CGI; # We'll need a good value for Pi later my $pi = 4 * atan2(1,1); my $query = new CGI; my @dob = Date::Calc::Decode_Date_US($query->param('dob')); my @start = Date::Calc::Decode_Date_US($query->param('start')); my @end = Date::Calc::Decode_Date_US($query->param('end')); my $days = Date::Calc::Delta_Days(@start, @end); my $dobdiff = Date::Calc::Delta_Days(@dob, @start); my @xvalues; foreach my $day (0..$days) { # Add_Delta_days returns a date offset from the start date. # It returns a list in the form (yyyy, mm, dd) push @xvalues, (Date::Calc::Add_Delta_Days(@start,$day))[2]; } my (@pvalues, @evalues, @ivalues); foreach my $count (0..$days) { push @pvalues, sin((($count+($dobdiff % 23))/23) * 2 * $pi); push @evalues, sin((($count+($dobdiff % 28))/28) * 2 * $pi); push @ivalues, sin((($count+($dobdiff % 33))/33) * 2 * $pi); } # Create a new bar graph my $graph = new GD::Graph::bars(500,300); # Set the attributes for the graph $graph->set( x_label => '', # No labels y_label => '', title => 'Your Biorhythm', y_plot_values => 0, y_max_value => 1, # sine range is -1 to 1 y_min_value => -1, y_tick_number => 8, long_ticks => 0, # use short ticks on axes x_label_skip => 3, # print every third x label zero_axis => 0, zero_axis_only => 0, ); # Add the legend to the graph $graph->set_legend('Physical', 'Emotional', 'Intellectual'); # Plot the graph and write it to STDOUT print STDOUT $query->header(-type => 'image/png'); binmode STDOUT; # switch to binary mode my $gd = $graph->plot( [ \@xvalues, \@pvalues, \@evalues, \@ivalues ] ); print STDOUT $gd->png;