#!/usr/bin/perl

use strict;
use warnings;

use GD;
use GD::Graph::bars;

my $font_file1 = '/usr/share/fonts/truetype/msttcorefonts/Trebuchet_MS_Bold.ttf';
my $font_file2 = '/usr/share/fonts/truetype/msttcorefonts/Arial_Bold.ttf';

make_chart(
    sub { int(rand(100)) },
    'random-1.png',
    title             => 'rand(100)  10,000 Samples',
    y_max_value       => 150,
    y_tick_number     => 3,
);

make_chart(
    sub { int(rand(50) + rand(50)) },
    'random-2.png',
    title             => 'rand(50)+rand(50) 10,000 Samples',
    y_max_value       => 250,
    y_tick_number     => 5,
);

make_chart(
    sub { int(rand(33) + rand(33) + rand(33)) },
    'random-3.png',
    title             => '3 Components 10,000 Samples',
    y_max_value       => 300,
    y_tick_number     => 6,
);

make_chart(
    sub { int(rand(25) + rand(25) + rand(25) + rand(25)) },
    'random-4.png',
    title             => '4 Components 10,000 Samples',
    y_max_value       => 350,
    y_tick_number     => 7,
);

make_chart(
    sub { int(rand(20) + rand(20) + rand(20) + rand(20) + rand(20)) },
    'random-5.png',
    title             => '5 Components 10,000 Samples',
    y_max_value       => 350,
    y_tick_number     => 7,
);

make_chart(
    sub { my $i = 0; $i += rand(10) foreach (1..10); return int($i); },
    'random-10.png',
    title             => '10 Components 10,000 Samples',
    y_max_value       => 550,
    y_tick_number     => 11,
);

make_chart(
    sub { my $i = 0; $i += rand(2) foreach (1..50); return int($i); },
    'random-50.png',
    title             => '50 Components 10,000 Samples',
    #y_max_value       => 800,
    #y_tick_number     => 13,
);

exit;

sub make_chart {
    my $rand_sub   = shift;
    my $filename   = shift;
    my %chart_opts = @_;

    my @bucket = (0) x 100;
    $bucket[ &$rand_sub ]++ foreach (1..10000);

    my @data = (
        [ 0..99 ],
        \@bucket,
    );

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

    my %settings = (
        x_label           => 'Value Returned',
        y_label           => 'Frequency',
        x_label_position  => 0.5,
        x_labels_vertical => 1,
        transparent       => 0,
        bgclr             => 'white',
        fgclr             => '#bbbbbb',
        axislabelclr      => '#333333',
        labelclr          => '#333333',
        textclr           => '#333333',
        box_axis          => 1,
        long_ticks        => 1,
        x_ticks           => 0,
        axis_space        => 4,
        line_width        => 2,
        bar_spacing       => 1,
        dclrs             => ['#b74345'],
        borderclrs        => ['#b74345'],
        %chart_opts
    );
    $graph->set( %settings ) or die $graph->error;

    $graph->set_title_font($font_file1, 20);
    $graph->set_x_label_font($font_file2, 16);
    $graph->set_y_label_font($font_file2, 16);
    $graph->set_x_axis_font(gdTinyFont);
    $graph->set_y_axis_font($font_file2, 11);

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

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