| 1 | # $Id$ |
|---|
| 2 | |
|---|
| 3 | package Google::Chart; |
|---|
| 4 | use Moose; |
|---|
| 5 | use Google::Chart::Axis; |
|---|
| 6 | use Google::Chart::Data; |
|---|
| 7 | use Google::Chart::Size; |
|---|
| 8 | use Google::Chart::Type; |
|---|
| 9 | use Google::Chart::Types; |
|---|
| 10 | use LWP::UserAgent; |
|---|
| 11 | use URI; |
|---|
| 12 | use overload |
|---|
| 13 | '""' => \&as_uri, |
|---|
| 14 | fallback => 1, |
|---|
| 15 | ; |
|---|
| 16 | our $USER_AGENT; |
|---|
| 17 | |
|---|
| 18 | use constant BASE_URI => URI->new("http://chart.apis.google.com/chart"); |
|---|
| 19 | |
|---|
| 20 | our $VERSION = '0.00001'; |
|---|
| 21 | our $AUTHORITY = 'cpan:DMAKI'; |
|---|
| 22 | |
|---|
| 23 | has 'type' => ( |
|---|
| 24 | is => 'rw', |
|---|
| 25 | does => 'Google::Chart::Type', |
|---|
| 26 | coerce => 1, |
|---|
| 27 | required => 1, |
|---|
| 28 | ); |
|---|
| 29 | |
|---|
| 30 | has 'data' => ( |
|---|
| 31 | is => 'rw', |
|---|
| 32 | does => 'Google::Chart::Data', |
|---|
| 33 | coerce => 1, |
|---|
| 34 | required => 1, |
|---|
| 35 | ); |
|---|
| 36 | |
|---|
| 37 | has 'size' => ( |
|---|
| 38 | is => 'rw', |
|---|
| 39 | isa => 'Google::Chart::Size', |
|---|
| 40 | coerce => 1, |
|---|
| 41 | required => 1, |
|---|
| 42 | lazy => 1, |
|---|
| 43 | default => sub { Google::Chart::Size->new( width => 400, height => 300 ) }, |
|---|
| 44 | ); |
|---|
| 45 | |
|---|
| 46 | has 'marker' => ( |
|---|
| 47 | is => 'rw', |
|---|
| 48 | isa => 'Google::Chart::Marker', |
|---|
| 49 | coerce => 1, |
|---|
| 50 | ); |
|---|
| 51 | |
|---|
| 52 | has 'axis' => ( |
|---|
| 53 | is => 'rw', |
|---|
| 54 | isa => 'Google::Chart::Axis', |
|---|
| 55 | coerce => 1, |
|---|
| 56 | ); |
|---|
| 57 | |
|---|
| 58 | has 'ua' => ( |
|---|
| 59 | is => 'rw', |
|---|
| 60 | isa => 'LWP::UserAgent', |
|---|
| 61 | required => 1, |
|---|
| 62 | lazy => 1, |
|---|
| 63 | default => sub { $USER_AGENT ||= LWP::UserAgent->new() } |
|---|
| 64 | ); |
|---|
| 65 | |
|---|
| 66 | __PACKAGE__->meta->make_immutable; |
|---|
| 67 | |
|---|
| 68 | no Moose; |
|---|
| 69 | |
|---|
| 70 | sub as_uri { |
|---|
| 71 | my $self = shift; |
|---|
| 72 | |
|---|
| 73 | my %query; |
|---|
| 74 | foreach my $c qw( type data size marker axis ) { |
|---|
| 75 | my $component = $self->$c; |
|---|
| 76 | next unless $component; |
|---|
| 77 | my @params = $component->as_query; |
|---|
| 78 | while (@params) { |
|---|
| 79 | my ($name, $value) = splice(@params, 0, 2); |
|---|
| 80 | next unless length $value; |
|---|
| 81 | $query{$name} = $value; |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | # If in case you want to change this for debugging or whatever... |
|---|
| 86 | my $uri = $ENV{GOOGLE_CHART_URI} ? |
|---|
| 87 | URI->new($ENV{GOOGLE_CHART_URI}) : |
|---|
| 88 | BASE_URI->clone; |
|---|
| 89 | $uri->query_form( %query ); |
|---|
| 90 | return $uri; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | sub render { |
|---|
| 94 | my $self = shift; |
|---|
| 95 | my $response = $self->ua->get($self->as_uri); |
|---|
| 96 | |
|---|
| 97 | if ($response->is_success) { |
|---|
| 98 | return $response->content; |
|---|
| 99 | } else { |
|---|
| 100 | die $response->status_line; |
|---|
| 101 | } |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | sub render_to_file { |
|---|
| 105 | my ($self, $filename) = @_; |
|---|
| 106 | |
|---|
| 107 | open my $fh, '>', $filename or die "can't open $filename for writing: $!\n"; |
|---|
| 108 | print $fh $self->render; |
|---|
| 109 | close $fh or die "can't close $filename: $!\n"; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | 1; |
|---|
| 113 | |
|---|
| 114 | __END__ |
|---|
| 115 | |
|---|
| 116 | =head1 NAME |
|---|
| 117 | |
|---|
| 118 | Google::Chart - Interface to Google Charts API |
|---|
| 119 | |
|---|
| 120 | =head1 SYNOPSIS |
|---|
| 121 | |
|---|
| 122 | use Google::Chart; |
|---|
| 123 | |
|---|
| 124 | my $chart = Google::Chart->new( |
|---|
| 125 | type => "Bar", |
|---|
| 126 | data => [ 1, 2, 3, 4, 5 ] |
|---|
| 127 | ); |
|---|
| 128 | print $chart->as_uri, "\n"; |
|---|
| 129 | print $chart->render_to_file( filename => 'filename.png' ); |
|---|
| 130 | |
|---|
| 131 | =head1 METHODS |
|---|
| 132 | |
|---|
| 133 | =head2 new(%args) |
|---|
| 134 | |
|---|
| 135 | =over 4 |
|---|
| 136 | |
|---|
| 137 | =item type |
|---|
| 138 | |
|---|
| 139 | Specifies the chart type, such as line, bar, pie, etc. If given a string like |
|---|
| 140 | 'Bar', it will instantiate an instance of Google::Chart::Type::Bar by |
|---|
| 141 | invoking argument-less constructor. |
|---|
| 142 | |
|---|
| 143 | If you want to pass parameters to the constructor, either pass in an |
|---|
| 144 | already instanstiated object, or pass in a hash, which will be coerced to |
|---|
| 145 | the appropriate object |
|---|
| 146 | |
|---|
| 147 | my $chart = Google::Chart->new( |
|---|
| 148 | type => { |
|---|
| 149 | module => "Bar", |
|---|
| 150 | args => { |
|---|
| 151 | orientation => "horizontal" |
|---|
| 152 | } |
|---|
| 153 | } |
|---|
| 154 | ); |
|---|
| 155 | |
|---|
| 156 | =item size |
|---|
| 157 | |
|---|
| 158 | Specifies the chart size. Strings like "400x300", hash references, or already |
|---|
| 159 | instantiated objects can be used: |
|---|
| 160 | |
|---|
| 161 | my $chart = Google::Chart->new( |
|---|
| 162 | size => "400x300", |
|---|
| 163 | ); |
|---|
| 164 | |
|---|
| 165 | my $chart = Google::Chart->new( |
|---|
| 166 | size => { |
|---|
| 167 | width => 400, |
|---|
| 168 | height => 300 |
|---|
| 169 | } |
|---|
| 170 | ); |
|---|
| 171 | |
|---|
| 172 | =back |
|---|
| 173 | |
|---|
| 174 | =head2 as_uri() |
|---|
| 175 | |
|---|
| 176 | Returns the URI that represents the chart object. |
|---|
| 177 | |
|---|
| 178 | =head2 render() |
|---|
| 179 | |
|---|
| 180 | Generates the chart image, and returns the contents. |
|---|
| 181 | |
|---|
| 182 | =head2 render_to_file( %args ) |
|---|
| 183 | |
|---|
| 184 | Generates the chart, and writes the contents out to the file specified by |
|---|
| 185 | `filename' parameter |
|---|
| 186 | |
|---|
| 187 | =cut |
|---|