#!/usr/bin/perl

use strict;
use warnings;

use GD::Graph::bars;

my @products = (
  'Buzzy Bees', 'Kiwifruit Wine', 'Paua Earrings'
);

my @sales_data = (
    [ 2006, 2007, 2008, 2009 ],   # X value labels
    [   54,   52,   50,   48 ],   # Buzzy Bees
    [   38,   37,   30,   17 ],   # Kiwifruit Wine
    [   24,   28,   34,   44 ],   # Paua Earrings
);

my $graph = GD::Graph::bars->new(850, 500);

$graph->set(
    title              => 'Product Sales',
    y_label            => 'Units Shipped (Millions)',
    x_label            => 'Product Sales by Year',
    x_label_position   => 0.5,
    transparent        => 0,
    bgclr              => 'white',
    box_axis           => 0,
    fgclr              => '#bbbbbb',
    axislabelclr       => '#333333',
    labelclr           => '#333333',
    textclr            => '#333333',
    legendclr          => '#333333',
    dclrs              => ['#ff782a', '#6daa3a', '#4d7296'],
    borderclrs         => [undef],
    y_max_value        => 60,
    y_tick_number      => 3,
    tick_length        => -4,
    long_ticks         => 0,
    x_ticks            => 0,
    axis_space         => 6,
    bar_spacing        => 1,
    bargroup_spacing   => 36,
    legend_placement     => 'RC',
    legend_marker_width  => 12,
    legend_marker_height => 12
) or die $graph->error;

$graph->set_legend(@products);

my $font_dir  = '/usr/share/fonts/truetype';
my $font_file = "$font_dir/freefont/FreeSans.ttf";

$graph->set_title_font($font_file, 20);
$graph->set_x_label_font($font_file, 16);
$graph->set_y_label_font($font_file, 16);
$graph->set_x_axis_font($font_file, 11);
$graph->set_y_axis_font($font_file, 11);
$graph->set_legend_font($font_file, 9);

my $gd = $graph->plot(\@sales_data) or die $graph->error;

my $filename = "$0.png";
open my $img, '>', $filename or die "open($filename): $!";
binmode $img;
print $img $gd->png;
