3 use Imager::Graph::Pie;
5 use Imager::Font::Test;
6 use Imager::Graph::Test qw(cmpimg);
10 or mkdir "testout", 0700
11 or die "Could not create output directory: $!";
15 use Imager qw(:handy);
17 #my $fontfile = 'ImUgly.ttf';
18 #my $font = Imager::Font->new(file=>$fontfile, type => 'ft2', aa=>1)
19 # or plan skip_all => "Cannot create font object: ",Imager->errstr,"\n";
20 my $font = Imager::Font::Test->new();
22 my @data = ( 100, 180, 80, 20, 2, 1, 0.5 );
23 my @labels = qw(alpha beta gamma delta epsilon phi gi);
27 my $pie = Imager::Graph::Pie->new;
28 ok($pie, "creating pie chart object");
30 # this may change output quality too
32 print "# Imager version: $Imager::VERSION\n";
33 print "# Font type: ",ref $font,"\n";
35 my $img1 = $pie->draw(data=>\@data, labels=>\@labels, font=>$font,
36 title=>{ text=>'Imager::Graph::Pie', size=>32 },
37 features=>{ outline=>1, labels=>1, pieblur=>0, },
38 outline=>{ line => '404040' },
41 ok($img1, "drawing first pie chart")
42 or print "# ",$pie->error,"\n";
43 cmpimg($img1, "testimg/t10_pie1.png", 196880977);
44 $img1->write(file=>'testout/t10_pie1.ppm')
45 or die "Cannot save pie1: ",$img1->errstr,"\n";
47 my $img2 = $pie->draw(data=>\@data,
50 title=>{ text=>'Imager::Graph::Pie', size=>36 },
51 features=>{ labelspconly=>1, _debugblur=>1,
53 legend=>{ border=>'000000', fill=>'C0C0C0', },
54 fills=>[ qw(404040 606060 808080 A0A0A0 C0C0C0 E0E0E0) ],
57 ok($img2, "drawing second pie chart")
58 or print "# ",$pie->error,"\n";
59 cmpimg($img2, "testimg/t10_pie2.png", 255956289);
60 $img2->write(file=>'testout/t10_pie2.ppm')
61 or die "Cannot save pie2: ",$img2->errstr,"\n";
63 my $img3 = $pie->draw(data=>\@data, labels=>\@labels,
64 font=>$font, style=>'fount_lin',
65 features=>[ 'legend', 'labelspconly', ],
66 legend=>{ valign=>'center' });
67 ok($img3, "third chart")
68 or print "# ",$pie->error,"\n";
69 $img3->write(file=>'testout/t10_lin_fount.ppm')
70 or die "Cannot save pie3: ",$img3->errstr,"\n";
71 cmpimg($img3, "testimg/t10_lin_fount.png", 180_000);
73 my $img4 = $pie->draw(data=>\@data, labels=>\@labels,
74 font=>$font, style=>'fount_rad',
75 features=>[ 'legend', 'labelspc', ],
76 legend=>{ valign=>'bottom',
79 ok($img4, "fourth chart")
80 or print "# ",$pie->error,"\n";
81 $img4->write(file=>'testout/t10_rad_fount.ppm')
82 or die "Cannot save pie3: ",$img4->errstr,"\n";
83 cmpimg($img4, "testimg/t10_rad_fount.png", 120_000);
85 my $img5 = $pie->draw(data=>\@data, labels=>\@labels,
86 font=>$font, style=>'mono',
87 features=>[ 'allcallouts', 'labelspc' ],
88 legend=>{ valign=>'bottom',
90 ok($img5, "fifth chart")
91 or print "# ",$pie->error,"\n";
92 $img5->write(file=>'testout/t10_mono.ppm')
93 or die "Cannot save pie3: ",$img5->errstr,"\n";
94 cmpimg($img5, "testimg/t10_mono.png", 550_000);
96 my $img6 = $pie->draw(data=>\@data, labels=>\@labels,
97 font=>$font, style=>'fount_lin',
98 features=>[ 'allcallouts', 'labelspc', 'legend' ],
103 orientation => 'horizontal',
104 fill => { solid => Imager::Color->new(0, 0, 0, 32) },
105 patchborder => undef,
108 ok($img6, "sixth chart")
109 or print "# ",$pie->error,"\n";
110 $img6->write(file=>'testout/t10_hlegend.ppm')
111 or die "Cannot save pie6: ",$img5->errstr,"\n";
112 cmpimg($img6, "testimg/t10_hlegend.png", 550_000);
116 # zero sized segments were drawn to cover the whole circle
117 my @data = ( 10, 8, 5, 0.000 );
118 my @labels = qw(alpha beta gamma);
120 local $SIG{__WARN__} =
131 features => [ 'legend', 'labelspc', 'outline' ],
133 ok($img, "create graph with no 'others'");
134 ok($img->write(file => 'testout/t10_noother.ppm'),
136 cmpimg($img, 'testimg/t10_noother.png', 500_000);
137 unless (is(@warned, 0, "should be no warnings")) {
138 diag($_) for @warned;
143 # no font parameter would crash
149 ok(!$im, "should fail to produce titled graph with no font");
150 like($pie->error, qr/title\.font/, "message should mention which font");
156 features => [ 'legend' ],
158 ok(!$im, "should fail to produce legended graph with no font");
159 like($pie->error, qr/legend\.font/, "message should mention which font");
165 features => [ 'legend' ],
166 legend => { orientation => "horizontal" },
168 ok(!$im, "should fail to produce horizontal legended graph with no font");
169 like($pie->error, qr/legend\.font/, "message should mention which font");
176 ok(!$im, "should fail to produce labelled graph with no font");
177 like($pie->error, qr/label\.font/, "message should mention which font");
183 features => [ 'allcallouts' ],
184 label => { font => $font },
186 ok(!$im, "should fail to produce callout labelled graph with no font");
187 like($pie->error, qr/callout\.font/, "message should mention which font");
189 # shouldn't need to set label font if doing all callouts
194 features => [ 'allcallouts' ],
195 callout => { font => $font },
197 ok($im, "should produce callout labelled graph with only callout font")
198 or print "# ", $pie->error, "\n";
200 # shouldn't need to set callout font if doing all labels
204 labels => [ qw/a b c/ ],
205 label => { font => $font }
207 ok($im, "should produce label only graph with only label font");
211 # draw with an empty data array is bad
212 # problem reported and fixed by Patrick Michaud
213 my $im = $pie->draw(data => []);
214 ok(!$im, "fail to draw with empty data");
215 like($pie->error, qr/No values/, "message appropriate");
218 { # pie charts can't represent negative values
219 # problem reported and fixed by Patrick Michaud
220 my $im = $pie->draw(data => [ 10, -1, 10 ]);
221 ok(!$im, "fail to draw with negative value");
222 is($pie->error, "Data index 1 is less than zero", "check message");
225 { # pie can't represent all zeros
226 # problem reported and fixed by Patrick Michaud
227 my $im = $pie->draw(data => [ 0, 0, 0 ]);
228 ok(!$im, "fail to draw with all zero values");
229 is($pie->error, "Sum of all data values is zero", "check message");
233 # test methods used to set features
234 # adds test coverage for otherwise uncovered methods
235 my $pie = Imager::Graph::Pie->new;
236 $pie->add_data_series(\@data);
237 $pie->set_labels(\@labels);
238 $pie->set_font($font);
239 $pie->set_style("mono");
240 $pie->show_callouts_onAll_segments();
241 $pie->show_label_percentages();
242 $pie->set_legend_horizontal_align("right");
243 $pie->set_legend_vertical_align("bottom");
244 my $im = $pie->draw();
246 ok($im, "made mono test using methods");
247 cmpimg($im, "testimg/t10_mono.png", 550_00);
251 # more method coverage
252 my $pie = Imager::Graph::Pie->new;
253 $pie->add_data_series(\@data);
254 $pie->set_labels(\@labels);
255 $pie->set_font($font);
256 $pie->set_style("fount_lin");
258 $pie->show_only_label_percentages();
259 $pie->set_legend_vertical_align("center");
260 my $im = $pie->draw();
262 ok($im, "made lin_found test using methods");
263 cmpimg($im, "testimg/t10_lin_fount.png", 180_000);
267 my $pie = Imager::Graph::Pie->new;
268 my $im = $pie->draw(width => -1, data => \@data);
269 ok(!$im, "shouldn't be able to create neg width image");
270 print "# ", $pie->error, "\n";
271 cmp_ok($pie->error, '=~', qr/^Error creating image/, "check error message");