日向夏特殊応援部隊

俺様向けメモ

Imager::Graph のサンプル

今、ちょうど簡単なお仕事でグラフ出さないといけないので、適当に書いてみた。GD::Graph の出力はどう見ても切ない貧弱な表示なので今回は触れない。

#!/usr/bin/perl

use utf8;
use strict;
use warnings;

use Carp;
use Imager;
use Imager::Graph::Pie;

my $chart = Imager::Graph::Pie->new;
my $font = Imager::Font->new( file => '/path/to/example.ttf', aa => 1 );

$chart->set_style('primary');
$chart->set_font($font);

my $img = $chart->draw(
    width => 500,
    height => 500,
    font => $font,
    bg => 'F0F0F0',
    fg => '333333',
    aa => 1,
    data => [30, 20, 35, 10, 1, 4],
    labels => ['諸葛孔明', '劉玄徳', '曹孟徳', '孫仲謀', '武安国', 'その他'],
    features => +{ dropshadow => 1, labels => 1, labelspc => 1, legend => 1,},
    title => +{
        text => '三国志で好きな登場人物',
        color => '000000',
        size => 16,
        font => $font,
    },
    label => +{
        font => $font,
        color => '333333',
    },
    legend => +{
        font => $font,
        color => '333333',
    },
    callout => +{
        leadaa => 1,
    },
    fills => [qw/BE1E2D EE8310 92D5EA 666699 009900 FF0000/],
    size => 300,
);

print STDERR $chart->error if ($chart->error);

$img->write( file => 'test.png' );

で出力はこんな感じ。

とりあえずソース読まないとどの辺りがカスタマイズ出来るかどうか分からない。Imager::Graph の %style_defs の内容が draw に渡せる値だと思ってほぼ間違いない。

とりあえず、渡せる設定項目とかいまいち良く分からないので、あれこれ弄る際のベースにご利用ください。