version 1.1, 2001/12/07 22:52:38
|
version 1.3, 2001/12/11 13:47:36
|
Line 24
|
Line 24
|
# |
# |
# http://www.lon-capa.org/ |
# http://www.lon-capa.org/ |
# |
# |
|
########################################################################### |
|
# |
# CGI-BIN interface to GD, used for making mathematical plots. |
# CGI-BIN interface to GD, used for making mathematical plots. |
# |
# |
# User specifies the following variables (given are defaults): |
# User specifies the following variables (given are defaults): |
Line 33
|
Line 35
|
# xmax = " 10.0" |
# xmax = " 10.0" |
# ymin = "-10.0" |
# ymin = "-10.0" |
# ymax = " 10.0" |
# ymax = " 10.0" |
|
# transparent (doesn't work with gif?) |
# frame |
# frame |
# drawaxes |
# drawaxes |
# drawtics |
# drawtics |
Line 51
|
Line 54
|
# size of a labelN is one of : |
# size of a labelN is one of : |
# giant, large, medium, small, tiny |
# giant, large, medium, small, tiny |
# |
# |
|
########################################################################### |
use GD; |
use GD; |
|
|
my @inputs = split(/&/,$ENV{'QUERY_STRING'}); |
my @inputs = split(/&/,$ENV{'QUERY_STRING'}); |
Line 73 $axis->{'ylen'} = $axis->{'ymax'} - $axi
|
Line 77 $axis->{'ylen'} = $axis->{'ymax'} - $axi
|
$vtic_every = &grab('vtic_every',1.0,\%In); |
$vtic_every = &grab('vtic_every',1.0,\%In); |
$htic_every = &grab('htic_every',1.0,\%In); |
$htic_every = &grab('htic_every',1.0,\%In); |
|
|
my $image = new GD::Image($height,$width); |
my $image = new GD::Image($width,$height); |
|
|
# allocate standard colors |
# allocate standard colors |
my $white = $image->colorAllocate(255,255,255); |
my @BGvalues = split /,/,&grab('bgcolor','255,255,255',\%In); |
my $black = $image->colorAllocate( 0, 0, 0); |
my @FGvalues = split /,/,&grab('fgcolor','0,0,0',\%In); |
|
my $bgcolor = $image->colorAllocate(@BGvalues); |
# Draw a black frame around the picture |
my $fgcolor = $image->colorAllocate(@FGvalues); |
&drawtics($htic_every,$vtic_every) if (exists($In{"drawtics"})); |
|
&drawaxes($axis) if (exists($In{"drawaxis"})); |
# Draw a fgcolor frame around the picture |
&frame(1) if (exists($In{'frame'})); |
&drawtics($htic_every,$vtic_every) if (exists($In{'drawtics'})); |
|
&drawaxes($axis) if (exists($In{'drawaxis'})); |
|
&drawframe(1) if (exists($In{'frame'})); |
|
# make the background transparent if needed (this doesn't work, at least |
|
# not for gif images, don't know if it works for png) |
|
$image->transparent($bgcolor) if (exists($In{'transparent'})); |
|
|
## Take care of labels and data series |
## Take care of labels and data series |
foreach (keys %In) { |
foreach (keys %In) { |
if (/^label/) { |
if (/^label/) { |
my ($x,$y,$size,$text) = split/,/,$In{$_}; |
my ($x,$y,$size,$text) = split/,/,$In{$_}; |
&drawstring($text,$x,$y,$black,$size); |
&drawstring($text,$x,$y,$fgcolor,$size); |
delete ($In{$_}); |
delete ($In{$_}); |
next; |
next; |
} elsif (/^xseries/) { |
} elsif (/^xseries/) { |
Line 101 foreach (keys %In) {
|
Line 110 foreach (keys %In) {
|
delete ($In{$yname}); |
delete ($In{$yname}); |
if ($#X != $#Y) { |
if ($#X != $#Y) { |
&drawstring("size of $xname and $yname do not match", |
&drawstring("size of $xname and $yname do not match", |
10,10,$black,"giant"); |
10,10,$fgcolor,"giant"); |
next; |
next; |
} |
} |
&drawcurve(\@X,\@Y); |
&drawcurve(\@X,\@Y); |
} |
} |
} |
} |
|
|
# make the background transparent and interlaced |
|
$image->transparent($white); |
|
|
|
# make sure we are writing to a binary stream |
|
binmode STDOUT; |
|
|
|
# Convert the image to PNG and print it on standard output |
# Tell the browser our mime-type |
print <<END; |
print <<END; |
Content-type: image/png |
Content-type: image/gif |
|
|
END |
END |
|
|
my $BinaryData=$image->plot(\@data)->png; |
my $BinaryData=$image->png; |
undef $image; |
undef $image; |
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 |
Line 156 sub transformPoint{
|
Line 160 sub transformPoint{
|
sub drawaxes{ |
sub drawaxes{ |
($x1,$y1) = &transformPoint($axis->{"xmin"},0,$image,$axis); |
($x1,$y1) = &transformPoint($axis->{"xmin"},0,$image,$axis); |
($x2,$y2) = &transformPoint($axis->{"xmax"},0,$image,$axis); |
($x2,$y2) = &transformPoint($axis->{"xmax"},0,$image,$axis); |
$image->line($x1,$y1,$x2,$y2,$black); |
$image->line($x1,$y1,$x2,$y2,$fgcolor); |
($x1,$y1) = &transformPoint(0,$axis->{"ymin"},$image,$axis); |
($x1,$y1) = &transformPoint(0,$axis->{"ymin"},$image,$axis); |
($x2,$y2) = &transformPoint(0,$axis->{"ymax"},$image,$axis); |
($x2,$y2) = &transformPoint(0,$axis->{"ymax"},$image,$axis); |
$image->line($x1,$y1,$x2,$y2,$black); |
$image->line($x1,$y1,$x2,$y2,$fgcolor); |
} |
} |
|
|
sub drawtics{ |
sub drawtics{ |
my ($htic_every,$vtic_every) = @_; |
my ($htic_every,$vtic_every) = @_; |
my ($width,$height) = $image->getBounds(); |
my ($width,$height) = $image->getBounds(); |
|
|
$ticwidth = ($width > 99 ? 10 : int($width /10) + 1); |
$ticwidth = ($width > 99 ? 5 : int($width /20) + 1); |
$ticheight = ($height > 99 ? 10 : int($height/10)); |
$ticheight = ($height > 99 ? 5 : int($height/20) + 1); |
|
|
# Do tics along y-axis |
# Do tics along y-axis |
for ($ntic = 0; $ntic <=int($axis->{"ylen"}/$vtic_every); $ntic++){ |
for ($ntic = 0; $ntic <=int($axis->{"ylen"}/$vtic_every); $ntic++){ |
Line 175 sub drawtics{
|
Line 179 sub drawtics{
|
my ($x2,$y2) = &transformPoint(0,$axis->{"ymin"}+$ntic*$vtic_every); |
my ($x2,$y2) = &transformPoint(0,$axis->{"ymin"}+$ntic*$vtic_every); |
$x1 -= $ticwidth; |
$x1 -= $ticwidth; |
$x2 += $ticwidth; |
$x2 += $ticwidth; |
$image->line($x1,$y1,$x2,$y2,$black); |
$image->line($x1,$y1,$x2,$y2,$fgcolor); |
} |
} |
# Do tics along x-axis |
# Do tics along x-axis |
for ($ntic = 0; $ntic <=int($axis->{"xlen"}/$htic_every); $ntic++){ |
for ($ntic = 0; $ntic <=int($axis->{"xlen"}/$htic_every); $ntic++){ |
Line 183 sub drawtics{
|
Line 187 sub drawtics{
|
my ($x2,$y2) = &transformPoint( $axis->{"xmin"}+$ntic*$htic_every,0); |
my ($x2,$y2) = &transformPoint( $axis->{"xmin"}+$ntic*$htic_every,0); |
$y1 -= $ticheight; |
$y1 -= $ticheight; |
$y2 += $ticheight; |
$y2 += $ticheight; |
$image->line($x1,$y1,$x2,$y2,$black); |
$image->line($x1,$y1,$x2,$y2,$fgcolor); |
} |
} |
} |
} |
|
|
Line 192 sub drawcurve{
|
Line 196 sub drawcurve{
|
for($i=0;$i< (@$X-1);$i++) { |
for($i=0;$i< (@$X-1);$i++) { |
($x1,$y1) = &transformPoint($X->[$i ],$Y->[$i ]); |
($x1,$y1) = &transformPoint($X->[$i ],$Y->[$i ]); |
($x2,$y2) = &transformPoint($X->[$i+1],$Y->[$i+1]); |
($x2,$y2) = &transformPoint($X->[$i+1],$Y->[$i+1]); |
$image->line($x1,$y1,$x2,$y2,$black); |
$image->line($x1,$y1,$x2,$y2,$fgcolor); |
} |
} |
} |
} |
|
|
sub frame{ |
sub drawframe{ |
# Draw a frame around the picture. |
# Draw a frame around the picture. |
my ($xoffset,$yoffset) = @_; |
my ($xoffset,$yoffset) = @_; |
$xoffset = $xoffset || 1; |
$xoffset = $xoffset || 1; |
$yoffset = $yoffset || $xoffset; |
$yoffset = $yoffset || $xoffset; |
my ($width,$height) = $image->getBounds(); |
my ($width,$height) = $image->getBounds(); |
$image->rectangle($xoffset-1,$yoffset-1,$width-$xoffset,$height-$yoffset,$black); |
$image->rectangle($xoffset-1,$yoffset-1,$width-$xoffset,$height-$yoffset,$fgcolor); |
} |
} |
|
|
sub drawstring{ |
sub drawstring{ |