| 1 | package Catalyst::View::SVGTTGraph; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use base qw(Catalyst::View); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 0 | ▶ | use UNIVERSAL::require; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | |||
| 8 | |||
| 9 | my($Revision) = '$Id: SVGTTGraph.pm,v 1.2 2006/02/18 03:03:28 takayama Exp $'; #' | ||
| 10 | |||
| 11 | our $VERSION = '0.02'; | ||
| 12 | |||
| 13 | use Data::Dumper; | ||
| 14 | |||
| 15 | =head1 NAME | ||
| 16 | |||
| 17 | Catalyst::View::SVGTTGraph - SVG Graph View Component for Catalyst | ||
| 18 | |||
| 19 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | in your config. | ||
| 22 | |||
| 23 | $c->config->{'View::SVGTTGraph'}->{image_format} = 'png'; | ||
| 24 | |||
| 25 | Supported formats are 'Image::LibRSVG->getSupportedFormats()' call to see. | ||
| 26 | |||
| 27 | in your View. | ||
| 28 | |||
| 29 | package MyApp::View::SVGTTGraph; | ||
| 30 | use base 'Catalyst::View::SVGTTGraph'; | ||
| 31 | |||
| 32 | in your controller. | ||
| 33 | |||
| 34 | sub pie_graph : Local { | ||
| 35 | my @fields = qw(Jan Feb Mar); | ||
| 36 | my @data_sales_02 = qw(12 45 21); | ||
| 37 | |||
| 38 | $c->svgttg->create('Pie', | ||
| 39 | {'height' => '500', | ||
| 40 | 'width' => '300', | ||
| 41 | 'fields' => \@fields, | ||
| 42 | }); | ||
| 43 | $c->svgttg->graph_obj->add_data({ | ||
| 44 | 'data' => \@data_sales_02, | ||
| 45 | 'title' => 'Sales 2002', | ||
| 46 | }); | ||
| 47 | } | ||
| 48 | |||
| 49 | sub end : Private { | ||
| 50 | my ( $self, $c ) = @_; | ||
| 51 | $c->forward('Catalyst::View::SVGTTGraph'); | ||
| 52 | } | ||
| 53 | |||
| 54 | and see L<SVG::TT::Graph>. | ||
| 55 | |||
| 56 | =head1 DESCRIPTION | ||
| 57 | |||
| 58 | Catalyst::View::SVGTTGraph is Catalyst view handler of SVG::TT::Graph. | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | =head1 METHODS | ||
| 63 | |||
| 64 | =head2 new | ||
| 65 | |||
| 66 | this method makes method named $c->svgttg. | ||
| 67 | $c->svgttg is an accessor to the object of Catalyst::View::SVGTTGraphObj. | ||
| 68 | $c->svgttg uses $c->stash->{'Catalyst::View::SVGTTGraph'}. | ||
| 69 | |||
| 70 | =cut | ||
| 71 | |||
| 72 | sub new { | ||
| 73 | my $class = shift; | ||
| 74 | my $c = shift; | ||
| 75 | my $self; | ||
| 76 | if( $Catalyst::VERSION >= 5.8 ) { | ||
| 77 | MRO::Compat->use or die "can not use MRO::Compat : $@\n"; | ||
| 78 | $self = $class->maybe::next::method( $c, @_ ); | ||
| 79 | } | ||
| 80 | else { | ||
| 81 | NEXT->use or die "can not use NEXT : $@\n"; | ||
| 82 | $self = $class->NEXT::new( $c, @_ ); | ||
| 83 | } | ||
| 84 | if( exists $c->config->{'View::SVGTTGraph'}->{image_format} | ||
| 85 | and $c->config->{'View::SVGTTGraph'}->{image_format} ne 'svg' ) { | ||
| 86 | Image::LibRSVG->use or die "can not use Image::LibRSVG : $@\n"; | ||
| 87 | die join( "\n", | ||
| 88 | '', | ||
| 89 | "this format is not support on this system : ".$c->config->{'View::SVGTTGraph'}->{image_format}, | ||
| 90 | "please check support formats. call Image::LibRSVG->getSupportedFormats()", | ||
| 91 | '' ) | ||
| 92 | unless( Image::LibRSVG->isFormatSupported( $c->config->{'View::SVGTTGraph'}->{image_format} ) ); | ||
| 93 | } | ||
| 94 | { | ||
| 95 | no strict 'refs'; | ||
| 96 | my $accessor = sub { | ||
| 97 | my $c = shift; | ||
| 98 | unless( $c->stash->{'Catalyst::View::SVGTTGraph'} ) { | ||
| 99 | my $obj = Catalyst::View::SVGTTGraphObj->new(); | ||
| 100 | # config | ||
| 101 | if( exists $c->config->{'View::SVGTTGraph'} and ref( $c->config->{'View::SVGTTGraph'} ) eq 'HASH' ) { | ||
| 102 | $obj->config( $c->config->{'View::SVGTTGraph'} ); | ||
| 103 | $obj->_rsvg( Image::LibRSVG->new ) | ||
| 104 | if( exists $obj->config->{image_format} ); | ||
| 105 | } | ||
| 106 | else { | ||
| 107 | $obj->config( {} ); | ||
| 108 | } | ||
| 109 | $c->stash->{'Catalyst::View::SVGTTGraph'} = $obj; | ||
| 110 | return $c->stash->{'Catalyst::View::SVGTTGraph'}; | ||
| 111 | } | ||
| 112 | }; | ||
| 113 | *{"${c}::svgttg"} = $accessor; | ||
| 114 | *{"${c}::_svgttg_accessor"} = $accessor; | ||
| 115 | } | ||
| 116 | return $self | ||
| 117 | } | ||
| 118 | |||
| 119 | =head2 process | ||
| 120 | |||
| 121 | create SVG Graph | ||
| 122 | |||
| 123 | =cut | ||
| 124 | |||
| 125 | my $content_types = { | ||
| 126 | 'png' => 'image/png', | ||
| 127 | 'tga' => 'image/targa', | ||
| 128 | 'ico' => 'image/vnd.microsoft.icon', | ||
| 129 | 'xpm' => 'image/x-xpixmap', | ||
| 130 | 'qtif' => 'image/x-quicktime', | ||
| 131 | 'xbm' => 'image/x-xbitmap', | ||
| 132 | 'icns' => '', | ||
| 133 | 'wbmp' => 'image/vnd.wap.wbmp', | ||
| 134 | 'bmp' => 'image/x-bmp', | ||
| 135 | 'wmf' => 'application/x-msmetafile', | ||
| 136 | 'tiff' => 'image/tiff', | ||
| 137 | 'pcx' => 'image/x-pcx', | ||
| 138 | 'ras' => 'image/x-cmu-raster', | ||
| 139 | 'gif' => 'image/gif', | ||
| 140 | 'ani' => 'application/octet-stream', | ||
| 141 | 'jpeg' => 'image/jpeg', | ||
| 142 | 'pnm' => 'image/x-portable-anymap', | ||
| 143 | 'jpeg2000' => 'image/jpeg', | ||
| 144 | 'svg' => 'image/svg+xml' | ||
| 145 | }; | ||
| 146 | |||
| 147 | sub process { | ||
| 148 | my $self = shift; | ||
| 149 | my $c = shift; | ||
| 150 | |||
| 151 | die "Catalyst::View::SVGTTGraph : graph object is undefined !" | ||
| 152 | unless( $c->svgttg->graph_obj ); | ||
| 153 | if( exists $c->svgttg->config->{image_format} | ||
| 154 | and $c->config->{'View::SVGTTGraph'}->{image_format} ne 'svg' | ||
| 155 | and Image::LibRSVG->isFormatSupported( $c->svgttg->config->{image_format} ) ) { | ||
| 156 | $c->svgttg->graph_obj->compress( 0 ); | ||
| 157 | $c->svgttg->_rsvg->loadImageFromString( $c->svgttg->burn ); | ||
| 158 | |||
| 159 | |||
| 160 | $c->res->header( 'Content-Type' => $content_types->{ $c->svgttg->config->{image_format} } ); | ||
| 161 | $c->res->body( $c->svgttg->_rsvg->getImageBitmap( $c->svgttg->config->{image_format} ) ); | ||
| 162 | } | ||
| 163 | else { | ||
| 164 | $c->res->header('Content-Type' => 'image/svg+xml'); | ||
| 165 | $c->res->header('Content-Encoding' => 'gzip') if( $c->svgttg->graph_obj->compress ); | ||
| 166 | $c->res->body($c->svgttg->burn); | ||
| 167 | } | ||
| 168 | return 1; | ||
| 169 | } | ||
| 170 | |||
| 171 | 1; | ||
| 172 | |||
| 173 | |||
| 174 | package Catalyst::View::SVGTTGraphObj; | ||
| 175 | |||
| 176 | use strict; | ||
| 177 | use base 'Class::Accessor::Fast'; | ||
| 178 | use UNIVERSAL::require; | ||
| 179 | |||
| 180 | sub new { | ||
| 181 | my $pkg = shift; | ||
| 182 | my $c = shift; | ||
| 183 | my $this = bless({}, $pkg); | ||
| 184 | $this->mk_accessors(qw(graph_obj _c config _rsvg)); | ||
| 185 | $this->graph_obj(undef); | ||
| 186 | $this->_c($c); | ||
| 187 | return $this; | ||
| 188 | } | ||
| 189 | |||
| 190 | =head2 $c->svgttg->create | ||
| 191 | |||
| 192 | The object of new SVG::TT::Graph is made. | ||
| 193 | Please input the kind of the graph to the first argument. | ||
| 194 | Thereafter, it comes to be able to use $c->svgttg->graph_obj. | ||
| 195 | |||
| 196 | $c->svgttg->create('Bar'); | ||
| 197 | or | ||
| 198 | $c->svgttg->create('Bar', {'height' => '500', 'width' => '300', 'fields' => \@fields}); | ||
| 199 | |||
| 200 | =cut | ||
| 201 | |||
| 202 | sub create { | ||
| 203 | my $this = shift; | ||
| 204 | my $type = shift; | ||
| 205 | |||
| 206 | my $opt = shift; | ||
| 207 | |||
| 208 | my $graph_pkg = "SVG::TT::Graph::$type"; | ||
| 209 | $graph_pkg->use or die "Catalyst::View::SVGTTGraph : use error !\n$@"; | ||
| 210 | $this->graph_obj($graph_pkg->new($opt)); | ||
| 211 | } | ||
| 212 | |||
| 213 | =head2 $c->svgttg->graph_obj | ||
| 214 | |||
| 215 | It accesses the object of SVG::TT::Graph. | ||
| 216 | Please use it after calling $c->svgttg->create. | ||
| 217 | |||
| 218 | $c->svgttg->graph_obj->add_data(....); | ||
| 219 | $c->svgttg->graph_obj->add_data(....); | ||
| 220 | |||
| 221 | =head2 $c->svgttg->burn | ||
| 222 | |||
| 223 | throws to SVG::TT::Graph->burn | ||
| 224 | |||
| 225 | $c->svgttg->burn; | ||
| 226 | |||
| 227 | =cut | ||
| 228 | |||
| 229 | sub burn { | ||
| 230 | my $this = shift; | ||
| 231 | return $this->graph_obj->burn; | ||
| 232 | } | ||
| 233 | |||
| 234 | =head2 $c->svgttg->clear | ||
| 235 | |||
| 236 | clear $c->stash->{'Catalyst::View::SVGTTGraph'} | ||
| 237 | |||
| 238 | =cut | ||
| 239 | |||
| 240 | sub clear { | ||
| 241 | my $this = shift; | ||
| 242 | $this->_c->stash->{'Catalyst::View::SVGTTGraph'} = undef; | ||
| 243 | } | ||
| 244 | |||
| 245 | =head1 SEE ALSO | ||
| 246 | |||
| 247 | L<Catalyst>, L<SVG::TT::Graph> | ||
| 248 | |||
| 249 | =head1 AUTHOR | ||
| 250 | |||
| 251 | Shota Takayama, C<shot[atmark]bindstorm.jp> | ||
| 252 | |||
| 253 | =head1 COPYRIGHT AND LICENSE | ||
| 254 | |||
| 255 | Copyright (c) Shanon, Inc. All Rights Reserved. L<http://www.shanon.co.jp/> | ||
| 256 | |||
| 257 | This program is free software, you can redistribute it and/or modify it under | ||
| 258 | the same terms as Perl itself. | ||
| 259 | |||
| 260 | =cut | ||
| 261 | |||
| 262 | 1; | ||