lib/Chart/Bars.pm

filter: Statementstmt 60.0% 3 / 5 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 71.4% 5 / 7 CC 1 SCAR 1.4
CC
1
Cov
60%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« blib/lib/Chart/Property/DataType/Color.pm ↑ Summary lib/Chart/Composite.pm »
1  
2 # vertical bars
3  
4 13 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package Chart::Bars;
7 our @ISA = qw(Chart::Base);
8 our $VERSION = 'v2.403.9';
9  
10 0 ▶use Chart::Base;
Subroutine BEGIN: called CC=1 SCAR=2.6
11 use GD;
12 use Carp;
13  
14 #>>>>>>>>>>>>>>>>>>>>>>>>>>#
15 # public methods #
16 #<<<<<<<<<<<<<<<<<<<<<<<<<<#
17  
18 #>>>>>>>>>>>>>>>>>>>>>>>>>>>#
19 # private methods #
20 #<<<<<<<<<<<<<<<<<<<<<<<<<<<#
21  
22 ## _draw_data : plotting the data for (vertical) bars
23 #
24 # The user may define the kind of labelling the data by setting
25 # 'label_values' to 'value' if she wants to have the absolut values
26 # 'label_values' to 'none' if she wants to have no values (default)
27 #
28 sub _draw_data {
29 my $self = shift;
30  
31 my $data = $self->{'dataref'};
32 my $misccolor = $self->_color_role_to_index('misc');
33 my ( $x1, $x2, $x3, $y1, $y2, $y3 );
34 my ( $width, $height, $delta1, $delta2, $map, $mod, $cut, $pink );
35 my ( $i, $j, $color );
36 my $temp = 0;
37 my $font = $self->{'legend_font'};
38 my $fontW = $self->{'legend_font'}->width;
39 my $fontH = $self->{'legend_font'}->height;
40 my $textcolor = $self->_color_role_to_index('text');
41  
42 # init the imagemap data field if they wanted it
43 if ( $self->true( $self->{'imagemap'} ) )
44 {
45 $self->{'imagemap_data'} = [];
46 }
47  
48 # find the longest label
49 # first we need the length of the values
50 # draw the bars
51 my $max_label_len = 0;
52 for $i ( 1 .. $self->{'num_datasets'} )
53 {
54 for $j ( 0 .. $self->{'num_datapoints'} )
55 {
56  
57 # don't try to draw anything if there's no data
58 if ( defined( $data->[$i][$j] )
59 && $data->[$i][$j] =~ /^[\-\+]{0,1}\s*[\d\.eE\-\+]+/ )
60 {
61 if ( defined $self->{'label_values'} && $self->{'label_values'} =~ /^value$/i )
62 {
63 my $label = sprintf( "%.2f", $data->[$i][$j] );
64 my $label_length = length($label);
65 $max_label_len = $label_length if ( $max_label_len < $label_length );
66 }
67 }
68 }
69 }
70 $max_label_len *= $fontH;
71  
72 # find both delta values ($delta1 for stepping between different
73 # datapoint names, $delta2 for stepping between datasets for that
74 # point) and the mapping constant
75 $width = $self->{'curr_x_max'} - $self->{'curr_x_min'};
76 $height = $self->{'curr_y_max'} - $self->{'curr_y_min'};
77 $delta1 =
78 ( $self->{'num_datapoints'} > 0 )
79 ? $width / ( $self->{'num_datapoints'} * 1 )
80 : $width;
81  
82 $map =
83 ( ( $self->{'max_val'} - $self->{'min_val'} ) > 0 )
84 ? $height / ( $self->{'max_val'} - $self->{'min_val'} )
85 : $height;
86  
87 if ( $self->true( $self->{'spaced_bars'} ) )
88 {
89  
90 #OLD: $delta2 = $delta1 / ($self->{'num_datasets'} + 2);
91 $delta2 =
92 ( ( $self->{'num_datasets'} + 2 ) > 0 )
93 ? $delta1 / ( $self->{'num_datasets'} + 2 )
94 : $delta1;
95 }
96 else
97 {
98 $delta2 =
99 ( $self->{'num_datasets'} > 0 )
100 ? $delta1 / $self->{'num_datasets'}
101 : $delta1;
102 }
103  
104 # get the base x-y values
105 $x1 = $self->{'curr_x_min'};
106  
107 if ( $self->{'min_val'} >= 0 )
108 {
109 $y1 = $self->{'curr_y_max'};
110 $mod = $self->{'min_val'};
111 }
112 elsif ( $self->{'max_val'} <= 0 )
113 {
114 $y1 = $self->{'curr_y_min'};
115 $mod = $self->{'max_val'};
116 }
117 else
118 {
119 $y1 = $self->{'curr_y_min'} + ( $map * $self->{'max_val'} );
120 $mod = 0;
121 $self->{'gd_obj'}->line( $self->{'curr_x_min'}, $y1, $self->{'curr_x_max'}, $y1, $misccolor );
122 }
123  
124 # draw the bars
125 for $i ( 1 .. $self->{'num_datasets'} )
126 {
127  
128 # get the color for this dataset
129 $color = $self->_color_role_to_index( 'dataset' . ( $i - 1 ) );
130  
131 # draw every bar for this dataset
132 for $j ( 0 .. $self->{'num_datapoints'} )
133 {
134  
135 # don't try to draw anything if there's no data
136 if ( defined( $data->[$i][$j] )
137 && $data->[$i][$j] =~ /^[\-\+]{0,1}\s*[\d\.eE\-\+]+/ )
138 {
139  
140 # find the bounds of the rectangle
141 if ( $self->true( $self->{'spaced_bars'} ) )
142 {
143 $x2 = ( $x1 + ( $j * $delta1 ) + ( $i * $delta2 ) );
144 }
145 else
146 {
147 $x2 = $x1 + ( $j * $delta1 ) + ( ( $i - 1 ) * $delta2 );
148 }
149 $y2 = $y1;
150 $x3 = $x2 + $delta2;
151 $y3 = $y1 - ( ( $data->[$i][$j] - $mod ) * $map );
152  
153 # cut the bars off, if needed
154 if ( $data->[$i][$j] > $self->{'max_val'} )
155 {
156 $y3 = $y1 - ( ( $self->{'max_val'} - $mod ) * $map );
157 $cut = 1;
158 }
159 elsif ( $data->[$i][$j] < $self->{'min_val'} )
160 {
161 $y3 = $y1 - ( ( $self->{'min_val'} - $mod ) * $map );
162 $cut = 1;
163 }
164 else
165 {
166 $cut = 0;
167 }
168  
169 # draw the bar
170 ## y2 and y3 are reversed in some cases because GD's fill
171 ## algorithm is lame
172 if ( $data->[$i][$j] > 0 )
173 {
174 $self->{'gd_obj'}->filledRectangle( $x2, $y3, $x3, $y2, $color );
175 if ( $self->true( $self->{'imagemap'} ) )
176 {
177 $self->{'imagemap_data'}->[$i][$j] = [ $x2, $y3, $x3, $y2 ];
178 }
179  
180 if ( defined $self->{'label_values'} && $self->{'label_values'} =~ /^value$/i )
181 {
182  
183 # draw data
184 my $labelX = $x2;
185 my $labelY = $y3 + $fontH; #$max_label_len;
186 if ( $labelY < 0 ) { $labelY = $y3; }
187 my $label = sprintf( "%.2f", $data->[$i][$j] );
188 $self->{'gd_obj'}->stringUp( $font, $labelX + $fontW * 0.5, $labelY, $label, $textcolor );
189 }
190  
191 }
192 else
193 {
194 $self->{'gd_obj'}->filledRectangle( $x2, $y2, $x3, $y3, $color );
195 if ( $self->true( $self->{'imagemap'} ) )
196 {
197 $self->{'imagemap_data'}->[$i][$j] = [ $x2, $y2, $x3, $y3 ];
198 }
199 }
200  
201 # now outline it. outline red if the bar had been cut off
202 unless ($cut)
203 {
204 $self->{'gd_obj'}->rectangle( $x2, $y3, $x3, $y2, $misccolor );
205 }
206 else
207 {
208 $pink = $self->{'gd_obj'}->colorAllocate( 255, 0, 255 );
209 $self->{'gd_obj'}->rectangle( $x2, $y3, $x3, $y2, $pink );
210 }
211  
212 }
213 else
214 {
215 if ( $self->true( $self->{'imagemap'} ) )
216 {
217 $self->{'imagemap_data'}->[$i][$j] = [ undef(), undef(), undef(), undef() ];
218 }
219 }
220 }
221 }
222  
223 # and finaly box it off
224 $self->{'gd_obj'}
225 ->rectangle( $self->{'curr_x_min'}, $self->{'curr_y_min'}, $self->{'curr_x_max'}, $self->{'curr_y_max'}, $misccolor );
226 return;
227  
228 }
229  
230 1; # be a good module and return 1
« blib/lib/Chart/Property/DataType/Color.pm ↑ Summary lib/Chart/Composite.pm »