| 1 | package Google::Chart::DBIC; |
|---|
| 2 | use Moose; |
|---|
| 3 | use Google::Chart (); |
|---|
| 4 | use Data::Dumper (); |
|---|
| 5 | use POSIX (); |
|---|
| 6 | use Scalar::Util (); |
|---|
| 7 | our $VERSION = '0.01'; |
|---|
| 8 | |
|---|
| 9 | has 'size' => ( |
|---|
| 10 | is => 'rw', |
|---|
| 11 | required => 1, |
|---|
| 12 | default => '400x400', |
|---|
| 13 | ); |
|---|
| 14 | |
|---|
| 15 | has 'type' => ( |
|---|
| 16 | is => 'rw', |
|---|
| 17 | required => 1, |
|---|
| 18 | default => 'line', |
|---|
| 19 | ); |
|---|
| 20 | |
|---|
| 21 | has 'color' => ( |
|---|
| 22 | is => 'rw', |
|---|
| 23 | isa => 'ArrayRef[Str]', |
|---|
| 24 | required => 1, |
|---|
| 25 | default => sub { |
|---|
| 26 | [qw(ff0000 00ff00 0000ff ffff00 ff00ff 00ffff)]; |
|---|
| 27 | }, |
|---|
| 28 | ); |
|---|
| 29 | |
|---|
| 30 | has 'resultset' => ( |
|---|
| 31 | is => 'rw', |
|---|
| 32 | isa => 'ArrayRef[Object]', |
|---|
| 33 | required => 1, |
|---|
| 34 | ); |
|---|
| 35 | |
|---|
| 36 | has 'axis_x' => ( |
|---|
| 37 | is => 'rw', |
|---|
| 38 | isa => 'Str', |
|---|
| 39 | ); |
|---|
| 40 | |
|---|
| 41 | has 'max_value' => ( |
|---|
| 42 | is => 'rw', |
|---|
| 43 | isa => 'Num', |
|---|
| 44 | ); |
|---|
| 45 | |
|---|
| 46 | no Moose; |
|---|
| 47 | |
|---|
| 48 | sub as_uri { |
|---|
| 49 | my $self = shift; |
|---|
| 50 | my $dataset = {}; |
|---|
| 51 | my $axis_x = []; |
|---|
| 52 | my $max_value = 0; |
|---|
| 53 | for my $row (@{$self->resultset}) { |
|---|
| 54 | if (!defined $self->axis_x && $row->has_column_loaded('axis_x')) { |
|---|
| 55 | my $v = $row->get_column('axis_x'); |
|---|
| 56 | push @$axis_x, $v unless grep {$v} @$axis_x; |
|---|
| 57 | } |
|---|
| 58 | for ($row->columns) { |
|---|
| 59 | next unless $row->has_column_loaded($_); |
|---|
| 60 | my $v = $row->$_; |
|---|
| 61 | if (defined $self->axis_x && $_ eq $self->axis_x){ |
|---|
| 62 | push @$axis_x, $v unless grep {$v} @$axis_x; |
|---|
| 63 | next; |
|---|
| 64 | } |
|---|
| 65 | next unless Scalar::Util::looks_like_number $v; |
|---|
| 66 | push @{$dataset->{$_}}, Scalar::Util::looks_like_number $v ? $v : undef; |
|---|
| 67 | $max_value = $v if $v >= $max_value; |
|---|
| 68 | } |
|---|
| 69 | } |
|---|
| 70 | $max_value = $self->max_value || $max_value; |
|---|
| 71 | my $chart = Google::Chart->new( |
|---|
| 72 | size => $self->size, |
|---|
| 73 | type => $self->type, |
|---|
| 74 | axis => [ |
|---|
| 75 | { |
|---|
| 76 | location => 'x', |
|---|
| 77 | labels => $axis_x ? $self->_roughen($axis_x, 5) : [], |
|---|
| 78 | }, |
|---|
| 79 | { |
|---|
| 80 | location => 'y', |
|---|
| 81 | labels => [ map { $_ * $max_value } map { 0.2 * $_ } (0 .. 5) ], |
|---|
| 82 | } |
|---|
| 83 | ], |
|---|
| 84 | data => { |
|---|
| 85 | module => 'Extended', |
|---|
| 86 | args => { |
|---|
| 87 | dataset => [values %$dataset], |
|---|
| 88 | max_value => $max_value, |
|---|
| 89 | }, |
|---|
| 90 | }, |
|---|
| 91 | color => [@{$self->color}[0 .. (scalar values %$dataset) - 1]], |
|---|
| 92 | legend => [keys %$dataset], |
|---|
| 93 | ); |
|---|
| 94 | return $chart->as_uri; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | sub _roughen { |
|---|
| 98 | my ($self, $arr, $count) = @_; |
|---|
| 99 | |
|---|
| 100 | return $arr if scalar @$arr < ($count - 1); |
|---|
| 101 | my $gap = POSIX::ceil($#{$arr}/ ($count - 1)); |
|---|
| 102 | my $ret = []; |
|---|
| 103 | for my $i (0 .. $#{$arr}) { |
|---|
| 104 | push @$ret, ($i % $#{$arr} && $i % $gap) ? '' : $arr->[$i]; |
|---|
| 105 | } |
|---|
| 106 | return $ret; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | 1; |
|---|
| 110 | __END__ |
|---|
| 111 | |
|---|
| 112 | =head1 NAME |
|---|
| 113 | |
|---|
| 114 | Google::Chart::DBIC - glue class for Google::Chart and DIBC |
|---|
| 115 | |
|---|
| 116 | =head1 SYNOPSIS |
|---|
| 117 | |
|---|
| 118 | use TestApp::Schema; |
|---|
| 119 | use Google::Chart::DBIC; |
|---|
| 120 | |
|---|
| 121 | my $schema = TestApp::Schema->connect( |
|---|
| 122 | "dbi:SQLite:$dbfile" |
|---|
| 123 | ); |
|---|
| 124 | my @list = TestApp::Schema->resultset('Climate')->search({ |
|---|
| 125 | place => 'Tokyo', |
|---|
| 126 | }{ |
|---|
| 127 | select => ['high','low','month'], |
|---|
| 128 | as => ['high','low','axis_x'], |
|---|
| 129 | }); |
|---|
| 130 | my $chart = Google::Chart::DBIC->new({ |
|---|
| 131 | resultset => @list, |
|---|
| 132 | size => '300x400', |
|---|
| 133 | type => 'line', |
|---|
| 134 | }); |
|---|
| 135 | my $uri = $chart->as_uri; |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | =head1 DESCRIPTION |
|---|
| 139 | |
|---|
| 140 | Google::Chart::DBIC is glue class for Google::Chart and DBIC. |
|---|
| 141 | |
|---|
| 142 | =head1 METHODS |
|---|
| 143 | |
|---|
| 144 | =head2 new(%args) |
|---|
| 145 | |
|---|
| 146 | Constructor. |
|---|
| 147 | |
|---|
| 148 | =over 4 |
|---|
| 149 | |
|---|
| 150 | =item type |
|---|
| 151 | |
|---|
| 152 | will be passed to Google::Chart constructor. |
|---|
| 153 | |
|---|
| 154 | =item size |
|---|
| 155 | |
|---|
| 156 | will be passed to Google::Chart constructor. |
|---|
| 157 | |
|---|
| 158 | =item resultset |
|---|
| 159 | |
|---|
| 160 | 'looks_like_number' value in the resultset will be plotted on the chart. |
|---|
| 161 | If there is a column named 'axis_x', it will be assigned as bottom axis of the chart. |
|---|
| 162 | |
|---|
| 163 | =item max_value |
|---|
| 164 | |
|---|
| 165 | specify max_value of the chart. defaults max value of the resultset. |
|---|
| 166 | |
|---|
| 167 | =item color |
|---|
| 168 | |
|---|
| 169 | specify color if you don't like default colors. |
|---|
| 170 | |
|---|
| 171 | =item axis_x |
|---|
| 172 | |
|---|
| 173 | specify column name to use as bottom axis of the chart. |
|---|
| 174 | |
|---|
| 175 | =back |
|---|
| 176 | |
|---|
| 177 | =head2 as_uri |
|---|
| 178 | |
|---|
| 179 | Returns google chart uri. |
|---|
| 180 | |
|---|
| 181 | =head1 AUTHOR |
|---|
| 182 | |
|---|
| 183 | Author E<lt>nobuo.danjou@gmail.comE<gt> |
|---|
| 184 | |
|---|
| 185 | This library is free software; you can redistribute it and/or modify |
|---|
| 186 | it under the same terms as Perl itself. |
|---|
| 187 | |
|---|
| 188 | =head1 SEE ALSO |
|---|
| 189 | |
|---|
| 190 | L<Google::Chart> |
|---|
| 191 | |
|---|
| 192 | =cut |
|---|