SVGraph/Core.pm untested

filter: Statementstmt 0.0% 0 / 297 Branchbran 0.0% 0 / 124 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 0.0% 0 / 31 Podpod 0.0% 0 / 24 totaltotal 0.0% 0 / 521 CC 63 SCAR 60.1
CC
63
Cov
0%
CalculateMinMax
1122.0
prepare_columns
42.0
GetMin
20.0
CRAP 406.2
1 package SVGraph::Core;
2  
3 1;
4  
5 package SVGraph;
6 use open ':utf8', ':std';
7 use encoding 'utf8';
8 use utf8;
9 use strict;
10  
11 =head1 NAME
12  
13 SVGraph::Core
14  
15 =head1 DESCRIPTION
16  
17 Library that generates cool SVG graphs
18  
19 =cut
20  
21 BEGIN
22 {
23 # allow to log events when function is available
24 eval{main::_log("<={LIB} ".__PACKAGE__);};
25 if ($@)
26 {
27 eval "package main; sub _log{}";
28 }
29 }
30  
31 =head1 DEPENDS
32  
33 libraries:
34  
35 SVG
36 SVGraph::columns
37 SVGraph::colors
38  
39 =cut
40  
41 use SVGraph::columns;
42 use SVGraph::colors;
43 use SVG;
44  
45  
46 sub GetAVG
47 {
48 my $self=shift;
49 my ($num,$all);
50
51 foreach (keys %{$self->{row}{labelH}})
52 {
53 if ($self->GetRowAVG($_))
54 {
55 $num+=$self->GetRowAVG($_);
56 $all++;
57 }
58 }
59  
60 return 0 unless $all;
61 return $num/$all;
62 }
63  
64  
65 sub GetMax
66 {
67 my $self=shift;
68 my $max;
69  
70 foreach (keys %{$self->{row}{labelH}})
71 {
72 $max=$self->GetRowMax($_) if $max<$self->GetRowMax($_);
73 }
74  
75 return $max;
76 }
77  
78  
79 sub GetMaxSum
80 {
81 my $self=shift;
82 my $max;
83  
84 foreach (keys %{$self->{row}{labelH}})
85 {
86 $max=$self->GetRowSum($_) if $max<$self->GetRowSum($_);
87 }
88  
89 return $max;
90 }
91  
92  
93 sub GetMin
94 {
95 my $self=shift;
96 my $min;
97  
98 foreach (keys %{$self->{row}{labelH}})
99 {
100 my $rowmin=$self->GetRowMin($_);
101 next unless defined $rowmin;
102 $min=$rowmin if ($min>$rowmin || !$min);
103 }
104  
105 return undef unless defined $min;
106 return $min;
107 }
108  
109  
110  
111  
112 #
113 # count of rows added trought addRowLabel, other rows are not counted
114 #
115 sub GetNumRows
116 {
117 my $self=shift;
118
119 return undef unless $self->{row}{label};
120
121 return @{$self->{row}{label}};
122 }
123  
124 #
125 # count of columns
126 #
127 sub GetNumColumns
128 {
129 my $self=shift;
130 my $null;
131
132 foreach (keys %{$self->{columns}})
133 {
134 $null++;
135 }
136
137 return $null;
138 }
139  
140 #
141 # number of added row by label
142 #
143 sub GetNumRow
144 {
145 my ($self,$var)=@_;
146
147 die "In this chart is none row! " unless $self->{row}{label};
148 my $null;
149
150 foreach (@{$self->{row}{label}}){return $null if $_ eq $var;$null++;}
151 main::_log("row with label='$var' not exist!",1);
152 return undef;
153 }
154  
155 #
156 # sum of the values of all columns in one row
157 #
158 sub GetRowSum
159 {
160 my $self=shift;
161 my $label=shift;
162 my $num;
163  
164 foreach (keys %{$self->{columns}}){$num+=$self->{columns}{$_}->{data}{$label};}
165
166 return $num;
167 }
168  
169 #
170 # average value from all columns in one row
171 #
172 sub GetRowAVG
173 {
174 my $self=shift;
175 my $label=shift;
176 my $num;
177 my $null;
178  
179 foreach (keys %{$self->{columns}})
180 {
181 if (exists $self->{columns}{$_}->{data}{$label})
182 {
183 $num+=$self->{columns}{$_}->{data}{$label};$null++;
184 }
185 }
186
187 return undef unless $null;
188 return $num/$null;
189 }
190  
191 #
192 # maximal value from all columns in one row
193 #
194 sub GetRowMax
195 {
196 my $self=shift;
197 my $label=shift;
198 my $max;
199  
200 foreach (keys %{$self->{columns}})
201 {
202 $max=$self->{columns}{$_}->{data}{$label} if $max<$self->{columns}{$_}->{data}{$label};
203 }
204
205 return $max;
206 }
207  
208 #
209 # minimal value from all columns in one row
210 #
211 sub GetRowMin
212 {
213 my $self=shift;
214 my $label=shift;
215 my $min;
216  
217 foreach (keys %{$self->{columns}})
218 {
219 next unless defined $self->{columns}{$_}->{data}{$label};
220 $min=$self->{columns}{$_}->{data}{$label} if ($min>$self->{columns}{$_}->{data}{$label} || !$min);
221 }
222  
223 return undef unless defined $min;
224 return $min;
225 }
226  
227  
228  
229 sub addRowLabel
230 {
231 my ($self,$null)=@_;
232  
233 return undef if $self->{row}{labelH}{$null};
234  
235 $self->{row}{labelH}{$null}=1;
236 push @{$self->{row}{label}},$null;
237  
238 return 1;
239 }
240  
241  
242  
243  
244 sub addRowMark
245 {
246 my ($self,$null,%env)=@_;
247  
248 %{$self->{row}{markH}{$null}}=%env;
249 print "mam $null $env{front}\n";
250  
251 return 1;
252 }
253  
254 sub addRowMarkArea
255 {
256 my ($self,$null,%env)=@_;
257
258 if (!$null)
259 {
260 $null="_start";
261 }
262
263 %{$self->{row}{markAH}{$null}}=%env;
264  
265 return 1;
266 }
267  
268 sub addValueMark
269 {
270 my ($self,$null,%env)=@_;
271
272 %{$self->{ValueMarkH}{$null}}=%env;
273
274 return 1;
275 }
276  
277 sub addValueMarkArea
278 {
279 my ($self,$null,%env)=@_;
280
281 if (!$null)
282 {
283 $null="_start";
284 }
285  
286 %{$self->{ValueMarkAH}{$null}}=%env;
287
288 return 1;
289 }
290  
291  
292  
293  
294  
295 #
296 # PREPARING THE COLORS OF THE COLUMNS
297 #
298 sub prepare_columns
299 {
300 my $self=shift;
301
302 # color control
303
304 foreach (@{$self->{columnsA}})
305 {
306 next unless $self->{columns}{$_}->{ENV}{color};
307
308 # if there is no color in the table, we delete it
309 # or if there is a column, which uses this color
310
311 if (!$SVGraph::colors::table{$self->{'columns'}{$_}->{'ENV'}{'color'}} ||
312 $self->{'colors_used'}{$self->{'columns'}{$_}->{'ENV'}{'color'}})
313 {
314 delete $self->{'columns'}{$_}->{'ENV'}{'color'};next;
315 }
316 $self->{'colors_used'}{$self->{'columns'}{$_}->{'ENV'}{'color'}}=1;
317 }
318
319
320 # color assigning to columns which have not assigned any color
321
322 foreach (@{$self->{columnsA}})
323 {
324 next if $self->{columns}{$_}->{ENV}{color};
325 reass:
326 foreach my $color(@SVGraph::colors::table_C)
327 {
328 next if $self->{colors_used}{$color};
329 $self->{colors_used}{$color}=1;
330 $self->{columns}{$_}->{ENV}{color}=$color;
331 last;
332 }
333 if (!$self->{columns}{$_}->{ENV}{color})
334 {
335 # color table reassing;
336 delete $self->{colors_used};
337 goto reass;
338 }
339 }
340
341 }
342  
343  
344  
345  
346  
347 # TITLE
348 sub prepare_title
349 {
350 my $self=shift;
351
352 my $font_size=calc_fontsize(
353 $self->{block_up},
354 s_from => 10,
355 s_to => 100,
356 o_from => 6,
357 o_to => 25
358 );
359
360 main::_log("title: $self->{block_up} = $font_size");
361
362 $self->{SVG}->text
363 (
364 x=>$self->{block_left},
365 y=>int(($self->{block_up}*0.33)+($font_size/2)),
366 style => {
367 'font-family' => 'Verdana',
368 'font-size' => $font_size.'px',
369 'font-weight'=> "900",
370 'fill' => $self->{'ENV'}{'title.color'} || "black",
371 # 'fill-opacity'=>(0.5+0.5*rand()),
372 # 'fill'=>"$colour_map[$index]",
373 # 'stroke'=>"#808080"
374 },
375 )->cdata($self->{ENV}{title}) if $self->{ENV}{title};
376  
377 return 1;
378 }
379  
380  
381  
382  
383  
384 sub prepare_legend
385 {
386 my $self=shift;
387
388 return undef unless $self->{ENV}{show_legend};
389
390 my $count;
391 foreach (keys %{$self->{columns}}){$count++};
392
393 =cut
394 my $block=$self->{SVG}->polyline(
395 points =>
396 ($self->{ENV}{x}).",".$self->{block_up}." ".
397 ($self->{block_right}+10).",".$self->{block_up}." ".
398 ($self->{block_right}+10).",".($self->{block_up}+(20*$count))." ".
399 ($self->{ENV}{x}).",".($self->{block_up}+(20*$count))." ",
400 'stroke-width' =>"0.5pt" ,
401 'stroke' =>"rgb(150,150,150)",
402 'fill' =>"rgb(240,240,240)",
403 'fill-opacity' =>"0.7",
404 'stroke-linecap' =>"round",
405 'stroke-linejoin' =>"round",
406 );
407 =cut
408  
409 my $count;
410 my $freq=20;
411 #foreach (keys %{$self->{columns}})
412 #@{$self->{columnsA}} = reverse @{$self->{columnsA}};
413
414
415 if ($self->{ENV}{show_legend_reverse})
416 {
417 @{$self->{columnsA}} = reverse @{$self->{columnsA}};
418 }
419
420 foreach (@{$self->{columnsA}})
421 {
422 my $color=$self->{columns}->{$_}{ENV}{color};
423 my %colors=%{$SVGraph::colors::table{$color}};
424 $count++;
425
426 my $width=(($self->{ENV}{x}-$self->{block_right})*0.07);
427 #my $height=int(($self->{ENV}{x}-$self->{block_right})*0.05);
428 my $height=($self->{ENV}{y}*0.03);
429 my $x=($self->{block_right}+(($self->{ENV}{x}-$self->{block_right})*0.15));
430
431 $self->{SVG}->rect(
432 x=>int($x),
433 y=>int($self->{block_up}+($count*($height*1.7)-$height)),
434 rx=>"2pt",
435 ry=>"2pt",
436 width=>$width,
437 height=>$height,
438 'stroke-width' =>"1pt" ,
439 'stroke' =>"rgb(0,0,0)",
440 'fill' =>"rgb(".$colors{N0}.")",
441 'stroke-linecap' =>"round",
442 'stroke-linejoin' =>"round",
443 );
444
445
446 my $font_size=calc_fontsize(
447 $self->{ENV}{y},
448 s_from => 100,
449 s_to => 500,
450 o_from => 6,
451 o_to => 13
452 );
453
454 $self->{SVG}->text
455 (
456 x=>int($x+($width*1.5)),
457 y=>int($self->{block_up}+($count*($height*1.7)-$height)+($font_size/2)+($height/4)),
458 style =>
459 {
460 'text-anchor' => 'start',
461 'font-family' => 'Verdana',
462 'font-size' => $font_size.'px',
463 'font-weight' => "600",
464 'fill' => "black",
465 },
466 )->cdata($_);
467
468 }
469
470
471 if ($self->{ENV}{show_legend_reverse})
472 {
473 @{$self->{columnsA}} = reverse @{$self->{columnsA}};
474 }
475
476 return 1;
477 }
478  
479  
480  
481  
482  
483  
484  
485 #
486 # adding columns to the graph
487 #
488 sub addColumn
489 {
490 my ($self,%env)=@_;
491  
492 return undef if $self->{columns}{$env{title}};
493  
494 $self->{columns}{$env{title}}=new SVGraph::columns(%env);
495 push @{$self->{columnsA}},$env{title};
496  
497 return ($self->{columns}{$env{title}});
498 }
499  
500  
501  
502  
503  
504  
505  
506  
507  
508  
509 #
510 # CALCULATION OF THE RANGE
511 #
512  
513  
514 sub CalculateMinMax
515 {
516 my($self,$minimal,$maximal,$div_min,$div_max)=@_;
517 my $log=0;
518 my $scale=$maximal-$minimal;
519 # my $div_min=9;
520 # my $div_max=15;
521 # my $minimal=0;
522 # my $maximal=849;
523 my $what; # min/max;
524
525 print "<=$minimal $maximal ($scale)\n" if $log;
526
527
528 #my $number=$maximal;$what="max";
529 #if ((-$minimal)>$maximal){$number=(-$minimal);$what="min"}
530
531 my $number=$scale;
532
533 print "<=$number\n" if $log;
534
535 #=head1
536
537 my $del=1;
538  
539 while ($number>100)
540 {
541 $number=$number/10;
542 $del=$del/10;
543 }
544 while ($number<10)
545 {
546 $number=$number*10;
547 $del=$del*10;
548 }
549
550 print "\t$number / $del (presun na 2 desatinne miesta)\n" if $log;
551
552 my $number2=$number;
553  
554 if ($number2!=int($number2)){$number2=int($number2+1);}
555
556 print "\t$number2 (zaokruhlenie nahor)\n" if $log;
557 print "\tprocessing...\n" if $log;
558  
559 my $cislo;
560 my %hash;
561
562
563 for my $plus(0..10)
564 {
565 $cislo=$number2+$plus;
566 print "\t$cislo\n" if $log;
567 for my $delitel($div_min..$div_max)
568 {
569 next if $cislo/$delitel != int($cislo/$delitel);
570 print "\t\tdelitelne $delitel\n" if $log;
571 $hash{$cislo}{$delitel}=$plus;
572 for my $test(0..$delitel)
573 {
574 my $out=($cislo*((1/$delitel)*($delitel-$test)));
575
576 print "\t\t\t".$out."\t" if $log;
577
578 while($out=~s|0$||){$hash{$cislo}{$delitel}--};
579 if($out=~/5$/){$hash{$cislo}{$delitel}-=0.5};
580
581 print $out."\n" if $log;
582 $hash{$cislo}{$delitel}+=(length($out)*1);
583 }
584 }
585 }
586  
587 print "\tcalculating\n" if $log;
588  
589 my ($del0,$cis0,$min0);
590  
591 foreach my $cislo(keys %hash)
592 {
593 foreach my $delitel(keys %{$hash{$cislo}})
594 {
595 print "\t\t$cislo / $delitel $hash{$cislo}{$delitel} \n " if $log;
596  
597 if ($min0>$hash{$cislo}{$delitel} || !$min0)
598 {
599 $del0=$delitel;
600 $cis0=$cislo;
601 $min0=$hash{$cislo}{$delitel};
602 }
603 }
604 }
605  
606 my ($minimal0,$maximal0);
607
608 print "\tnormalizing\n" if $log;
609 print "\t\not localize color at SVGraph.pm line t$cis0 $del0 (best selected)\n" if $log;
610
611 $scale=$cis0/$del;
612 print "\t\t".($scale)." $del0 (scale returned from 2 digits to normal)\n" if $log;
613
614 my $div=$scale/$del0;
615 print "\t\t".($div)." (divider)\n" if $log;
616
617 print "\t\t$minimal (minimal to normalization)\n" if $log;
618
619 if ((int($minimal/$div) > ($minimal/$div)) && ($minimal<0))
620 {
621 print "som tu\n" if $log;
622 $minimal0=(int($minimal/$div)-1)*$div;
623 }
624 else
625 {
626 # print "int($minimal/$div)*$div\n";
627 $minimal0=int($minimal/$div)*$div;
628 # $minimal0=$minimal;
629 }
630
631
632 print "\t\t$minimal0 (normalized minimal)\n" if $log;
633
634
635 print "\t\t$maximal (maximal to normalization)\n" if $log;
636 if ((int($maximal/$div) < ($maximal/$div)) && ($maximal>0))
637 {
638 $maximal0=(int($maximal/$div)+1)*$div;
639 }
640 else
641 {
642 $maximal0=int($maximal/$div)*$div;
643 }
644
645 print "\t\t$maximal0 (normalized maximal)\n" if $log;
646
647 # $minimal0=$minimal;
648 # $maximal0=$maximal;
649
650 my $del1=($maximal0-$minimal0)/$div;$del1.="";
651 #my $del1=int((0.75-0.54)/0.03);
652 #print "($maximal0-$minimal0)/$div\n";
653
654 print "\t\t$del1 (normalized lines)\n" if $log;
655
656 print "\t\t".($maximal0-$minimal)." (rozdiel)\n" if $log;
657
658
659 print "\toutput\n" if $log;
660 print "\t\t$maximal0 (maximal)\n" if $log;
661 print "\t\t$minimal0 (minimal)\n" if $log;
662 print "\t\t$del1 (divider)\n" if $log;
663
664 return ($minimal0,$maximal0,$del1);
665 }
666  
667  
668  
669 sub calc_fontsize
670 {
671 my $height=shift;
672 my %env=@_;
673
674 my $pi=3.1415689;
675
676 # rozsah velkosti grafu
677 my $s_from=$env{'s_from'} || 10;
678 my $s_to=$env{'s_to'} || 100;
679
680 # rozsah velkosti pisma
681 my $o_from=$env{'o_from'} || 6;
682 my $o_to=$env{'o_to'} || 25;
683
684 my $rel=($height-$s_from)/($s_to-$s_from);
685 my $quadrant=($pi/2)*$rel;
686 my $cc=int(cos(($pi/2)*3+$quadrant)*1000)/1000;
687 my $size=int($o_from+($cc*($o_to-$o_from)));
688 # print "$height=$size\n";
689
690 return $size;
691 }
692  
693  
694 sub save
695 {
696 my $self=shift;
697 my $filename=shift;
698
699 open (SVG,">".$filename);
700 print SVG $self->prepare();
701 close (SVG);
702 return 1;
703 }
704  
705  
706 1;