Annotation of loncom/cgi/graph.png, revision 1.31

1.1       minaeibi    1: #!/usr/bin/perl
                      2: #
1.31    ! matthew     3: # $Id: graph.png,v 1.30 2003/10/29 14:47:43 matthew Exp $
1.5       minaeibi    4: #
                      5: # Copyright Michigan State University Board of Trustees
                      6: #
                      7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      8: #
                      9: # LON-CAPA is free software; you can redistribute it and/or modify
                     10: # it under the terms of the GNU General Public License as published by
                     11: # the Free Software Foundation; either version 2 of the License, or
                     12: # (at your option) any later version.
                     13: #
                     14: # LON-CAPA is distributed in the hope that it will be useful,
                     15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     17: # GNU General Public License for more details.
                     18: #
                     19: # You should have received a copy of the GNU General Public License
                     20: # along with LON-CAPA; if not, write to the Free Software
                     21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     22: #
                     23: # /home/httpd/cgi-bin/graph.gif
                     24: #
                     25: # http://www.lon-capa.org/
                     26: #
1.1       minaeibi   27: # The LearningOnline Network with CAPA
1.22      matthew    28: #
1.1       minaeibi   29: # A CGI script that dynamically outputs a graphical chart for lonstatistics.
1.5       minaeibi   30: # 
                     31: #### 
1.22      matthew    32: 
                     33: =pod
                     34: 
                     35: =head1 NAME
                     36: 
                     37: graph.png
                     38: 
                     39: =head1 SYNOPSIS
                     40: 
1.31    ! matthew    41: produces plots from data stored in users environment.
1.22      matthew    42: 
                     43: =head1 DESCRIPTION
                     44: 
1.31    ! matthew    45: graph.png is a cgi-bin script which produces plots based on data stored
        !            46: in the users environment.  The users cookie is checked prior to producing
        !            47: a plot.  The query string is expected to be an identifier, $id.  
        !            48: The parameters defining the plot must be stored in the environment as 
        !            49: $ENV{'cgi.'.$id.'.'.$dataname}.  Two types of plots can be produced, 'bar'
        !            50: and 'xy'.  The 'xy' graph can will 1 or 2 y-axes if the parameter
        !            51: 'two_axes' is set to false or true respectively.  See perldoc GD::Graph and
        !            52: loncommon::DrawBarGraph, loncommon::DrawXYGraph, and loncommon::DrawXYYGraph.
1.22      matthew    53: 
                     54: =cut
1.1       minaeibi   55: 
                     56: use strict;
1.24      matthew    57: use lib '/home/httpd/lib/perl';
1.9       minaeibi   58: use GD::Graph::bars;
1.29      matthew    59: use GD::Graph::lines;
1.1       minaeibi   60: use GD::Graph::colour;
                     61: use GD::Graph::Data;
1.24      matthew    62: use LONCAPA::loncgi();
1.1       minaeibi   63: 
1.21      matthew    64: sub unescape {
                     65:     my $str=shift;
                     66:     $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
                     67:     return $str;
                     68: }
                     69: 
1.29      matthew    70: sub error {
                     71:     my ($error) = @_;
                     72:     my $Str = <<"END";
                     73: Content-type: text/html
                     74: 
                     75: <html>
                     76: <head><title>Bad Graph</title></head>
                     77: <body>
                     78: <p>
                     79: There was an error producing the graph you requested.
                     80: </p><p>
                     81: $error
                     82: </p>
                     83: </body>
                     84: </html>
                     85: END
                     86:     return $Str;
                     87: }
                     88: 
                     89: my $id = $ENV{'QUERY_STRING'};
                     90: 
                     91: #
1.31    ! matthew    92: # usage: &get_env($name,$default)
1.29      matthew    93: sub get_env {
                     94:     my $key = 'cgi.'.$id.'.'.(shift());
                     95:     return shift if (! exists($ENV{$key}));
                     96:     return $ENV{$key};
                     97: }
                     98: 
1.24      matthew    99: if (! &LONCAPA::loncgi::check_cookie_and_load_env()) {
                    100:     print <<END;
                    101: Content-type: text/html
                    102: 
                    103: <html>
                    104: <head><title>Bad Cookie</title></head>
                    105: <body>
1.28      matthew   106: Your cookie information is incorrect.
1.24      matthew   107: </body>
                    108: </html>
                    109: END
1.26      albertel  110:     exit;
1.24      matthew   111: }
                    112: 
1.4       matthew   113: $|=1;   # Autoflush after each print/write
1.1       minaeibi  114: 
1.31    ! matthew   115: ##
        !           116: ## Set up the plot
        !           117: ##
1.29      matthew   118: my $colordefaults = join(',',
                    119:                          ('#33ff00', 
                    120:                           '#0033cc','#990000','#aaaa66','#663399','#ff9933',
                    121:                           '#66ccff','#ff9999','#cccc33','#660000','#33cc66',
                    122:                           ));
                    123: 
                    124: my $height   = &get_env('height',300);
                    125: my $width    = &get_env('width', 400);
                    126: my $PlotType = &get_env('PlotType','bar');
                    127: 
                    128: my %GraphSettings = (
                    129:                      title           => &unescape(&get_env('title','')),
                    130:                      x_label         => &unescape(&get_env('xlabel','')),
                    131:                      y_label         => &unescape(&get_env('ylabel','')),
1.24      matthew   132:                      x_label_position => 0.5,
1.29      matthew   133:                      dclrs           => [split(',',&get_env('Colors',
                    134:                                                             $colordefaults))],
1.24      matthew   135:                      fgclr           => 'black',
                    136:                      boxclr          => 'white',
                    137:                      accentclr       => 'dblue',
                    138:                      valuesclr       => '#ffff77',
                    139:                      l_margin        => 10,
                    140:                      b_margin        => 10,
                    141:                      r_margin        => 10,
                    142:                      t_margin        => 10,
                    143:                      transparent     => 0,
1.29      matthew   144:                      );
1.24      matthew   145: 
1.29      matthew   146: $GraphSettings{'x_label_skip'}  = &get_env('xskip',1);
                    147: $GraphSettings{'x_tick_offset'} = &get_env('x_tick_offset',0);
