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