--- loncom/cgi/graph.png 2003/10/09 22:04:37 1.24 +++ loncom/cgi/graph.png 2003/10/29 14:47:43 1.30 @@ -1,6 +1,6 @@ #!/usr/bin/perl # -# $Id: graph.png,v 1.24 2003/10/09 22:04:37 matthew Exp $ +# $Id: graph.png,v 1.30 2003/10/29 14:47:43 matthew Exp $ # # Copyright Michigan State University Board of Trustees # @@ -57,6 +57,7 @@ escape( value ) means the values must be use strict; use lib '/home/httpd/lib/perl'; use GD::Graph::bars; +use GD::Graph::lines; use GD::Graph::colour; use GD::Graph::Data; use LONCAPA::loncgi(); @@ -67,6 +68,35 @@ sub unescape { return $str; } +sub error { + my ($error) = @_; + my $Str = <<"END"; +Content-type: text/html + + +Bad Graph + +

+There was an error producing the graph you requested. +

+$error +

+ + +END + return $Str; +} + +my $id = $ENV{'QUERY_STRING'}; + +# +# &get_env($name,$default) +sub get_env { + my $key = 'cgi.'.$id.'.'.(shift()); + return shift if (! exists($ENV{$key})); + return $ENV{$key}; +} + if (! &LONCAPA::loncgi::check_cookie_and_load_env()) { print < Bad Cookie -Your cookie information is incorrect. What\'s up with that? +Your cookie information is incorrect. END - return; + exit; } $|=1; # Autoflush after each print/write -my $identifier = $ENV{'QUERY_STRING'}; -my $Title = &unescape($ENV{$identifier.'.title'}); -my $xlabel = &unescape($ENV{$identifier.'.xlabel'}); -my $ylabel = &unescape($ENV{$identifier.'.ylabel'}); -my $Max = $ENV{$identifier.'.Max'}; -my $NumBars = $ENV{$identifier.'.NumBars'}; -my $data1 = $ENV{$identifier.'.data1'}; -my $data2 = $ENV{$identifier.'.data2'}; - -my @data11=split(/\,/,$data1); -my @data12=split(/\,/,$data2); -my $skip_x = 1; -my $bar_width=10; - -# -# Labels are always digits -my @xlabels; -for (my $nIdx=0; $nIdx<$NumBars; $nIdx++ ) { - $xlabels[$nIdx]=$nIdx+1; -} - -my @data =(\@xlabels,\@data11,\@data12); - -# -# Customize graph based on the -my $width; -my $height = 200; - -if ($NumBars < 10) { - $width = 120+$NumBars*15; - $skip_x = 1; - $bar_width = 15; -} elsif ($NumBars <= 25) { - $width = 120+$NumBars*11; - $skip_x = 5; - $bar_width = 8; -} elsif ($NumBars <= 50) { - $width = 120+$NumBars*8; - $skip_x = 5; - $bar_width = 4; -} else { - $width = 120+$NumBars*8; - $skip_x = 5; - $bar_width = 4; -} - -my $x_tick_offset = 0; -if ($skip_x > 1) { - $x_tick_offset = $skip_x - 1; -} -my $MyGraph = GD::Graph::bars->new($width,$height); -my $error = ''; -if (! $MyGraph->set( x_label => $xlabel, - y_label => $ylabel, +my $colordefaults = join(',', + ('#33ff00', + '#0033cc','#990000','#aaaa66','#663399','#ff9933', + '#66ccff','#ff9999','#cccc33','#660000','#33cc66', + )); + +my $height = &get_env('height',300); +my $width = &get_env('width', 400); +my $PlotType = &get_env('PlotType','bar'); + +my %GraphSettings = ( + title => &unescape(&get_env('title','')), + x_label => &unescape(&get_env('xlabel','')), + y_label => &unescape(&get_env('ylabel','')), x_label_position => 0.5, - long_ticks => 1, - tick_length => 0, - x_ticks => 0, - title => $Title, - y_max_value => $Max, - x_label_skip => $skip_x, - x_tick_offset => $x_tick_offset, - # - dclrs => [ qw(lgreen dgreen lyellow - lpurple cyan lorange)], - bar_width => $bar_width, - cumulate => 2, - zero_axis => 1, + dclrs => [split(',',&get_env('Colors', + $colordefaults))], fgclr => 'black', boxclr => 'white', accentclr => 'dblue', @@ -159,61 +138,95 @@ if (! $MyGraph->set( x_label => b_margin => 10, r_margin => 10, t_margin => 10, - # transparent => 0, - )) { - $error = $MyGraph->error; - print <<"END"; -Content-type: text/html + ); - -Bad Graph - -

