SVGraph/2D/lines.pm untested

filter: Statementstmt 0.0% 0 / 167 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 300 CC 33 SCAR 69.4
CC
33
Cov
0%
prepare
1122.0
new
2.0
BEGIN
2.0
CRAP 1028.7
1 #!/bin/perl
2 package SVGraph::2D::lines;
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}="lines";
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 # draw the block
40 $self->prepare_block();
41  
42 # add the text
43 $self->prepare_title();
44  
45 # prepare the columns
46 $self->prepare_columns();
47
48 #
49 # VYPOCITANIE POCTU ROWSOV - $self->{grid_x_main_lines}
50 # VYPOCITANIE MAX VALUE - $self->{value_max}
51 #
52 $self->prepare_axis_calculate();
53 $self->prepare_axis_x_markArea(front=>0);
54 $self->prepare_axis_x();
55 $self->prepare_axis_x_mark(front=>0);
56
57 $self->prepare_axis_y_markArea(front=>0);
58 $self->prepare_axis_y();
59 $self->prepare_axis_y_mark(front=>0);
60
61 $self->prepare_legend();
62
63
64
65
66
67 foreach my $color(keys %SVGraph::colors::table)
68 {
69 my $g = $self->{SVG}->gradient
70 (
71 '-type' => "linear",
72 'id' => "gr_".$color."_0",
73 'x1'=>"0%",'y1'=>"0%",'x2'=>"0%",'y2'=>"100%",
74 );
75 $g->stop(offset=>"0%",style=>"stop-color:rgb(".$SVGraph::colors::table{$color}{'B0'}.");stop-opacity:1");
76 #$g->stop(offset=>"50%",style=>"stop-color:rgb(".$SVGraph::colors::table{$color}{'L1'}.");stop-opacity:1");
77 $g->stop(offset=>"100%",style=>"stop-color:rgb(".$SVGraph::colors::table{$color}{'L1'}.");stop-opacity:1");
78 }
79
80
81 # drawing the lines
82
83 my @data_stacked;
84 # foreach my $column(keys %{$self->{columns}})
85 foreach my $column(@{$self->{columnsA}})
86 {
87 my $color=$self->{columns}->{$column}{ENV}{color};
88 my %colors=%{$SVGraph::colors::table{$color}};
89
90 # foreach (keys %colors)
91 # {
92 # print "C:$_ $colors{$_}[0]\n";
93 # }
94
95
96 my $count;
97 my $points;
98 my ($point_x,$point_y);
99 my (@points_x,@points_y,@points_data);
100 # my ();
101
102 #$points_data[$_]="0" unless $points_data[$_];
103
104 # foreach my $data($self->{columns}{$column}->GetValues())
105 foreach my $row(@{$self->{row}{label}})
106 {
107 my $data=$self->{columns}{$column}->{data}{$row};
108
109 #next unless defined $self->{columns}{$column}->{data}{$row};
110
111 my $data_o=$data; # data original
112 my $data_w=$data; # data for display;
113 $count++;
114
115 #$data=$data/($self->GetRowSum($count-1)/100) if $self->{ENV}{type}=~/percentage/;
116 $data=$data/($self->GetRowSum($row)/100) if $self->{ENV}{type}=~/percentage/;
117
118 $data_stacked[$count]+=$data;
119 #$data=$data_stacked[$count] if $self->{ENV}{type}=~/stacked/;
120 if ($self->{ENV}{type}=~/stacked/)
121 {
122 if ($self->{ENV}{type}=~/percentage/)
123 {
124 #$data_w="$data_o(".((int($data*100))/100)."%)";
125 $data_w=((int($data*100))/100)."%";
126 $data=100-$data_stacked[$count]+$data;
127 }
128 else
129 {
130 #$data=$self->GetRowSum($count-1)-$data_stacked[$count]+$data;
131 $data=$self->GetRowSum($row)-$data_stacked[$count]+$data;
132 }
133 }
134
135 # print "$column - $count - $data (".($self->GetRowSum($count-1)).") $self->{value_max_all}\n"
136 # if $self->{value_max_all}==$self->GetRowSum($count-1);
137
138 my $height=(($data-$self->{grid_y_scale_minimum})/($self->{grid_y_scale}/100))*($self->{block_height}/100);
139 $height=int($height*100)/100;
140 $height=0 if $height < 0;
141 $height=$self->{block_height} if $height > $self->{block_height};
142 my $width=($count-1)*($self->{block_width}/($self->{grid_x_main_lines}-1));
143 $width=int($width*100)/100;
144
145 $points.=($self->{block_left}+$width).",".($self->{block_down}-$height)." ";
146 push @points_x,($self->{block_left}+$width);
147 push @points_y,($self->{block_down}-$height);
148 push @points_data, $data_w;
149 }
150
151 if ($self->{ENV}{show_lines_smooth_range})
152 {
153 for my $sin_koef(3,4,6,10)
154 {
155 my $points_sin;
156 $points_sin.="M$points_x[0],$points_y[0] ";
157 for (1..@points_x-2)
158 {
159 $points_sin.="S".($points_x[$_]+($points_x[$_-1]-$points_x[$_+1])/$sin_koef).",".($points_y[$_]+($points_y[$_-1]-$points_y[$_+1])/$sin_koef)." $points_x[$_],$points_y[$_] ";
160 }
161 $points_sin.="S$points_x[-1],$points_y[-1] $points_x[-1],$points_y[-1]";
162 $self->{SVG}->path(
163 d=>$points_sin,
164 'stroke-width' =>"0.5pt" ,
165 'stroke' =>"rgb(".$colors{G0}.")",
166 style =>
167 {
168 'fill' =>"white",
169 'fill-opacity' =>"0",
170 }
171 );
172 }
173 }
174
175 if (
176 $self->{ENV}{show_lines_smooth} ||
177 $self->{ENV}{show_areas} ||
178 $self->{columns}{$column}->{ENV}{show_area} ||
179 $self->{columns}{$column}->{ENV}{show_line_smooth}
180 )
181 {
182
183 my $min=$self->{grid_y_scale_minimum};$min=0 if $min<0;
184 my $height=
185 (
186 ($min-$self->{grid_y_scale_minimum})/($self->{grid_y_scale}/100)
187 )*($self->{block_height}/100);
188
189 my $opacity="0";
190 my $points_sin;
191 my $sin_koef=6;
192
193 my $first=-1;
194 my $last;
195 for (0..@points_x-2)
196 {
197 next unless defined $points_data[$_];
198
199 $last=$_;
200 if ($first == -1)
201 {
202 $points_sin.="M$points_x[$_],$points_y[$_] ";
203 $first=$_;
204 next;
205 }
206
207 my $x=int($points_x[$_]+($points_x[$_-1]-$points_x[$_+1])/$sin_koef);
208 my $y=int($points_y[$_]+($points_y[$_-1]-$points_y[$_+1])/$sin_koef);
209
210 if ($y>$self->{block_down}){$y=$self->{block_down}}
211
212 # nebudem ohybat krivku pokial je buduca hodnota "undefined"
213 if (not defined $points_data[$_+1])
214 {
215 $x=$points_x[$_];
216 $y=$points_y[$_];
217 }
218
219 $points_sin.="S".
220 ($x).",".
221 ($y)." $points_x[$_],$points_y[$_] ";
222
223 }
224
225 $last=@points_x-1 if defined $points_data[@points_x-1];
226
227 $points_sin.="S$points_x[$last],$points_y[$last] $points_x[$last],$points_y[$last]";
228
229 if
230 (
231 (
232 ($self->{ENV}{show_areas})||
233 ($self->{columns}{$column}->{ENV}{show_area})
234 )&&
235 (!$self->{columns}{$column}->{ENV}{show_line})
236 )
237 {
238 my $points_sin=$points_sin;
239 $points_sin.=
240 " S".$points_x[$last].",".($self->{block_down}-$height)." ".$points_x[$last].",".($self->{block_down}-$height).
241 " S".$points_x[$first].",".($self->{block_down}-$height)." ".$points_x[$first].",".($self->{block_down}-$height);
242 # $points_sin.=
243 # " S".$self->{block_right}.",".($self->{block_down}-$height)." ".$self->{block_right}.",".($self->{block_down}-$height).
244 # " S".$self->{block_left}.",".($self->{block_down}-$height)." ".$self->{block_left}.",".($self->{block_down}-$height);
245 $opacity="1";
246 $opacity=$self->{ENV}{show_areas_opacity} if $self->{ENV}{show_areas_opacity};
247 $opacity=$self->{columns}{$column}->{ENV}{show_area_opacity} if $self->{columns}{$column}->{ENV}{show_area_opacity};
248 $self->{SVG}->path(
249 d=>$points_sin,
250 'stroke-width' =>"0pt" ,
251 'fill' =>"url(#gr_".$color."_0)",
252 'fill-opacity' =>$opacity,
253 );
254 }
255
256 if
257 (
258 ($self->{ENV}{show_lines_smooth})||
259 ($self->{columns}{$column}->{ENV}{show_line_smooth})
260 )
261 {
262 my %plus;
263
264 $plus{'stroke-dasharray'}=$self->{'ENV'}{'show_line_dasharray'}
265 if $self->{'ENV'}{'show_line_dasharray'};
266 $plus{'stroke-dasharray'}=$self->{'columns'}{$column}->{'ENV'}{'show_line_dasharray'}
267 if $self->{'columns'}{$column}->{'ENV'}{'show_line_dasharray'};
268
269 $self->{SVG}->path(
270 d=>$points_sin,
271 'stroke-width' =>"2pt" ,
272 'stroke' =>"rgb(".$colors{N0}.")",
273 # 'stroke-linecap' =>"round",
274 'stroke-linejoin' =>"round",
275 'fill-opacity' =>"0",
276 # 'stroke-dasharray' => "5,5",
277 #'stroke-dashoffset' => 5,
278 %plus
279 );
280 }
281
282
283 }
284
285
286
287 if ($self->{ENV}{show_lines})
288 {
289 my $min=$self->{grid_y_scale_minimum};$min=0 if $min<0;
290 my $height=(($min-$self->{grid_y_scale_minimum})/($self->{grid_y_scale}/100))*($self->{block_height}/100);
291 my @points_x=@points_x;
292 my @points_y=@points_y;
293 my $opacity="0";
294
295
296
297 if(
298 (
299 ($self->{ENV}{show_areas})||
300 ($self->{columns}{$column}->{ENV}{show_area})
301 )&&
302 (!$self->{columns}{$column}->{ENV}{show_line})
303 )
304 {
305 $opacity="1";
306 $opacity=$self->{ENV}{show_areas_opacity} if $self->{ENV}{show_areas_opacity};
307 $opacity=$self->{columns}{$column}->{ENV}{show_area_opacity} if $self->{ENV}{show_area_opacity};
308 push @points_x,$self->{block_right},$self->{block_left};
309 push @points_y,($self->{block_down}-$height),($self->{block_down}-$height);
310 }
311
312 my $points=$self->{SVG}->get_path(
313 x => [@points_x],
314 y => [@points_y],
315 -type => 'path',
316 );
317
318 $self->{SVG}->path(
319 %$points,
320 'stroke-width' =>"1pt" ,
321 'stroke' =>"rgb(".$colors{N0}.")",
322 'stroke-linecap' =>"round",
323 'stroke-linejoin' =>"round",
324 # style =>
325 # {
326 #'fill' =>"rgb(".$colors{B1}.")",
327 'fill' =>"url(#gr_".$color."_0)",
328 'fill-opacity' =>$opacity,
329 # }
330 );
331 }
332
333 for (0..@points_x-1)
334 {
335
336
337 if ($self->{ENV}{show_points} || $self->{columns}{$column}->{ENV}{show_points})
338 {
339
340 my $circle=$self->{SVG}->circle
341 (
342 cx => $points_x[$_],
343 cy => $points_y[$_],
344 r => 2,
345 'fill' => "white",
346 'stroke' => "rgb(".$colors{N0}.")",
347 'stroke-width' => "1pt",
348 ) if $points_y[$_] ne $self->{block_down};
349
350 =head1
351 if ($self->{ENV}{show_points_animate} || $self->{columns}{$column}->{ENV}{show_points_animate})
352 {
353
354 my $circle2=$self->{SVG}->circle
355 (
356 cx => $points_x[$_],
357 cy => $points_y[$_],
358 r => 10,
359 'fill' => "white",
360 'stroke' => "rgb(".$colors{N0}.")",
361 'stroke-width' => "1pt",
362 'fill-opacity' => "0",
363 'stroke-opacity' => "0",
364 );
365
366 $circle2->animate
367 (
368 'attributeName'=>"r",
369 'begin'=>"mouseover",
370 'end'=>"mouseout",
371 'from'=>10,
372 'to'=>30,
373 # 'values'=>"30",
374 'dur'=>"2s",
375 # 'repeatDur'=>"freeze"
376 'restart'=>"whenNotActive"
377 );
378 $circle2->animate
379 (
380 'attributeName'=>"fill-opacity",
381 'begin'=>"mouseover",
382 'end'=>"mouseout",
383 'from'=>0,
384 'to'=>0.5,
385 # 'values'=>"30",
386 'dur'=>"2s",
387 # 'repeatDur'=>"freeze"
388 'restart'=>"whenNotActive"
389 );
390 $circle2->animate
391 (
392 'attributeName'=>"stroke-opacity",
393 'begin'=>"mouseover",
394 'end'=>"mouseout",
395 'from'=>0,
396 'to'=>0.9,
397 # 'values'=>"30",
398 'dur'=>"2s",
399 # 'repeatDur'=>"freeze"
400 'restart'=>"whenNotActive"
401 );
402
403 }
404 =cut
405
406 }
407
408 $self->{SVG}->circle(
409 cx=>(($points_x[$_]+$points_x[$_-1])/2),
410 cy=>(($points_y[$_]+$points_y[$_-1])/2),r=>2,
411 'fill' => "rgb(".$colors{B1}.")",
412 'stroke' => "rgb(".$colors{L0}.")",
413 'stroke-width' => "1pt",
414 ) if $self->{ENV}{show_points_middle};
415
416
417 if ($points_data[$_])
418 {
419
420 if (
421 $self->{ENV}{show_data_background} ||
422 $self->{columns}{$column}->{ENV}{show_data_background}
423 )
424 {
425 my $width=length($points_data[$_])*5;
426
427 my $rect=$self->{SVG}->rect(
428 'x' => ($points_x[$_]+2),
429 'y' => ($points_y[$_]-11),
430 'width' => $width+3,
431 'height' => 10,
432 'fill' =>"rgb(255,255,255)",
433 'fill-opacity' =>"1",
434 'stroke' =>"rgb(125,125,125)",
435 'stroke-width' =>"1pt",
436 'stroke-linecap' =>"round",
437 'stroke-linejoin' =>"round",
438 'rx' => "2pt",
439 'ry' => "2pt",
440 );
441 =head1
442 if ($self->{ENV}{show_data} || $self->{columns}{$column}->{ENV}{show_data})
443 {
444 $rect->text
445 (
446 x => 10,
447 y => 10,
448 style =>
449 {
450 'font-family' => 'Verdana',
451 'font-size' => 8,
452 'font-weight' => 400,
453 # 'fill' => "rgb(".$colors{B3}.")",
454 'fill' => "rgb(0,0,0)",
455 # 'stroke' => "rgb(0,0,0)",
456 # 'stroke-width' => "1pt",
457 'stroke-linecap' =>"round",
458 'stroke-linejoin' =>"round",
459 },
460 )->cdata($points_data[$_]);
461 }
462 =cut
463 }
464 if ($self->{ENV}{show_data} || $self->{columns}{$column}->{ENV}{show_data})
465 {
466 $self->{SVG}->text
467 (
468 x => $points_x[$_]+3,
469 y => $points_y[$_]-3,
470 style =>
471 {
472 'font-family' => 'Verdana',
473 'font-size' => '8px',
474 'font-weight' => 400,
475 # 'fill' => "rgb(".$colors{B3}.")",
476 'fill' => "rgb(0,0,0)",
477 # 'stroke' => "rgb(0,0,0)",
478 # 'stroke-width' => "1pt",
479 'stroke-linecap' =>"round",
480 'stroke-linejoin' =>"round",
481 },
482 )->cdata($points_data[$_]);
483 }
484
485 }
486
487
488 #=head1
489 # animated
490 if
491 (
492 ($self->{'ENV'}{'show_data_SMIL'} || $self->{columns}{$column}->{'ENV'}{'show_data_SMIL'})
493 &&($points_data[$_])
494 )
495 {
496
497 my $width=length($points_data[$_])*5;
498
499 my $box=$self->{SVG}->polyline(
500 'points' =>
501 ($points_x[$_]-3).",".($points_y[$_]-5)." ".
502 ($points_x[$_]+$width).",".($points_y[$_]-5)." ".
503 ($points_x[$_]+$width).",".($points_y[$_]+5)." ".
504 ($points_x[$_]-3).",".($points_y[$_]+5)." ".
505 ($points_x[$_]-3).",".($points_y[$_]-5)." ",
506 'fill' =>"rgb(255,255,255)",
507 'fill-opacity' =>"0.7",
508 'stroke' =>"rgb(0,0,0)",
509 'stroke-width' =>"1",
510 'stroke-opacity' => "0",
511 'fill-opacity' => "0",
512 'stroke-linecap' =>"round",
513 'stroke-linejoin' =>"round",
514 );
515
516 $box->animate
517 (
518 'attributeName'=>"fill-opacity",
519 'begin'=>"mouseover",
520 'end'=>"mouseout",
521 # 'from'=>"0",
522 'values'=>"1",
523 # 'dur'=>"0.5s",
524 # 'repeatDur'=>"freeze"
525 'restart'=>"whenNotActive"
526 );
527 }
528 #=cut
529
530 }
531  
532 =head1
533 $self->{SVG}->polyline(
534 points => $points
535 # .$self->{block_right}.",".$self->{block_down}." "
536 # .$self->{block_left}.",".$self->{block_down}
537 ,
538 'stroke-width' =>"1pt" ,
539 'stroke' =>"black",
540 style =>
541 {
542 'fill' =>"white",
543 ' fill-opacity' =>"0",
544 }
545 );
546 =cut
547  
548 }
549  
550 $self->prepare_axis_x_markArea(front=>1);
551 $self->prepare_axis_x_mark(front=>1);
552 $self->prepare_axis_y_markArea(front=>1);
553 $self->prepare_axis_y_mark(front=>1);
554  
555  
556 $self->prepare_axis();
557  
558 $self->prepare_legend_label();
559  
560 # output
561 $self->{SVG_out}=$self->{SVG}->xmlify
562 (
563 # -namespace => "svg",
564 # -pubid => "-//W3C//DTD SVG 1.0//EN",
565 -inline => 1
566 );
567  
568 return $self->{SVG_out};
569 }
570  
571  
572  
573  
574  
575  
576