- add horizontal legend boxes
[imager-graph.git] / t / t10pie.t
1 #!perl -w
2 use strict;
3 use Imager::Graph::Pie;
4 use lib 't/lib';
5 use Imager::Font::Test;
6 use Test::More;
7
8 -d 'testout' 
9   or mkdir "testout", 0700 
10   or die "Could not create output directory: $!";
11
12 ++$|;
13
14 my $testnum = 1;
15
16 use Imager qw(:handy);
17
18 #my $fontfile = 'ImUgly.ttf';
19 #my $font = Imager::Font->new(file=>$fontfile, type => 'ft2', aa=>1)
20 #  or plan skip_all => "Cannot create font object: ",Imager->errstr,"\n";
21 my $font = Imager::Font::Test->new();
22
23 my @data = ( 100, 180, 80, 20, 2, 1, 0.5 );
24 my @labels = qw(alpha beta gamma delta epsilon phi gi);
25
26 plan tests => 13;
27
28 my $pie = Imager::Graph::Pie->new;
29 ok($pie, "creating pie chart object");
30
31 # this may change output quality too
32
33 print "# Imager version: $Imager::VERSION\n";
34 print "# Font type: ",ref $font,"\n";
35
36 my $img1 = $pie->draw(data=>\@data, labels=>\@labels, font=>$font, 
37                       title=>{ text=>'Imager::Graph::Pie', size=>32 },
38                       features=>{ outline=>1, labels=>1, pieblur=>0, },
39                       outline=>{ line => '404040' },
40                      );
41
42 ok($img1, "drawing first pie chart")
43   or print "# ",$pie->error,"\n";
44 cmpimg($img1, "testimg/t10_pie1.png", 196880977);
45 $img1->write(file=>'testout/t10_pie1.ppm')
46   or die "Cannot save pie1: ",$img1->errstr,"\n";
47
48 my $img2 = $pie->draw(data=>\@data,
49                       labels=>\@labels,
50                       font=>$font, 
51                       title=>{ text=>'Imager::Graph::Pie', size=>36 },
52                       features=>{ labelspconly=>1, _debugblur=>1,
53                                   legend=>1 },
54                       legend=>{ border=>'000000', fill=>'FF8080', },
55                       fills=>[ qw(404040 606060 808080 A0A0A0 C0C0C0 E0E0E0) ],
56                      );
57
58 ok($img2, "drawing second pie chart")
59   or print "# ",$pie->error,"\n";
60 cmpimg($img2, "testimg/t10_pie2.png", 255956289);
61 $img2->write(file=>'testout/t10_pie2.ppm')
62   or die "Cannot save pie2: ",$img2->errstr,"\n";
63
64 my ($im_version) = $Imager::VERSION =~ /(\d\.[\d_]+)/;
65 {
66   $im_version > 0.38
67     or skip("very old Imager", 6);
68   my $img3 = $pie->draw(data=>\@data, labels=>\@labels,
69                         font=>$font, style=>'fount_lin', 
70                         features=>[ 'legend', 'labelspconly', ],
71                         legend=>{ valign=>'center' });
72   ok($img3, "third chart")
73     or print "# ",$pie->error,"\n";
74   $img3->write(file=>'testout/t10_lin_fount.ppm')
75     or die "Cannot save pie3: ",$img3->errstr,"\n";
76   cmpimg($img3, "testimg/t10_lin_fount.png", 180_000);
77
78   my $img4 = $pie->draw(data=>\@data, labels=>\@labels,
79                         font=>$font, style=>'fount_rad', 
80                         features=>[ 'legend', 'labelspc', ],
81                         legend=>{ valign=>'bottom', 
82                                   halign=>'left',
83                                   border=>'000080' });
84   ok($img4, "fourth chart")
85     or print "# ",$pie->error,"\n";
86   $img4->write(file=>'testout/t10_rad_fount.ppm')
87     or die "Cannot save pie3: ",$img4->errstr,"\n";
88   cmpimg($img4, "testimg/t10_rad_fount.png", 120_000);
89
90   my $img5 = $pie->draw(data=>\@data, labels=>\@labels,
91                         font=>$font, style=>'mono', 
92                         features=>[ 'allcallouts', 'labelspc' ],
93                         legend=>{ valign=>'bottom', 
94                                   halign=>'right' });
95   ok($img5, "fifth chart")
96     or print "# ",$pie->error,"\n";
97   $img5->write(file=>'testout/t10_mono.ppm')
98     or die "Cannot save pie3: ",$img5->errstr,"\n";
99   cmpimg($img5, "testimg/t10_mono.png", 550_000);
100
101   my $img6 = $pie->draw(data=>\@data, labels=>\@labels,
102                         font=>$font, style=>'fount_lin', 
103                         features=>[ 'allcallouts', 'labelspc', 'legend' ],
104                         legend=>
105                         {
106                          valign=>'top', 
107                          halign=>'center',
108                          orientation => 'horizontal',
109                          fill => { solid => Imager::Color->new(0, 0, 0, 32) },
110                          patchborder => undef,
111                          #size => 30,
112                         });
113   ok($img6, "sixth chart")
114     or print "# ",$pie->error,"\n";
115   $img6->write(file=>'testout/t10_hlegend.ppm')
116     or die "Cannot save pie6: ",$img5->errstr,"\n";
117   cmpimg($img6, "testimg/t10_hlegend.png", 550_000);
118 }
119
120 sub cmpimg {
121   my ($img, $file, $limit) = @_;
122
123   $limit ||= 10000;
124
125  SKIP:
126   {
127     $Imager::formats{png}
128       or skip("No PNG support", 1);
129
130     my $cmpimg = Imager->new;
131     $cmpimg->read(file=>$file)
132       or return ok(0, "Cannot read $file: ".$cmpimg->errstr);
133     my $diff = Imager::i_img_diff($img->{IMG}, $cmpimg->{IMG});
134     cmp_ok($diff, '<', $limit, "Comparison to $file ($diff)");
135   }
136 }