GDGraph-1.44 版本 GD画图总结
注: GDGraph-1.44英文文档及相应的perl GD包可以去http://cpan.org下载,至于包下载后如何安装则可以参考前面的文章《Windows 下如何手工安装perl模块包》
1.GD::Graph包可以创建的统计图最常见的有以下几种
1)GD::Graph::lines #创建折线图
2)GD::Graph::bars and GD::Graph::bars #水平柱状图或垂直柱状图
3)GD::Graph::points #点状图
4)GD::Graph::linespoints #折线图,但是数据部分用点显示
5)GD::Graph::pie #饼图
2. 常见的几种图的code源码perl-CGI
2.1 饼状图
use strict;
use CGI;
use GD::Graph::pie
use constant TITLE =>"Pie Chart"; #图标的标题
my $q =new CGI;
print $q->header( -type => "image/png",-expires => "-1d");
my $graph_pie = new GD::Graph::pie( 200, 200 );
my @data = (
[ qw( blocker naormal trivial Enhancement ) ],
[ 13, 24, 23, 19 ],
);
$graph_pie->set(
'3d' => 0, #是三维饼图还是二维饼图
dclrs=>[qw(green marine pink cyan)], #饼图各种分类的颜色
legend_placement => 'TT', # 图例位置
legend_spacing => 10, #图例空间
pie_height => 20,
start_angle => 235, #开始分图的角度
legend_spacing => 10,
l_margin => 20 , # 左边距
b_margin => 20, # 下边距
r_margin => 20, # 右边距
t_margin => 20, # 上边距
);
$graph_pie->set_value_font("c:/WINDOWS/fonts/arial.ttf",10); #设置数值的字体
$graph_pie->set_legend( qw( blocker naormal trivial Enhancement ));
#设置图例,如果需要显示百分比的则需要自己组串,支持带入变量
$graph_pie->set_legend_font("c:/WINDOWS/fonts/arial.ttf",10);#设置图例的字体
my $gd_image =$graph_pie->plot( /@data)||die $graph_pie->error;#画图
#下面代码是将生成的图片保存到本地
open ($FH ,"> c:/bugzilla/bug_severity_pie.png")||die "open failed!";
binmode $FH;
print $FH $gd_image->png ;
close $FH;
2.2 折线图
use strict;
use CGI;
use GD::Graph::line;
my $q =new CGI;
print $q->header( -type => "image/png",-expires => "-1d");
$graph = new GD::Graph::lines( 400,300);
$graph->set_x_label_font("c:/WINDOWS/fonts/arial.ttf",10);#设置字体
$graph->set_y_label_font("c:/WINDOWS/fonts/arial.ttf",10);
$graph->set_x_axis_font("c:/WINDOWS/fonts/arial.ttf",8 );
$graph->set_y_axis_font("c:/WINDOWS/fonts/arial.ttf",10);
$graph->set_legend_font("c:/WINDOWS/fonts/arial.ttf",10);
$graph->set_legend("bugs");
$graph->set(
x_label=> 'MM-DD', #横坐标
y_label=>'Bugs', #纵坐标
x_label_skip=>1, #刻度显示
x_label_position=>1,#横坐标显示的位置
y_label_position=>1,#纵坐标显示的位置
borderclrs => "purple",
#bgclr=>'lyellow',
dclrs => [ qw(yellow) ],#折线的颜色
t_margin=>1 , #边界
#transparent => 0, #背景是否透明
bar_spacing => 6,
shadow_depth => 2,
#shadowclr => 'lblue',
show_values => 5,
legend_placement => "RT",
y_max_value => $maxbugs,
y_tick_number => 2,
y_label_skip => 1,
box_axis=>1, #
x_labels_vertical=>1,#如果横坐标的刻度说明比较长的话就需要用到该参数
labelclr=>'lblue', #坐标的颜色
axislabelclr=>'black', #轴的颜色
valuesclr=>'black', # 值的颜色
l_margin => 10 , # 各个边距
b_margin => 10,
r_margin => 10,
t_margin => 10,
) || die $graph->error;
其他就不举例了,总之,除了饼图有点区别外,其他的统计图设置都差不多,没有太大的区别!具体情况可以参靠该包的详细说明文档,
此外1.44包自身还带有很多例子,适合第一次使用该包的同学学习。