グラフサンプル (No.-15) グラフ名:棒グラフ 画像名:sample18.gif CGI名:sample18.pl
sample18.gif


【要点】
(1)縦棒グラフ、横棒グラフを描く
   GD::Graph::bars;    #縦棒グラフ
   GD::Graph::hbars;   #横棒グラフ
  for my \$graph (GD::Graph::bars->new(600, 400),
               GD::Graph::hbars->new(600, 400))
(2)棒の色を決める
  'dclrs' => [ (lblue lyellow blue yellow lgreen lred
                green red purple orange pink dyellow) ],
(3)Y軸目盛線の制御
  long_ticks    => 1, #true(=1)の場合、目盛りの線を軸いっぱいに描きます。
                      #デフォルトは0です。
                      #long_ticksが0の場合、tick_length
                      #の値で描かれます。
                      #tick_lengthのデフォルトは4です。
  'tick_length'      => 0, # long_ticks=>0の場合有効になります。
  'y_tick_number'    => 5, #Y軸目盛の数
  'y_number_format'  => '%d', #Y軸目盛の値を編集します。指定値はPerlのsprintf参照。
       
(4)X軸の目盛を描かない
  'x_ticks' => 0,  # 1:trueならば、X軸に目盛が描かれます。
                   #long_tickstick_lengthの値により描かれます。
                   #tick_lengthのデフォルトは4です。
                   # 0:X軸に目盛を描かない。   
(5)軸ラベルの位置を決める
  x_label_position => .5, # 0:左寄せ 1:右寄せ 1/2:中央揃え。
                         #0〜1の値で指定します。デフォルト: 3/4 。
  y_label_position => .5, # 0:下詰め 1:上詰め 1/2:中央揃え。
                         #0〜1の値で指定します。デフォルト: 1/2 。
(6)目盛の値の位置を決める
  'y_plot_values' => 1, #Y軸の目盛の値が軸の脇際に描かれます。
  'x_plot_values' => 1, #X軸の目盛の値が軸の脇際に描かれます。
                        #デフォルトは何れも1です。
(7)棒(バー)の描き方
  cumulate  => 1, #  1:データセットは互いの上に積み重なります(積み上げ)。
                  #  棒グラフ、面グラフのみサポート。overwriteも参照して下さい。
(8)グラフの背景色を決める
  'bgclr' => 'white', #グラフの背景色は、白。
sample18.pl

chdir("C:\\Inetpub\\wwwroot\\uriage"); #カレントディレクトリ指定(IISの場合)
use lib 'C:/Inetpub/wwwroot/uriage'; #GD::Graphライブラリ格納フォルダ GD (固定)の場所
#
no strict; # TODO fix this
use GD::Graph::bars;
use GD::Graph::hbars;
require 'save.pl';

# CONTRIB Edwin Hildebrand.
#
# See changes in bars.pm: Check for bar height rounding errors when 
# stacking bars.

@dat = qw(
        991006 991007 991114 991117 991118 991119 991120 
        991121 991122 991123 991124 991125 991126 991127 
        991128 991129 991130 991201 991204 991205 991206 
        991207 991208
);

@sub = qw(0 0 0 0 0 0 0 0 1 1 1 1 2 3 1 1 1 1 2 2 6 8 8);
@def = qw(0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0);
@rej = qw(0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0);
@opn = qw(4 4 4 5 4 4 4 4 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3);
@ass = qw(0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0);
@wrk = qw(1 2 2 2 2 2 1 1 2 2 2 1 1 1 1 1 1 1 1 1 3 6 5);
@fin = qw(0 0 0 0 0 0 1 0 0 0 0 1 1 1 2 2 2 2 2 2 2 2 2);
@ver = qw(0 0 0 0 0 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1);
@con = qw(0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0);
@tst = qw(0 0 0 0 0 0 0 0 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1);
@rev = qw(0 0 0 0 0 0 0 0 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1);
@cco = qw(0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 0 0 0 0 0 0);
@cls = qw(0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 1 0 0 0 0);
@sld = qw(0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 2 2 3 3 3 4);

# setup x data
push(@data,\@dat);         # push x labels into plot data
push(@data,\@sub);
push(@data,\@def);
push(@data,\@rej);
push(@data,\@opn);
push(@data,\@ass);         # push x values into plot data
push(@data,\@wrk);         # (push order must match legend label order)
push(@data,\@fin);
push(@data,\@ver);
push(@data,\@con);
push(@data,\@tst);
push(@data,\@rev);
push(@data,\@cco);
push(@data,\@cls);
push(@data,\@sld);

# setup legend labels
@legend = qw(
        Submitted Deferred Rejected Opened Assigned Work
        Finished Verified Configured Tested Reviewed
        Closed-CO Closed Sealed
);

my @names = qw/sample18 sample18-h/;

for my $graph (GD::Graph::bars->new(600, 400),
               GD::Graph::hbars->new(600, 400))
{
    my $name = shift @names;
    print STDERR "Processing $name\n";

    # set graph legend
    $graph->set_legend(@legend);

    # set graph options
    $graph->set(
       'dclrs'            => [ qw(lblue lyellow blue yellow lgreen lred
                                  green red purple orange pink dyellow) ],
       'title'            => "States by Time",
       'x_label'          => "Time",
       'y_label'          => "# OF thingies",
       'long_ticks'       => 1,
       'tick_length'      => 0,
       'x_ticks'          => 0,
       'x_label_position' => .5,
       'y_label_position' => .5,
       'cumulate'         => 1,

       'bgclr'            => 'white',
       'transparent'      => 0,
       'interlaced'       => 1,

       'y_tick_number'    => 5,
       'y_number_format'  => '%d',
       #'y_max_value'      => 25,
       #'y_min_value'      => 0,
       'y_plot_values'    => 1,
       'x_plot_values'    => 1,
       'zero_axis'        => 1,
    );

   $graph->set('x_labels_vertical'=> 1) unless $name =~/-h$/;

    $graph->plot(\@data);
    save_chart($graph, $name);
}


(参考)
  ●本ソースコードの1〜3行目(挿入) 1 chdir("C:\Inetpub\wwwroot\uriage"); #カレントディレクトリ指定(IISの場合) 2 use lib 'C:/Inetpub/wwwroot/uriage'; #GD::Graphライブラリ格納フォルダ GD (固定)の場所 3 # Windows IIS 走行用に元ソースに挿入   ●>save.pl(グラフ画像保存ルーチン)

最終更新日:2005.1.17