View/SVG/TT/Graph.pm

filter: Statementstmt 80.0% 8 / 10 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 85.7% 12 / 14 CC 1 SCAR 0.7
CC
1
Cov
80%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Helper/View/SVG/TT/Graph.pm ↑ Summary
1 package Catalyst::View::SVG::TT::Graph;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶BEGIN { extends 'Catalyst::View'; }
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use Image::LibRSVG;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use MIME::Types;
9  
10 our $VERSION = 0.0226;
11  
12 has 'format' => ( is => 'ro', isa => 'Str', default => 'svg' );
13  
14 has 'chart_conf' => ( is => 'ro', isa => 'HashRef', default => sub {{}} );
15  
16 has 't' => ( is => 'ro', isa => 'MIME::Types', default => sub { MIME::Types->new } );
17  
18 =head1 NAME
19  
20 Catalyst::View::SVG::TT::Graph - SVG::TT::Graph charts (in svg/png/gif/jpeg..) for your Catalyst application
21  
22 =head1 SYNOPSIS
23  
24 Create your view class:
25  
26 ./script/myapp_create.pl view Chart SVG::TT::Graph
27  
28 Set your chart preferences in your config:
29  
30 <View::Chart>
31 format png
32 <chart_conf>
33 style_sheet /path/to/stylesheet.css
34 show_graph_title 1
35 </chart_conf>
36 </View::Chart>
37  
38 Stash your chart data in your controller:
39  
40 $c->stash->{chart_title} = 'Sales data'; # optional
41
42 $c->stash->{chart_type} = 'Bar'; # or Pie/Line/BarHorizontal
43
44 $c->stash->{chart_conf} = {
45 height => 400,
46 width => 600
47 };
48
49 $c->stash->{chart_fields} = [ qw(Jan Feb March ..) ];
50 $c->stash->{chart_data} = [ 120, 102, ..];
51  
52 In your end method:
53  
54 $c->forward($c->view('Chart'));
55  
56 If you want, say a comparative line graph of mutiple sets of data:
57  
58 $c->stash->{chart_type} = 'Line';
59
60 $c->stash->{chart_data} = [
61 { title => 'Barcelona', data => [ ... ] },
62 { title => 'Atletico', data => [ ... ] },
63 ];
64  
65 =cut
66  
67 =head1 METHODS
68  
69 =head2 process
70  
71 Generate the SVG::TT::Graph chart
72  
73 =cut
74  
75 sub process {
76 my ( $self, $c ) = @_;
77  
78 my ( $type, $fields, $data ) = map {
79 $c->stash->{"chart_" . $_} or croak("\$c->stash->{chart_$_} not set")
80 } qw(type fields data);
81  
82 $type =~ m/^(Bar(Horizontal)?|Pie|Line)$/ or croak("Invalid chart type $type");
83  
84 my $conf = {
85 %{ $self->chart_conf },
86 %{ $c->stash->{chart_conf} }
87 };
88  
89 $conf->{fields} = $fields;
90
91 if (my $title = $c->stash->{chart_title} || $conf->{graph_title}) {
92 $conf->{graph_title} = $title;
93 $conf->{show_graph_title} = 1;
94 }
95  
96 my $class = "SVG::TT::Graph::$type";
97  
98 Catalyst::Utils::ensure_class_loaded($class);
99 my $svgttg = $class->new($conf);
100 if ('HASH' eq ref($data)) {
101 $svgttg->add_data($data);
102 } elsif ('ARRAY' eq ref($data)) {
103 if ('HASH' eq ref($data->[0])) {
104 foreach my $datum (@$data) {
105 $svgttg->add_data($datum);
106 }
107 } else {
108 $svgttg->add_data( { data => $data } );
109 }
110 }
111  
112 my @formats = qw(gif jpeg png bmp ico pnm xbm xpm);
113 my $frestr = '^(' . join('|', @formats) . ')$';
114 my $format = $c->stash->{format} || $self->format;
115  
116 if ($format =~ m/$frestr/) {
117 Image::LibRSVG->isFormatSupported($format)
118 or croak("Format $format is not supported");
119 $svgttg->compress(0);
120 my $img = $svgttg->burn;
121 my $rsvg = Image::LibRSVG->new();
122 $rsvg->loadImageFromString($img);
123 my $mtype = $self->t->mimeTypeOf($format);
124 $c->res->content_type($mtype);
125 $c->res->body($rsvg->getImageBitmap($format));
126 } elsif ($format eq 'svg') {
127 $c->res->content_type("image/svg+xml");
128 $c->res->content_encoding("gzip");
129 $c->res->body($svgttg->burn);
130 }
131 }
132  
133 =head1 CONFIG OPTIONS
134  
135 B<Note:> These can be overridden by stashing parameters with the same name
136  
137 =head2 format
138  
139 Can be svg, png, gif, jpeg or any other format supported by L<Image::LibRSVG>
140  
141 =head2 chart_conf
142  
143 A hashref that takes all options to L<SVG::TT::Graph>::type . For the correct
144 options, see the corresponding documentation:
145  
146 L<Bar Options|SVG::TT::Graph::Bar/new()>,
147  
148 L<Pie Options|SVG::TT::Graph::Pie/new()>,
149  
150 L<Line Options|SVG::TT::Graph::Line/new()>,
151  
152 L<BarLine Options|SVG::TT::Graph::BarLine/new()>,
153  
154 L<TimeSeries Options|SVG::TT::Graph::TimeSeries/new()>,
155  
156 L<BarHorizontal Options|SVG::TT::Graph::BarHorizontal/new()>
157  
158 =head1 STASHED PARAMETERS
159  
160 =head2 format
161  
162 An optional output format (svg/png/gif/jpeg..). Overrides config format
163  
164 =head2 chart_title
165  
166 An optional title for your chart
167  
168 =head2 chart_type
169  
170 Bar / Pie / Line / BarHorizontal / BarLine / TimeSeries
171  
172 =head2 chart_conf
173  
174 Any options taken by L<SVG::TT::Graph>
175  
176 =head2 chart_fields
177  
178 A list (array reference) of fields to show in your graph:
179  
180 $c->stash->{fields} = [ 'Jan', 'Feb', 'March' .. ];
181  
182 =head2 chart_data
183  
184 If all you want is a singe data set, can be a hash reference of the form:
185  
186 $c->stash->{chart_data} = { title => 'sales', values => [ 1.4, 2.2, ... ] }
187  
188 or a simple ArrayRef if you don't want a title
189  
190 $c->stash->{chart_data} = [ 1.4, 2.2, ... ]
191  
192 If you want multiple data sets, use an array reference with each set in a hashref:
193  
194 $c->stash->{chart_data} = [
195 { title => 'Barcelona', data => [ .. ] },
196 { title => 'Atletico', data => [ .. ] }
197 ];
198  
199 =head1 SAMPLE CHARTS
200  
201 See L<http://leo.cuckoo.org/projects/SVG-TT-Graph/>
202  
203 =head1 KNOWN BUGS
204  
205 For jpeg pie charts, background color transparency doesn't work
206  
207 =head1 REPOSITORY
208  
209 See L<git://github.com/terencemo/Catalyst--View--SVG--TT--Graph.git>
210  
211 =head1 SEE ALSO
212  
213 L<SVG::TT::Graph>, L<Image::LibRSVG>
214  
215 =head1 AUTHOR
216  
217 Terence Monteiro <terencemo[at]cpan.org>
218  
219 =head1 LICENSE
220  
221 This library is free software. You can redistribute it and/or modify it under
222 the same terms as Perl itself.
223  
224 =cut
225  
226 no Moose;
227  
228 1;
« Helper/View/SVG/TT/Graph.pm ↑ Summary