首页 > 编程语言 >perl SVG 作图之线图

perl SVG 作图之线图

时间:2022-12-24 22:45:25浏览次数:59  
标签:title 作图 SVG show Graph perl graph data

今天尝试用perl来画一个简单的图,根据数据我们这里画线图。使用的模块是SVG::TT::Graph,两年前更新过,相比perl模块动不动十几年没有新版本,这个模块算不错了,出图格式为SVG格式。

cpan仓库地址:

https://metacpan.org/pod/SVG::TT::Graph::Line

模块安装:

直接使用cpanm 安装,这个模块支持线图,条形图,饼图、热图、气泡图等几种类型

cpanm SVG::TT::Graph

image

画图:

仓库中有使用说明,比较简单,参数样式自己多调试知道什么意思了。

全局样式默认值如下:
use SVG::TT::Graph::Line;
 
# Field names along the X axis
my @fields = qw(Jan Feb Mar);
 
my $graph = SVG::TT::Graph::Line->new({
  # Required
  'fields'                 => \@fields,
 
  # Optional - defaults shown
  'height'                 => '500',
  'width'                  => '300',
 
  'show_data_points'       => 1,
  'show_data_values'       => 1,
  'stacked'                => 0,
 
  'min_scale_value'        => '0',
  'max_scale_value'        => undef,
  'area_fill'              => 0,
  'show_x_labels'          => 1,
  'stagger_x_labels'       => 0,
  'rotate_x_labels'        => 0,
  'show_y_labels'          => 1,
  'scale_integers'         => 0,
  'scale_divisions'        => '20',
  'y_label_formatter'      => sub { return @_ },
  'x_label_formatter'      => sub { return @_ },
 
  'show_x_title'           => 0,
  'x_title'                => 'X Field names',
 
  'show_y_title'           => 0,
  'y_title_text_direction' => 'bt',
  'y_title'                => 'Y Scale',
 
  'show_graph_title'       => 0,
  'graph_title'            => 'Graph Title',
  'show_graph_subtitle'    => 0,
  'graph_subtitle'         => 'Graph Sub Title',
  'key'                    => 0,
  'key_position'           => 'right',
 
  # Stylesheet defaults
  'style_sheet'             => '/includes/graph.css', # internal stylesheet
  'random_colors'           => 0,
});
数据部分内容如下:

image

画图代码:
  • 输出画图文件为SVG格式: perl plot_line.pl data.txt > line.svg
use strict;
use warnings;
use SVG::TT::Graph::Line;

open F,shift or die "$!";
<F>;
my (@qs, @qf, @qg, @indv,$n);
while(<F>){
    chomp;
        $n++;
    my @arr = split;
        push @indv,$n;
    while(my($i,$v)=each @arr){
                if($i==2){  push @qg,$v; }
        if($i==4){  push @qf,$v; }
        if($i==5){  push @qs,$v; }
    }
}
close F;

my $plot = SVG::TT::Graph::Line->new(
    {
        'fields' => \@indv,
        'height' => '500',
        'width'  => '900',

                # 以下属性1为true, 0为false
        'show_data_points' => 1, # 显示点
        'show_data_values' => 0, # 显示数据

                'stacked' => 0, # 堆叠
        'area_fill' => 0,   # 填充

        'show_x_labels' => 1,
        'show_y_labels' => 1,
        'stagger_x_labels' => 0, # 刻度数据双层显示
        'stagger_y_labels' => 0,
        'rotate_x_labels' => 1,  # x轴内容角度
        'scale_integers' => 0, # 坐标轴刻度整数刻度

        'show_x_title' => 1,
        'show_y_title'  => 1,
        'x_title' => 'Time',
                'y_title' => 'K value',

                'show_graph_title' => 1,
                'graph_title'  => 'K value plot',
                'show_graph_subtitle' => 0,
                'graph_subtitle'  => 'Graph Sub Title',

        'y_label_formatter' => sub { return @_ }, #格式化{return 'pre_' . $_[0] }
        'x_label_formatter' => sub { return @_ },

        'key' => 1, # 显示图例
        'key_position' => 'right', # 图例位置

                'random_colors' => 1,
    }
);

$plot->add_data({ 'data' => \@qf, 'title' => 'Group 1' });
$plot->add_data({ 'data' => \@qs, 'title' => 'Group 2' });
$plot->add_data({ 'data' => \@qg, 'title'  => 'Group 3' });

print $plot->burn();
结果图:

image

整体上来说代码比较简单,图片样式控制参数太少,只能说勉强能看。

标签:title,作图,SVG,show,Graph,perl,graph,data
From: https://www.cnblogs.com/mmtinfo/p/17003488.html

相关文章