Graph/BarLine.pm

filter: Statementstmt 80.7% 21 / 26 Branchbran 50.0% 4 / 8 Conditioncond 66.6% 4 / 6 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 85.7% 6 / 7 Podpod 0.0% 0 / 1 totaltotal 70.5% 36 / 51 CC 7 SCAR 15.4
CC
7
Cov
72%
get_template
12.0
_init
4.1
_set_defaults
2.0
CRAP 4.6
« Graph/BarHorizontal.pm ↑ Summary Graph/Line.pm »
1 package SVG::TT::Graph::BarLine;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use SVG::TT::Graph;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use base qw(SVG::TT::Graph);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 my $template;
10  
11 =head1 NAME
12  
13 SVG::TT::Graph::Bar - Create presentation quality SVG bar graphs easily
14  
15 =head1 SYNOPSIS
16  
17 use SVG::TT::Graph::Bar;
18  
19 my @fields = qw(Jan Feb Mar);
20 my @data_sales_02 = qw(12 45 21);
21  
22 my $graph = SVG::TT::Graph::Bar->new({
23 'height' => '500',
24 'width' => '300',
25 'fields' => \@fields,
26 });
27
28 $graph->add_data({
29 'data' => \@data_sales_02,
30 'title' => 'Sales 2002',
31 });
32
33 print "Content-type: image/svg+xml\r\n\r\n";
34 print $graph->burn();
35  
36 =head1 DESCRIPTION
37  
38 This object aims to allow you to easily create high quality
39 SVG bar graphs. You can either use the default style sheet
40 or supply your own. Either way there are many options which can
41 be configured to give you control over how the graph is
42 generated - with or without a key, data elements at each point,
43 title, subtitle etc.
44  
45 =head1 METHODS
46  
47 =head2 new()
48  
49 use SVG::TT::Graph::Bar;
50
51 # Field names along the X axis
52 my @fields = qw(Jan Feb Mar);
53
54 my $graph = SVG::TT::Graph::Bar->new({
55 # Required
56 'fields' => \@fields,
57
58 # Optional - defaults shown
59 'height' => '500',
60 'width' => '300',
61 'show_data_values' => 1,
62  
63 'scale_divisions' => '',
64 'min_scale_value' => '0',
65 'bar_gap' => 1,
66  
67 'show_x_labels' => 1,
68 'stagger_x_labels' => 0,
69 'rotate_x_labels' => 0,
70 'show_y_labels' => 1,
71 'scale_integers' => 0,
72 'show_secondary_y_labels' => 1,
73  
74 'show_x_title' => 0,
75 'x_title' => 'X Field names',
76  
77 'show_y_title' => 0,
78 'show_secondary_y_title' => 0,
79 'y_title_text_direction' => 'bt',
80 'secondary_y_title_text_direction' => 'bt',
81 'y_title' => 'Y Scale',
82 'secondary_y_title' => 'Y Scale 2',
83  
84 'show_graph_title' => 0,
85 'graph_title' => 'Graph Title',
86 'show_graph_subtitle' => 0,
87 'graph_subtitle' => 'Graph Sub Title',
88  
89 'key' => 0,
90 'key_position' => 'right',
91  
92 # Optional - defaults to using internal stylesheet
93 'style_sheet' => '/includes/graph.css',
94 });
95  
96 The constructor takes a hash reference, fields (the names for each
97 field on the X axis) MUST be set, all other values are defaulted to those
98 shown above - with the exception of style_sheet which defaults
99 to using the internal style sheet.
100  
101 =head2 add_data()
102  
103 my @data_sales_02 = qw(12 45 21);
104  
105 $graph->add_data({
106 'data' => \@data_sales_02,
107 'title' => 'Sales 2002',
108 });
109  
110 This method allows you to add data to the graph object.
111 It can be called several times to add more data sets in,
112 but the likely hood is you should be using SVG::TT::Graph::Line
113 as it won't look great!
114  
115 =head2 clear_data()
116  
117 my $graph->clear_data();
118  
119 This method removes all data from the object so that you can
120 reuse it to create a new graph but with the same config options.
121  
122 =head2 burn()
123  
124 print $graph->burn();
125  
126 This method processes the template with the data and
127 config which has been set and returns the resulting SVG.
128  
129 This method will croak unless at least one data set has
130 been added to the graph object.
131  
132 =head2 config methods
133  
134 my $value = $graph->method();
135 my $confirmed_new_value = $graph->method($value);
136
137 The following is a list of the methods which are available
138 to change the config of the graph object after it has been
139 created.
140  
141 =over 4
142  
143 =item height()
144  
145 Set the height of the graph box, this is the total height
146 of the SVG box created - not the graph it self which auto
147 scales to fix the space.
148  
149 =item width()
150  
151 Set the width of the graph box, this is the total width
152 of the SVG box created - not the graph it self which auto
153 scales to fix the space.
154  
155 =item style_sheet()
156  
157 Set the path to an external stylesheet, set to '' if
158 you want to revert back to using the defaut internal version.
159  
160 To create an external stylesheet create a graph using the
161 default internal version and copy the stylesheet section to
162 an external file and edit from there.
163  
164 =item show_data_values()
165  
166 Show the value of each element of data on the graph
167  
168 =item bar_gap()
169  
170 Whether to have a gap between the bars or not, default
171 is '1', set to '0' if you don't want gaps.
172  
173 =item show_x_labels()
174  
175 Whether to show labels on the X axis or not, defaults
176 to 1, set to '0' if you want to turn them off.
177  
178 =item stagger_x_labels()
179  
180 This puts the labels at alternative levels so if they
181 are long field names they will not overlap so easily.
182 Default it '0', to turn on set to '1'.
183  
184 =item rotate_x_labels()
185  
186 This turns the X axis labels by 90 degrees.
187 Default it '0', to turn on set to '1'.
188  
189 =item show_y_labels()
190  
191 Whether to show labels on the Y axis or not, defaults
192 to 1, set to '0' if you want to turn them off.
193  
194 =item scale_integers()
195  
196 Ensures only whole numbers are used as the scale divisions.
197 Default it '0', to turn on set to '1'. This has no effect if
198 scale divisions are less than 1.
199  
200 =item show_secondary_y_labels()
201  
202 Whether to show labels on the right hand side Y axis or not, defaults
203 to 1, set to '0' if you want to turn them off.
204  
205 =item min_scale_value()
206  
207 The point at which the Y axis starts, defaults to '0',
208 if set to '' it will default to the minimum data value.
209  
210 =item scale_divisions()
211  
212 This defines the gap between markers on the Y axis,
213 default is a 10th of the max_value, e.g. you will have
214 10 markers on the Y axis. NOTE: do not set this too
215 low - you are limited to 999 markers, after that the
216 graph won't generate.
217  
218 =item show_x_title()
219  
220 Whether to show the title under the X axis labels,
221 default is 0, set to '1' to show.
222  
223 =item x_title()
224  
225 What the title under X axis should be, e.g. 'Months'.
226  
227 =item show_y_title()
228  
229 Whether to show the title on the left hand side Y axis,
230 default is 0, set to '1' to show.
231  
232 =item show_secondary_y_title()
233  
234 Whether to show the title on the right hand side Y axis,
235 default is 0, set to '1' to show.
236  
237 =item y_title_text_direction()
238  
239 Aligns writing mode for Y axis label. Defaults to 'bt' (Bottom to Top).
240 Change to 'tb' (Top to Bottom) to reverse.
241  
242 =item secondary_y_title_text_direction()
243  
244 Aligns writing mode for right hand Y axis label. Defaults to 'bt' (Bottom to Top).
245 Change to 'tb' (Top to Bottom) to reverse.
246  
247 =item y_title()
248  
249 What the title under Y axis should be, e.g. 'Sales in thousands'.
250  
251 =item secondary_y_title()
252  
253 What the title on the right hand side Y axis should be, e.g. 'Number of deals'.
254  
255 =item show_graph_title()
256  
257 Whether to show a title on the graph, defaults
258 to 0, set to '1' to show.
259  
260 =item graph_title()
261  
262 What the title on the graph should be.
263  
264 =item show_graph_subtitle()
265  
266 Whether to show a subtitle on the graph, defaults
267 to 0, set to '1' to show.
268  
269 =item graph_subtitle()
270  
271 What the subtitle on the graph should be.
272  
273 =item key()
274  
275 Whether to show a key, defaults to 0, set to
276 '1' if you want to show it.
277  
278 =item key_position()
279  
280 Where the key should be positioned, defaults to
281 'right', set to 'bottom' if you want to move it.
282  
283 =back
284  
285 =head1 NOTES
286  
287 The default stylesheet handles upto 12 data sets, if you
288 use more you must create your own stylesheet and add the
289 additional settings for the extra data sets. You will know
290 if you go over 12 data sets as they will have no style and
291 be in black.
292  
293 =head1 EXAMPLES
294  
295 For examples look at the project home page
296 http://leo.cuckoo.org/projects/SVG-TT-Graph/
297  
298 =head1 EXPORT
299  
300 None by default.
301  
302 =head1 ACKNOWLEDGEMENTS
303  
304 Stephen Morgan for creating the TT template and SVG.
305  
306 =head1 AUTHOR
307  
308 Leo Lapworth (LLAP@cuckoo.org)
309  
310 =head1 SEE ALSO
311  
312 L<SVG::TT::Graph>,
313 L<SVG::TT::Graph::Line>,
314 L<SVG::TT::Graph::Bar>,
315 L<SVG::TT::Graph::BarHorizontal>,
316 L<SVG::TT::Graph::Pie>,
317 L<SVG::TT::Graph::TimeSeries>
318  
319 =cut
320  
321 sub get_template {
322 0 ▶ my $self = shift;
Subroutine get_template: not called CC=3 SCAR=24.8
Pod undocumented
323 # read in template
324 0 ▶ return $template if $template;
Branch: 1 branch
BranchTrueFalseTotal
if $template 0 0 0
325 0 ▶ while(<DATA>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline DATA)) 0 0 0
326 0 $template .= $_ . "\r\n";
327 }
328 0 return $template;
329 }
330  
331 sub _init {
332 2 ▶ my $self = shift;
Subroutine _init: called CC=4 SCAR=14.1
333 croak "fields was not supplied or is empty"
334 unless defined $self->{'config'}->{fields}
335 && ref($self->{'config'}->{fields}) eq 'ARRAY'
336 2 ▶ && scalar(@{$self->{'config'}->{fields}}) > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"config"}{"fields"} and ref $self->{"config"}{"fields"} eq "ARRAY" and scalar @{$self->{"config"}{"fields"};} > 0 1 1 2
Condition66%4/6
defined $self->{"config"}{"fields"} and ref $self->{"config"}{"fields"} eq "ARRAY" and scalar @{$self->{"config"}{"fields"};} > 0
!ll&&!rl&&r
101
defined $self->{"config"}{"fields"} and ref $self->{"config"}{"fields"} eq "ARRAY"
!ll&&!rl&&r
101
MC/DC33%1/3
defined $self->{"config"}{"fields"} and ref $self->{"config"}{"fields"} eq "ARRAY" and scalar @{$self->{"config"}{"fields"};} > 0
defined $self->{"config"}{"fields"} ref $self->{"config"}{"fields"} eq "ARRAY" scalar @{$self->{"config"}{"fields"};} > 0
Truth table50%2/4
A and B and C
A= defined $self->{"config"}{"fields"}B= ref $self->{"config"}{"fields"} eq "ARRAY"C= scalar @{$self->{"config"}{"fields"};} > 0
ABCresult
0XX0
10X0
1100
1111
337 }
338  
339 sub _set_defaults {
340 2 ▶ my $self = shift;
Subroutine _set_defaults: called CC=2 SCAR=6.9
341
342 2 my %default = (
343 'width' => '500',
344 'height' => '300',
345 'style_sheet' => '',
346 'show_data_values' => 1,
347
348 'min_scale_value' => '0',
349 'scale_divisions' => '',
350 'bar_gap' => 1,
351  
352 'show_x_labels' => 1,
353 'rotate_x_labels' => 0,
354 'stagger_x_labels' => 0,
355 'show_y_labels' => 1,
356 'scale_integers' => 0,
357 'show_secondary_y_labels' => 1,
358  
359 'show_x_title' => 0,
360 'x_title' => 'X Field names',
361
362 'show_y_title' => 0,
363 'show_secondary_y_title' => 0,
364 'y_title_text_direction' => 'bt',
365 'secondary_y_title_text_direction' => 'bt',
366 'y_title' => 'Y Scale',
367 'secondary_y_title' => 'Y Scale 2',
368
369 'show_graph_title' => 0,
370 'graph_title' => 'Graph Title',
371 'show_graph_subtitle' => 0,
372 'graph_subtitle' => 'Graph Sub Title',
373 'key' => 0,
374 'key_position' => 'right', # bottom or right
375 );
376
377 2 ▶ while( my ($key,$value) = each %default ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %default) 54 2 56
378 54 $self->{config}->{$key} = $value;
379 }
380 }
381  
382 2 1;
383 __DATA__
384 <?xml version="1.0"?>
385 <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
386 "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
387 [% stylesheet = 'included' %]
388  
389 [% IF config.style_sheet && config.style_sheet != '' %]
390 <?xml-stylesheet href="[% config.style_sheet %]" type="text/css"?>
391 [% ELSE %]
392 [% stylesheet = 'excluded' %]
393 [% END %]
394  
395 <svg width="[% config.width %]" height="[% config.height %]" viewBox="0 0 [% config.width %] [% config.height %]" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
396 <!-- \\\\\\\\\\\\\\\\\\\\\\\\\\\\ -->
397 <!-- Created with SVG::TT::Graph -->
398 <!-- Stephen Morgan / Leo Lapworth -->
399 <!-- //////////////////////////// -->
400  
401 [% IF stylesheet == 'excluded' %]
402 <!-- include default stylesheet if none specified -->
403 <defs>
404 <style type="text/css">
405 <![CDATA[
406 /* Copy from here for external style sheet */
407 .svgBackground{
408 fill:#ffffff;
409 }
410 .graphBackground{
411 fill:#f0f0f0;
412 }
413  
414 /* graphs titles */
415 .mainTitle{
416 text-anchor: middle;
417 fill: #000000;
418 font-size: 14px;
419 font-family: "Arial", sans-serif;
420 font-weight: normal;
421 }
422 .subTitle{
423 text-anchor: middle;
424 fill: #999999;
425 font-size: 12px;
426 font-family: "Arial", sans-serif;
427 font-weight: normal;
428 }
429  
430 .axis{
431 stroke: #000000;
432 stroke-width: 1px;
433 }
434  
435 .guideLines{
436 stroke: #666666;
437 stroke-width: 1px;
438 stroke-dasharray: 5 5;
439 }
440  
441 .xAxisLabels{
442 text-anchor: middle;
443 fill: #000000;
444 font-size: 12px;
445 font-family: "Arial", sans-serif;
446 font-weight: normal;
447 }
448  
449 .yAxisLabels{
450 text-anchor: end;
451 fill: #000000;
452 font-size: 12px;
453 font-family: "Arial", sans-serif;
454 font-weight: normal;
455 }
456  
457 .xAxisTitle{
458 text-anchor: middle;
459 fill: #ff0000;
460 font-size: 14px;
461 font-family: "Arial", sans-serif;
462 font-weight: normal;
463 }
464  
465 .yAxisTitle{
466 fill: #ff0000;
467 text-anchor: middle;
468 font-size: 14px;
469 font-family: "Arial", sans-serif;
470 font-weight: normal;
471 }
472  
473 .dataPointLabel{
474 fill: #000000;
475 text-anchor:middle;
476 font-size: 10px;
477 font-family: "Arial", sans-serif;
478 font-weight: normal;
479 }
480  
481 .staggerGuideLine{
482 fill: none;
483 stroke: #000000;
484 stroke-width: 0.5px;
485 }
486 /* default line styles */
487 .line1{
488 fill: none;
489 stroke: #ff0000;
490 stroke-width: 1px;
491 }
492 .line2{
493 fill: none;
494 stroke: #0000ff;
495 stroke-width: 1px;
496 }
497 .line3{
498 fill: none;
499 stroke: #00ff00;
500 stroke-width: 1px;
501 }
502 .line4{
503 fill: none;
504 stroke: #ffcc00;
505 stroke-width: 1px;
506 }
507 .line5{
508 fill: none;
509 stroke: #00ccff;
510 stroke-width: 1px;
511 }
512 .line6{
513 fill: none;
514 stroke: #ff00ff;
515 stroke-width: 1px;
516 }
517 .line7{
518 fill: none;
519 stroke: #00ffff;
520 stroke-width: 1px;
521 }
522 .line8{
523 fill: none;
524 stroke: #ffff00;
525 stroke-width: 1px;
526 }
527 .line9{
528 fill: none;
529 stroke: #cc6666;
530 stroke-width: 1px;
531 }
532 .line10{
533 fill: none;
534 stroke: #663399;
535 stroke-width: 1px;
536 }
537 .line11{
538 fill: none;
539 stroke: #339900;
540 stroke-width: 1px;
541 }
542 .line12{
543 fill: none;
544 stroke: #9966FF;
545 stroke-width: 1px;
546 }
547 /* default fill styles for multiple datasets (probably only use a single dataset on this graph though) */
548 .key1,.fill1{
549 fill: #ff0000;
550 fill-opacity: 0.2;
551 stroke: none;
552 stroke-width: 0.5px;
553 }
554 .key2,.fill2{
555 fill: #0000ff;
556 fill-opacity: 0.2;
557 stroke: none;
558 stroke-width: 1px;
559 }
560 .key3,.fill3{
561 fill: #00ff00;
562 fill-opacity: 0.2;
563 stroke: none;
564 stroke-width: 1px;
565 }
566 .key4,.fill4{
567 fill: #ffcc00;
568 fill-opacity: 0.2;
569 stroke: none;
570 stroke-width: 1px;
571 }
572 .key5,.fill5{
573 fill: #00ccff;
574 fill-opacity: 0.2;
575 stroke: none;
576 stroke-width: 1px;
577 }
578 .key6,.fill6{
579 fill: #ff00ff;
580 fill-opacity: 0.2;
581 stroke: none;
582 stroke-width: 1px;
583 }
584 .key7,.fill7{
585 fill: #00ffff;
586 fill-opacity: 0.2;
587 stroke: none;
588 stroke-width: 1px;
589 }
590 .key8,.fill8{
591 fill: #ffff00;
592 fill-opacity: 0.2;
593 stroke: none;
594 stroke-width: 1px;
595 }
596 .key9,.fill9{
597 fill: #cc6666;
598 fill-opacity: 0.2;
599 stroke: none;
600 stroke-width: 1px;
601 }
602 .key10,.fill10{
603 fill: #663399;
604 fill-opacity: 0.2;
605 stroke: none;
606 stroke-width: 1px;
607 }
608 .key11,.fill11{
609 fill: #339900;
610 fill-opacity: 0.2;
611 stroke: none;
612 stroke-width: 1px;
613 }
614 .key12,.fill12{
615 fill: #9966FF;
616 fill-opacity: 0.2;
617 stroke: none;
618 stroke-width: 1px;
619 }
620 .keyText{
621 fill: #000000;
622 text-anchor:start;
623 font-size: 10px;
624 font-family: "Arial", sans-serif;
625 font-weight: normal;
626 }
627 /* End copy for external style sheet */
628 ]]>
629 </style>
630 </defs>
631 [% END %]
632 <!-- svg bg -->
633 <rect x="0" y="0" width="[% config.width %]" height="[% config.height %]" class="svgBackground"/>
634
635 <!-- ///////////////// CALCULATE GRAPH AREA AND BOUNDARIES //////////////// -->
636 <!-- get dimensions of actual graph area (NOT SVG area) -->
637 [% w = config.width %]
638 [% h = config.height %]
639  
640 <!-- set start/default coords of graph -->
641 [% x = 0 %]
642 [% y = 0 %]
643
644 [% char_width = 9 %]
645 [% half_char_height = 2.5 %]
646 [% title_height = 25 %]
647  
648 <!-- calc min and max values for the first dataset (ie bar graph) -->
649 [% min_value = 99999999999 %]
650 [% max_value = 0 %]
651 [% secondary_max_value = 0 %]
652 [% secondary_max_y_label_length = 0 %]
653 [% secondary_max_y_label_size = 0 %]
654 [% max_key_size = 0 %]
655 [% max_x_label_size = 0 %]
656 <!-- find largest labels values in turn -->
657 [% FOREACH field = config.fields %]
658 [% IF max_x_label_size < field.length %]
659 [% max_x_label_size = field.length %]
660 [% END %]
661  
662 [% IF min_value > data.0.data.$field && data.0.data.$field != '' %]
663 [% min_value = data.0.data.$field %]
664 [% END %]
665  
666 [% IF max_value < data.0.data.$field && data.0.data.$field != '' %]
667 [% max_value = data.0.data.$field %]
668 [% END %]
669  
670 [% IF secondary_max_value < data.1.data.$field && data.1.data.$field != '' %]
671 [% secondary_max_value = data.1.data.$field %]
672 [% END %]
673 [% IF secondary_max_y_label_size < data.1.data.$field.length %]
674 [% secondary_max_y_label_size = data.1.data.$field.length %]
675 [% END %]
676  
677 [% IF max_key_size < data.0.title.length %]
678 [% max_key_size = data.0.title.length %]
679 [% END %]
680 [% END %]
681  
682  
683 [% secondary_max_y_label_length = secondary_max_y_label_size * char_width %]
684
685  
686 <!-- CALC HEIGHT AND Y COORD DIMENSIONS -->
687 <!-- reduce height of graph area if there is labelling on x axis -->
688 [% IF config.show_x_labels %][% h = h - 20 %][% END %]
689
690 <!-- reduce height if x labels are rotated -->
691 [% max_x_label_length = 0 %]
692 [% IF config.rotate_x_labels %]
693 [% max_x_label_length = max_x_label_size * char_width %]
694 [% h = h - max_x_label_length %]
695 [% END %]
696
697 <!-- stagger x labels if overlapping occurs -->
698 [% stagger = 0 %]
699 [% IF config.stagger_x_labels %]
700 [% stagger = 17 %]
701 [% h = h - stagger %]
702 [% END %]
703
704 [% IF config.show_x_title %][% h = h - title_height - stagger %][% END %]
705
706 <!-- pad top of graph if y axis has data labels so labels do not get chopped off -->
707 [% IF config.show_y_labels %][% h = h - 10 %][% y = y + 10 %][% END %]
708
709 <!-- reduce height if graph has title or subtitle -->
710 [% IF config.show_graph_title %][% h = h - title_height %][% y = y + title_height %][% END %]
711 [% IF config.show_graph_subtitle %][% h = h - 10 %][% y = y + 10 %][% END %]
712
713  
714
715 <!-- reduce graph dimensions if there is a KEY -->
716 [% key_box_size = 12 %]
717
718 [% IF config.key && config.key_position == 'right' %]
719 [% IF config.show_secondary_y_title %]
720 [% w = w - (max_key_size * (char_width - 1)) - (key_box_size * 3 ) %]
721 [% ELSE %]
722 [% w = w - (max_key_size * (char_width - 1)) - (key_box_size * 3 ) - title_height %]
723 [% END %]
724 [% ELSIF config.key && config.key_position == 'bottom' %]
725 [% IF data.size < 4 %]
726 [% h = h - ((data.size + 1) * (key_box_size + key_padding))%]
727 [% ELSE %]
728 [% h = h - (4 * (key_box_size + key_padding))%]
729 [% END %]
730 [% END %]
731  
732 <!-- find start value for scale on y axis -->
733 [% IF config.min_scale_value || config.min_scale_value == '0' %]
734 [% min_scale_value = config.min_scale_value %]
735 [% ELSE %]
736 <!-- setting lowest value to be min_value as no min_scale_value defined -->
737 [% min_scale_value = min_value %]
738 [% END %]
739
740 <!-- base line -->
741 [% base_line = h + y %]
742
743 <!-- how much padding between largest bar and top of graph -->
744 [% IF (max_value - min_scale_value) == 0 %]
745 [% top_pad = 10 %]
746 [% ELSE %]
747 [% top_pad = (max_value - min_scale_value) / 20 %]
748 [% END %]
749
750 <!-- how much padding between largest bar and top of graph for secondary y axis -->
751 [% IF (secondary_max_value - min_scale_value) == 0 %]
752 [% secondary_top_pad = 10 %]
753 [% ELSE %]
754 [% secondary_top_pad = (secondary_max_value - min_scale_value) / 20 %]
755 [% END %]
756
757
758 [% scale_range = (max_value + top_pad) - min_scale_value %]
759 [% secondary_scale_range = (secondary_max_value + secondary_top_pad) - min_scale_value %]
760  
761 <!-- default to 10 scale_divisions if none have been set -->
762 [% IF config.scale_divisions %]
763 [% scale_division = config.scale_divisions %]
764 [% ELSE %]
765 [% scale_division = scale_range / 10 FILTER format('%2.01f') %]
766 [% END %]
767
768 <!-- default to 10 scale_divisions if none have been set for secondary y axis -->
769 [% IF config.scale_divisions %]
770 [% secondary_scale_division = config.scale_divisions %]
771 [% ELSE %]
772 [% secondary_scale_division = secondary_scale_range / 10 FILTER format('%2.01f') %]
773 [% END %]
774  
775 [% IF config.scale_integers %]
776 [% IF scale_division < 1 %]
777 [% scale_division = 1 %]
778 [% ELSIF scale_division.match('.') %]
779 [% scale_division = scale_division FILTER format('%2.0f') %]
780 [% END %]
781 [% END %]
782
783 <!-- JUMP THE GUN AND CALC BAR WIDTHS AS THESE ARE USED FOR PADDING IF LARGE X LABELS ARE USED -->
784 <!-- get number of data points on x scale -->
785 [% dx = config.fields.size %]
786  
787 [% secondary_title_padding = 0 %]
788 [% IF config.show_secondary_y_labels %]
789 [% w = w - secondary_max_y_label_length - title_height %]
790 [% secondary_title_padding = secondary_title_padding + secondary_max_y_label_length + 10 %]
791 [% END %]
792
793 [% IF config.show_secondary_y_title %]
794 [% w = w - title_height - char_width %]
795 [% secondary_title_padding = secondary_title_padding + title_height %]
796 [% END %]
797  
798  
799 <!-- get distribution width on x axis -->
800 [% data_widths_x = w / dx %]
801 [% dw = data_widths_x FILTER format('%2.02f') %]
802  
803  
804  
805 <!-- CALC WIDTH AND X COORD DIMENSIONS -->
806 <!-- reduce width of graph area if there is large labelling on x axis -->
807 [% half_label_width = (config.fields.0.length / 2) * char_width %]
808
809 [% IF half_label_width > (dw / 2) %]
810 <!-- calc difference and pad accordingly -->
811 [% space_b4_y_axis = half_label_width - (dw / 2) %]
812 <!-- only need to pad the left side if a key is present -->
813 [% IF config.key && config.key_position == 'right' %]
814 [% w = w - space_b4_y_axis %]
815 [% ELSE %]
816 <!-- pad both sides -->
817 [% w = w - (space_b4_y_axis * 2) %]
818 [% END %]
819 [% x = x + space_b4_y_axis %]
820 [% END %]
821
822 <!-- find the string length of max value -->
823 [% max_value_length = max_value.length %]
824
825 <!-- label width in pixels -->
826 [% max_value_length_px = max_value_length * char_width %]
827 <!-- If the y labels are shown but the size of the x labels are small, pad for y labels -->
828
829 [% IF config.show_y_labels && space_b4_y_axis < max_value_length_px %]
830 <!-- allow slightly more padding if small labels -->
831 [% IF max_value_length < 2 %]
832 [% w = w - (max_value_length * (char_width * 2)) %]
833 [% x = x + (max_value_length * (char_width * 2)) %]
834 [% ELSE %]
835 [% w = w - max_value_length_px %]
836 [% x = x + max_value_length_px %]
837 [% END %]
838 [% END %]
839
840 [% IF config.show_y_title && space_b4_y_axis < max_value_length_px %]
841 [% w = w - title_height %]
842 [% x = x + title_height %]
843 [% END %]
844
845  
846  
847  
848  
849  
850 <!-- ////////////////////////////// BUILD GRAPH AREA ////////////////////////////// -->
851 <!-- graph bg -->
852 <rect x="[% x %]" y="[% y %]" width="[% w %]" height="[% h %]" class="graphBackground"/>
853  
854 <!-- axis -->
855 <path d="M[% x %] [% y %] v[% h %]" class="axis" id="xAxis"/>
856 <path d="M[% x %] [% base_line %] h[% w %]" class="axis" id="yAxis"/>
857  
858 <path d="M[% x + w %] [% y %] v[% h %]" class="axis" id="xAxis"/>
859 <!-- ////////////////////////////// AXIS DISTRIBUTIONS //////////////////////////// -->
860 <!-- get number of data points on x scale -->
861 [% dx = config.fields.size %]
862 <!-- get distribution width on x axis -->
863 [% data_widths_x = w / dx %]
864  
865 <!-- decimal point control removed due to exaggerated error when large groups of data used -->
866 [%# dw = data_widths_x.match('(\d+[\.\d\d])').0 %]
867  
868 [% dw = data_widths_x %]
869 [% IF config.bar_gap %]
870 [% bar_gap = 10 %]
871 [% IF dw < bar_gap %]
872 [% bar_gap = dw / 2 %]
873 [% END %]
874 [% ELSE %]
875 [% bar_gap = 0 %]
876 [% END %]
877  
878 [% i = dw %]
879 [% count = 0 %]
880 [% stagger_count = 0 %]
881  
882 <!-- x axis labels -->
883 [% IF config.show_x_labels %]
884 [% FOREACH field = config.fields %]
885 [% IF count == 0 %]
886 <text x="[% x + (dw / 2) - (bar_gap / 2) %]" y="[% base_line + 15 %]" [% IF config.rotate_x_labels %]transform="rotate(90 [% x + (dw / 2) - (bar_gap / 2) - half_char_height %] [% base_line + 15 %])" style="text-anchor: start" [% END %]class="xAxisLabels">[% field %]</text>
887 [% i = i - dw %]
888 [% ELSE %]
889 [% IF stagger_count == 2 %]
890 <text x="[% x + i + (dw / 2) - (bar_gap / 2) %]" y="[% base_line + 15 %]" [% IF config.rotate_x_labels %]transform="rotate(90 [% x + i + (dw / 2) - (bar_gap / 2) - half_char_height %] [% base_line + 15 %])" style="text-anchor: start" [% END %]class="xAxisLabels">[% field %]</text>
891 [% stagger_count = 0 %]
892 [% ELSE %]
893 <text x="[% x + i + (dw / 2) - (bar_gap / 2) %]" y="[% base_line + 15 + stagger %]" [% IF config.rotate_x_labels %]transform="rotate(90 [% x + i + (dw / 2) - (bar_gap / 2) - half_char_height %] [% base_line + 15 + stagger %])" style="text-anchor: start" [% END %]class="xAxisLabels">[% field %]</text>
894 <path d="M[% x + i + (dw / 2) - (bar_gap / 2) %] [% base_line %] v[% stagger %]" class="staggerGuideLine" />
895 [% END %]
896 [% END %]
897 [% i = i + dw %]
898 [% count = count + 1 %]
899 [% stagger_count = stagger_count + 1 %]
900 [% END %]
901 [% END %]
902  
903  
904 <!-- distribute Y scale -->
905 [% dy = scale_range / scale_division %]
906 <!-- ensure y_data_points butt up to edge of graph -->
907 [% y_marker_height = h / dy %]
908 [% dy = y_marker_height.match('(\d+[\.\d\d])').0 %]
909 [% count = 0 %]
910 [% y_value = min_scale_value %]
911 [% IF config.show_y_labels %]
912 [% WHILE (dy * count) < h %]
913 [% IF count == 0 %]
914 <!-- no stroke for first line -->
915 <text x="[% x - 5 %]" y="[% base_line - (dy * count) %]" class="yAxisLabels">[% y_value %]</text>
916 [% ELSE %]
917 <text x="[% x - 5 %]" y="[% base_line - (dy * count) %]" class="yAxisLabels">[% y_value %]</text>
918 <path d="M[% x %] [% base_line - (dy * count) %] h[% w %]" class="guideLines"/>
919 [% END %]
920 [% y_value = y_value + scale_division %]
921 [% count = count + 1 %]
922 [% END %]
923 [% END %]
924  
925  
926 <!-- distribute secondary Y scale -->
927  
928 <!-- ensure y_data_points butt up to edge of graph -->
929 [% secondary_y_marker_height = h / dy %]
930  
931 [% s_count = 0 %]
932 [% secondary_y_value = min_scale_value %]
933 [% IF config.show_secondary_y_labels %]
934 [% WHILE (dy * s_count) < h %]
935 <text x="[% x + w + 5 %]" y="[% base_line - (dy * s_count) %]" class="yAxisLabels" style="text-anchor: start">[% secondary_y_value %]</text>
936  
937 [% secondary_y_value = secondary_y_value + secondary_scale_division %]
938 [% s_count = s_count + 1 %]
939 [% END %]
940 [% END %]
941  
942  
943  
944  
945  
946  
947  
948  
949  
950  
951  
952 <!-- ////////////////////////////// AXIS TITLES ////////////////////////////// -->
953  
954 <!-- x axis title -->
955 [% IF config.show_x_title %]
956 [% IF !config.show_x_labels %]
957 [% y_xtitle = 15 %]
958 [% ELSE %]
959 [% y_xtitle = 35 %]
960 [% END %]
961 <text x="[% (w / 2) + x %]" y="[% h + y + y_xtitle + stagger + max_x_label_length %]" class="xAxisTitle">[% config.x_title %]</text>
962 [% END %]
963  
964 <!-- y axis title -->
965 [% IF config.show_y_title %]
966 [% IF config.y_title_text_direction == 'tb' %]
967 <text x="11" y="[% (h / 2) + y %]" class="yAxisTitle" style="writing-mode:tb;">[% config.y_title %]</text>
968 [% ELSE %]
969 <text class="yAxisTitle" transform="translate(15,[% (h / 2) + y %]) rotate(270)">[% config.y_title %]</text>
970 [% END %]
971 [% END %]
972  
973 <!-- secondary y axis title -->
974 [% IF config.show_secondary_y_title %]
975 [% IF config.secondary_y_title_text_direction == 'tb' %]
976 <text x="[% x + w + secondary_title_padding %]" y="[% (h / 2) + y %]" class="yAxisTitle" style="writing-mode:tb;">[% config.secondary_y_title %]</text>
977 [% ELSE %]
978 <text class="yAxisTitle" transform="translate([% x + w + secondary_title_padding %],[% (h / 2) + y %]) rotate(270)">[% config.secondary_y_title %]</text>
979 [% END %]
980 [% END %]
981  
982  
983  
984 <!-- ////////////////////////////// SHOW DATA ////////////////////////////// -->
985 [% bar_width = dw - bar_gap %]
986  
987 [% divider = dy / scale_division %]
988 [% secondary_divider = dy / secondary_scale_division %]
989  
990 [% xcount = 0 %]
991 [% line = data.size %]
992
993 <!-- Draw BAR CHART -->
994 [% FOREACH field = config.fields %]
995 [% dcount = 1 %]
996 <path d="M[% x + (dw * xcount) %] [% base_line %] V[% base_line - (data.0.data.$field * divider) %] h[% bar_width %] V[% base_line %] Z" class="fill[% dcount %]"/>
997 [% IF config.show_data_values %]
998 <text x="[% (dw * xcount) + x + (dw / 2) - (bar_gap / 2) %]" y="[% base_line - (data.0.data.$field * divider) - 6 %]" class="dataPointLabel">[% data.0.data.$field %]</text>
999 [% END %]
1000 [% dcount = dcount + 1 %]
1001 [% xcount = xcount + 1 %]
1002 [% END %]
1003
1004 <!-- Draw Line graph -->
1005 <path d="M
1006 [% xcount = 0 %]
1007 [% FOREACH field = config.fields %]
1008 [% IF xcount == 1 %] L [% END %]
1009 [% (dw * xcount) + x + (bar_width / 2) %] [% base_line - (data.1.data.$field * secondary_divider) %]
1010 [% xcount = xcount + 1 %]
1011 [% END %]" class="line[% line %]"/>
1012
1013 [% xcount = 0 %]
1014 [% FOREACH field = config.fields %]
1015 [% IF config.show_data_points %]
1016 <!-- datapoint shown -->
1017 <circle cx="[% (dw * xcount) + x + (bar_width / 2) %]" cy="[% base_line - (data.1.data.$field * secondary_divider) %]" r="2.5" class="dataPoint[% line %]"/>
1018 [% END %]
1019
1020 [% IF config.show_data_values %]
1021 <!-- datavalue shown -->
1022 <text x="[% (dw * xcount) + x + (bar_width / 2) %]" y="[% base_line - (data.1.data.$field * secondary_divider) - 6 %]" class="dataPointLabel">[% data.1.data.$field %]</text>
1023 [% END %]
1024 [% xcount = xcount + 1 %]
1025 [% END %]
1026  
1027  
1028 <!-- //////////////////////////////// KEY /////// ////////////////////////// -->
1029 [% key_count = 1 %]
1030 [% key_padding = 5 %]
1031 [% IF config.key && config.key_position == 'right' %]
1032 [% FOREACH dataset = data %]
1033 <rect x="[% x + w + 20 + secondary_max_y_label_length + title_height %]" y="[% y + (key_box_size * key_count) + (key_count * key_padding) %]" width="[% key_box_size %]" height="[% key_box_size %]" class="key[% key_count %]"/>
1034 <text x="[% x + w + 20 + key_box_size + key_padding + secondary_max_y_label_length + title_height %]" y="[% y + (key_box_size * key_count) + (key_count * key_padding) + key_box_size %]" class="keyText">[% dataset.title %]</text>
1035 [% key_count = key_count + 1 %]
1036 [% END %]
1037 [% ELSIF config.key && config.key_position == 'bottom' %]
1038 <!-- calc y position of start of key -->
1039 [% y_key = base_line %]
1040 [% IF config.show_x_title %][% y_key = y_key + title_height %][% END %]
1041 [% IF config.rotate_x_labels && config.show_x_labels %]
1042 [% y_key = y_key + max_x_label_length %]
1043 [% ELSIF config.show_x_labels && stagger < 1 %]
1044 [% y_key = y_key + 20 %]
1045 [% END %]
1046
1047 [% y_key_start = y_key %]
1048 [% x_key = x %]
1049 [% FOREACH dataset = data %]
1050 [% IF key_count == 4 || key_count == 7 || key_count == 10 %]
1051 <!-- wrap key every 3 entries -->
1052 [% x_key = x_key + 200 %]
1053 [% y_key = y_key - (key_box_size * 4) - 2 %]
1054 [% END %]
1055 <rect x="[% x_key %]" y="[% y_key + (key_box_size * key_count) + (key_count * key_padding) + stagger %]" width="[% key_box_size %]" height="[% key_box_size %]" class="key[% key_count %]"/>
1056  
1057 <text x="[% x_key + key_box_size + key_padding %]" y="[% y_key + (key_box_size * key_count) + (key_count * key_padding) + key_box_size + stagger %]" class="keyText">[% dataset.title %]</text>
1058 [% key_count = key_count + 1 %]
1059 [% END %]
1060
1061 [% END %]
1062
1063 <!-- //////////////////////////////// MAIN TITLES ////////////////////////// -->
1064  
1065 <!-- main graph title -->
1066 [% IF config.show_graph_title %]
1067 <text x="[% config.width / 2 %]" y="15" class="mainTitle">[% config.graph_title %]</text>
1068 [% END %]
1069  
1070 <!-- graph sub title -->
1071 [% IF config.show_graph_subtitle %]
1072 [% IF config.show_graph_title %]
1073 [% y_subtitle = 30 %]
1074 [% ELSE %]
1075 [% y_subtitle = 15 %]
1076 [% END %]
1077 <text x="[% config.width / 2 %]" y="[% y_subtitle %]" class="subTitle">[% config.graph_subtitle %]</text>
1078 [% END %]
1079 </svg>
« Graph/BarHorizontal.pm ↑ Summary Graph/Line.pm »