GD-Graph3d

 view release on metacpan or  search on metacpan

t/visual-test.pl  view on Meta::CPAN

@data = (
           [ '(Unknown)' ],
           [ 100 ]
        );
$graph->set(
            title             => 'Organisational Use',
);         

ok( compare( $graph->plot( \@data ), "pie100-$GD_Graph_VERSION.png" ) );

#--------------------------------------------------#
# Stacked bar chart with legend                    #
#--------------------------------------------------#
$graph = new GD::Graph::bars3d();

@data = ( 
           ["1".."7"],
           [ 37,  25,   9,  10,   1,  30,  34],
           [ 12,  25,  56,  23,  51,  12,   8],
           [ 42,  25,  18,  32,   8,  13,  20],
);

$graph->set(
    cumulate     => 1,
    x_label      => 'Number',
    y_label      => 'Usage',
    title        => 'Total usage',
    box_axis     => 0,
    y_long_ticks => 1,
	 legend_placement => 'RC',
);

$graph->set_legend( 'Red', 'Green', 'Blue' );

ok( compare( $graph->plot( \@data ), 'stackbar-legend.png' ) );



exit 0;

##############################################################################
#                                END OF TESTS                                #
##############################################################################


#
# A convenience function to build a graph and compare it with
# a graphics file already done
#
# Returns true on success, false on failure.
# If the failure is in building the graph then returns undef.
# If the failure is in the comparison of images, returns 0.
#
sub compare {
	my( $graph, $file ) = @_;
	# Normalize the filename into the test directory
	($file) = fileparse( $file );
	$file = File::Spec->catfile( $FindBin::RealBin, $file );
	# Open the file it should look like
	if( open( FILE, $file ) ) {
		my $gd = GD::Image->newFromPng( \*FILE ) || die "Error loading $file: $!\n";
		close FILE;
		# See if GD compares them (bypass user part if possible)
		return 1 unless ($graph->compare( $gd ) & GD_CMP_IMAGE);
	} else {
		warn "Cannot open $file: $!\n";
	} # end if
	
	# The images differ!
	# So write the image to a file and ask the user if they compare
	my $f2 = _write( $graph, $file );
	if( defined $f2 ) {
		my $r = prompt( "Do the images '$file' and '$f2' look substantially similar?", 'n' );
		print "\n";
		# Now remove the file, unless in STORE mode
		unlink $f2 unless $STORE;
		return ( $r =~ /^y/i ) ? 1 : 0;
		return 0;
	} else {
		warn "Could not save file $f2: $!\n";
		return 0;	# Failure!
	} # end if
} # end compare


#
# Writes a graphic to a file in whatever format the current engine supports
#
sub _write {
	# GLOBAL: $export_format
	my( $g, $f ) = @_;
	
	# Get the base filename, insert -t and format and put in t/ folder 
	($f) = fileparse( $f, '\..*' );
	$f = File::Spec->catfile( $FindBin::RealBin, $f );
	$f = "$f-t.$export_format";
	
	# Write out in whatever format GD prefers
	open( FILE, ">$f" ) || return undef;
	binmode FILE;
	print FILE $g->$export_format;
	close FILE;
	
	# Give back the filename
	return $f;
} # end _write



( run in 0.871 second using v1.01-cache-2.11-cpan-39bf76dae61 )