--- loncom/cgi/graph.png 2003/10/21 15:19:25 1.28 +++ loncom/cgi/graph.png 2003/10/27 21:21:08 1.29 @@ -1,6 +1,6 @@ #!/usr/bin/perl # -# $Id: graph.png,v 1.28 2003/10/21 15:19:25 matthew Exp $ +# $Id: graph.png,v 1.29 2003/10/27 21:21:08 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 + + +
+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 <-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',undef); + +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; +} +# +# 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))]); +} - -'.join(',',@{$Data[0]}).''; + $error .= '
'.join(',',@{$Data[1]}).''; + $error .= '
'.join(',',@{$Data[2]}).''; + print &error($error); + exit; +} + my $BinaryData=$plot->png; undef($MyGraph); undef($plot); if (! defined($BinaryData)) { - print <<"END"; -Content-type: text/html - - -