Skip to content

Instantly share code, notes, and snippets.

@SebDeclercq
Created September 23, 2014 12:57
Show Gist options
  • Save SebDeclercq/74d73b7674101785dacc to your computer and use it in GitHub Desktop.
Save SebDeclercq/74d73b7674101785dacc to your computer and use it in GitHub Desktop.
package pieChart {
use Moose;
has 'label', is => 'ro', isa => 'Str';
has 'data', is => 'ro', isa => 'ArrayRef';
has 'pcsuccess', is => 'ro', isa => 'Int';
has 'pcerror', is => 'ro', isa => 'Int';
sub png {
use GD::Graph::pie;
my $self = shift;
my $chart = GD::Graph::pie->new();
my $colors = [qw/green red/];
my $catego = [qw/success error/];
my $data;
if (defined $self->data) {
$data = $self->data;
} else {
$data = [$self->pcsuccess,$self->pcerror];
}
my @pie = ($catego,$data);
$chart->set(dclrs => $colors,
title => "% of SUCCESS(".uc($self->label).")");
$chart->plot(\@pie);
open IMG,">",$self->label.".png" or die $!;
binmode IMG;
print IMG $chart->gd->png;
close IMG;
}
return 1;
}
#! /usr/bin/perl -w
use strict;
use warnings;
use feature 'say';
use myFirstObject;
my $data = [55,45];
my $name = "mon super gros essai";
my $test = pieChart->new(label => $name,data => $data);
$test->png or die "Unable to create PNG chart : $!";
my $essai = pieChart->new(label => 'second test', pcsuccess => 62,
pcerror => 28);
$essai->png or die "Unable to create PNG chart : $!";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment