#!/usr/bin/perl -w
use strict;
use Chart::Clicker;
use Chart::Clicker::Axis;
use Chart::Clicker::Data::DataSet;
use Chart::Clicker::Data::Series;
use Chart::Clicker::Decoration::Grid;
use Chart::Clicker::Decoration::Legend;
use Chart::Clicker::Decoration::Plot;
use Chart::Clicker::Drawing qw(:positions);
use Chart::Clicker::Drawing::Insets;
use Chart::Clicker::Renderer::Area qw/:options/;
use Math::GSL::SF qw/:bessel/;

my $function = shift || 'gsl_sf_bessel_J0';
my $name     = shift || "Bessel Function J0(x)";
my $filename = shift || 'chart.png';
my $chart    = new Chart::Clicker({ format => 'png', width => 400, height => 300 });
my @x        = map { $_/ 100 } ( 0 .. 3000 ) ;
my @negx     = map { -$_ } reverse @x ;
my $series;

{
    no strict 'refs';
    $series = new Chart::Clicker::Data::Series({
        keys    => [@negx, @x],
        values  => [map { $function->($_) } (@negx,@x) ],
        name    => $name,
    });
}

my $dataset = new Chart::Clicker::Data::DataSet({ series => [ $series ] });
$chart->datasets([ $dataset ]);

my $legend = new Chart::Clicker::Decoration::Legend({
    margins => new Chart::Clicker::Drawing::Insets({
        top => 3
})
});
$chart->add($legend, $CC_BOTTOM);

my $daxis = new Chart::Clicker::Axis({
    orientation => $CC_HORIZONTAL,
    position    => $CC_BOTTOM,
    format      => '%d',
    tick_values => [-25,-20,-15,-10,-5,0,5,10,15,20,25],
});
$chart->add($daxis, $CC_AXIS_BOTTOM);
my $raxis = new Chart::Clicker::Axis({
    orientation => $CC_VERTICAL,
    position    => $CC_LEFT,
    format      => '%0.2f'
});
$chart->add($raxis, $CC_AXIS_LEFT);
$chart->range_axes([ $raxis ]);
$chart->domain_axes([ $daxis ]);

my $grid = new Chart::Clicker::Decoration::Grid();
$chart->add($grid, $CC_CENTER, 0);

my $renderer = new Chart::Clicker::Renderer::Area( { fade => 1 });
my $plot = new Chart::Clicker::Decoration::Plot();

$plot->renderers([$renderer]);
$chart->plot($plot);
$chart->add($plot, $CC_CENTER);
$chart->prepare();
$chart->draw();
$chart->write($filename);