1.31    ! matthew   148: $GraphSettings{'y_max_value'}   = &get_env('y_max_value',1);
1.29      matthew   149: 
                    150: my $MyGraph;
                    151: if ($PlotType eq 'bar') {
                    152:     # Pick up bar graph settings
                    153:     $GraphSettings{'bar_width'}     = &get_env('bar_width',undef);
                    154:     $GraphSettings{'long_ticks'}    = 1;
                    155:     $GraphSettings{'tick_length'}   = 0;
                    156:     $GraphSettings{'x_ticks'}       = 0;
                    157:     $GraphSettings{'cumulate'}      = 2;
                    158:     $GraphSettings{'zero_axis'}     = 1;
                    159: } else {
                    160:     #
                    161:     # X label skip setup
                    162:     my $skip_x = &get_env('xskip',1);
                    163:     my $x_tick_offset = &get_env('x_tick_offset',$skip_x-1);
                    164:     my $zero_axis = &get_env('zero_axis',1);
                    165:     #
                    166:     # Fill up %GraphSettings
                    167:     $GraphSettings{'long_ticks'}    = 1;
                    168:     $GraphSettings{'tick_length'}   = 0;
                    169:     $GraphSettings{'x_ticks'}       = &get_env('x_ticks',0),;
                    170:     $GraphSettings{'x_label_skip'}  = $skip_x;
                    171:     $GraphSettings{'x_tick_offset'} = $x_tick_offset;
                    172:     $GraphSettings{'zero_axis'}     = 1;
1.30      matthew   173:     if (&get_env('two_axes',0)) {
                    174:         $GraphSettings{'two_axes'}     = 1;
                    175:         $GraphSettings{'y1_max_value'} = &get_env('y1_max_value',0);
                    176:         $GraphSettings{'y1_min_value'} = &get_env('y1_min_value',1);
                    177:         $GraphSettings{'y2_max_value'} = &get_env('y2_max_value',1);
                    178:         $GraphSettings{'y2_min_value'} = &get_env('y2_min_value',1);
                    179:     }
1.29      matthew   180: }
                    181: #
                    182: # Pick up miscellanious values passed in by the user
                    183: #
                    184: # Create the plot and check it out
                    185: if ($PlotType eq 'bar') {
                    186:     $MyGraph = GD::Graph::bars->new($width,$height);
                    187: } else {
                    188:     $MyGraph = GD::Graph::lines->new($width,$height);
                    189: }
                    190: if (! defined($MyGraph)) {
                    191:     print &error('Unable to create initial graph');
1.24      matthew   192:     return;
                    193: }
1.1       minaeibi  194: 
1.29      matthew   195: ##
                    196: ## Build the @Data array
                    197: my $NumSets = &get_env('NumSets');
                    198: my @Data;  # stores the data for the graph
                    199: my @xlabels  = split(',',&get_env('labels'));
                    200: push(@Data,\@xlabels);
                    201: for (my $i=1;$i<=$NumSets;$i++) {
                    202:     push(@Data,[split(',',&get_env('data.'.$i))]);
                    203: }
1.24      matthew   204: 
1.29      matthew   205: my $error = '';
                    206: if (! $MyGraph->set(%GraphSettings)) {
                    207:     print &error($MyGraph->error);
1.24      matthew   208:     return;
                    209: }
1.1       minaeibi  210: 
1.29      matthew   211: my $plot = $MyGraph->plot(\@Data);
                    212: if (! defined($plot)) {
                    213:     my $error = 'Unable to plot the data provided.';
1.30      matthew   214: # Debugging code:    
                    215: #    $error .= '<pre>'.join(',',@{$Data[0]}).'</pre>';
                    216: #    $error .= '<pre>'.join(',',@{$Data[1]}).'</pre>';
                    217: #    $error .= '<pre>'.join(',',@{$Data[2]}).'</pre>' if (ref($Data[2]));
                    218: #    $error .= '<pre>'.join(',',@{$Data[3]}).'</pre>' if (ref($Data[3]));
1.29      matthew   219:     print &error($error);
                    220:     exit;
                    221: }
                    222: 
1.24      matthew   223: my $BinaryData=$plot->png;
                    224: undef($MyGraph);
                    225: undef($plot);
                    226: 
                    227: if (! defined($BinaryData)) {
1.29      matthew   228:     print &error('Unable to render graph as image');
                    229:     exit;
1.24      matthew   230: }
1.12      minaeibi  231: 
1.16      albertel  232: # Tell the server we are sending a png graphic
1.1       minaeibi  233: print <<END;
1.16      albertel  234: Content-type: image/png
1.1       minaeibi  235: 
                    236: END
                    237: 
                    238: binmode(STDOUT);
1.16      albertel  239: #open IMG,"|pngtopnm|ppmtogif 2>/dev/null"; # convert into a gif image
                    240: #print IMG $BinaryData; # output image
                    241: #$|=1; # be sure to flush before closing
                    242: #close IMG;
                    243: print $BinaryData;

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