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

1.1       minaeibi    1: #!/usr/bin/perl
                      2: #
1.23    ! matthew     3: # $Id: graph.png,v 1.22 2003/10/08 15:44:49 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.9       minaeibi   58: use GD::Graph::bars;
1.1       minaeibi   59: use GD::Graph::colour;
                     60: use GD::Graph::Data;
                     61: 
1.21      matthew    62: sub unescape {
                     63:     my $str=shift;
                     64:     $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
                     65:     return $str;
                     66: }
                     67: 
1.4       matthew    68: $|=1;   # Autoflush after each print/write
1.23    ! matthew    69: my ($Titr,$xlab,$ylab,$Max,$NumBars,$data1,$data2)=split(/&/,$ENV{'QUERY_STRING'});
1.21      matthew    70: $Titr = &unescape($Titr);
                     71: $xlab = &unescape($xlab);
                     72: $ylab = &unescape($ylab);
1.11      minaeibi   73: 
                     74: my @data11=split(/\,/,$data1);
                     75: my @data12=split(/\,/,$data2);
1.18      minaeibi   76: my $skip_x = 1;
1.23    ! matthew    77: #my $bar_space=1;
        !            78: my $bar_width=10;
1.1       minaeibi   79: 
                     80: my @xlabels;
1.17      minaeibi   81: 
1.23    ! matthew    82: for (my $nIdx=0; $nIdx<$NumBars; $nIdx++ ) {
        !            83:     $xlabels[$nIdx]=$nIdx+1;
        !            84: }
        !            85: 
        !            86: #if ($Titr =~ /^Percentage$/){
        !            87: #    for (my $nIdx=0; $nIdx<$NumBars; $nIdx++ ) {
        !            88: #        $xlabels[$nIdx]=$nIdx;
        !            89: #    }
        !            90: #    @data11=();
        !            91: #    @data11=split(/\,/,$data2);
        !            92: #    @data12=();
        !            93: #    $Titr = '';
        !            94: #} else {
        !            95: #} 
1.11      minaeibi   96: 
                     97: my @data =(\@xlabels,\@data11,\@data12);
1.1       minaeibi   98: 
1.21      matthew    99: my $width;
                    100: my $height = 200;
1.12      minaeibi  101: 
1.23    ! matthew   102: if ($NumBars < 10) {
        !           103:     $width = 120+$NumBars*15;
        !           104:     $skip_x = 1;
        !           105:     $bar_width = 15;
        !           106: } elsif ($NumBars <= 25) {
        !           107:     $width = 120+$NumBars*11;
        !           108:     $skip_x = 5;
        !           109:     $bar_width = 8;
        !           110: } elsif ($NumBars <= 50) {
        !           111:     $width = 120+$NumBars*8;
        !           112:     $skip_x = 5;
        !           113:     $bar_width = 4;
1.19      minaeibi  114: } else {
1.23    ! matthew   115:     $width = 120+$NumBars*8;
        !           116:     $skip_x = 5;
        !           117:     $bar_width = 4;
1.11      minaeibi  118: }
1.1       minaeibi  119: 
1.21      matthew   120: my $x_tick_offset = 0;
                    121: if ($skip_x > 1) {
                    122:     $x_tick_offset = $skip_x - 1;
                    123: }
1.1       minaeibi  124: 
1.21      matthew   125: my $MyGraph = GD::Graph::bars->new($width,$height);
1.1       minaeibi  126: 
                    127: $MyGraph->set( 
1.14      minaeibi  128:     x_label         => $xlab,
1.11      minaeibi  129:     y_label         => $ylab,
1.21      matthew   130:     x_label_position => 0.5,
1.11      minaeibi  131:     long_ticks      => 1,
                    132:     tick_length     => 0,
                    133:     x_ticks         => 0,
                    134:     title           => $Titr,
1.1       minaeibi  135:     y_max_value     => $Max,
1.11      minaeibi  136: #    y_tick_number   => $ytic,
1.23    ! matthew   137: #    y_label_skip    => 5,
1.18      minaeibi  138:     x_label_skip    => $skip_x,   
1.21      matthew   139:     x_tick_offset   => $x_tick_offset,
1.11      minaeibi  140: 
1.13      minaeibi  141:     dclrs           => [ qw( lgreen dgreen lyellow lpurple cyan lorange)],
1.23    ! matthew   142:     bar_width       => $bar_width,
        !           143: #    bar_spacing     => $bar_space,
1.11      minaeibi  144:     cumulate        => 2,
                    145:     zero_axis        => 1,
                    146: 
                    147: #    legend_placement    => 'RT',
                    148: 
                    149:     fgclr               => 'black',
1.13      minaeibi  150:     boxclr              => 'white',
1.11      minaeibi  151:     accentclr           => 'dblue',
                    152:     valuesclr           => '#ffff77',
                    153:     l_margin            => 10,
                    154:     b_margin            => 10,
                    155:     r_margin            => 10,
                    156:     t_margin            => 10,
1.1       minaeibi  157: 
                    158:     transparent     => 0,
                    159: ) or warn $MyGraph->error; 
1.14      minaeibi  160: 
1.12      minaeibi  161: 
1.16      albertel  162: # Tell the server we are sending a png graphic
1.1       minaeibi  163: print <<END;
1.16      albertel  164: Content-type: image/png
1.1       minaeibi  165: 
                    166: END
                    167: 
                    168: my $BinaryData=$MyGraph->plot(\@data)->png;
                    169: undef $MyGraph;
                    170: binmode(STDOUT);
1.16      albertel  171: #open IMG,"|pngtopnm|ppmtogif 2>/dev/null"; # convert into a gif image
                    172: #print IMG $BinaryData; # output image
                    173: #$|=1; # be sure to flush before closing
                    174: #close IMG;
                    175: print $BinaryData;

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