Annotation of loncom/cgi/graph.png, revision 1.22
1.1 minaeibi 1: #!/usr/bin/perl
2: #
1.22 ! matthew 3: # $Id: graph.png,v 1.21 2003/03/26 21:52:37 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.11 minaeibi 69: my ($Titr,$xlab,$ylab,$Max,$PNo,$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;
77: my $bar_space=10;
1.1 minaeibi 78:
79: my @xlabels;
1.17 minaeibi 80:
1.19 minaeibi 81: if ($Titr =~ /^Percentage$/){
1.17 minaeibi 82: for (my $nIdx=0; $nIdx<$PNo; $nIdx++ ) {
1.18 minaeibi 83: $xlabels[$nIdx]=$nIdx;
1.17 minaeibi 84: }
85: @data11=();
86: @data11=split(/\,/,$data2);
1.19 minaeibi 87: @data12=();
88: $Titr = '';
1.17 minaeibi 89: } else {
90: for (my $nIdx=0; $nIdx<$PNo; $nIdx++ ) {
91: $xlabels[$nIdx]=$nIdx+1;
92: }
1.1 minaeibi 93: }
1.11 minaeibi 94:
95: my @data =(\@xlabels,\@data11,\@data12);
1.1 minaeibi 96:
1.21 matthew 97: my $width;
98: my $height = 200;
1.12 minaeibi 99:
1.11 minaeibi 100: if ($xlab=~/^Concepts$/){
1.21 matthew 101: $width=270;
102: } elsif ($xlab=~/^Problem\snumber$/){
103: $width=450;
1.19 minaeibi 104: } else {
1.21 matthew 105: $width=($PNo==100) ? 800 : (120+$PNo*10);
1.18 minaeibi 106: $skip_x=5;
107: $bar_space=1;
1.11 minaeibi 108: }
1.1 minaeibi 109:
1.21 matthew 110: my $x_tick_offset = 0;
111: if ($skip_x > 1) {
112: $x_tick_offset = $skip_x - 1;
113: }
1.1 minaeibi 114:
1.21 matthew 115: my $MyGraph = GD::Graph::bars->new($width,$height);
1.1 minaeibi 116:
117: $MyGraph->set(
1.14 minaeibi 118: x_label => $xlab,
1.11 minaeibi 119: y_label => $ylab,
1.21 matthew 120: x_label_position => 0.5,
1.11 minaeibi 121: long_ticks => 1,
122: tick_length => 0,
123: x_ticks => 0,
124: title => $Titr,
1.1 minaeibi 125: y_max_value => $Max,
1.11 minaeibi 126: # y_tick_number => $ytic,
1.18 minaeibi 127: y_label_skip => 5,
128: x_label_skip => $skip_x,
1.21 matthew 129: x_tick_offset => $x_tick_offset,
1.11 minaeibi 130:
1.13 minaeibi 131: dclrs => [ qw( lgreen dgreen lyellow lpurple cyan lorange)],
1.1 minaeibi 132:
1.18 minaeibi 133: bar_spacing => $bar_space,
1.11 minaeibi 134: cumulate => 2,
135: zero_axis => 1,
136:
137: # legend_placement => 'RT',
138:
139: fgclr => 'black',
1.13 minaeibi 140: boxclr => 'white',
1.11 minaeibi 141: accentclr => 'dblue',
142: valuesclr => '#ffff77',
143: l_margin => 10,
144: b_margin => 10,
145: r_margin => 10,
146: t_margin => 10,
1.1 minaeibi 147:
148: transparent => 0,
149: ) or warn $MyGraph->error;
1.14 minaeibi 150:
1.12 minaeibi 151:
1.16 albertel 152: # Tell the server we are sending a png graphic
1.1 minaeibi 153: print <<END;
1.16 albertel 154: Content-type: image/png
1.1 minaeibi 155:
156: END
157:
158: my $BinaryData=$MyGraph->plot(\@data)->png;
159: undef $MyGraph;
160: binmode(STDOUT);
1.16 albertel 161: #open IMG,"|pngtopnm|ppmtogif 2>/dev/null"; # convert into a gif image
162: #print IMG $BinaryData; # output image
163: #$|=1; # be sure to flush before closing
164: #close IMG;
165: print $BinaryData;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>