SVGraph/2D/map.pm untested

filter: Statementstmt 0.0% 0 / 222 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 324 CC 38 SCAR 50.7
CC
38
Cov
0%
prepare
306.0
prepare_legend
132.0
prepare_colors
56.0
CRAP 158.9
1 #!/bin/perl
2 package SVGraph::2D::map;
3  
4 use SVG;
5 use strict;
6 use SVGraph::world;
7 use SVGraph::colors;
8 use SVGraph::2D;
9  
10  
11 our @ISA=("SVGraph::world", "SVGraph::2D", "SVGraph::colors");
12  
13  
14 # display ranges used, when are not defined
15 my %ranges = (
16 world =>{
17 x_translate => 25,
18 y_translate => 0,
19 x =>810,
20 y =>450
21 },
22 europe =>{
23 x_translate => -345,
24 y_translate => -20,
25 x =>150,
26 y =>130
27 },
28 america =>{
29 x_translate => 0,
30 y_translate => 0,
31 x =>400,
32 y =>360
33 },
34 africa =>{
35 x_translate => -360,
36 y_translate => -115,
37 x =>170,
38 y =>180
39 },
40 asia =>{
41 x_translate => -440,
42 y_translate => -25,
43 x =>350,
44 y =>220
45 },
46 oceania =>{
47 x_translate => -630,
48 y_translate => -206,
49 x =>150,
50 y =>100
51 },
52 slovakia =>{
53 x_translate => -102,
54 y_translate => -259,
55 x =>450,
56 y =>250
57 }
58 );
59  
60  
61 my ($koef, $min, $max);
62  
63  
64 sub new
65 {
66 my $class=shift;
67 my %env=@_;
68 my $self={};
69
70  
71 unless($env{continent}){die "Missing map parameter.\n";}
72  
73 # australia and oceania is the same
74 if($env{continent} eq "australia"){$env{continent} = "oceania";}
75 $self->{continent} = $env{continent};
76  
77  
78 # do we have parameters?
79  
80 if($env{x})
81 {
82 $self->{ENV}{x} = $env{x};
83 }
84 else
85 {
86 $self->{ENV}{x} = $ranges{${env}{continent}}{x};
87 }
88  
89 if($env{y})
90 {
91 $self->{ENV}{y} = $env{y};
92 }
93 else
94 {
95 $self->{ENV}{y} = $ranges{${env}{continent}}{y};
96 }
97  
98 if(defined $env{show_legend})
99 {
100 $self->{ENV}{show_legend} = $env{show_legend};
101 }
102 else
103 {
104 $self->{ENV}{show_legend} = 1;
105 }
106  
107 $self->{ENV}{title} = $env{title};
108 $self->{ENV}{x_translate} = $ranges{${env}{continent}}{x_translate};
109 $self->{ENV}{y_translate} = $ranges{${env}{continent}}{y_translate};
110  
111 return bless $self,$class;
112 }
113  
114 sub prepare
115 {
116  
117 my $self = shift;
118
119  
120 #
121 # new svg graphics x*scale, y*scale
122 #
123  
124 $self->{SVG}=SVG->new(width=>$self->{ENV}{x},height=>$self->{ENV}{y});
125 $self->{SVG}->script()->CDATA("setTimeout('reload()', ".($self->{ENV}{reload}*1000).");") if $self->{ENV}{reload};
126  
127 # preparing colors of the countries
128 $self->prepare_colors();
129  
130 # drawing the BLOCK
131 $self->prepare_block(color => 205);
132 # adding text
133 $self->prepare_title();
134  
135 # preparing the columns
136 $self->prepare_columns();
137
138 $self->prepare_legend();
139  
140  
141 # scale
142 my $scale_x = (($self->{block_right} - $self->{block_left})/$ranges{$self->{continent}}{x})*0.95;
143 my $scale_y = (($self->{block_down} - $self->{block_up})/$ranges{$self->{continent}}{y})*1.09;
144  
145 #translations
146 my $trans_x = $self->{ENV}{x_translate}*$scale_x + $self->{block_left};
147 my $trans_y = $self->{ENV}{y_translate}*$scale_y + $self->{block_up};
148  
149 # group countries
150 my $continent_group = $self->{SVG}->group(id=>$self->{continent});
151  
152 my @columns = keys %{$self->{columns}};
153
154 # we have only one column
155 my $column = $columns[0];
156
157 my ($h, $s, $v);
158  
159 # hue of our color from table
160 if($self->{columns}{$column}->{ENV}{color})
161 {
162 $h = $SVGraph::colors::table_H{$self->{columns}{$column}->{ENV}{color}};
163 }
164 else # color is not defined
165 {
166 $h = $SVGraph::colors::table_H{'green'};
167 }
168
169 $v = 255;
170  
171 # drawing the whole world
172 if($self->{continent} eq 'world')
173 {
174 foreach (keys %SVGraph::world::countries)
175 {
176  
177 # saturation defines the value of a country
178 $s = $self->{columns}{$column}->{data}{$_}/10;
179  
180  
181 if($min < 0)
182 {
183 if($s <= 0)
184 {
185 $s = abs(abs($s) - abs(($min*$koef)/100)) * abs(($min*$koef)/100);
186 }
187 else
188 {
189 $s = abs(abs($s) + abs(($min*$koef)/100)) * abs(($min*$koef)/100)
190 }
191 }
192  
193 # all countries have the same color intensity
194 if($koef == 0){$s = 1}
195
196 # no white countries
197 if($s < 0.05){$s = 0.05}
198
199 my @rgb = SVGraph::colors::HSVtoRGB($h,$s,$v);
200
201
202 # color of this country
203 my $country_color = 'rgb('. $rgb[0] . ', ' . $rgb[1] . ', ' . $rgb[2] . ')';
204  
205
206 $self->{SVG}->path(
207 style=>'font-size:12;fill:'.$country_color.
208 ';stroke:#000000;stroke-width:0.25;stroke-miterlimit:1',
209 d=>''.$SVGraph::world::countries{$_}{d}.'',
210 transform=>'matrix('.$scale_x.',0,0,'.$scale_y .','.$trans_x.','.$trans_y.')'
211 );
212 }
213  
214 # end, everything is drawed
215 $self->{SVG_OUT} = $self->{SVG}->xmlify;
216
217 return $self->{SVG_OUT};
218 }
219  
220 #drawing slovakia
221 elsif($self->{continent} eq 'slovakia')
222 {
223 foreach (keys %SVGraph::world::slovakia)
224 {
225 # saturation defines the value of a country
226 $s = $self->{columns}{$column}->{data}{$_}/100;
227  
228  
229 if($min < 0)
230 {
231 if($s <= 0)
232 {
233 $s = abs(abs($s) - abs(($min*$koef)/100)) * abs(($min*$koef)/100);
234 }
235 else
236 {
237 $s = abs(abs($s) + abs(($min*$koef)/100)) * abs(($min*$koef)/100)
238 }
239 }
240  
241 # all countries have the same color intensity
242 if($koef == 0){$s = 1}
243
244 # no white countries
245 if($s < 0.05){$s = 0.05}
246
247 my @rgb = SVGraph::colors::HSVtoRGB($h,$s,$v);
248
249
250 # color of this country
251 my $country_color = 'rgb('. $rgb[0] . ', ' . $rgb[1] . ', ' . $rgb[2] . ')';
252
253 $self->{SVG}->path(
254 style=>'font-size:12;fill:'.$country_color.
255 ';stroke:#000000;stroke-width:0.25;stroke-miterlimit:1',
256 d=>''.$SVGraph::world::slovakia{$_}{d}.'',
257 transform=>'matrix('.$scale_x.',0,0,'.$scale_y .','.$trans_x.','.$trans_y.')'
258 );
259 }
260  
261 # end, everything is drawed
262 $self->{SVG_OUT} = $self->{SVG}->xmlify;
263
264 return $self->{SVG_OUT};
265 }
266
267
268 $self->{continent} =~ tr/A-Z/a-z/;
269
270 # drawing only one continent
271 foreach my $country (@{$SVGraph::world::continents{$self->{continent}}})
272 {
273  
274 # saturation defines the value of a country
275 my $s = $self->{columns}{$column}->{data}{$country}/100;
276  
277  
278 if($min < 0)
279 {
280 if($s <= 0)
281 {
282 $s = abs(abs($s) - abs(($min*$koef)/100)) * abs(($min*$koef)/100);
283 }
284 else
285 {
286 $s = abs(abs($s) + abs(($min*$koef)/100)) * abs(($min*$koef)/100)
287 }
288 }
289  
290 # all countries have the same color intensity
291 if($koef == 0){$s = 1}
292  
293 # no white countries
294 if($s < 0.05){$s = 0.05}
295  
296 my @rgb = SVGraph::colors::HSVtoRGB($h,$s,$v);
297  
298 # color of this country
299 my $country_color = 'rgb('. $rgb[0] . ', ' . $rgb[1] . ', ' . $rgb[2] . ')';
300  
301 $continent_group->path(
302 style=>'font-size:12;fill:'.$country_color.';stroke:#000000;stroke-width:0.25;stroke-miterlimit:1',
303 d=>''.$SVGraph::world::countries{$country}{d}.'',
304 transform=> 'matrix('.$scale_x.',0,0,'.$scale_y .','.$trans_x.','.$trans_y.')'
305 );
306 }
307
308 $self->{SVG_OUT} = $self->{SVG}->xmlify;
309
310 return $self->{SVG_OUT};
311 }
312  
313  
314 #
315 # preparing colors of the countries
316 #
317 sub prepare_colors
318 {
319 my $self = shift;
320 $min = 0;
321 $max = 0;
322  
323 my @columns = keys %{$self->{columns}};
324  
325 # we have only one column
326 my $column = $columns[0];
327  
328
329
330 # finding min and max from inserted values
331 foreach my $country (@{$SVGraph::world::continents{$self->{continent}}})
332 {
333 unless(defined $self->{columns}{$column}->{data}{$country}){next;}
334
335 if ($self->{columns}{$column}->{data}{$country} < $min)
336 {
337 $min = $self->{columns}{$column}->{data}{$country};
338 next;
339 }
340
341 if ($self->{columns}{$column}->{data}{$country} > $max)
342 {
343 $max = $self->{columns}{$column}->{data}{$country};
344 }
345 }
346  
347 # all countries are the same color
348 if(($max - $min) == 0)
349 {
350 foreach my $country (keys %{$self->{columns}{$column}->{data}})
351 {
352 $self->{columns}{$column}->{data}{$country} = 100;
353 }
354 return;
355 }
356
357 # or not
358 if($min > 0)
359 {
360 $koef = 100/(abs($max) - abs($min));
361 }
362 elsif($min < 0 && $max < 0)
363 {
364 $koef = 100/(abs($min) - abs($max));
365 }
366 else
367 {
368 $koef = 100/(abs($max) + abs($min));
369 }
370  
371 foreach my $country (keys %{$self->{columns}{$column}->{data}})
372 {
373 $self->{columns}{$column}->{data}{$country} *= $koef;
374 }
375 }
376  
377 #
378 # prepare of the legend
379 #
380 # overrided method
381  
382 sub prepare_legend
383 {
384 my $self=shift;
385
386 return undef unless $self->{ENV}{show_legend};
387  
388 my @columns = keys %{$self->{columns}};
389  
390 # we have only one column
391 my $column = $columns[0];
392  
393 # color
394 my ($h, $s, $v);
395  
396 # hue of our color from table
397 if($self->{columns}{$column}->{ENV}{color})
398 {
399 $h = $SVGraph::colors::table_H{$self->{columns}{$column}->{ENV}{color}};
400 }
401 else # color is not defined
402 {
403 $h = $SVGraph::colors::table_H{'green'};
404 }
405
406 $v = 255;
407  
408 my $width=(($self->{ENV}{x}-$self->{block_right})*0.07);
409 my $height=($self->{ENV}{y}*0.03);
410 my $x=($self->{block_right}+(($self->{ENV}{x}-$self->{block_right})*0.15));
411  
412 my (@rgb,$text);
413  
414 my @values;
415  
416 if($koef == 0)
417 {
418 foreach (keys %{$self->{columns}{$column}->{data}})
419 {
420 push (@values, $self->{columns}{$column}->{data}{$_});
421 }
422 }
423 else
424 {
425 foreach (keys %{$self->{columns}{$column}->{data}})
426 {
427 push (@values, $self->{columns}{$column}->{data}{$_}/$koef);
428 }
429 }
430
431 my @legend;
432  
433 my $step;
434  
435 if($min > 0)
436 {
437 $step = $max - $min;
438 }
439 elsif($min < 0 && $max < 0)
440 {
441 $step = abs($min) - abs($max);
442 }
443 else
444 {
445 $step = abs($min) + $max;
446 }
447  
448 if((scalar @values) < 6 && (scalar @values)>0)
449 {
450 $step = $step/(scalar @values);
451 $s = 1/(scalar @values+1);
452  
453 my $tmp;
454  
455 for (0 .. $#values)
456 {
457 $tmp = $max - $_ * $step;
458 push (@legend, $tmp);
459 }
460 @legend = reverse @legend;
461 }
462 elsif((scalar @values)<=0)
463 {
464 # there are no values of countries, the legend will be not displayed
465 return;
466 }
467 # only 5 things in legend
468 else
469 {
470 $s = 1/5;
471 $step = $step/5;
472  
473 my $tmp;
474  
475 for (0 .. 4)
476 {
477 $tmp = $max - $_ * $step;
478 push (@legend, $tmp);
479
480 }
481 @legend = reverse @legend;
482 }
483  
484  
485 my $previous_num;
486  
487 for (0 .. $#legend+1)
488 {
489 # adjustment of the range for the legend
490 my $divider = 2 - $_/($#legend+2);
491
492 # first loop
493 if ($_ == 0)
494 {
495 @rgb = SVGraph::colors::HSVtoRGB($h,0.05,$v);
496
497 $text = $min . " - ". int($legend[$_]/$divider);
498 }
499 # last loop
500 elsif($_ == $#legend+1)
501 {
502  
503 if($min > 0)
504 {
505 @rgb = SVGraph::colors::HSVtoRGB($h,1,$v);
506
507 $text = $previous_num . " - " . $max;
508 }
509 else
510 {
511 @rgb = SVGraph::colors::HSVtoRGB($h,1,$v);
512
513 $text = $max;
514 }
515 }
516 else
517 {
518 @rgb = SVGraph::colors::HSVtoRGB($h,$s*$_,$v);
519
520 $text = $previous_num . " - ". int($legend[$_]/$divider+1);
521 }
522  
523 $previous_num = int($legend[$_]/$divider);
524  
525 my $color = 'rgb('. $rgb[0] . ', ' . $rgb[1] . ', ' . $rgb[2] . ')';
526  
527 $self->{SVG}->rect(
528 x=>int($x),
529 y=>int($self->{block_up}+($_*($height*1.7)-$height)),
530 rx=>"2pt",
531 ry=>"2pt",
532 width=>$width,
533 height=>$height,
534 'stroke-width' =>"1pt" ,
535 'stroke' =>$color,
536 'fill' =>$color,
537 'stroke-linecap' =>"round",
538 'stroke-linejoin' =>"round",
539 );
540  
541 my $textsize=$height*0.6;
542
543  
544 $self->{SVG}->text
545 (
546 x=>int($x+($width*1.5)),
547 y=>int($self->{block_up}+($_*($height*1.7)-$height)+($textsize/2)+($height/4)),
548 style =>
549 {
550 'text-anchor' => 'start',
551 'font-family' => 'Verdana',
552 'font-size' => int($textsize).'px',
553 'font-weight' => "600",
554 'fill' => "black",
555 },
556 )->cdata($text);
557 }
558
559 return 1;
560 }
561  
562 1;