lib/Chart/Lines.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
« lib/Chart/HorizontalBars.pm ↑ Summary lib/Chart/LinesPoints.pm »
1  
2 # chart type with lines
3  
4 13 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package Chart::Lines;
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 go here #
16 #<<<<<<<<<<<<<<<<<<<<<<<<<<#
17  
18 #>>>>>>>>>>>>>>>>>>>>>>>>>>>#
19 # private methods go here #
20 #<<<<<<<<<<<<<<<<<<<<<<<<<<<#
21  
22 ## @method private _draw_data
23 # finally get around to plotting the data for lines
24 sub _draw_data
25 {
26 my $self = shift;
27 my $data = $self->{'dataref'};
28 my $misccolor = $self->_color_role_to_index('misc');
29 my ( $x1, $x2, $x3, $y1, $y2, $y3, $mod, $abs_x_max, $abs_y_max, $tan_alpha );
30 my ( $width, $height, $delta, $delta_num, $map, $t_x_min, $t_x_max, $t_y_min, $t_y_max );
31 my ( $i, $j, $color, $brush, $zero_offset );
32 my $repair_top_flag = 0;
33 my $repair_bottom_flag = 0;
34  
35 # init the imagemap data field if they asked for it
36 if ( $self->true( $self->{'imagemap'} ) )
37 {
38 $self->{'imagemap_data'} = [];
39 }
40  
41 # find the delta value between data points, as well
42 # as the mapping constant
43 $width = $self->{'curr_x_max'} - $self->{'curr_x_min'};
44 $height = $self->{'curr_y_max'} - $self->{'curr_y_min'};
45 $delta = $width / ( $self->{'num_datapoints'} > 0 ? $self->{'num_datapoints'} : 1 );
46 $map = $height / ( $self->{'max_val'} - $self->{'min_val'} );
47  
48 #for a xy-plot, use this delta and maybe an offset for the zero-axes
49 if ( $self->true( $self->{'xy_plot'} ) )
50 {
51 $delta_num = $width / ( $self->{'x_max_val'} - $self->{'x_min_val'} );
52  
53 if ( $self->{'x_min_val'} <= 0 && $self->{'x_max_val'} >= 0 )
54 {
55 $zero_offset = abs( $self->{'x_min_val'} ) * abs($delta_num);
56 }
57 elsif ( $self->{'x_min_val'} > 0 || $self->{'x_max_val'} < 0 )
58 {
59 $zero_offset = -$self->{'x_min_val'} * $delta_num;
60 }
61 else
62 {
63 $zero_offset = 0;
64 }
65 }
66  
67 # get the base x-y values
68 if ( $self->true( $self->{'xy_plot'} ) )
69 {
70 $x1 = $self->{'curr_x_min'};
71 }
72 else
73 {
74 $x1 = $self->{'curr_x_min'} + ( $delta / 2 );
75 }
76 if ( $self->{'min_val'} >= 0 )
77 {
78 $y1 = $self->{'curr_y_max'};
79 $mod = $self->{'min_val'};
80 }
81 elsif ( $self->{'max_val'} <= 0 )
82 {
83 $y1 = $self->{'curr_y_min'};
84 $mod = $self->{'max_val'};
85 }
86 else
87 {
88 $y1 = $self->{'curr_y_min'} + ( $map * $self->{'max_val'} );
89 $mod = 0;
90 $self->{'gd_obj'}->line( $self->{'curr_x_min'}, $y1, $self->{'curr_x_max'}, $y1, $misccolor );
91 }
92  
93 # draw the lines
94  
95 for $i ( 1 .. $self->{'num_datasets'} )
96 {
97  
98 # get the color for this dataset, and set the brush
99 $color = $self->_color_role_to_index( 'dataset' . ( $i - 1 ) );
100 $brush = $self->_prepare_brush($color);
101 $self->{'gd_obj'}->setBrush($brush);
102  
103 # draw every line for this dataset
104 for $j ( 1 .. $self->{'num_datapoints'} - 1 )
105 {
106  
107 # don't try to draw anything if there's no data
108 if ( defined( $data->[$i][$j] ) and defined( $data->[$i][ $j - 1 ] ) )
109 {
110 if ( $self->true( $self->{'xy_plot'} ) )
111 {
112 $x2 = $x1 + $delta_num * $data->[0][ $j - 1 ] + $zero_offset;
113 $x3 = $x1 + $delta_num * $data->[0][$j] + $zero_offset;
114 }
115 else
116 {
117 $x2 = $x1 + ( $delta * ( $j - 1 ) );
118 $x3 = $x1 + ( $delta * $j );
119 }
120 $y2 = $y1 - ( ( $data->[$i][ $j - 1 ] - $mod ) * $map );
121 $y3 = $y1 - ( ( $data->[$i][$j] - $mod ) * $map );
122  
123 # now draw the line
124 # ----------------
125 # stepline option added by G.ST. 2005/02
126 #----------------
127 if ( $self->true( $self->{'stepline'} ) )
128 {
129 if ( $self->{'stepline_mode'} =~ /^begin$/i )
130 {
131 $self->{'gd_obj'}->line( $x2, $y2, $x3, $y2, gdBrushed );
132 $self->{'gd_obj'}->line( $x3, $y2, $x3, $y3, gdBrushed );
133 }
134 else
135 {
136 $self->{'gd_obj'}->line( $x2, $y2, $x2, $y3, gdBrushed );
137 $self->{'gd_obj'}->line( $x2, $y3, $x3, $y3, gdBrushed );
138 }
139 }
140  
141 # -----------------------------------
142 # end stepline option
143 #------------------------------------
144 else
145 {
146 $self->{'gd_obj'}->line( $x2, $y2, $x3, $y3, gdBrushed );
147 }
148  
149 # set the flags, if the lines are out of the borders of the chart
150 if ( ( $data->[$i][$j] > $self->{'max_val'} ) || ( $data->[$i][ $j - 1 ] > $self->{'max_val'} ) )
151 {
152 $repair_top_flag = 1;
153 }
154  
155 if ( ( $self->{'max_val'} <= 0 )
156 && ( ( $data->[$i][$j] > $self->{'max_val'} ) || ( $data->[$i][ $j - 1 ] > $self->{'max_val'} ) ) )
157 {
158 $repair_top_flag = 1;
159 }
160 if ( ( $data->[$i][$j] < $self->{'min_val'} ) || ( $data->[$i][ $j - 1 ] < $self->{'min_val'} ) )
161 {
162 $repair_bottom_flag = 1;
163 }
164  
165 # store the imagemap data if they asked for it
166 if ( $self->true( $self->{'imagemap'} ) )
167 {
168 $self->{'imagemap_data'}->[$i][ $j - 1 ] = [ $x2, $y2 ];
169 $self->{'imagemap_data'}->[$i][$j] = [ $x3, $y3 ];
170 }
171 }
172 else
173 {
174 if ( $self->true( $self->{'imagemap'} ) )
175 {
176 $self->{'imagemap_data'}->[$i][ $j - 1 ] = [ undef(), undef() ];
177 $self->{'imagemap_data'}->[$i][$j] = [ undef(), undef() ];
178 }
179 }
180 }
181 }
182  
183 # and finaly box it off
184 $self->{'gd_obj'}
185 ->rectangle( $self->{'curr_x_min'}, $self->{'curr_y_min'}, $self->{'curr_x_max'}, $self->{'curr_y_max'}, $misccolor );
186  
187 #get the width and the heigth of the complete picture
188 ( $abs_x_max, $abs_y_max ) = $self->{'gd_obj'}->getBounds();
189  
190 #repair the chart, if the lines are out of the borders of the chart
191 if ($repair_top_flag)
192 {
193  
194 #overwrite the ugly mistakes
195 $self->{'gd_obj'}->filledRectangle(
196 $self->{'curr_x_min'} - ( $self->{'brush_size'} / 2 ),
197 0, $self->{'curr_x_max'},
198 $self->{'curr_y_min'} - 1,
199 $self->_color_role_to_index('background')
200 );
201  
202 #save the actual x and y values
203 $t_x_min = $self->{'curr_x_min'};
204 $t_x_max = $self->{'curr_x_max'};
205 $t_y_min = $self->{'curr_y_min'};
206 $t_y_max = $self->{'curr_y_max'};
207  
208 #get back to the point, where everything began
209 $self->{'curr_x_min'} = 0;
210 $self->{'curr_y_min'} = 0;
211 $self->{'curr_x_max'} = $abs_x_max;
212 $self->{'curr_y_max'} = $abs_y_max;
213  
214 #draw the title again
215 if ( $self->{'title'} )
216 {
217 $self->_draw_title;
218 }
219  
220 #draw the sub title again
221 if ( $self->{'sub_title'} )
222 {
223 $self->_draw_sub_title;
224 }
225  
226 #draw the top legend again
227 if ( $self->{'legend'} =~ /^top$/i )
228 {
229 $self->_draw_top_legend;
230 }
231  
232 #reset the actual values
233 $self->{'curr_x_min'} = $t_x_min;
234 $self->{'curr_x_max'} = $t_x_max;
235 $self->{'curr_y_min'} = $t_y_min;
236 $self->{'curr_y_max'} = $t_y_max;
237 }
238  
239 if ($repair_bottom_flag)
240 {
241  
242 #overwrite the ugly mistakes
243 $self->{'gd_obj'}->filledRectangle(
244 $self->{'curr_x_min'} - ( $self->{'brush_size'} / 2 ),
245 $self->{'curr_y_max'} + 1,
246 $self->{'curr_x_max'}, $abs_y_max, $self->_color_role_to_index('background')
247 );
248  
249 #save the actual x and y values
250 $t_x_min = $self->{'curr_x_min'};
251 $t_x_max = $self->{'curr_x_max'};
252 $t_y_min = $self->{'curr_y_min'};
253 $t_y_max = $self->{'curr_y_max'};
254  
255 #get back to the point, where everything began
256 $self->{'curr_x_min'} = 0;
257 $self->{'curr_y_min'} = 0;
258 $self->{'curr_x_max'} = $abs_x_max;
259 $self->{'curr_y_max'} = $abs_y_max - 1;
260  
261 # mark off the graph_border space
262 $self->{'curr_y_max'} -= 2 * $self->{'graph_border'};
263  
264 #draw the bottom legend again
265 if ( $self->{'legend'} =~ /^bottom$/i )
266 {
267 $self->_draw_bottom_legend;
268 }
269  
270 #draw the x label again
271 if ( $self->{'x_label'} )
272 {
273 $self->_draw_x_label;
274 }
275  
276 #get back to the start point for the ticks
277 $self->{'curr_x_min'} = $self->{'temp_x_min'};
278 $self->{'curr_y_min'} = $self->{'temp_y_min'};
279 $self->{'curr_x_max'} = $self->{'temp_x_max'};
280 $self->{'curr_y_max'} = $self->{'temp_y_max'};
281  
282 #draw the x ticks again
283 if ( $self->true( $self->{'xy_plot'} ) )
284 {
285 $self->_draw_x_number_ticks;
286 }
287 else
288 {
289 $self->_draw_x_ticks;
290 }
291  
292 #reset the actual values
293 $self->{'curr_x_min'} = $t_x_min;
294 $self->{'curr_x_max'} = $t_x_max;
295 $self->{'curr_y_min'} = $t_y_min;
296 $self->{'curr_y_max'} = $t_y_max;
297 }
298  
299 return;
300  
301 }
302  
303 ## @fn private int _prepare_brush($color)
304 # set the gdBrush object to trick GD into drawing fat lines
305 #
306 sub _prepare_brush
307 {
308 my $self = shift;
309 my $color = shift;
310 my $radius = $self->{'brush_size'} / 2;
311 my ( @rgb, $brush, $white, $newcolor );
312  
313 # get the rgb values for the desired color
314 @rgb = $self->{'gd_obj'}->rgb($color);
315  
316 # create the new image
317 $brush = GD::Image->new( $radius * 2, $radius * 2 );
318  
319 # get the colors, make the background transparent
320 $white = $brush->colorAllocate( 255, 255, 255 );
321 $newcolor = $brush->colorAllocate(@rgb);
322 $brush->transparent($white);
323  
324 # draw the circle
325 $brush->arc( $radius - 1, $radius - 1, $radius, $radius, 0, 360, $newcolor );
326  
327 # set the new image as the main object's brush
328 return $brush;
329 }
330  
331 ## be a good module and return 1
332 1;
« lib/Chart/HorizontalBars.pm ↑ Summary lib/Chart/LinesPoints.pm »