hoist all the copies of cmpimg into a common module
[imager-graph.git] / t / t14bar.t
CommitLineData
267997be 1#!perl -w
2use strict;
3use Imager::Graph::Bar;
4use lib 't/lib';
5use Imager::Font::Test;
6use Test::More;
119bb3de 7use Imager::Graph::Test 'cmpimg';
267997be 8
9-d 'testout'
10 or mkdir "testout", 0700
11 or die "Could not create output directory: $!";
12
13++$|;
14
15use Imager qw(:handy);
16
4cb01c6b 17plan tests => 11;
267997be 18
19my @warned;
20local $SIG{__WARN__} =
21 sub {
22 print STDERR $_[0];
23 push @warned, $_[0]
24 };
25
26
27#my $fontfile = 'ImUgly.ttf';
28#my $font = Imager::Font->new(file=>$fontfile, type => 'ft2', aa=>1)
29# or plan skip_all => "Cannot create font object: ",Imager->errstr,"\n";
30my $font = Imager::Font::Test->new();
31
32my @data = ( 100, 180, 80, 20, 2, 1, 0.5 );
33my @labels = qw(alpha beta gamma delta epsilon phi gi);
34
86f73dc2
TC
35{
36 my $bar = Imager::Graph::Bar->new();
37 $bar->set_font($font);
38 ok($bar, "creating bar chart object");
267997be 39
86f73dc2
TC
40 $bar->add_data_series(\@data);
41 $bar->set_labels(\@labels);
267997be 42
86f73dc2
TC
43 my $img1 = $bar->draw();
44 ok($img1, "drawing bar chart");
267997be 45
86f73dc2 46 $img1->write(file=>'testout/t14_bar.ppm') or die "Can't save img1: ".$img1->errstr."\n";
5a50139d 47 cmpimg($img1, 'testimg/t14_bar.ppm', 80_000);
86f73dc2
TC
48}
49
50{ # alternative interfaces
51 my $bar = Imager::Graph::Horizontal->new();
52 $bar->set_font($font);
53 ok($bar, "creating bar chart object");
54
55 $bar->add_bar_data_series(\@data);
56 $bar->set_labels(\@labels);
57
58 my $img1 = $bar->draw();
59 ok($img1, "drawing bar chart");
60
61 $img1->write(file=>'testout/t14_bar2.ppm') or die "Can't save img1: ".$img1->errstr."\n";
5a50139d 62 cmpimg($img1, 'testimg/t14_bar.ppm', 80_000);
86f73dc2 63}
267997be 64
4cb01c6b
TC
65{
66 my $bar = Imager::Graph::Bar->new();
67 $bar->set_font($font);
68 ok($bar, "creating bar chart object");
69
70 $bar->add_data_series([ @data, -25 ]);
71 $bar->set_labels([ @labels, "neg" ]);
72
73 my $img1 = $bar->draw();
74 ok($img1, "drawing bar chart (negative values)");
75
76 $img1->write(file=>'testout/t14_bar3.ppm') or die "Can't save img1: ".$img1->errstr."\n";
77 #cmpimg($img1, 'testimg/t14_bar.ppm', 80_000);
78}
79
80{
81 my $bar = Imager::Graph::Bar->new();
82 $bar->set_font($font);
83 ok($bar, "creating bar chart object");
84
85 $bar->add_data_series([ @data, -25 ]);
86 $bar->set_labels([ @labels, "neg" ]);
87
88 my $fountain = Imager::Fountain->simple(colors => [ "#C0C0FF", "#E0E0FF" ],
89 positions => [ 0, 1 ]);
90
91 my %fill =
92 (
93 fountain => "linear",
94 segments => $fountain,
95 xa_ratio => 0.5,
96 ya_ratio => -0.1,
97 xb_ratio => 0.55,
98 yb_ratio => 1.1,
99 );
100
101 $bar->set_negative_background(\%fill);
102
103 my $img1 = $bar->draw();
104 ok($img1, "drawing bar chart (negative values, custom fill)");
105
106 $img1->write(file=>'testout/t14_bar4.ppm') or die "Can't save img1: ".$img1->errstr."\n";
107 #cmpimg($img1, 'testimg/t14_bar.ppm', 80_000);
108}
109
267997be 110unless (is(@warned, 0, "should be no warnings")) {
111 diag($_) for @warned;
112}
113