version 1.23, 2003/10/09 15:56:41
|
version 1.29, 2003/10/27 21:21:08
|
Line 55 escape( value ) means the values must be
|
Line 55 escape( value ) means the values must be
|
=cut |
=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 { |
sub unescape { |
my $str=shift; |
my $str=shift; |
Line 65 sub unescape {
|
Line 68 sub unescape {
|
return $str; |
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,$NumBars,$data1,$data2)=split(/&/,$ENV{'QUERY_STRING'}); |
|
$Titr = &unescape($Titr); |
my $colordefaults = join(',', |
$xlab = &unescape($xlab); |
('#33ff00', |
$ylab = &unescape($ylab); |
'#0033cc','#990000','#aaaa66','#663399','#ff9933', |
|
'#66ccff','#ff9999','#cccc33','#660000','#33cc66', |
my @data11=split(/\,/,$data1); |
)); |
my @data12=split(/\,/,$data2); |
|
my $skip_x = 1; |
my $height = &get_env('height',300); |
#my $bar_space=1; |
my $width = &get_env('width', 400); |
my $bar_width=10; |
my $PlotType = &get_env('PlotType','bar'); |
|
|
my @xlabels; |
my %GraphSettings = ( |
|
title => &unescape(&get_env('title','')), |
for (my $nIdx=0; $nIdx<$NumBars; $nIdx++ ) { |
x_label => &unescape(&get_env('xlabel','')), |
$xlabels[$nIdx]=$nIdx+1; |
y_label => &unescape(&get_env('ylabel','')), |
} |
x_label_position => 0.5, |
|
dclrs => [split(',',&get_env('Colors', |
#if ($Titr =~ /^Percentage$/){ |
$colordefaults))], |
# for (my $nIdx=0; $nIdx<$NumBars; $nIdx++ ) { |
fgclr => 'black', |
# $xlabels[$nIdx]=$nIdx; |
boxclr => 'white', |
# } |
accentclr => 'dblue', |
# @data11=(); |
valuesclr => '#ffff77', |
# @data11=split(/\,/,$data2); |
l_margin => 10, |
# @data12=(); |
b_margin => 10, |
# $Titr = ''; |
r_margin => 10, |
#} else { |
t_margin => 10, |
#} |
transparent => 0, |
|
); |
my @data =(\@xlabels,\@data11,\@data12); |
|
|
$GraphSettings{'x_label_skip'} = &get_env('xskip',1); |
my $width; |
$GraphSettings{'x_tick_offset'} = &get_env('x_tick_offset',0); |
my $height = 200; |
$GraphSettings{'y_max_value'} = &get_env('Max',undef); |
|
|
if ($NumBars < 10) { |
my $MyGraph; |
$width = 120+$NumBars*15; |
if ($PlotType eq 'bar') { |
$skip_x = 1; |
# Pick up bar graph settings |
$bar_width = 15; |
$GraphSettings{'bar_width'} = &get_env('bar_width',undef); |
} elsif ($NumBars <= 25) { |
$GraphSettings{'long_ticks'} = 1; |
$width = 120+$NumBars*11; |
$GraphSettings{'tick_length'} = 0; |
$skip_x = 5; |
$GraphSettings{'x_ticks'} = 0; |
$bar_width = 8; |
$GraphSettings{'cumulate'} = 2; |
} elsif ($NumBars <= 50) { |
$GraphSettings{'zero_axis'} = 1; |
$width = 120+$NumBars*8; |
|
$skip_x = 5; |
|
$bar_width = 4; |
|
} else { |
} else { |
$width = 120+$NumBars*8; |
# |
$skip_x = 5; |
# X label skip setup |
$bar_width = 4; |
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 $x_tick_offset = 0; |
# |
if ($skip_x > 1) { |
# Fill up %GraphSettings |
$x_tick_offset = $skip_x - 1; |
$GraphSettings{'long_ticks'} = 1; |
} |
$GraphSettings{'tick_length'} = 0; |
|
$GraphSettings{'x_ticks'} = &get_env('x_ticks',0),; |
my $MyGraph = GD::Graph::bars->new($width,$height); |
$GraphSettings{'x_label_skip'} = $skip_x; |
|
$GraphSettings{'x_tick_offset'} = $x_tick_offset; |
$MyGraph->set( |
$GraphSettings{'zero_axis'} = 1; |
x_label => $xlab, |
} |
y_label => $ylab, |
# |
x_label_position => 0.5, |
# Pick up miscellanious values passed in by the user |
long_ticks => 1, |
# |
tick_length => 0, |
# Create the plot and check it out |
x_ticks => 0, |
if ($PlotType eq 'bar') { |
title => $Titr, |
$MyGraph = GD::Graph::bars->new($width,$height); |
y_max_value => $Max, |
} else { |
# y_tick_number => $ytic, |
$MyGraph = GD::Graph::lines->new($width,$height); |
# y_label_skip => 5, |
} |
x_label_skip => $skip_x, |
if (! defined($MyGraph)) { |
x_tick_offset => $x_tick_offset, |
print &error('Unable to create initial graph'); |
|
return; |
dclrs => [ qw( lgreen dgreen lyellow lpurple cyan lorange)], |
} |
bar_width => $bar_width, |
|
# bar_spacing => $bar_space, |
## |
cumulate => 2, |
## Build the @Data array |
zero_axis => 1, |
my $NumSets = &get_env('NumSets'); |
|
my @Data; # stores the data for the graph |
# legend_placement => 'RT', |
my @xlabels = split(',',&get_env('labels')); |
|
push(@Data,\@xlabels); |
fgclr => 'black', |
for (my $i=1;$i<=$NumSets;$i++) { |
boxclr => 'white', |
push(@Data,[split(',',&get_env('data.'.$i))]); |
accentclr => 'dblue', |
} |
valuesclr => '#ffff77', |
|
l_margin => 10, |
|
b_margin => 10, |
|
r_margin => 10, |
|
t_margin => 10, |
|
|
|
transparent => 0, |
my $error = ''; |
) or warn $MyGraph->error; |
if (! $MyGraph->set(%GraphSettings)) { |
|
print &error($MyGraph->error); |
|
return; |
|
} |
|
|
|
my $plot = $MyGraph->plot(\@Data); |
|
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 165 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 |