blib/lib/SWF/Chart.pm untested

filter: Statementstmt 0.0% 0 / 230 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 369 CC 31 SCAR 29.9
CC
31
Cov
0%
xml
56.0
AUTOLOAD
42.0
chart_value_text
20.0
CRAP 20.0
1 package SWF::Chart;
2  
3 =pod
4  
5 =head1 NAME
6  
7 SWF::Chart - Perl interface to the SWF Chart generation tool
8  
9 =head1 SYNOPSIS
10  
11 use SWF::Chart;
12  
13 my $g = SWF::Chart->new;
14  
15 $g->set_titles(qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
16  
17 # Add a single data set
18 $g->add_dataset(1, 3, 5, 7, 2, 4, 6);
19  
20 # Add multiple datasets
21 $g->add_dataset([qw(1 3 5 7 11 13 17 23 29 31 37 41)],
22 [qw(1 1 2 3 5 8 13 21 34 55 89 144)]);
23  
24 # Add multiple datasets with labels
25 $g->add_dataset('Label 1' => \@set1,
26 'Label 2' => \@set2);
27  
28 $g->text_properties(bold => 0,
29 size => 10,
30 color => '333333');
31  
32 $g->chart_rect(positive_color => '555555',
33 positive_alpha => 100);
34  
35 $g->series_color('DEADBE');
36  
37 print $g->xml;
38  
39 =head1 DESCRIPTION
40  
41 This module is the Perl interface to the SWF Charts flash graphing tool. It constructs the XML file this flash movie requires via an OO interface. Each configurable option that is listed on the SWF Charts reference page has a companion method in this module. See:
42  
43 http://www.maani.us/charts/index.php?menu=Reference
44  
45 When using this module, please be sure to use the latest version of the XML/SWF
46 Charts flash movie. Earlier versions of that flash movie supported a different
47 XML structure for which this module is not backward compatible.
48  
49 Note that there are a few extra helper functions that this module provides:
50  
51 =over 4
52  
53 =item chart_data (set_titles/add_dataset)
54  
55 The 'chart_data' option has been split into two different methods, 'set_titles' and 'add_dataset'. The original 'chart_data' option held all of this information, but from an interface standpoint, it makes more sense to separate these.
56  
57 =item text_properties
58  
59 This method sets the 'font', 'bold', 'size' and 'color' properties of all options that effect text ('axis_category', 'axis_value', 'chart_value', 'legend_label' and 'draw_text'). See "L<text_properties|/"MODULE SPECIFIC METHODS">" for more information.
60  
61 =item hide_legend
62  
63 This method employes a hack to hide the legend of a graph since this option does not currently exist in SWF Chart.
64  
65 =back
66  
67 =head1 MODULE SPECIFIC METHODS
68  
69 The following methods do not have a direct analog to any SWF Chart option.
70  
71 =over 4
72  
73 =cut
74  
75 #--------------------------------------#
76 # Dependencies
77  
78 use strict;
79  
80 #--------------------------------------#
81 # Global Variables
82  
83 our $VERSION = '1.4';
84  
85 # What version of the XML/SWF Charts flash module do we support?
86 our $SWF_VERSION = '4.5';
87  
88 #--------------------------------------#
89 # Constants
90  
91 # The OPTIONS hash keeps track of how all the options are represented in XML.
92 # If the first value is 'elem' it means that the option is a single element.
93 # For 'elem', if there is no second value, then that option is an empty content
94 # element that uses parameters, ie:
95 #
96 # foobar => ['elem']
97 #
98 # and if the set call for this option looks like:
99 #
100 # $g->foobar(param1 => 'value1', param2 => 'value2')
101 #
102 # then this XML will be produced:
103 #
104 # <foobar param1="value1" param2="value2" />
105 #
106 # For 'elem', if the second value is 1, then the element has content, ie:
107 #
108 # fizpow => ['elem', 1]
109 #
110 # and if the set call for this option looks like:
111 #
112 # $g->fizpow('value')
113 #
114 # then this XML will be produced:
115 #
116 # <fizpow>value</fizpow>
117 #
118 # If the first value if 'container' then the second value will be another option
119 # in OPTIONS that gives the format for the elements contained by this container.
120 # For example, if the option definition is:
121 #
122 # mumblyjoe => ['container', 'fizpow']
123 #
124 # and if the set call for this option looks like:
125 #
126 # $g->mumblyjoe('foo');
127 # $g->mumblyjoe('bar');
128 #
129 # then this XML will be produced:
130 #
131 # <mumblyjoe><fizpow>foo</fizpow><fizpow>bar</fizpow></mumblyjoe>
132 #
133 # To get different behavior, override the default AUTOLOAD method.
134  
135 use constant OPTIONS => {
136 axis_category => ['elem'],
137 axis_ticks => ['elem'],
138 axis_value => ['elem'],
139 axis_value_text => ['container', 'string'],
140  
141 chart_border => ['elem'],
142 chart_data => ['elem'],
143 chart_grid_h => ['elem'],
144 chart_grid_v => ['elem'],
145 chart_pref => ['elem'],
146 chart_rect => ['elem'],
147 chart_transition => ['elem'],
148 chart_type => ['elem', 1],
149 chart_value => ['elem'],
150 chart_value_text => ['container', 'string'],
151  
152 legend_label => ['elem'],
153 legend_rect => ['elem'],
154 legend_transition => ['elem'],
155  
156 link => ['elem'],
157 link_data => ['elem'],
158 live_update => ['elem'],
159  
160 series_color => ['container', 'color'],
161 series_explode => ['container', 'number'],
162 series_gap => ['elem'],
163 series_switch => ['elem'],
164  
165 circle => ['elem'],
166 line => ['elem'],
167 rect => ['elem'],
168 text => ['elem'],
169 string => ['elem', 1],
170 color => ['elem', 1],
171 number => ['elem', 1],
172 };
173  
174 # A list of properties that if passed to any of the above options should be
175 # considered text properties.
176 use constant TEXT_PROPS => {font => 1,
177 bold => 1,
178 size => 1,
179 color => 1,
180 };
181  
182 #--------------------------------------#
183 # Public Class Methods
184  
185 =pod
186  
187 =item $g = SWF::Chart->new
188  
189 Creates a new SWF Chart object. Does not take any parameters.
190  
191 =cut
192  
193 sub new {
194 my $class = shift;
195 my $self = bless {}, ref $class || $class;
196  
197 return $self;
198 }
199  
200 sub DESTROY { }
201  
202 #--------------------------------------#
203 # Public Instance Methods
204  
205 =pod
206  
207 =item $g->set_titles
208  
209 Sets the titles for each column of data. There should be as many titles as data points you pass to 'add_dataset'. If this method is called multiple times, only the last set of titles given will be used.
210  
211 =cut
212  
213 sub set_titles {
214 my $self = shift;
215 my (@titles) = @_;
216  
217 # Allow either an array ref or an actual array to be passed in
218 $self->{rows}->[0] = ref $titles[0] ? $titles[0] : \@titles;
219  
220 # Make sure to include the required empty cell for the title row
221 unshift @{$self->{rows}->[0]}, undef;
222  
223 return 1;
224 }
225  
226 =pod
227  
228 =item $g->add_dataset(@row);
229  
230 =item $g->add_dataset(\@row1, \@row2, \@row3)
231  
232 =item $g->add_dataset('Region A' => \@row1,
233 'Region B' => \@row2)
234  
235 Adds rows of data to be charted. Accepts a list of values for a single row, a list of array references for multiple rows or a hash where the key is the row label and the value is an array reference of values for that row. If this method is called more than once, each row is added after the existing rows rather than replacing them.
236  
237 =cut
238  
239 sub add_dataset {
240 my $self = shift;
241 my (@set) = @_;
242  
243 # Initialize rows with a blank first row saved for the titles
244 $self->{rows} ||= [undef];
245  
246 # Reformat to be in the form (label => \@row)
247 if ($set[0] and ref $set[0]) {
248 @set = map { undef, [@$_] } @set;
249 } elsif ($set[1] and not ref $set[1]) {
250 @set = (undef, [@set]);
251 }
252  
253 while (@set) {
254 my ($label, $r) = (shift @set, shift @set);
255  
256 # Add the label
257 unshift @$r, $label;
258 push @{$self->{rows}}, $r;
259 }
260  
261 return 1;
262 }
263  
264 =pod
265  
266 =item $g->chart_value_text(@row);
267  
268 =item $g->chart_value_text(\@row1, \@row2, \@row3)
269  
270 Adds alternate text for the values displayed. Accepts a list of values for a single row or a list of array references for multiple rows. If this method is called more than once, each row's alternate text is added after the existing rows rather than replacing them.
271  
272 =cut
273  
274 sub chart_value_text {
275 my $self = shift;
276 my (@text) = @_;
277 my @add_rows = ref $text[0] ? @text : (\@text);
278  
279 return unless @add_rows;
280  
281 my @rows = ['row', undef, [(['null']) x (scalar(@{$add_rows[0]})+1)]];
282 my $data = $self->{opts}->{'chart_value_text'} ||=
283 ['chart_value_text', undef, \@rows];
284  
285 foreach my $r (@add_rows) {
286 my @str;
287 push @{$data->[2]}, ['row', undef, \@str];
288  
289 foreach my $t (undef, @$r) {
290 if (defined $t) {
291 push @str, ['string', undef, $t];
292 } else {
293 push @str, ['null'];
294 }
295 }
296 }
297  
298 return 1;
299 }
300  
301 =pod
302  
303 =item $g->hide_legend
304  
305 This method is a hack to allow easy hiding of the legend. It achives this by setting the legend y-coordinate to -9999, placing it (hopefully) far off canvas. If at some point a native 'hide_legend' is implimented, that will be used instead.
306  
307 =cut
308  
309 sub hide_legend {
310 my $self = shift;
311  
312 # This is the ordained way to remove the legend...yuk
313 $self->legend_rect(y => -9999);
314 }
315  
316 =pod
317  
318 =item $g->text_properties(%param)
319  
320 This method sets text properties for all options that affect text. This makes it easy to sets the font for all text in a graph to be 'Arial', or all to a particular point size. The keys to %param are the properties that can be changed:
321  
322 =over 4
323  
324 =item font
325  
326 The font face to use for all text. Arial is the default font and is the only one embedded in the flash movie. If any other font is set here it must be installed on the clients machine or SWF Chart will default to the closest font.
327  
328 =item bold
329  
330 A boolean that determines whether the font is bold or not
331  
332 =item size
333  
334 The font size in points
335  
336 =item color
337  
338 The hex color for text
339  
340 =back
341  
342 This method acts as a convience method to set all text properties at once. This means that if you call this after setting font properties for a specific option, those values will be overwritten. If you would prefer this to operate as a way to set defaults, then call this method first and then set the additional font prorperites.
343  
344 =cut
345  
346 sub text_properties {
347 my $self = shift;
348 my (%param) = @_;
349  
350 # Clean the values on the way in so we don't inadvertantly overwrite stuff
351 foreach my $k (keys %param) {
352 delete $param{$k} unless exists TEXT_PROPS->{$k};
353 }
354 $self->{defaults}->{text_props} = \%param;
355  
356 # Set the defaults for attribute only tags
357 $self->axis_category(%param);
358 $self->axis_value(%param);
359 $self->chart_value(%param);
360 $self->legend_label(%param);
361  
362 # Set the defaults for the draw_text tags
363 if (exists $self->{opts}->{draw_text}) {
364  
365 # Get the array of <text> elements and loop through them
366 my $text = $self->{opts}->{draw_text}->[2];
367 foreach my $t (@$text) {
368  
369 # Iterate over each attribute currently set on this <text> element
370 foreach my $k (keys %param) {
371  
372 # Update it unless its already set
373 next if exists $t->[1]->{$k};
374 $t->[1]->{$k} = $param{$k};
375 }
376 }
377 }
378 }
379  
380 =pod
381  
382 =back
383  
384 =head1 SWF CHART METHODS
385  
386 The following methods have a direct relationship to the options SWF Charts accepts. Please refer to the SWF Chart reference page for details on these options. These methods are split into three categories:
387  
388 =head2 Parameter Methods
389  
390 These methods take a hash of parameters that map to the parameters given on the SWF Chart reference page. If called more than once, each call overwrites the previous parameter values. If a particular parameter is not given, then the previous value for that parameter is retained. Example:
391  
392 $g->chart_border(color => '00FF00',
393 top_thickness => 0,
394 bottom_thickness => 0);
395  
396 The 'chart_border' option now has a border color of '00FF00' and zero top and bottom thickness. After this call:
397  
398 $g->chart_border(top_thickness => 5,
399 bottom_thickness => 5);
400  
401 The top and bottom thickness will become 5, but the color will remain '00FF00'.
402  
403 =over 4
404  
405 =item *
406  
407 $g->axis_category(%param)
408  
409 =item *
410  
411 $g->axis_ticks(%param)
412  
413 =item *
414  
415 $g->axis_value(%param)
416  
417 =item *
418  
419 $g->chart_border(%param)
420  
421 =item *
422  
423 $g->chart_data(%param)
424  
425 =item *
426  
427 $g->chart_grid_h(%param)
428  
429 =item *
430  
431 $g->chart_grid_v(%param)
432  
433 =item *
434  
435 $g->chart_pref(%param)
436  
437 =item *
438  
439 $g->chart_rect(%param)
440  
441 =item *
442  
443 $g->chart_value(%param)
444  
445 =item *
446  
447 $g->chart_value_text(%param)
448  
449 =item *
450  
451 $g->legend_bg(%param)
452  
453 =item *
454  
455 $g->legend_label(%param)
456  
457 =item *
458  
459 $g->legend_rect(%param)
460  
461 =item *
462  
463 $g->link(%param)
464  
465 =item *
466  
467 $g->link_data(%param)
468  
469 =item *
470  
471 $g->live_update(%param)
472  
473 =item *
474  
475 $g->series_gap(%param)
476  
477 =item *
478  
479 $g->series_switch(%param)
480  
481 =back
482  
483 =head2 Value Methods
484  
485 These methods take a scalar value. If called more than once, each call overwrites the previous value.
486  
487 =over 4
488  
489 =item *
490  
491 $g->chart_type($value)
492  
493 =back
494  
495 =head2 Repeatable Methods
496  
497 These methods can be called more than once. Each time they are called they add additional data rather than replace existing data. Currently there is no way to change the parameters given on previous calls.
498  
499 =over 4
500  
501 =item *
502  
503 $g->draw($thing1 => $param, $thing2 => $param, ...)
504  
505 Draw one or more primitives to the chart. Options for $thing are:
506  
507 =over 4
508  
509 =item circle
510  
511 =item image
512  
513 =item line
514  
515 =item rect
516  
517 =item text
518  
519 =back
520  
521 Valid options for the $param hash are the parameters given for the elements of
522 the same name within the 'draw' command L<http://www.maani.us/xml_charts/index.php?menu=Reference&submenu=draw>
523  
524 The only difference is when drawing 'text' you must pass the value for the text
525 via a 'value' key to the $param hash. Example:
526  
527 $g->draw(text => {bold => 1,
528 x => 20,
529 y => 20,
530 value => 'The quick brown fox',
531 },
532 line => { ... },
533 ...
534 )
535  
536 =item *
537  
538 $g->draw_circle(%param)
539  
540 Same as $g->draw(circle => \%param)
541  
542 =item *
543  
544 $g->draw_image(%param)
545  
546 Same as $g->draw(image => \%param)
547  
548 =item *
549  
550 $g->draw_line(%param)
551  
552 Same as $g->draw(line => \%param)
553  
554 =item *
555  
556 $g->draw_rect(%param)
557  
558 Same as $g->draw(rect => \%param)
559  
560 =item *
561  
562 $g->draw_text($text, %param)
563  
564 Same as $g->draw(text => \%param)
565  
566 =item *
567  
568 $g->series_color($value)
569  
570 =item *
571  
572 $g->series_explode($value)
573  
574 =back
575  
576 =cut
577  
578 sub draw {
579 my $self = shift;
580 my (%items) = @_;
581  
582 foreach my $type (keys %items) {
583 my $opts = $items{$type};
584 my $value = $opts->{value};
585 $value = delete $opts->{value} if $type eq 'text';
586 $self->_draw_thing($type, $value, $opts);
587 }
588 }
589  
590 sub draw_circle { shift->_draw_thing('circle', undef, @_) }
591  
592 sub draw_image { shift->_draw_thing('image', undef, @_) }
593  
594 sub draw_line { shift->_draw_thing('line', undef, @_) }
595  
596 sub draw_rect { shift->_draw_thing('rect', undef, @_) }
597  
598 sub draw_text {
599 my $self = shift;
600 my ($text, %param) = @_;
601 my $text_defaults = $self->{defaults}->{text_props};
602  
603 # If there are defaults copy them to the unset values
604 if ($text_defaults) {
605 foreach my $k (keys %$text_defaults) {
606 next if exists $param{$k};
607 $param{$k} = $text_defaults->{$k};
608 }
609 }
610  
611 $self->_draw_thing('text', $text, \%param);
612 }
613  
614 sub _draw_thing {
615 my $self = shift;
616 my ($thing, $value) = (shift, shift);
617  
618 # Accept either a hash or a hash ref as the fourth arg of @_
619 my $param = ref $_[0] ? $_[0] : {@_};
620 my $data = $self->{opts}->{draw} ||= ['draw', undef, []];
621  
622 if ($thing eq 'text') {
623 push @{$data->[2]}, [$thing, $param, $value];
624 } else {
625 push @{$data->[2]}, [$thing, $param];
626 }
627 }
628  
629 use vars qw( $AUTOLOAD );
630 sub AUTOLOAD {
631 my $obj = $_[0];
632 (my $option = $AUTOLOAD) =~ s!.+::!!;
633 no strict 'refs';
634  
635 my $type = OPTIONS->{$option};
636  
637 die "No such graph option '$option'" unless $type;
638  
639 if ($type->[0] eq 'elem') {
640 # This type just sets a single value with no attributes
641 if ($type->[1]) {
642 *$AUTOLOAD = sub {
643 my $self = shift;
644 my ($value) = @_;
645 $self->{opts}->{$option} = [$option, undef, $value];
646 };
647 }
648 # This type just sets attributes with no value
649 else {
650 *$AUTOLOAD = sub {
651 my $self = shift;
652 my (%param) = @_;
653 my $elem = $self->{opts}->{$option} ||= [$option, {}];
654  
655 # Update each attribute
656 foreach my $k (keys %param) {
657 $elem->[1]->{$k} = $param{$k};
658 }
659 };
660 }
661 } elsif ($type->[0] eq 'container') {
662 *$AUTOLOAD = sub {
663 my $self = shift;
664 my $data = $self->{opts}->{$option} ||= [$option, undef, []];
665  
666 push @{$data->[2]}, (@_ % 2 ? [$type->[1], undef, $_[0]]
667 : [$type->[1], {@_}]);
668 };
669 }
670  
671 goto &$AUTOLOAD;
672 }
673  
674 sub xml {
675 my $self = shift;
676 my (%param) = @_;
677 my $format = $param{format} || 0;
678  
679 my $output = '<?xml version="1.0" encoding="utf-8"?><chart>';
680 $output .= "\n" if $format;
681  
682 # Output all the chart preferences and settings
683 foreach my $opt (keys %{$self->{opts}}) {
684 $output .= $self->_elem_as_string($self->{opts}->{$opt});
685 $output .= "\n" if $format;
686 }
687  
688 # Output the data for this chart
689 $output .= '<chart_data>';
690 $output .= "\n" if $format;
691 my $rows = $self->{rows};
692  
693 # Write the labels for the chart
694 $output .= $self->_xml_chart_data_labels($rows->[0]);
695 $output .= "\n" if $format;
696  
697 # Write the rows
698 foreach my $r (@$rows[1..$#$rows]) {
699 $output .= $self->_xml_chart_data_row($r);
700 $output .= "\n" if $format;
701 }
702  
703 $output .= '</chart_data>';
704 $output .= "\n" if $format;
705  
706 $output .= '</chart>';
707  
708 return $output;
709 }
710  
711 #--------------------------------------#
712 # Private Instance Methods
713  
714 sub _xml_chart_data_labels {
715 my $self = shift;
716 my ($row) = @_;
717 my $output = '<row><null/>';
718  
719 foreach my $t (@$row[1..$#$row]) {
720 $output .= "<string>$t</string>";
721 }
722  
723 $output .= '</row>';
724  
725 return $output;
726 }
727  
728 sub _xml_chart_data_row {
729 my $self = shift;
730 my ($row) = @_;
731 my $output = '<row>';
732  
733 if ($row->[0]) {
734 $output .= '<string>'.$row->[0].'</string>';
735 } else {
736 $output .= '<null/>';
737 }
738  
739 foreach my $n (@$row[1..$#$row]) {
740 $n ||= '' unless defined $n;
741 $output .= "<number>$n</number>";
742 }
743  
744 $output .= '</row>';
745  
746 return $output;
747 }
748  
749 # ['name', {param => 1}, [$child1, $child2, $child3]]
750 sub _elem_as_string {
751 my $self = shift;
752 my ($node) = @_;
753 my ($name, $attr, $value) = @$node;
754 my $out = "<$name";
755  
756 $out .= ' '.join(' ', map { $_.'="'.$attr->{$_}.'"' } keys %$attr) if $attr;
757  
758 if ($value) {
759 $out .= '>';
760  
761 if (ref $value) {
762 foreach my $child (@$value) {
763 $out .= $self->_elem_as_string($child);
764 }
765 } else {
766 $out .= $value;
767 }
768  
769 $out .= "</$name>";
770 } else {
771 $out .= ' />';
772 }
773  
774 return $out;
775 }
776  
777 1;
778  
779 __END__