Graph/Bar.pm

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