version 1.19, 2003/01/13 00:07:51
|
version 1.29, 2003/10/27 21:21:08
|
Line 25
|
Line 25
|
# http://www.lon-capa.org/ |
# http://www.lon-capa.org/ |
# |
# |
# The LearningOnline Network with CAPA |
# The LearningOnline Network with CAPA |
# Behrouz Minaei |
# |
# YEAR=2001 |
|
# 9/13/01, 9/25/01, 10/6/01, 10/9/01, 12/25/01 |
|
# YEAR=2002 |
|
# 2/1/, 5/13, 5/15 |
|
# YEAR=2003 |
|
# 1/7/ |
|
# A CGI script that dynamically outputs a graphical chart for lonstatistics. |
# A CGI script that dynamically outputs a graphical chart for lonstatistics. |
# |
# |
#### |
#### |
|
|
|
=pod |
|
|
|
=head1 NAME |
|
|
|
graph.png |
|
|
|
=head1 SYNOPSIS |
|
|
|
produces plots based on input |
|
|
|
=head1 DESCRIPTION |
|
|
|
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 strict; |
|
use lib '/home/httpd/lib/perl'; |
use GD::Graph::bars; |
use GD::Graph::bars; |
|
use GD::Graph::lines; |
use GD::Graph::colour; |
use GD::Graph::colour; |
use GD::Graph::Data; |
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 |
$|=1; # Autoflush after each print/write |
my ($Titr,$xlab,$ylab,$Max,$PNo,$data1,$data2)=split(/&/,$ENV{'QUERY_STRING'}); |
|
|
|
my @data11=split(/\,/,$data1); |
my $colordefaults = join(',', |
my @data12=split(/\,/,$data2); |
('#33ff00', |
my $skip_x = 1; |
'#0033cc','#990000','#aaaa66','#663399','#ff9933', |
my $bar_space=10; |
'#66ccff','#ff9999','#cccc33','#660000','#33cc66', |
|
)); |
my @xlabels; |
|
|
my $height = &get_env('height',300); |
if ($Titr =~ /^Percentage$/){ |
my $width = &get_env('width', 400); |
for (my $nIdx=0; $nIdx<$PNo; $nIdx++ ) { |
my $PlotType = &get_env('PlotType','bar'); |
$xlabels[$nIdx]=$nIdx; |
|
} |
my %GraphSettings = ( |
@data11=(); |
title => &unescape(&get_env('title','')), |
@data11=split(/\,/,$data2); |
x_label => &unescape(&get_env('xlabel','')), |
@data12=(); |
y_label => &unescape(&get_env('ylabel','')), |
$Titr = ''; |
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',undef); |
|
|
|
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 { |
} else { |
for (my $nIdx=0; $nIdx<$PNo; $nIdx++ ) { |
# |
$xlabels[$nIdx]=$nIdx+1; |
# 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); |
my @data =(\@xlabels,\@data11,\@data12); |
# |
|
# Fill up %GraphSettings |
my $Range1; |
$GraphSettings{'long_ticks'} = 1; |
my $Range2; |
$GraphSettings{'tick_length'} = 0; |
|
$GraphSettings{'x_ticks'} = &get_env('x_ticks',0),; |
if ($xlab=~/^Concepts$/){ |
$GraphSettings{'x_label_skip'} = $skip_x; |
$Range1=270; |
$GraphSettings{'x_tick_offset'} = $x_tick_offset; |
$Range2=200; |
$GraphSettings{'zero_axis'} = 1; |
} elsif ($xlab=~/^Problem_number$/){ |
} |
$Range1=450; |
# |
$Range2=200; |
# 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 { |
} else { |
$Range1=800;#*$PNo; |
$MyGraph = GD::Graph::lines->new($width,$height); |
$Range2=200; |
} |
$skip_x=5; |
if (! defined($MyGraph)) { |
$bar_space=1; |
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 $MyGraph = GD::Graph::bars->new($Range1,$Range2); |
my $error = ''; |
|
if (! $MyGraph->set(%GraphSettings)) { |
$MyGraph->set( |
print &error($MyGraph->error); |
x_label => $xlab, |
return; |
y_label => $ylab, |
} |
long_ticks => 1, |
|
tick_length => 0, |
|
x_ticks => 0, |
|
title => $Titr, |
|
y_max_value => $Max, |
|
# y_tick_number => $ytic, |
|
y_label_skip => 5, |
|
x_label_skip => $skip_x, |
|
|
|
dclrs => [ qw( lgreen dgreen lyellow lpurple cyan lorange)], |
|
|
|
bar_spacing => $bar_space, |
|
cumulate => 2, |
|
zero_axis => 1, |
|
|
|
# legend_placement => 'RT', |
|
|
|
fgclr => 'black', |
|
boxclr => 'white', |
|
accentclr => 'dblue', |
|
valuesclr => '#ffff77', |
|
l_margin => 10, |
|
b_margin => 10, |
|
r_margin => 10, |
|
t_margin => 10, |
|
|
|
transparent => 0, |
my $plot = $MyGraph->plot(\@Data); |
) or warn $MyGraph->error; |
if (! defined($plot)) { |
|
my $error = 'Unable to plot the data provided.'; |
|
$error .= '<pre>'.join(',',@{$Data[0]}).'</pre>'; |
|
$error .= '<pre>'.join(',',@{$Data[1]}).'</pre>'; |
|
$error .= '<pre>'.join(',',@{$Data[2]}).'</pre>'; |
|
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 |
# Tell the server we are sending a png graphic |
print <<END; |
print <<END; |
Line 125 Content-type: image/png
|
Line 224 Content-type: image/png
|
|
|
END |
END |
|
|
my $BinaryData=$MyGraph->plot(\@data)->png; |
|
undef $MyGraph; |
|
binmode(STDOUT); |
binmode(STDOUT); |
#open IMG,"|pngtopnm|ppmtogif 2>/dev/null"; # convert into a gif image |
#open IMG,"|pngtopnm|ppmtogif 2>/dev/null"; # convert into a gif image |
#print IMG $BinaryData; # output image |
#print IMG $BinaryData; # output image |