Diff for /loncom/cgi/graph.png between versions 1.1 and 1.30

version 1.1, 2001/10/10 15:26:00 version 1.30, 2003/10/29 14:47:43
Line 1 Line 1
 #!/usr/bin/perl  #!/usr/bin/perl
 #  #
 # The LearningOnline Network with CAPA  # $Id$
   #
   # Copyright Michigan State University Board of Trustees
   #
   # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   #
   # LON-CAPA is free software; you can redistribute it and/or modify
   # it under the terms of the GNU General Public License as published by
   # the Free Software Foundation; either version 2 of the License, or
   # (at your option) any later version.
 #  #
 # Behrouz Minaei  # LON-CAPA is distributed in the hope that it will be useful,
 # 9/13/2001, 9/25/2001  # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # 10/6/2001, 10,9,2001  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   # GNU General Public License for more details.
   #
   # You should have received a copy of the GNU General Public License
   # along with LON-CAPA; if not, write to the Free Software
   # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   #
   # /home/httpd/cgi-bin/graph.gif
   #
   # http://www.lon-capa.org/
   #
   # The LearningOnline Network with CAPA
 #  #
 # A CGI script that dynamically outputs a graphical chart for lonstatistics.  # A CGI script that dynamically outputs a graphical chart for lonstatistics.
   # 
   #### 
   
 use strict;  =pod
 use GD::Graph::bars;  
 use GD::Graph::colour;  
 use GD::Graph::Data;  
   
 my ($cid, $Tag, $Max, $PNo, $data) = split(/&/,$ENV{'QUERY_STRING'});  =head1 NAME
   
 my @data1=split(/\,/,$data);  graph.png
      
 my @xlabels;  
 for (my $nIdx=0; $nIdx<$PNo; $nIdx++ ) {  
     $xlabels[$nIdx]=$nIdx+1;  
 }   
 my @data =(\@xlabels,\@data1);  
   
 #print "Content-type: text/html\n\n";  
 #print $ENV{'QUERY_STRING';  
 #print "<br>";  
 #print $Max;  
 #print "<br>";  
 #print $PNo;  
 #print "<br>";  
 #print $data;  
 #exit;  
   
 my $Range;  
 if ( $PNo > 10 ) {$Range = 30*$PNo;}  
 else { $Range = 300+30*$PNo; }  
   
 if ( $Max < 1 ) { $Max = 1; }  
 elsif ( $Max < 10 ) { $Max = 10; }  
 elsif ( $Max < 100 ) { $Max = 100; }  
   
 my $MyGraph = GD::Graph::bars->new($Range, 400);  
   
 $MyGraph->set(   
     x_label         => 'Problems #',  
     y_label         => $Tag,  
     title           => 'LON-CAPA Graphical Chart, Course: '.$cid,  
     y_max_value     => $Max,  
     y_tick_number   => 10,  
     y_label_skip    => 1,  
     x_label_skip    => 2,  
       
     # colors  
     dclrs           => [ qw(green lblue lyellow lpurple cyan lorange)],  
       
     # shadows  
     bar_spacing     => 4,  
     shadow_depth    => 1,  
     shadowclr       => 'dred',  
   
     transparent     => 0,  =head1 SYNOPSIS
 ) or warn $MyGraph->error;   
   
 # Tell the server we are sending a gif graphic  produces plots based on input
 print <<END;  
 Content-type: image/gif  
   
 END  =head1 DESCRIPTION
   
 my $BinaryData=$MyGraph->plot(\@data)->png;  
 undef $MyGraph;  
 binmode(STDOUT);  
 open IMG,"|pngtopnm|ppmtogif"; # convert into a gif image  
 print IMG $BinaryData; # output image  
 $|=1; # be sure to flush before closing  
 close IMG;  
   
   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::lines;
   use GD::Graph::colour;
   use GD::Graph::Data;
   use LONCAPA::loncgi();
   
   sub unescape {
       my $str=shift;
       $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
       return $str;
   }
   
   sub error {
       my ($error) = @_;
       my $Str = <<"END";
   Content-type: text/html
   
   <html>
   <head><title>Bad Graph</title></head>
   <body>
   <p>
   There was an error producing the graph you requested.
   </p><p>
   $error
   </p>
   </body>
   </html>
   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 <<END;
   Content-type: text/html
   
   <html>
   <head><title>Bad Cookie</title></head>
   <body>
   Your cookie information is incorrect.
   </body>
   </html>
   END
       exit;
   }
   
   $|=1;   # Autoflush after each print/write
   
   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,
                        dclrs           => [split(',',&get_env('Colors',
                                                               $colordefaults))],
                        fgclr           => 'black',
                        boxclr          => 'white',
                        accentclr       => 'dblue',
                        valuesclr       => '#ffff77',
                        l_margin        => 10,
                        b_margin        => 10,
                        r_margin        => 10,
                        t_margin        => 10,
                        transparent     => 0,
                        );
   
   $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;
   }
   
   ##
   ## 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))]);
   }
   
   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 .= '<pre>'.join(',',@{$Data[0]}).'</pre>';
   #    $error .= '<pre>'.join(',',@{$Data[1]}).'</pre>';
   #    $error .= '<pre>'.join(',',@{$Data[2]}).'</pre>' if (ref($Data[2]));
   #    $error .= '<pre>'.join(',',@{$Data[3]}).'</pre>' if (ref($Data[3]));
       print &error($error);
       exit;
   }
   
   my $BinaryData=$plot->png;
   undef($MyGraph);
   undef($plot);
   
   if (! defined($BinaryData)) {
       print &error('Unable to render graph as image');
       exit;
   }
   
   # Tell the server we are sending a png graphic
   print <<END;
   Content-type: image/png
   
   END
   
   binmode(STDOUT);
   #open IMG,"|pngtopnm|ppmtogif 2>/dev/null"; # convert into a gif image
   #print IMG $BinaryData; # output image
   #$|=1; # be sure to flush before closing
   #close IMG;
   print $BinaryData;

Removed from v.1.1  
changed lines
  Added in v.1.30


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>