Graph/bars.pm untested

filter: Statementstmt 0.0% 0 / 169 Branchbran 0.0% 0 / 82 Conditioncond 0.0% 0 / 26 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 298 CC 42 SCAR 46.0
CC
42
Cov
0%
draw_values
182.0
draw_data_set_v
132.0
draw_data_set_h
110.0
CRAP 99.7
1 #==========================================================================
2 # Copyright (c) 1995-1998 Martien Verbruggen
3 #--------------------------------------------------------------------------
4 #
5 # Name:
6 # GD::Graph::bars.pm
7 #
8 # $Id: bars.pm,v 1.27 2007/04/27 05:26:47 ben Exp $
9 #
10 #==========================================================================
11
12 package GD::Graph::bars;
13  
14 ($GD::Graph::bars::VERSION) = '$Revision: 1.27 $' =~ /\s([\d.]+)/;
15  
16 use strict;
17  
18 use GD::Graph::axestype;
19 use GD::Graph::utils qw(:all);
20 use GD::Graph::colour qw(:colours);
21  
22 @GD::Graph::bars::ISA = qw(GD::Graph::axestype);
23  
24 use constant PI => 4 * atan2(1,1);
25  
26 sub initialise
27 {
28 my $self = shift;
29 $self->SUPER::initialise();
30 $self->set(correct_width => 1);
31 }
32  
33 sub draw_data
34 {
35 my $self = shift;
36  
37 $self->SUPER::draw_data() or return;
38  
39 unless ($self->{no_axes})
40 {
41 # redraw the 'zero' axis
42 if ($self->{rotate_chart})
43 {
44 $self->{graph}->line(
45 $self->{zeropoint}, $self->{top},
46 $self->{zeropoint}, $self->{bottom},
47 $self->{fgci} );
48 }
49 else
50 {
51 $self->{graph}->line(
52 $self->{left}, $self->{zeropoint},
53 $self->{right}, $self->{zeropoint},
54 $self->{fgci} );
55 }
56 }
57
58 return $self;
59 }
60  
61 sub _top_values
62 {
63 my $self = shift;
64 my @topvalues;
65  
66 if ($self->{cumulate})
67 {
68 my $data = $self->{_data};
69 for my $i (0 .. $data->num_points - 1)
70 {
71 push @topvalues, $data->get_y_cumulative($data->num_sets, $i);
72 }
73 }
74  
75 return \@topvalues;
76 }
77  
78 #
79 # Draw the shadow
80 #
81 sub _draw_shadow
82 {
83 my $self = shift;
84 my ($ds, $i, $value, $topvalues, $l, $t, $r, $b) = @_;
85 my $bsd = $self->{shadow_depth} or return;
86 my $bsci = $self->set_clr(_rgb($self->{shadowclr}),$self->{alpha});
87  
88 if ($self->{cumulate})
89 {
90 return if $ds > 1;
91 $value = $topvalues->[$i];
92 if ($self->{rotate_chart})
93 {
94 $r = ($self->val_to_pixel($i + 1, $value, $ds))[0];
95 }
96 else
97 {
98 $t = ($self->val_to_pixel($i + 1, $value, $ds))[1];
99 }
100 }
101  
102 # XXX Clean this up
103 if ($value >= 0)
104 {
105 if ($self->{rotate_chart})
106 {
107 $self->{graph}->filledRectangle(
108 $l, $t + $bsd, $r - $bsd, $b + $bsd, $bsci);
109 }
110 else
111 {
112 $self->{graph}->filledRectangle(
113 $l + $bsd, $t + $bsd, $r + $bsd, $b, $bsci);
114 }
115 }
116 else
117 {
118 if ($self->{rotate_chart})
119 {
120 $self->{graph}->filledRectangle(
121 $l + $bsd, $t, $r + $bsd, $b, $bsci);
122 }
123 else
124 {
125 $self->{graph}->filledRectangle(
126 $l + $bsd, $b, $r + $bsd, $t + $bsd, $bsci);
127 }
128 }
129 }
130  
131 sub draw_data_set_h
132 {
133 my $self = shift;
134 my $ds = shift;
135  
136 my $bar_s = $self->{bar_spacing}/2;
137  
138 # Pick a data colour
139 my $dsci = $self->set_clr($self->pick_data_clr($ds),$self->{alpha});
140 # contrib "Bremford, Mike" <mike.bremford@gs.com>
141 my $brci;
142 my @rgb = $self->pick_border_clr($ds);
143 if (@rgb > 0){
144 $brci = $self->set_clr(@rgb,$self->{alpha});
145 } else {
146 $brci = undef;
147 }
148  
149 my @values = $self->{_data}->y_values($ds) or
150 return $self->_set_error("Impossible illegal data set: $ds",
151 $self->{_data}->error);
152  
153 my $topvalues = $self->_top_values;
154  
155 for my $i (0 .. $#values)
156 {
157 my $value = $values[$i];
158 next unless defined $value;
159  
160 my $l = $self->_get_bottom($ds, $i);
161 $value = $self->{_data}->get_y_cumulative($ds, $i)
162 if ($self->{cumulate});
163  
164 # CONTRIB Jeremy Wadsack
165 #
166 # cycle_clrs option sets the color based on the point,
167 # not the dataset.
168 $dsci = $self->set_clr($self->pick_data_clr($i + 1),$self->{alpha})
169 if $self->{cycle_clrs};
170 $brci = $self->set_clr($self->pick_data_clr($i + 1),$self->{alpha})
171 if $self->{cycle_clrs} > 1;
172  
173 # get coordinates of right and center of bar
174 my ($r, $xp) = $self->val_to_pixel($i + 1, $value, $ds);
175  
176 # calculate top and bottom of bar
177 my ($t, $b);
178 my $window = $self->{x_step} - $self->{bargroup_spacing};
179  
180 if (ref $self eq 'GD::Graph::mixed' || $self->{overwrite})
181 {
182 $t = $xp - $window/2 + $bar_s + 1;
183 $b = $xp + $window/2 - $bar_s;
184 }
185 else
186 {
187 $t = $xp
188 - $window/2
189 + ($ds - 1) * $window/$self->{_data}->num_sets
190 + $bar_s + 1; # GRANTM thinks this +1 should be conditional on bargroup_spacing being absent
191 $b = $xp
192 - $window/2
193 + $ds * $window/$self->{_data}->num_sets
194 - $bar_s;
195 }
196  
197 # draw the bar
198 $self->_draw_shadow($ds, $i, $value, $topvalues, $l, $t, $r, $b);
199  
200 if ($value < 0) { ($r,$l) = ($l,$r) }
201  
202 $self->{graph}->filledRectangle($l, $t, $r, $b, $dsci)
203 if defined $dsci;
204 $self->{graph}->rectangle($l, $t, $r, $b, $brci)
205 if defined $brci && $b - $t > $self->{accent_treshold};
206  
207 $self->{_hotspots}->[$ds]->[$i] = ['rect', $l, $t, $r, $b];
208 }
209  
210 return $ds;
211 }
212  
213 sub draw_data_set_v
214 {
215 my $self = shift;
216 my $ds = shift;
217  
218 my $bar_s = $self->{bar_spacing}/2;
219  
220 # Pick a data colour
221 my $dsci = $self->set_clr($self->pick_data_clr($ds),$self->{alpha});
222 # contrib "Bremford, Mike" <mike.bremford@gs.com>
223 my $brci;
224 my @rgb = $self->pick_border_clr($ds);
225 if (@rgb > 0){
226 $brci = $self->set_clr(@rgb,$self->{alpha});
227 } else {
228 $brci = undef;
229 }
230  
231  
232 my @values = $self->{_data}->y_values($ds) or
233 return $self->_set_error("Impossible illegal data set: $ds",
234 $self->{_data}->error);
235  
236 my $topvalues = $self->_top_values;
237  
238 my ($bar_sets,$ds_adj) = ( $self->{_data}->num_sets , $ds );
239 if ( $self->isa( 'GD::Graph::mixed' ) ) {
240 my @types = $self->types;
241 $bar_sets = grep { $_ eq 'bars' } @types;
242 $ds_adj = grep { $_ eq 'bars' } @types[0..$ds-1];
243 }
244  
245 for (my $i = 0; $i < @values; $i++)
246 {
247 my $value = $values[$i];
248 next unless defined $value;
249  
250 my $bottom = $self->_get_bottom($ds, $i);
251 $value = $self->{_data}->get_y_cumulative($ds, $i)
252 if ($self->{cumulate});
253  
254 # CONTRIB Jeremy Wadsack
255 #
256 # cycle_clrs option sets the color based on the point,
257 # not the dataset.
258 $dsci = $self->set_clr($self->pick_data_clr($i + 1),$self->{alpha})
259 if $self->{cycle_clrs};
260 $brci = $self->set_clr($self->pick_data_clr($i + 1),$self->{alpha})
261 if $self->{cycle_clrs} > 1;
262  
263 # get coordinates of top and center of bar
264 my ($xp, $t) = $self->val_to_pixel($i + 1, $value, $ds);
265  
266 # calculate left and right of bar
267 my ($l, $r);
268 my $window = $self->{x_step} - $self->{bargroup_spacing};
269  
270 if ($self->{overwrite})
271 {
272 $l = $xp - $window/2 + $bar_s + 1;
273 $r = $xp + $window/2 - $bar_s;
274 }
275 else
276 {
277 $l = $xp
278 - $window/2
279 + ($ds_adj - 1) * $window/$bar_sets
280 + $bar_s + 1; # GRANTM thinks this +1 should be conditional on bargroup_spacing being absent
281 $r = $xp
282 - $window/2
283 + $ds_adj * $window/$bar_sets
284 - $bar_s;
285 }
286  
287 # draw the bar
288 $self->_draw_shadow($ds, $i, $value, $topvalues, $l, $t, $r, $bottom);
289  
290 if ($value < 0) { ($bottom,$t) = ($t,$bottom) }
291 $self->{graph}->filledRectangle($l, $t, $r, $bottom, $dsci)
292 if defined $dsci;
293 $self->{graph}->rectangle($l, $t, $r, $bottom, $brci)
294 if defined $brci && $r - $l > $self->{accent_treshold};
295 $self->{_hotspots}->[$ds]->[$i] = ['rect', $l, $t, $r, $bottom]
296 }
297  
298 return $ds;
299 }
300  
301 sub draw_data_set
302 {
303 $_[0]->{rotate_chart} ? goto &draw_data_set_h : goto &draw_data_set_v;
304 }
305  
306 sub draw_values
307 {
308 my $self = shift;
309  
310 return $self unless $self->{show_values};
311 my $has_args = @_;
312
313 my $text_angle = $self->{values_vertical} ? PI/2 : 0;
314 my @numPoints = $self->{_data}->num_points();
315 my @datasets = $has_args ? @_ : 1 .. $self->{_data}->num_sets;
316  
317 for my $dsn ( @datasets )
318 { # CONTRIB Romeo Juncu
319 my @values = ();
320 if (!$self->get("cumulate")) {
321 @values = $self->{_data}->y_values($dsn) or
322 return $self->_set_error("Impossible illegal data set: $dsn",
323 $self->{_data}->error);
324 } else {
325 my $nPoints = $numPoints[$dsn] || 0;
326 my $vec = $has_args ? \@datasets : undef;
327 @values = map { $self->{_data}->get_y_cumulative($dsn, $_, $vec) }
328 (0..$nPoints - 1) ;
329 }
330 my @display = $self->{show_values}->y_values($dsn) or next;
331  
332 for (my $i = 0; $i < @values; $i++)
333 {
334 next unless defined $display[$i];
335  
336 my $value = $display[$i];
337 if (defined $self->{values_format})
338 {
339 $value = ref $self->{values_format} eq 'CODE' ?
340 &{$self->{values_format}}($value) :
341 sprintf($self->{values_format}, $value);
342 }
343  
344 my ($xp, $yp);
345 if (defined($self->{x_min_value}) && defined($self->{x_max_value}))
346 {
347 ($xp, $yp) = $self->val_to_pixel(
348 $self->{_data}->get_x($i), $values[$i], $dsn);
349 }
350 else
351 {
352 ($xp, $yp) = $self->val_to_pixel($i+1, $values[$i], $dsn);
353 }
354 if ($self->{rotate_chart})
355 {
356 $xp += $self->{values_space};
357 unless ($self->{overwrite})
358 {
359 $yp -= $self->{x_step}/2 - ($dsn - 0.5)
360 * $self->{x_step}/@datasets;
361 }
362 }
363 else
364 {
365 $yp -= $self->{values_space};
366 unless ($self->{overwrite})
367 {
368 $xp -= $self->{x_step}/2 - ($dsn - 0.5)
369 * $self->{x_step}/@datasets;
370 }
371 }
372  
373 $self->{gdta_values}->set_text($value);
374 $self->{gdta_values}->draw($xp, $yp, $text_angle);
375 }
376 }
377  
378 return $self
379 }
380  
381 "Just another true value";