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

1.1       minaeibi    1: #!/usr/bin/perl
                      2: #
1.24    ! matthew     3: # $Id: graph.png,v 1.23 2003/10/09 15:56:41 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: 
                     41: produces plots based on input
                     42: 
                     43: =head1 DESCRIPTION
                     44: 
                     45: graph.png is a cgi-bin script which produces plots based on input data.
                     46: 
                     47: The query string is expected to be as follows (without whitespace):
                     48: 
                     49: escape(Plot title) & escape(X label)& escape(Y label) & Maximum Y value &
                     50: Number of bars & $data1 & $data2
                     51: 
                     52: $data1 and $data2 are expected to be comma seperated lists of numbers.
                     53: escape( value ) means the values must be run through lonnet::escape.
                     54: 
                     55: =cut
1.1       minaeibi   56: 
                     57: use strict;
1.24    ! matthew    58: use lib '/home/httpd/lib/perl';
1.9       minaeibi   59: use GD::Graph::bars;
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.24    ! matthew    70: if (! &LONCAPA::loncgi::check_cookie_and_load_env()) {
        !            71:     print <<END;
        !            72: Content-type: text/html
        !            73: 
        !            74: <html>
        !            75: <head><title>Bad Cookie</title></head>
        !            76: <body>
        !            77: Your cookie information is incorrect.  What\'s up with that?
        !            78: </body>
        !            79: </html>
        !            80: END
        !            81:     return;
        !            82: }
        !            83: 
1.4       matthew    84: $|=1;   # Autoflush after each print/write
1.24    ! matthew    85: my $identifier = $ENV{'QUERY_STRING'};
        !            86: my $Title   = &unescape($ENV{$identifier.'.title'});
        !            87: my $xlabel  = &unescape($ENV{$identifier.'.xlabel'});
        !            88: my $ylabel  = &unescape($ENV{$identifier.'.ylabel'});
        !            89: my $Max     = $ENV{$identifier.'.Max'};
        !            90: my $NumBars = $ENV{$identifier.'.NumBars'};
        !            91: my $data1   = $ENV{$identifier.'.data1'};
        !            92: my $data2   = $ENV{$identifier.'.data2'};
1.11      minaeibi   93: 
                     94: my @data11=split(/\,/,$data1);
                     95: my @data12=split(/\,/,$data2);
1.18      minaeibi   96: my $skip_x = 1;
1.23      matthew    97: my $bar_width=10;
1.1       minaeibi   98: 
1.24    ! matthew    99: #
        !           100: # Labels are always digits
1.1       minaeibi  101: my @xlabels;
1.23      matthew   102: for (my $nIdx=0; $nIdx<$NumBars; $nIdx++ ) {
                    103:     $xlabels[$nIdx]=$nIdx+1;
                    104: }
                    105: 
1.11      minaeibi  106: my @data =(\@xlabels,\@data11,\@data12);
1.1       minaeibi  107: 
1.24    ! matthew   108: #
        !           109: # Customize graph based on the 
1.21      matthew   110: my $width;
                    111: my $height = 200;
1.12      minaeibi  112: 
1.23      matthew   113: if ($NumBars < 10) {
                    114:     $width = 120+$NumBars*15;
                    115:     $skip_x = 1;
                    116:     $bar_width = 15;
                    117: } elsif ($NumBars <= 25) {
                    118:     $width = 120+$NumBars*11;
                    119:     $skip_x = 5;
                    120:     $bar_width = 8;
                    121: } elsif ($NumBars <= 50) {
                    122:     $width = 120+$NumBars*8;
                    123:     $skip_x = 5;
                    124:     $bar_width = 4;
1.19      minaeibi  125: } else {
1.23      matthew   126:     $width = 120+$NumBars*8;
                    127:     $skip_x = 5;
                    128:     $bar_width = 4;
1.11      minaeibi  129: }
1.1       minaeibi  130: 
1.21      matthew   131: my $x_tick_offset = 0;
                    132: if ($skip_x > 1) {
                    133:     $x_tick_offset = $skip_x - 1;
                    134: }
1.1       minaeibi  135: 
1.21      matthew   136: my $MyGraph = GD::Graph::bars->new($width,$height);
1.24    ! matthew   137: my $error = '';
        !           138: if (! $MyGraph->set( x_label         => $xlabel,
        !           139:                      y_label         => $ylabel,
        !           140:                      x_label_position => 0.5,
        !           141:                      long_ticks      => 1,
        !           142:                      tick_length     => 0,
        !           143:                      x_ticks         => 0,
        !           144:                      title           => $Title,
        !           145:                      y_max_value     => $Max,
        !           146:                      x_label_skip    => $skip_x,   
        !           147:                      x_tick_offset   => $x_tick_offset,
        !           148:                      #
        !           149:                      dclrs           => [ qw(lgreen dgreen lyellow 
        !           150:                                              lpurple cyan lorange)],
        !           151:                      bar_width       => $bar_width,
        !           152:                      cumulate        => 2,
        !           153:                      zero_axis       => 1,
        !           154:                      fgclr           => 'black',
        !           155:                      boxclr          => 'white',
        !           156:                      accentclr       => 'dblue',
        !           157:                      valuesclr       => '#ffff77',
        !           158:                      l_margin        => 10,
        !           159:                      b_margin        => 10,
        !           160:                      r_margin        => 10,
        !           161:                      t_margin        => 10,
        !           162:                      #
        !           163:                      transparent     => 0,
        !           164:                      )) {
        !           165:     $error = $MyGraph->error;
        !           166:     print <<"END";
        !           167: Content-type: text/html
        !           168: 
        !           169: <html>
        !           170: <head><title>Bad Graph</title></head>
        !           171: <body>
        !           172: <p>
        !           173: There was an error producing the graph you requested.
        !           174: </p><p>
        !           175: $error
        !           176: </p>
        !           177: </body>
        !           178: </html>
        !           179: END
        !           180:     return;
        !           181: }
1.1       minaeibi  182: 
1.24    ! matthew   183: my $plot = $MyGraph->plot(\@data);
        !           184: if (! defined($plot)) {
        !           185:     print <<"END";
        !           186: Content-type: text/html
        !           187: 
        !           188: <html>
        !           189: <head><title>Bad Graph</title></head>
        !           190: <body>
        !           191: The system was unable to create the graph you requested.
        !           192: </body>
        !           193: </html>
        !           194: END
        !           195:     return;
        !           196: }
1.1       minaeibi  197: 
1.24    ! matthew   198: my $BinaryData=$plot->png;
        !           199: undef($MyGraph);
        !           200: undef($plot);
        !           201: 
        !           202: if (! defined($BinaryData)) {
        !           203:     print <<"END";
        !           204: Content-type: text/html
        !           205: 
        !           206: <html>
        !           207: <head><title>Bad Graph</title></head>
        !           208: <body>
        !           209: The system was unable to produce a png image of the graph you requested.
        !           210: </body>
        !           211: </html>
        !           212: END
        !           213:     return;
        !           214: }
1.14      minaeibi  215: 
1.12      minaeibi  216: 
1.16      albertel  217: # Tell the server we are sending a png graphic
1.1       minaeibi  218: print <<END;
1.16      albertel  219: Content-type: image/png
1.1       minaeibi  220: 
                    221: END
                    222: 
                    223: binmode(STDOUT);
1.16      albertel  224: #open IMG,"|pngtopnm|ppmtogif 2>/dev/null"; # convert into a gif image
                    225: #print IMG $BinaryData; # output image
                    226: #$|=1; # be sure to flush before closing
                    227: #close IMG;
                    228: print $BinaryData;

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