You are here iC Home > Perl > Graphics & Charts > Graphics & Charts - Code

Perl

2.11.1 Graphics & Charts - Code

09.05.2008
2.11 Graphics & Charts [  up  ] - [ a - z ] - [ search PC ] - [ top ] 2.12 Related Books

files/graph lines pl.txt
#!/usr/bin/perl -w
use strict;
use warnings;

use GD::Graph::lines;

print "Content-type: text/html\n\n";

&main();

#############################
sub main() {
#############################
	my @data = (
		["1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th"],
		[1, 5, 6, 7, 3.5, 2, 3, 4.5, 4.5],
		[1, 2, 5, 6, 3, 1.5, 1, 3, 4],
		[ sort { $a <=> $b } (1, 2, 5, 6, 3, 1.5, 1, 3, 4) ]
	);

	my $graph = GD::Graph::lines->new(400, 300);

	$graph->set(
		x_label		=> 'X Label',
		y_label		=> 'Y Label',
		title		=> 'Some simple graph',
		y_max_value	=> 8,
		y_tick_number	=> 8,
		y_label_skip	=> 2
	) or die $graph->error;

	my $format = $graph->export_format;
	open(IMG, ">graph_lines.$format") or die $!;
	binmode IMG;
	print IMG $graph->plot(\@data)->$format();
	close IMG;

	print qq~<a href="graph_lines.$format">graph_lines.$format</a>~;
}

files/histogram pl.txt
#!/usr/bin/perl -w
use strict;
use GD::Graph::histogram;
use GD::Graph::Data;

my @data;
for (my $i = 0; $i < 100; $i++)
{
	push(@data, rand(50));
}

my $my_graph = GD::Graph::histogram->new;

my $name = 'histogram';

print STDERR "Processing $name\n";

$my_graph->set( 
	x_label         => 'X Label',
	y_label         => 'Count',
	title           => 'A Simple Histogram Chart',
	x_labels_vertical => 1,
	bar_spacing     => 0,
	shadow_depth    => 1,
	shadowclr       => 'dred',
	transparent     => 0,
	# histogram_bins => 4
	# histogram_type => 'percentage'
) 
or warn $my_graph->error;

$my_graph->plot(\@data) or die $my_graph->error;

my $ext = $my_graph->export_format;

open(OUT, ">$name.$ext") or die "Cannot open $name.$ext for write: $!";
binmode OUT;
print OUT $my_graph->gd->$ext();
close OUT;



Advanced search tips
2.11 Graphics & Charts [  up  ] - [ top ] 2.12 Related Books



[ home ] - [ search ] - [ feedback ]

copyright by reto - created with mytexi