-There was an error producing the graph you requested. -

-$error -

- - -END +$GraphSettings{'x_label_skip'} = &get_env('xskip',1); +$GraphSettings{'x_tick_offset'} = &get_env('x_tick_offset',0); +$GraphSettings{'y_max_value'} = &get_env('Max',1); + +my $MyGraph; +if ($PlotType eq 'bar') { + # Pick up bar graph settings + $GraphSettings{'bar_width'} = &get_env('bar_width',undef); + $GraphSettings{'long_ticks'} = 1; + $GraphSettings{'tick_length'} = 0; + $GraphSettings{'x_ticks'} = 0; + $GraphSettings{'cumulate'} = 2; + $GraphSettings{'zero_axis'} = 1; +} else { + # + # X label skip setup + my $skip_x = &get_env('xskip',1); + my $x_tick_offset = &get_env('x_tick_offset',$skip_x-1); + my $zero_axis = &get_env('zero_axis',1); + # + # Fill up %GraphSettings + $GraphSettings{'long_ticks'} = 1; + $GraphSettings{'tick_length'} = 0; + $GraphSettings{'x_ticks'} = &get_env('x_ticks',0),; + $GraphSettings{'x_label_skip'} = $skip_x; + $GraphSettings{'x_tick_offset'} = $x_tick_offset; + $GraphSettings{'zero_axis'} = 1; + if (&get_env('two_axes',0)) { + $GraphSettings{'two_axes'} = 1; + $GraphSettings{'y1_max_value'} = &get_env('y1_max_value',0); + $GraphSettings{'y1_min_value'} = &get_env('y1_min_value',1); + $GraphSettings{'y2_max_value'} = &get_env('y2_max_value',1); + $GraphSettings{'y2_min_value'} = &get_env('y2_min_value',1); + } +} +# +# Pick up miscellanious values passed in by the user +# +# Create the plot and check it out +if ($PlotType eq 'bar') { + $MyGraph = GD::Graph::bars->new($width,$height); +} else { + $MyGraph = GD::Graph::lines->new($width,$height); +} +if (! defined($MyGraph)) { + print &error('Unable to create initial graph'); return; } -my $plot = $MyGraph->plot(\@data); -if (! defined($plot)) { - print <<"END"; -Content-type: text/html +## +## Build the @Data array +my $NumSets = &get_env('NumSets'); +my @Data; # stores the data for the graph +my @xlabels = split(',',&get_env('labels')); +push(@Data,\@xlabels); +for (my $i=1;$i<=$NumSets;$i++) { + push(@Data,[split(',',&get_env('data.'.$i))]); +} - -Bad Graph - -The system was unable to create the graph you requested. - - -END +my $error = ''; +if (! $MyGraph->set(%GraphSettings)) { + print &error($MyGraph->error); return; } +my $plot = $MyGraph->plot(\@Data); +if (! defined($plot)) { + my $error = 'Unable to plot the data provided.'; +# Debugging code: +# $error .= '
'.join(',',@{$Data[0]}).'
'; +# $error .= '
'.join(',',@{$Data[1]}).'
'; +# $error .= '
'.join(',',@{$Data[2]}).'
' if (ref($Data[2])); +# $error .= '
'.join(',',@{$Data[3]}).'
' if (ref($Data[3])); + print &error($error); + exit; +} + my $BinaryData=$plot->png; undef($MyGraph); undef($plot); if (! defined($BinaryData)) { - print <<"END"; -Content-type: text/html - - -Bad Graph - -The system was unable to produce a png image of the graph you requested. - - -END - return; + print &error('Unable to render graph as image'); + exit; } - # Tell the server we are sending a png graphic print <