我有每个学生的数据,例如
Student Name Score
Jack 89
Jill 70
Sandy 40
现在,我试图使用GD::Graph::Bar在条形图中绘制这些图,但是由于我对perl和模块非常陌生,我看到我可以手动声明将要绘制的图表中的所有X和Y值。
但是由于我不知道每个学生的名字和分数(从文本文件中提取),所以我希望能够自动完成这些值,
我认为哈希键和值是一种很好的方法。所以我把所有的东西放在哈希表中,%散列(学生名)=(分数)
有人能帮我把这个画成条形图或者指导我吗?或者你会推荐另一种方法吗?
谢谢
“最新情况
这是我可以通过输入学生的名字来手工绘制图形的部分。
my $graph = GD::Graph::bars->new(800, 800);
@data = (
["Jack","Jill"],
['30','50'],
);
$graph->set(
x_label => 'Students',
y_label => 'Scores',
title => 'Student Vs. Scores',
y_max_value => 60,
y_tick_number => 8,
y_label_skip => 2
) or die $graph->error;
my $gd = $graph->plot(\@data) or die $graph->error;
open(IMG, '>file.png') or die $!;
binmode IMG;
print IMG $gd->png;
发布于 2013-08-09 16:53:45
假设数据文件如下所示,使用制表符分隔符。
Student Name Score
Jack 89
Jill 70
Sandy 40
您可以这样做,将x
轴和y
轴值从数据文件推送到数组。
use strict;
use warnings;
use CGI qw( :standard );
use GD::Graph::bars;
open my $fh, '<', 'data.txt' or die $!;
my (@x, @y);
while (<$fh>) {
next if $. == 1; # skip header line
push @x, (split /\t/)[0]; # push 'Student Names' into @x array
push @y, (split /\t/)[1]; # push 'Score' into @y array
}
close $fh;
my $graph = GD::Graph::bars->new(800, 800);
$graph->set(
x_label => 'Students',
y_label => 'Scores',
title => 'Student Vs. Scores',
) or warn $graph->error;
my @data = (\@x, \@y);
$graph->plot(\@data) or die $graph->error();
print header(-type=>'image/jpeg'), $graph->gd->jpeg;
例如,给你:
如果您想使用多个y
轴值,假设您有另一个带Score2
的制表符分隔符列,那么您可以很容易地执行这样的操作。
my (@x, @y, @y2);
while (<$fh>) {
next if $. == 1;
push @x, (split /\t/)[0];
push @y, (split /\t/)[1];
push @y2, (split /\t/)[2];
}
并将@data
数组更改为:
my @data = (\@x, \@y, \@y2);
你的结果是:
发布于 2013-08-09 16:43:36
根据文档,您需要将数组数组传递给GD::plot
::bar的plot
方法。听起来好像您已经有了一个散列,所以您需要将它转换成一个数组。有很多方法可以做到这一点,但这里有一个例子:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my %hash = (
Larry => 15,
Curly => 16,
Moe => 20
);
my (@names, @scores);
while (my ($name, $score) = each %hash) {
push @names, $name;
push @scores, $score;
}
my @data = (\@names, \@scores);
print Dumper(\@data);
# $VAR1 = [
# [
# 'Moe',
# 'Curly',
# 'Larry'
# ],
# [
# 20,
# 16,
# 15
# ]
# ];
无论您怎样做,都要确保在内部数组中保留顺序。
发布于 2013-08-09 16:52:00
我修改了代码来自GD::图中的示例目录
use warnings;
use strict;
use GD::Graph::bars;
use GD::Graph::Data;
my %students = (
Jack => 89,
Jill => 70,
Sandy => 40,
);
my @scores;
my @names;
for (keys %students) {
push @names, $_;
push @scores, $students{$_};
}
my $data = GD::Graph::Data->new([
[@names],
[@scores],
]) or die GD::Graph::Data->error;
my $my_graph = GD::Graph::bars->new();
$my_graph->set(
x_label => 'Name',
y_label => 'Score',
title => 'A Simple Bar Chart',
) or warn $my_graph->error;
$my_graph->plot($data) or die $my_graph->error();
save_chart($my_graph, 'graph');
sub save_chart {
my $chart = shift or die "Need a chart!";
my $name = shift or die "Need a name!";
local(*OUT);
my $ext = $chart->export_format;
open(OUT, ">$name.$ext") or
die "Cannot open $name.$ext for write: $!";
binmode OUT;
print OUT $chart->gd->$ext();
close OUT;
}
https://stackoverflow.com/questions/18150841
复制相似问题