You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

96 lines
3.1 KiB

#!/usr/bin/perl -w
#
# $Id: piechart.pl,v 1.4 2004/02/10 17:08:43 jeffo Exp $
#
use strict;
use GD;
use GD::Text::Align;
use GD::Graph::pie;
use GD::Graph::colour qw(:colours);
use CGI qw(:standard);
use Data::Dumper;
my $xdim = param('xdim');
if (not $xdim) {$xdim = 500;}
my $ydim = param('ydim');
if (not $ydim) {$ydim = 200};
my $NoLegend = param('nolegend') ;
if (not $NoLegend) {$NoLegend = 0} ;
my $Three_D = 1 ;
if (param('not3d')) {$Three_D = 0;}
my $graph = GD::Graph::pie->new($xdim,$ydim);
my @data = ([map("$_ %",split(/:/,param('values')))],
[split(/:/,param('values'))],
);
my @labels = split(/:/,param('labels'));
my @parm_colors = split(/:/,param('colors'));
add_colour('decentblue' => [32,112,255]);
my $colors = $graph->get('dclrs');
$colors->[2] = 'decentblue';
my $colour ; my $index = 0 ;
foreach $colour (@parm_colors) {
$colors->[$index] = $colour ;
$index ++ ;
}
# print STDERR Dumper($colors);
my $maxlen = 0;
foreach (@labels) {if ($maxlen < length($_)) {$maxlen = length($_);}}
my $legendwidth = 12*($maxlen+2) + 16 + 32;
if ($NoLegend) {$legendwidth = 1} ;
my %opt = ('transparent' => 0,
'dclrs' => $colors,
'bgclr' => 'white',
'fgclr' => 'black',
'accentclr' => 'black',
'labelclr' => 'black',
# 'textclr' => 'black',
'l_margin' => 1,
'b_margin' => 1,
't_margin' => 1,
'r_margin' => $legendwidth,
'3d' => $Three_D,
'pie_height' => 40,
'start_angle' => 180);
if (param('title')) {$opt{'title'} = param('title');}
if (param('xlabel')) {$opt{'x_label'} = param('xlabel');}
if (param('ylabel')) {$opt{'y_label'} = param('ylabel');}
if (param('ymax')) {$opt{'y_max_value'} = param('ymax');}
if (param('ymin')) {$opt{'y_min_value'} = param('ymin');}
if (param('yticknum')) {$opt{'y_tick_number'} = param('yticknum');}
$graph->set(%opt) or die $graph->error;
#$graph->set_title_font("Helvetica");
$graph->set_label_font("/var/www/x4/Fonts/VERDANAB.TTF",12);
#$graph->set_legend_font("/var/www/x4/Fonts/verdana.ttf",10);
$graph->set_value_font("/var/www/x4/Fonts/VERDANAB.TTF",12);
my $gd = $graph->plot(\@data) or die $graph->error;
my $xoff = $xdim - $legendwidth + 8;
my $yoff = ($ydim - 16*(2*@labels+1))/2;
#my $deltay = ($ydim - 2*$yoff - 32)/@labels;
#open(IMG, '>file.png') or die $!;
my $black = $gd->colorClosest(0,0,0);
my @gdt = ();
unless ($NoLegend) {
$gd->rectangle($xoff,$yoff,$xdim-8,$ydim-$yoff,$black);
for (my $i=0; $i < @labels; $i++) {
$gd->rectangle($xoff+16,$yoff+16+$i*32,$xoff+32,$yoff+32+$i*32,$black);
my $color = $gd->colorAllocate(_rgb($colors->[$i]));
$gd->fill($xoff+24,$yoff+24+$i*32,$color);
$gdt[$i] = GD::Text::Align->new($gd) or die GD::Text::Align::error();
if ($gdt[$i]->can_do_ttf()) {
# $gdt[$i]->set_font("/var/www/x4/Fonts/verdana.ttf", 12);
$gdt[$i]->set_font("/var/www/x4/Fonts/VERDANAB.TTF", 12);
} else {
$gdt[$i]->set_font(gdGiantFont);
}
$gdt[$i]->set(color => $black);
$gdt[$i]->set_text($labels[$i]);
$gdt[$i]->draw($xoff+40, $yoff+32+$i*32, 0);
#$gd->string(gdGiantFont,$xoff+40,$yoff+18+$i*32,$labels[$i],$black);
}
}
binmode STDOUT;
print header("image/png");
print $gd->png;