使用perl制作数据图表

今天看到一篇不错的文章,是利用perl进行制作数据图表,,自己实验下,效果还不错。

1,首先要下载一个 ChartDirector 软件包
下载地址:


2,安装ChartDirector
[root@localhost ~]# tar -zxvf chartdir_perl_linux.tar.gz
[root@localhost ~]# cd ChartDirector/
[root@localhost ~]# cp -rf lib/*  /usr/lib/perl5/5.8.5/
使用 perl -V 查找得到 perl 的@INC,将 lib 下的内容copy到INC的任一路径中。

3,复制以下 perl 制表脚本,保存为 chart_create.pl

[root@localhost ~]# more chart_create.pl
#!/usr/bin/perl
#chart_create.pl
#author: supersun06@gmail.com
#url: 


use strict;
use File::Basename;
use lib dirname($0);
use perlchartdir;
use Getopt::Std;

sub line_grap;
sub bar_grap;
sub usage;

my %opts;
getopt('fmtpxy',\%opts);

my $file=$opts{f};
my $mode=$opts{m};
my $title=$opts{t};
my $png_file=$opts{p};
my $x_title=$opts{x};
my $y_title=$opts{y};

$x_title="X" if $x_title eq '';
$y_title="Y" if $y_title eq '';
usage unless $mode eq "line" || $mode eq "bar";
usage if  -z $title || -z $png_file;
die "$file isn't exsit\n" unless -f $file;



my $data={};
open FD,$file;
my @entries=;
my $head=[];
@{$head}=split /\s+/,$entries[0];
foreach my $entry (@entries){
        chomp $entry;
        my @utils=split /\s+/,$entry;
        foreach my $i (0..scalar(@$head)-1){
                push @{$$data{$$head[$i]}},$utils[$i];
        }
}
if($mode eq "bar"){
        bar_grap($png_file,$title,$x_title,$y_title,$head,$data);
}else{
        line_grap($png_file,$title,$x_title,$y_title,$head,$data);
}




sub usage {
        print "$0 -f datafile -m line|bar -t graph_title -p image_name\n";
        exit;
}

sub line_grap {
        my ($file,$head,$x_title,$y_title,$lable,$data)=@_;

        my $x_lable_tag=shift @$lable;
        print "$x_lable_tag\n";
        shift @{$$data{$x_lable_tag}};
        my @corlor=(0xffff00, 0x00ff00, 0x9999ff, 0xff0000, 0x223366,0xff8800, 0xa0bdc4, 0x999966, 0x333366, 0xc3c3e6);

        my $c = new XYChart(1200, 400, 0xffffc0, 0x000000, 1);
        $c->setPlotArea(80, 50, 1100, 300, 0xffffff, -1, -1, 0xc0c0c0, -1);
        $c->addLegend(45, 12, 0, "", 8)->setBackground($perlchartdir::Transparent);
        $c->addTitle($head, "arialbd.ttf", 9, 0xffffff)->setBackground($c->patternColor([0x004000, 0x008000], 2));
        $c->yAxis()->setTitle($y_title);
        $c->xAxis()->setTitle($x_title);
        $c->yAxis()->setLabelFormat("{value}");
        $c->xAxis()->setLabels($$data{$x_lable_tag});
        my $layer = $c->addLineLayer();
        foreach my $util (@$lable){
                my $col=shift @corlor;
                my $tag=shift @{$$data{$util}};
                $layer->addDataSet($$data{$util}, $col, $tag)->setDataSymbol($perlchartdir::SquareSymbol, 7);
        }



        #$layer->setDataLabelFormat("{value}");
        $c->makeChart("$file")
}

sub bar_grap {
        my ($file,$head,$x_title,$y_title,$lable,$data)=@_;
        my $x_lable_tag= shift @$lable;
        print "$x_lable_tag\n";
        shift @{$$data{$x_lable_tag}};

        my @corlor=(0xffff00, 0x00ff00, 0x9999ff, 0xff0000, 0x223366,0xff8800, 0xa0bdc4, 0x999966, 0x333366, 0xc3c3e6);

        my $c = new XYChart(1200, 400);
        $c->addTitle($head, "", 10);
        $c->setPlotArea(80, 50, 1100, 300, 0xffffc0, 0xffffe0);
        $c->addLegend(55, 18, 0, "", 8)->setBackground($perlchartdir::Transparent);
        $c->yAxis()->setTitle($y_title);
        $c->xAxis()->setTitle($x_title);
        $c->yAxis()->setTopMargin(20);
        $c->xAxis()->setLabels($$data{$x_lable_tag});
        my $layer = $c->addBarLayer2($perlchartdir::Side, 7);
        foreach my $util (@$lable){
                my $col=shift @corlor;
                my $tag=shift @{$$data{$util}};
                $layer->addDataSet($$data{$util},$col, "$tag");
        }
        $c->makeChart("$file")
}


4,脚本用法如下:

[root@localhost ~]# ./chart_create.pl
./chart_create.pl -f datafile -m line|bar -t graph_title -p image_name

chart_create.pl  -f 数据文件 -m 图表类型 -t 图表标题 -p 图片文件名 -x X轴标题 -y Y轴标题

图表类型为:
bar     柱状图
line    曲线图

数据文件格式如下:
[root@localhost ~]# more data
date                    v1        v2        v3
20090101                3031132   3253513   3035196
20090102                3610310   3545421   3332149
20090103                3756983   3815154   3997797
20090104                3926538   3875646   3902116
20090105                3905990   4000435   3936811

5,运行此脚本后,制图表如下:

perl chart_create.pl -f data -m line -t "mobile" -p line.png -x date -y pv

perl chart_create.pl -f data -m bar -t "mobile " -p bar.png -x date -y pv

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

omyligaga

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值