--- loncom/cgi/graph.png 2003/10/09 15:56:41 1.23 +++ loncom/cgi/graph.png 2003/10/09 22:04:37 1.24 @@ -1,6 +1,6 @@ #!/usr/bin/perl # -# $Id: graph.png,v 1.23 2003/10/09 15:56:41 matthew Exp $ +# $Id: graph.png,v 1.24 2003/10/09 22:04:37 matthew Exp $ # # Copyright Michigan State University Board of Trustees # @@ -55,9 +55,11 @@ escape( value ) means the values must be =cut use strict; +use lib '/home/httpd/lib/perl'; use GD::Graph::bars; use GD::Graph::colour; use GD::Graph::Data; +use LONCAPA::loncgi(); sub unescape { my $str=shift; @@ -65,37 +67,46 @@ sub unescape { return $str; } +if (! &LONCAPA::loncgi::check_cookie_and_load_env()) { + print < +Bad Cookie + +Your cookie information is incorrect. What\'s up with that? + + +END + return; +} + $|=1; # Autoflush after each print/write -my ($Titr,$xlab,$ylab,$Max,$NumBars,$data1,$data2)=split(/&/,$ENV{'QUERY_STRING'}); -$Titr = &unescape($Titr); -$xlab = &unescape($xlab); -$ylab = &unescape($ylab); +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_space=1; my $bar_width=10; +# +# Labels are always digits my @xlabels; - for (my $nIdx=0; $nIdx<$NumBars; $nIdx++ ) { $xlabels[$nIdx]=$nIdx+1; } -#if ($Titr =~ /^Percentage$/){ -# for (my $nIdx=0; $nIdx<$NumBars; $nIdx++ ) { -# $xlabels[$nIdx]=$nIdx; -# } -# @data11=(); -# @data11=split(/\,/,$data2); -# @data12=(); -# $Titr = ''; -#} else { -#} - my @data =(\@xlabels,\@data11,\@data12); +# +# Customize graph based on the my $width; my $height = 200; @@ -123,40 +134,84 @@ if ($skip_x > 1) { } my $MyGraph = GD::Graph::bars->new($width,$height); +my $error = ''; +if (! $MyGraph->set( x_label => $xlabel, + y_label => $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, + fgclr => 'black', + boxclr => 'white', + accentclr => 'dblue', + valuesclr => '#ffff77', + l_margin => 10, + 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 + return; +} -$MyGraph->set( - x_label => $xlab, - y_label => $ylab, - x_label_position => 0.5, - long_ticks => 1, - tick_length => 0, - x_ticks => 0, - title => $Titr, - y_max_value => $Max, -# y_tick_number => $ytic, -# y_label_skip => 5, - x_label_skip => $skip_x, - x_tick_offset => $x_tick_offset, - - dclrs => [ qw( lgreen dgreen lyellow lpurple cyan lorange)], - bar_width => $bar_width, -# bar_spacing => $bar_space, - cumulate => 2, - zero_axis => 1, - -# legend_placement => 'RT', - - fgclr => 'black', - boxclr => 'white', - accentclr => 'dblue', - valuesclr => '#ffff77', - l_margin => 10, - b_margin => 10, - r_margin => 10, - t_margin => 10, +my $plot = $MyGraph->plot(\@data); +if (! defined($plot)) { + print <<"END"; +Content-type: text/html + + +Bad Graph + +The system was unable to create the graph you requested. + + +END + return; +} - transparent => 0, -) or warn $MyGraph->error; +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; +} # Tell the server we are sending a png graphic @@ -165,8 +220,6 @@ Content-type: image/png END -my $BinaryData=$MyGraph->plot(\@data)->png; -undef $MyGraph; binmode(STDOUT); #open IMG,"|pngtopnm|ppmtogif 2>/dev/null"; # convert into a gif image #print IMG $BinaryData; # output image