如果不从文件中读取,而是把数据放在程序中,就没有问题,请大家指教
下面是我从文件中读取数据然后画图的程序:
#!d:/perl/bin/perl
use GD;
print "Content-type: image/png\n\n";
my $file="test.txt";
sub min{
my @a=@_;
my $s=pop @a;;
@a=map {$_-$s} @a;
return @a;
}
my @new;
open(FILE,$file) or die;
while(my $line=<FILE>){
chomp $line;
my @num=split /,/$dd[4];
push @new,@num;
}
close(FILE);
@new=sort {$a<=>$b} @new;
my $cha=$new[-1]-$new[0];
my $bei=$cha/700;
#my $bei=int ($cha/600);
my $size=$cha/$bei;
# create a new image 图片大小按压缩之后X1.1倍
my $im = new GD::Image($size*1.1+100,100);
# allocate some colors
my $white = $im->colorAllocate(255,255,255);
my $black = $im->colorAllocate(0,0,0);
my $red = $im->colorAllocate(255,0,0);
my $blue = $im->colorAllocate(0,0,255);
# make the background transparent and interlaced
$im->transparent($white);
$im->interlaced('true');
my $one=0;
my $two=20;
my $three=10;
my $four=5;
open(FILE,$file) or die;
while(my $line=<FILE>){
chomp $line;
my @dd=split /\t/,$line;
my $name=$dd[1];
my @num=split /,/,$dd[4];
@num=sort {$a<=>$b} @num;
@num= &min(@num,$new[0]); ##传递参数给子程序的时候,会将所有参数存为一个数组 @_
for(my $i=0;$i<@num;){
$im->rectangle($num[$i]/$bei+100,$one,$num[$i+1]/$bei+100,$two,
$black);
$im->fill($num[$i]/$bei+100+($num[$i+1]/$bei-$num[$i]/$bei)/2,$three,
$black);
$i=$i+2;
}
for(my $i=1;$i<@num-1;){
$im->line($num[$i]/$bei+100,$three,$num[$i+1]/$bei+100,$three,
$black);
$i=$i+2;
}
$im->string(gdLargeFont,0,$four,$name,$black);
# make sure we are writing to a binary stream
binmode STDOUT;
# Convert the image to PNG and print it on standard output
print $im->png;
$one=$one+40;
$two=$two+40;
$three=$three+40;
$four=$four+35;
}
close(FILE);
--~--~---------~--~----~------------~-------~--~----~
您收到此信息是由于您订阅了 Google 论坛"PerlChina Mongers 讨论组"论坛。
要在此论坛发帖,请发电子邮件到 perlchina@googlegroups.com
要退订此论坛,请发邮件至 perlchina+unsubscribe@googlegroups.com
更多选项,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问该论坛
-~----------~----~----~----~------~----~------~--~---
没有评论:
发表评论