SVGraph/2D/columns.pm untested

filter: Statementstmt 0.0% 0 / 146 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 204 CC 22 SCAR 61.0
CC
22
Cov
0%
prepare
506.0
new
2.0
BEGIN
2.0
CRAP 445.5
1 #!/bin/perl
2 package SVGraph::2D::columns;
3 use SVGraph::2D;
4 use strict;
5  
6  
7 our @ISA=("SVGraph::2D");
8  
9  
10  
11 sub new
12 {
13 my $class=shift;
14 my %env=@_;
15 my $self={};
16  
17 %{$self->{ENV}}=%env;
18  
19 $self->{grid_y_scale_minimum}=$env{grid_y_scale_minimum};
20 $self->{grid_y_scale_maximum}=$env{grid_y_scale_maximum};
21 $self->{grid_y_main_spacing}=$env{grid_y_main_spacing};
22  
23 $self->{type}="columns";
24  
25 return bless $self,$class;
26 }
27  
28  
29  
30  
31  
32 sub prepare
33 {
34 my $self=shift;
35
36 $self->{SVG}=SVG->new(width=>$self->{ENV}{x},height=>$self->{ENV}{y});
37 $self->{SVG}->script()->CDATA("setTimeout('reload()', ".($self->{ENV}{reload}*1000).");") if $self->{ENV}{reload};
38
39 # drawing the BLOCK
40 $self->prepare_block();
41 # adding text
42 $self->prepare_title();
43 # preparing the columns
44 $self->prepare_columns();
45
46 #
47 # counting number of rows - $self->{grid_x_main_lines}
48 # counting max value - $self->{value_max}
49 #
50 $self->prepare_axis_calculate();
51 $self->prepare_axis_x_markArea(front=>0);
52 $self->prepare_axis_x();
53 $self->prepare_axis_x_mark(front=>0);
54
55 $self->prepare_axis_y_markArea(front=>0);
56 $self->prepare_axis_y();
57 $self->prepare_axis_y_mark(front=>0);
58
59 $self->prepare_legend();
60
61
62 # reversing items for drawing, that they will be in the same order as the legend
63 if ($self->{ENV}{type}=~/stacked/)
64 {
65 @{$self->{columnsA}}=reverse @{$self->{columnsA}};
66 }
67
68
69 foreach my $color(keys %SVGraph::colors::table)
70 {
71
72 my $g = $self->{SVG}->gradient
73 (
74 '-type' => "linear",
75 'id' => "gr_".$color."_0",
76 'x1'=>"0%",'y1'=>"0%",'x2'=>"100%",'y2'=>"0%",
77 );
78 $g->stop(offset=>"0%",style=>"stop-color:rgb(".$SVGraph::colors::table{$color}{'N0'}.");stop-opacity:1");
79 $g->stop(offset=>"50%",style=>"stop-color:rgb(".$SVGraph::colors::table{$color}{'L1'}.");stop-opacity:1");
80 $g->stop(offset=>"100%",style=>"stop-color:rgb(".$SVGraph::colors::table{$color}{'N0'}.");stop-opacity:1");
81 }
82
83 my $g = $self->{SVG}->gradient
84 (
85 '-type' => "linear",
86 'id' => "gr_highlight_0",
87 'x1'=>"0%",'y1'=>"0%",'x2'=>"100%",'y2'=>"0%",
88 );
89 $g->stop(offset=>"0%",style=>"stop-color:rgb(0,0,0);stop-opacity:1");
90 $g->stop(offset=>"50%",style=>"stop-color:rgb(100,100,100);stop-opacity:1");
91 $g->stop(offset=>"100%",style=>"stop-color:rgb(0,0,0);stop-opacity:1");
92
93
94 my $count=0;
95 foreach my $row(@{$self->{row}{label}})
96 {
97 $count++;
98
99
100 my $rows=0;
101 my $data_a;
102 #foreach my $column(keys %{$self->{columns}})
103 foreach my $column(@{$self->{columnsA}})
104 {
105
106 my $color=$self->{columns}->{$column}{ENV}{color};
107 my %colors=%{$SVGraph::colors::table{$color}};
108
109 my $data=$self->{columns}{$column}->{data}{$row};
110
111 $data=$data/($self->GetRowSum($row)/100) if $self->{ENV}{type}=~/percentage/;
112 $data_a+=$data;
113 my $data_o=$data; # data original
114 my $data_w=$data; # data for display;
115
116 my $del;
117 my $x;
118 my $height;
119 my $height0;
120 my $xm;
121
122 next unless $data_o;
123
124 my $width=($self->{block_width}/($self->{grid_x_main_lines}-1));
125
126 #print "$self->{grid_y_scale}\n";
127 if ($self->{ENV}{type}=~/stacked/)
128 {
129 $height=(($data_a-$self->{grid_y_scale_minimum})/($self->{grid_y_scale}/100))*
130 ($self->{block_height}/100);
131  
132 $height=int($height*100)/100;
133 $height=0 if $height < 0;
134 $height=$self->{block_height} if $height > $self->{block_height};
135  
136 my $min=$self->{grid_y_scale_minimum};$min=0 if $min<0;
137 $height0=((($data_a-$data)-$self->{grid_y_scale_minimum})/($self->{grid_y_scale}/100))*
138 ($self->{block_height}/100);
139
140 $del=($self->{block_width_scale}-($width*0.50));
141
142 $x=($count-1)*($self->{block_width}/($self->{grid_x_main_lines}-1));
143 $x+=($width*0.25);
144 }
145
146 elsif ($self->{ENV}{type}=~/overlap/)
147 {
148 $height=(($data-$self->{grid_y_scale_minimum})/($self->{grid_y_scale}/100))*
149 ($self->{block_height}/100);
150  
151 $height=int($height*100)/100;
152 $height=0 if $height < 0;
153 $height=$self->{block_height} if $height > $self->{block_height};
154  
155 my $min=$self->{grid_y_scale_minimum};$min=0 if $min<0;
156 $height0=(($min-$self->{grid_y_scale_minimum})/($self->{grid_y_scale}/100))*
157 ($self->{block_height}/100);
158  
159 $del=(($self->{block_width_scale}-4)/($self->GetNumColumns()+1));
160  
161 $x=($count-1)*($self->{block_width}/($self->{grid_x_main_lines}-1));$x+=2;
162  
163 $x+=($rows*$del);
164  
165 $xm=-$del;
166 }
167 else
168 {
169 $height=(($data-$self->{grid_y_scale_minimum})/($self->{grid_y_scale}/100))*
170 ($self->{block_height}/100);
171  
172 $height=int($height*100)/100;
173 $height=0 if $height < 0;
174 $height=$self->{block_height} if $height > $self->{block_height};
175  
176 my $min=$self->{grid_y_scale_minimum};$min=0 if $min<0;
177 $height0=(($min-$self->{grid_y_scale_minimum})/($self->{grid_y_scale}/100))*($self->{block_height}/100);
178 $del=($self->{block_width_scale}-6)/$self->GetNumColumns();
179  
180 $xm=1;
181  
182 $x=($count-1)*($self->{block_width}/($self->{grid_x_main_lines}-1));$x+=3+($xm/2);
183  
184 $x+=($rows*$del);
185 #$xm=2;
186 }
187  
188 if ($del-$xm<0.5)
189 {
190 die "cannot rows on size ".($del-$xm)."\n";
191 }
192  
193  
194 my $cl=$self->{SVG}->polyline(
195 points =>
196 ($self->{block_left}+$x).",".($self->{block_down}-$height0)." ".
197 ($self->{block_left}+$x+$del-$xm).",".($self->{block_down}-$height0)." ".
198 ($self->{block_left}+$x+$del-$xm).",".($self->{block_down}-$height)." ".
199 ($self->{block_left}+$x).",".($self->{block_down}-$height)." ".
200 ($self->{block_left}+$x).",".($self->{block_down}-$height0)." ",
201 'stroke-width' =>"0pt",
202 # 'stroke' =>"rgb(".$colors{B2}.")",
203 'stroke' =>"rgb(0,0,0)",
204 # 'stroke' =>"black",
205 # 'stroke-' =>"black",
206 'stroke-linecap' =>"butt",
207 # 'stroke-linejoin' =>"butt",
208 # 'fill' =>"rgb(".$colors{N0}.")",
209 # 'fill' =>"rgb(".$colors{N0}.")",
210 'fill' =>"url(#gr_".$color."_0)",
211 'fill-opacity' =>"1"
212 );
213
214 =head1
215 $cl->animate
216 (
217 'attributeName'=>"fill",
218 'begin'=>"mouseover",
219 'end'=>"mouseout",
220 # 'from'=>"1pt",
221 # 'to'=>"1pt",
222 'values'=>"url(#gr_highlight_0)",
223 # 'dur'=>"10s",
224 # 'repeatDur'=>"freeze"
225 'restart'=>"whenNotActive"
226 );
227 =cut
228
229 =head1
230 $cl->animate
231 (
232 'attributeName'=>"fill",
233 'begin'=>"mouseover",
234 'end'=>"mouseout",
235 'from'=>"rgb(".$colors{L0}.")",
236 'to'=>"rgb(".$colors{N0}.")",
237 # 'values'=>"30",
238 'dur'=>"10s",
239 # 'repeatDur'=>"freeze"
240 'restart'=>"whenNotActive"
241 );
242 $cl->animate
243 (
244 'attributeName'=>"stroke",
245 'begin'=>"mouseover",
246 'end'=>"mouseout",
247 'from'=>"rgb(".$colors{L0}.")",
248 'to'=>"rgb(".$colors{B2}.")",
249 # 'values'=>"30",
250 'dur'=>"10s",
251 # 'repeatDur'=>"freeze"
252 'restart'=>"whenNotActive"
253 );
254 =cut
255
256 $data_w="0" unless $data_w;
257 my $d_x=3;
258 my $d_y=-2;
259
260 if ($self->{ENV}{show_data_background} || $self->{columns}{$column}->{ENV}{show_data_background})
261 {
262 my $rx="2pt";
263 my $ry="2pt";
264 my $width=length($data_w)*5;
265 my $fill="rgb(255,255,255)";
266 my $fill_opacity="0.5";
267
268 if ($self->{columns}{$column}->{ENV}{show_data_summary})
269 {
270 $width=length($self->GetRowSum($row))*5;
271 $rx="0pt";
272 $ry="0pt";
273 $fill="rgb(255,255,255)";
274 $fill_opacity="1";
275 }
276
277 $self->{SVG}->rect(
278 'x' => (($self->{block_left}+$x)-1+$d_x),
279 'y' => (($self->{block_down}-$height-3)-8+$d_y),
280 'width' => $width+3,
281 'height' => 10,
282 'fill' =>$fill,
283 'fill-opacity' =>$fill_opacity,
284 'stroke' =>"rgb(125,125,125)",
285 'stroke-width' =>"1pt",
286 'stroke-linecap' =>"round",
287 'stroke-linejoin' =>"round",
288 'rx' => $rx,
289 'ry' => $ry,
290 );
291
292 }
293
294 if ($self->{ENV}{show_data} || $self->{columns}{$column}->{ENV}{show_data})
295 {
296 $self->{SVG}->text
297 (
298 'x' => ($self->{block_left}+$x+$d_x),
299 'y' => ($self->{block_down}-$height-3+$d_y),
300 style =>
301 {
302 'font-family' => 'Verdana',
303 'font-size' => '8pt',
304 'font-weight' => 400,
305 # 'fill' => "rgb(".$colors{B3}.")",
306 'fill' => "rgb(0,0,0)",
307 # 'stroke' => "rgb(0,0,0)",
308 # 'stroke-width' => "1pt",
309 'stroke-linecap' =>"round",
310 'stroke-linejoin' =>"round",
311 },
312 )->cdata($data_w.$self->{'ENV'}{'data_suffix'});
313 }
314
315 if ($self->{columns}{$column}->{ENV}{show_data_summary})
316 {
317 $self->{SVG}->text
318 (
319 'x' => ($self->{block_left}+$x+$d_x),
320 'y' => ($self->{block_down}-$height-3+$d_y),
321 style =>
322 {
323 'font-family' => 'Verdana',
324 'font-size' => 8,
325 'font-weight' => 400,
326 # 'fill' => "rgb(".$colors{B3}.")",
327 'fill' => "rgb(0,0,0)",
328 # 'stroke' => "rgb(0,0,0)",
329 # 'stroke-width' => "1pt",
330 'stroke-linecap' =>"round",
331 'stroke-linejoin' =>"round",
332 },
333 )->cdata($self->GetRowSum($row));
334 }
335  
336
337 # $width=int($width*100)/100;
338
339 $rows++;
340 }
341  
342 }
343
344
345
346
347
348 # drawing the lines
349 =head1
350 my @data_stacked;
351 foreach my $column(keys %{$self->{columns}})
352 {
353 my $color=$self->{columns}->{$column}{ENV}{color};
354 my %colors=%{$SVGraph::colors::table{$color}};
355
356 my $count;
357 my $points;
358
359 foreach my $row(@{$self->{row}{label}})
360 {
361 my $data=$self->{columns}{$column}->{data}{$row};
362 my $data_o=$data; # data original
363 my $data_w=$data; # data for display;
364 $count++;
365 $data=$data/($self->GetRowSum($row)/100) if $self->{ENV}{type}=~/percentage/;
366 $data_stacked[$count]+=$data;
367 #$data=$data_stacked[$count] if $self->{ENV}{type}=~/stacked/;
368 if ($self->{ENV}{type}=~/stacked/)
369 {
370 if ($self->{ENV}{type}=~/percentage/)
371 {
372 #$data_w="$data_o(".((int($data*100))/100)."%)";
373 $data_w=((int($data*100))/100)."%";
374 $data=100-$data_stacked[$count]+$data;
375 }
376 else
377 {
378 #$data=$self->GetRowSum($count-1)-$data_stacked[$count]+$data;
379 $data=$self->GetRowSum($row)-$data_stacked[$count]+$data;
380 }
381 }
382
383 my $height=(($data-$self->{grid_y_scale_minimum})/($self->{grid_y_scale}/100))*($self->{block_height}/100);
384 $height=int($height*100)/100;
385 $height=0 if $height < 0;
386 $height=$self->{block_height} if $height > $self->{block_height};
387
388 my $width=($count-1)*($self->{block_width}/($self->{grid_x_main_lines}-1));
389
390 $width+=($count*10);
391
392 print "mam $count\n";
393
394 $width=int($width*100)/100;
395
396
397
398 $self->{SVG}->polyline(
399 points =>
400 ($self->{block_left}+$width).",".($self->{block_down})." ".
401 ($self->{block_left}+$width+10).",".($self->{block_down})." ".
402 ($self->{block_left}+$width+10).",".($self->{block_down}-$height)." ".
403 ($self->{block_left}+$width).",".($self->{block_down}-$height)." ".
404 ($self->{block_left}+$width).",".($self->{block_down})." ",
405 'stroke-width' =>"1.5pt",
406 'stroke' =>"black",
407 # 'stroke-' =>"black",
408 'stroke-linecap' =>"round",
409 'stroke-linejoin' =>"round",
410 'fill' =>"rgb(".$colors{N0}.")",
411 'fill-opacity' =>"0.5",
412 # 'stroke-linecap'=>"square",
413 );
414
415 }
416  
417 }
418 =cut
419  
420  
421  
422  
423  
424 $self->prepare_axis_x_markArea(front=>1);
425 $self->prepare_axis_x_mark(front=>1);
426 $self->prepare_axis_y_markArea(front=>1);
427 $self->prepare_axis_y_mark(front=>1);
428  
429  
430 $self->prepare_axis();
431
432 $self->prepare_legend_label();
433
434 # output
435 $self->{SVG_out}=$self->{SVG}->xmlify
436 (
437 # -namespace => "svga",
438 # -pubid => "-//W3C//DTD SVG 1.0//EN",
439 -inline => 1
440 );
441
442 return $self->{SVG_out};
443 }
444  
445  
446  
447  
448  
449  
450