--- loncom/cgi/graph.png 2003/01/08 03:31:54 1.17 +++ loncom/cgi/graph.png 2003/10/15 21:08:39 1.25 @@ -1,6 +1,6 @@ #!/usr/bin/perl # -# $Id: graph.png,v 1.17 2003/01/08 03:31:54 minaeibi Exp $ +# $Id: graph.png,v 1.25 2003/10/15 21:08:39 matthew Exp $ # # Copyright Michigan State University Board of Trustees # @@ -25,92 +25,193 @@ # http://www.lon-capa.org/ # # The LearningOnline Network with CAPA -# Behrouz Minaei -# YEAR=2001 -# 9/13/01, 9/25/01, 10/6/01, 10/9/01, 12/25/01 -# YEAR=2002 -# 2/1/, 5/13, 5/15 -# YEAR=2003 -# 1/7/ +# # A CGI script that dynamically outputs a graphical chart for lonstatistics. # #### +=pod + +=head1 NAME + +graph.png + +=head1 SYNOPSIS + +produces plots based on input + +=head1 DESCRIPTION + +graph.png is a cgi-bin script which produces plots based on input data. + +The query string is expected to be as follows (without whitespace): + +escape(Plot title) & escape(X label)& escape(Y label) & Maximum Y value & +Number of bars & $data1 & $data2 + +$data1 and $data2 are expected to be comma seperated lists of numbers. +escape( value ) means the values must be run through lonnet::escape. + +=cut + use strict; +use lib '/home/httpd/lib/perl'; use GD::Graph::bars; use GD::Graph::colour; use GD::Graph::Data; +use LONCAPA::loncgi(); -$|=1; # Autoflush after each print/write -my ($Titr,$xlab,$ylab,$Max,$PNo,$data1,$data2)=split(/&/,$ENV{'QUERY_STRING'}); +sub unescape { + my $str=shift; + $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg; + return $str; +} -my @data11=split(/\,/,$data1); -my @data12=split(/\,/,$data2); +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 $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 $NumSets = $ENV{$identifier.'.NumSets'}; +my @Colors = split(',',$ENV{$identifier.'.Colors'}); + +# +# Labels are always digits my @xlabels; +for (my $nIdx=0; $nIdx<$NumBars; $nIdx++ ) { + $xlabels[$nIdx]=$nIdx+1; +} +my @data; # stores the data for the graph +push(@data,\@xlabels); +for (my $i=1;$i<=$NumSets;$i++) { + push(@data,[split(',',$ENV{$identifier.'.data.'.$i})]); +} -if ($xlab=~/^Percentage$/){ - for (my $nIdx=0; $nIdx<$PNo; $nIdx++ ) { - $xlabels[$nIdx]=$data11[$nIdx]; - } - @data11=(); - @data11=split(/\,/,$data2); - @data12=(); -} else { - for (my $nIdx=0; $nIdx<$PNo; $nIdx++ ) { - $xlabels[$nIdx]=$nIdx+1; - } -} - -my @data =(\@xlabels,\@data11,\@data12); - -my $Range1; -my $Range2; - -if ($xlab=~/^Concepts$/){ - $Range1=270; - $Range2=200; -} elsif ($xlab=~/^Percentage$/){ - $Range1=250+20*$PNo; - $Range2=200; +my $skip_x = 1; +my $bar_width=10; + +# +# 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 { - $Range1=450; - $Range2=200; + $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($Range1,$Range2); +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 => \@Colors, + 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, - long_ticks => 1, - tick_length => 0, - x_ticks => 0, - title => $Titr, - y_max_value => $Max, -# y_tick_number => $ytic, - y_label_skip => 1, - - dclrs => [ qw( lgreen dgreen lyellow lpurple cyan lorange)], - - bar_spacing => 10, - 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 @@ -119,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