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