public
Created

  • Download Gist
chart-clicker-example.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
#!/usr/bin/env perl
 
package Geometry::Primitive::Triangle;
 
use Moose;
use Geometry::Primitive::Point;
extends 'Geometry::Primitive::Polygon';
 
has 'origin' => (
is => 'rw',
isa => 'Geometry::Primitive::Point',
coerce => 1,
);
 
has 'width' => (
is => 'rw',
isa => 'Num',
required => 1
);
 
override 'point_count' => sub {
my $self = shift;
my @points;
my ($x, $y, $width) = ($self->origin->x, $self->origin->y, $self->width);
 
push @points, [$x - $width/2, $y + (sqrt(3)/6*$width)];
push @points, [$x + $width/2, $y + (sqrt(3)/6*$width)];
push @points, [$x, $y - (sqrt(3)/3*$width)];
 
map { $self->add_point(Geometry::Primitive::Point->new(x => $_->[0], y => $_->[1])) } @points;
return scalar @points;
};
 
package main;
 
use Modern::Perl;
use Smart::Comments;
use Chart::Clicker;
use Chart::Clicker::Data::Series;
use Chart::Clicker::Data::DataSet;
use Chart::Clicker::Drawing::ColorAllocator;
use Chart::Clicker::Renderer::Point;
use Readonly;
 
Readonly my $BLUE => Graphics::Color::RGB->new(red => 0, green => 0, blue => 0.75, alpha => 1);
Readonly my $RED => Graphics::Color::RGB->new(red => 0.75, green => 0, blue => 0, alpha => 0.8);
Readonly my $GREY => Graphics::Color::RGB->new(red => 0.9, green => 0.9, blue => 0.9, alpha => 1);
Readonly my $SHAPE_BRUSH => Graphics::Primitive::Brush->new(width => 2, color => $BLUE);
 
my $chart = Chart::Clicker->new;
my $ca = Chart::Clicker::Drawing::ColorAllocator->new;
 
my $context = $chart->get_context('default');
my $mark_context = Chart::Clicker::Context->new(name => 'mark');
$mark_context->share_axes_with($context);
 
my $series = Chart::Clicker::Data::Series->new(
keys => [ 1,2,3,4,5 ],
values => [ 52,74,52,82,14 ],
);
 
my $dataset = Chart::Clicker::Data::DataSet->new(series => [$series]);
my $mark_dataset = Chart::Clicker::Data::DataSet->new(series => [$series]);
$mark_dataset->context('mark');
$chart->add_to_contexts($mark_context);
 
$mark_context->renderer(Chart::Clicker::Renderer::Point->new);
$mark_context->renderer->shape(Geometry::Primitive::Triangle->new({width => 15}));
$mark_context->renderer->shape_brush($SHAPE_BRUSH);
 
my $x_axis = $context->domain_axis;
my $y_axis = $context->range_axis;
$x_axis->fudge_amount(0.1);
$y_axis->fudge_amount(0.1);
 
$chart->add_to_datasets($dataset);
$chart->add_to_datasets($mark_dataset);
 
$ca->add_to_colors($BLUE);
$ca->add_to_colors($GREY);
$chart->color_allocator($ca);
 
$chart->write_output('chart.png');

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.