+ -
当前位置:首页 → 问答吧 → 使用perl制作数据图表

使用perl制作数据图表

时间:2010-09-04

来源:互联网

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

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

下载地址:
http://www.advsofteng.com/download.html

2,安装ChartDirector


QUOTE:
[root@localhost ~]# tar -zxvf chartdir_perl_linux.tar.gz
[root@localhost ~]# cd ChartDirector/
[root@localhost ~]# cp -rf lib/*  /usr/lib/perl5/5.8.8/


使用 perl -V 查找得到 perl 的@INC,将 lib 下的内容copy到INC的任一路径中。


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

[root@localhost ~]# more chart_create.pl
  1. #!/usr/bin/perl
  2. #chart_create.pl
  3. #author: [email protected]
  4. #url:  http://supersun.biz


  5. use strict;
  6. use File::Basename;
  7. use lib dirname($0);
  8. use perlchartdir;
  9. use Getopt::Std;

  10. sub line_grap;
  11. sub bar_grap;
  12. sub usage;

  13. my %opts;
  14. getopt('fmtpxy',\%opts);

  15. my $file=$opts{f};
  16. my $mode=$opts{m};
  17. my $title=$opts{t};
  18. my $png_file=$opts{p};
  19. my $x_title=$opts{x};
  20. my $y_title=$opts{y};

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



  26. my $data={};
  27. open FD,$file;
  28. my @entries=<FD>;
  29. my $head=[];
  30. @{$head}=split /\s+/,$entries[0];
  31. foreach my $entry (@entries){
  32.         chomp $entry;
  33.         my @utils=split /\s+/,$entry;
  34.         foreach my $i (0..scalar(@$head)-1){
  35.                 push @{$data{$head[$i]}},$utils[$i];
  36.         }
  37. }
  38. if($mode eq "bar"){
  39.         bar_grap($png_file,$title,$x_title,$y_title,$head,$data);
  40. }else{
  41.         line_grap($png_file,$title,$x_title,$y_title,$head,$data);
  42. }




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

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

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

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



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

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

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

  76.         my $c = new XYChart(1200, 400);
  77.         $c->addTitle($head, "", 10);
  78.         $c->setPlotArea(80, 50, 1100, 300, 0xffffc0, 0xffffe0);
  79.         $c->addLegend(55, 18, 0, "", 8)->setBackground($perlchartdir::Transparent);
  80.         $c->yAxis()->setTitle($y_title);
  81.         $c->xAxis()->setTitle($x_title);
  82.         $c->yAxis()->setTopMargin(20);
  83.         $c->xAxis()->setLabels($data{$x_lable_tag});
  84.         my $layer = $c->addBarLayer2($perlchartdir::Side, 7);
  85.         foreach my $util (@$lable){
  86.                 my $col=shift @corlor;
  87.                 my $tag=shift @{$data{$util}};
  88.                 $layer->addDataSet($data{$util},$col, "$tag");
  89.         }
  90.         $c->makeChart("$file")
  91. }
复制代码
4,脚本用法如下:

[root@localhost ~]# ./chart_create.pl


QUOTE:
./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


QUOTE:
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,运行此脚本后,制图表如下:
  1. perl chart_create.pl -f data -m line -t "mobile" -p line.png -x date -y pv
复制代码
效果如下:
下载 (10 KB)
2010-09-04 07:27
  1. perl chart_create.pl -f data -m bar -t "mobile " -p bar.png -x date -y pv
复制代码
效果如下:
下载 (5.3 KB)
2010-09-04 07:27


原文来自http://blog.chinaunix.net/u/25264/showart_1918205.html

作者: jiannma   发布时间: 2010-09-04

是挺不错的,但似乎是商业的图表库,需要license.

作者: climby   发布时间: 2010-09-04