#!/usr/local/bin/perl # # Pie Chart Baker! # Rudy Rucker, (c) Nov, 2002 # use CGI ':standard'; use GD::Graph::pie; my $query = new CGI; for (1..100) { defined $query->param('value'.$_) or last; $query->param('value'.$_) == 0 and next; my ($l, $v) = ($query->param('label'.$_), $query->param('value'.$_)); $v =~ /^[0-9\.]{1,9}$/ or next; #valid numbers, please $l =~ tr/a-zA-Z0-9 \-=+\.\?\!/_/cd; #pick a few valid characters. $l =~ s/^(.{16}).*$/$1/; #limit the length of a label push @{$data[0]}, $l; push @{$data[1]}, $v; } $query->param('Td') eq 'on' ? ($threeD = 1) : ($threeD = 0); unless (@data) { $threeD = 1; $query->param('title', 'Rudy Drinks...'); @data = ( ["coffee","beer","milk","water","soda"], [90,70,40,110,40]); } my $graph = GD::Graph::pie->new(300, 300); $graph->set( dclrs => [ qw(red green white lblue lyellow) ] ); $graph->set( title => $query->param('title'), '3d' => $threeD, 'accentclr' => "yellow") or warn $graph->error; $graph->set_value_font(GD::gdMediumBoldFont); my $imageObject = $graph->plot(\@data) or die $graph->error; my $blob = $imageObject->png; print "Content-type: image/png\n"; print "Content-length: ",length($blob),"\n\n"; print $blob; __END__ This is a short example of pie chart (via CGI) generation. pie slice colors $graph->set( dclrs => [ qw(red green white lblue cyan) ] ); 3d If set to a true value, the pie chart will be drawn with a 3d look. Default: 1. pie_height The thickness of the pie when 3d is true. Default: 0.1 x height. start_angle The angle at which the first data slice will be displayed, with 0 degrees being "6 o'clock". Default: 0. suppress_angle If a pie slice is smaller than this angle (in degrees), a label will not be drawn on it. Default: 0. label Print this label below the pie. Default: undef. in under 10 lines (note cheating with multiple commands per line!) #!/bin/perl use CGI ':standard'; use GD::Graph::pie; my $query = new CGI; for (1..100) { $query->param('value'.$_) or last; my ($l, $v) = ($query->param('label'.$_), $query->param('value'.$_)); push @{$data[0]}, $l; push @{$data[1]}, $v; } my $graph = GD::Graph::pie->new(300, 300); my $imageObject = $graph->plot(\@data) or die $graph->error; print "Content-type: image/png\n\n", $imageObject->png;