首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >GD::图与Perl

GD::图与Perl
EN

Stack Overflow用户
提问于 2013-08-09 15:54:28
回答 3查看 6.8K关注 0票数 5

我有每个学生的数据,例如

代码语言:javascript
运行
复制
    Student Name         Score
    Jack                  89
    Jill                  70
    Sandy                 40

现在,我试图使用GD::Graph::Bar在条形图中绘制这些图,但是由于我对perl和模块非常陌生,我看到我可以手动声明将要绘制的图表中的所有X和Y值。

但是由于我不知道每个学生的名字和分数(从文本文件中提取),所以我希望能够自动完成这些值,

我认为哈希键和值是一种很好的方法。所以我把所有的东西放在哈希表中,%散列(学生名)=(分数)

有人能帮我把这个画成条形图或者指导我吗?或者你会推荐另一种方法吗?

谢谢

“最新情况

这是我可以通过输入学生的名字来手工绘制图形的部分。

代码语言:javascript
运行
复制
 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;
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-08-09 16:53:45

假设数据文件如下所示,使用制表符分隔符。

代码语言:javascript
运行
复制
Student Name         Score
Jack                  89
Jill                  70
Sandy                 40

您可以这样做,将x轴和y轴值从数据文件推送到数组。

代码语言:javascript
运行
复制
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的制表符分隔符列,那么您可以很容易地执行这样的操作。

代码语言:javascript
运行
复制
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数组更改为:

代码语言:javascript
运行
复制
my @data = (\@x, \@y, \@y2);

你的结果是:

票数 3
EN

Stack Overflow用户

发布于 2013-08-09 16:43:36

根据文档,您需要将数组数组传递给GD::plot::bar的plot方法。听起来好像您已经有了一个散列,所以您需要将它转换成一个数组。有很多方法可以做到这一点,但这里有一个例子:

代码语言:javascript
运行
复制
#!/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
#           ]
#        ];

无论您怎样做,都要确保在内部数组中保留顺序。

票数 1
EN

Stack Overflow用户

发布于 2013-08-09 16:52:00

我修改了代码来自GD::图中的示例目录

代码语言:javascript
运行
复制
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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18150841

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档