ChartDirector
http://www.advsofteng.com/
下载地址
http://www.advsofteng.com/download.html
安装ChartDirector
使用
perl -V
得到perl的@INC
将lib下的内容copy到INC的任一路径中
cd ChartDirector
cp -rf lib/* /usr/lib/perl5/5.8.5/
如果自己定制目录的话,在编写脚本时,可在脚本头部加如下代码:
如:
use lib "/home/supersun/ChartDirector/lib/";
chart_create.pl 用法:
chart_create.pl -f 数据文件 -m 图表类型 -t 图表标题 -p 图片文件名 -x X轴标题 -y Y轴标题
图表类型为:
bar 柱状图
line 曲线图
数据文件格式:
date host1 host2 host3 host4 host5 host6 host7 host8
20090101 3031132 3253513 3035196 3130065 3323986 3024694 3076513 3287113
20090102 3610310 3545421 3332149 3862804 3352384 3361875 3341946 3412295
20090103 3756983 3815154 3997797 3941998 3760445 3730529 3721120 3672436
20090104 3926538 3875646 3902116 4038819 4058506 3886227 3846330 4052068
20090105 3905990 4000435 3936811 3863121 3843673 3844999 3786345 3868149
#!/usr/bin/perl
#chart_create.pl
#author: supersun06@gmail.com
#url: http://supersun.biz
use strict;
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=<FD>;
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 -x x_title -y y_title\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")
}
运行
perl chart_create.pl -f data -m line -t "a test " -p line.png -x date -y pv
perl chart_create.pl -f data -m line -t "a test" -p bar.png -x date -y pv


Windows下使用方法:
下载ActivePerl
http://www.activestate.com/activeperl/features/
下载ChartDirector,平台选择windows
http://download2.advsofteng.com/chartdir_perl_win32.zip
下载脚本,将其存放到D:\perl\chartdir_perl_win32\ChartDirector\lib\chart_create.pl
chart_create.plperl chart_create.pl -f c.txt -m bar -t "a test " -p line.png -x date -y pv
运行命令即可。
最近评论