| 1 | |||
| 2 | package Google::Chart::Data; | ||
| 3 | 16 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 16 | ▶ | use Google::Chart::DataSet; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 16 | ▶ | use Google::Chart::Encoding::Extended; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 16 | ▶ | use Google::Chart::Encoding::Simple; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 16 | ▶ | use Google::Chart::Encoding::Text; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 0 | ▶ | use Google::Chart::Types; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | use namespace::clean -except => qw(meta); | ||
| 10 | |||
| 11 | has datasets => ( | ||
| 12 | traits => ['Array'], | ||
| 13 | is => 'ro', | ||
| 14 | isa => 'ArrayRef[Google::Chart::DataSet]', | ||
| 15 | lazy_build => 1, | ||
| 16 | handles => { | ||
| 17 | add_dataset => 'push', | ||
| 18 | dataset_count => 'count', | ||
| 19 | get_dataset_at => 'get', | ||
| 20 | } | ||
| 21 | ); | ||
| 22 | |||
| 23 | has encoding => ( | ||
| 24 | is => 'ro', | ||
| 25 | does => 'Google::Chart::Encoding', | ||
| 26 | lazy_build => 1, | ||
| 27 | writer => 'set_encoding' | ||
| 28 | ); | ||
| 29 | |||
| 30 | sub _build_datasets { [] } | ||
| 31 | sub _build_encoding { | ||
| 32 | my $self = shift; | ||
| 33 | if (! Class::MOP::is_class_loaded('Google::Chart::Encoding::Text')) { | ||
| 34 | Class::MOP::load_class( 'Google::Chart::Encoding::Text' ); | ||
| 35 | } | ||
| 36 | Google::Chart::Encoding::Text->new(); | ||
| 37 | } | ||
| 38 | |||
| 39 | around BUILDARGS => sub { | ||
| 40 | my $next = shift; | ||
| 41 | my $class = shift; | ||
| 42 | |||
| 43 | # A dataset must be an array of arrays or array of values | ||
| 44 | my @dataset; | ||
| 45 | my %args; | ||
| 46 | |||
| 47 | if (@_ == 1 && ref $_[0] eq 'ARRAY') { | ||
| 48 | @dataset = @{$_[0]}; | ||
| 49 | } else { | ||
| 50 | %args = @_; | ||
| 51 | @dataset = @{ delete $args{data} || [] }; | ||
| 52 | } | ||
| 53 | |||
| 54 | if (! ref $dataset[0] ) { | ||
| 55 | @dataset = ([ @dataset]); | ||
| 56 | } | ||
| 57 | |||
| 58 | foreach (@dataset) { | ||
| 59 | if ( ! blessed $_ ) { | ||
| 60 | $_ = Google::Chart::DataSet->new(data => $_); | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | my $args = $class->$next( %args, data => \@dataset ); | ||
| 65 | return $args; | ||
| 66 | }; | ||
| 67 | |||
| 68 | sub prepare_query { | ||
| 69 | my ($self, $chart) = @_; | ||
| 70 | |||
| 71 | # Data delimiter *DIFFERS* between chart types (can you believe it?) | ||
| 72 | my $data; | ||
| 73 | my $encoding = $self->encoding; | ||
| 74 | if ( $encoding->isa('Google::Chart::Encoding::Text') && | ||
| 75 | $chart->isa('Google::Chart::Type::Pie') ) { | ||
| 76 | $data = $encoding->encode( $self->datasets, ',' ); | ||
| 77 | } else { | ||
| 78 | $data = $encoding->encode( $self->datasets ); | ||
| 79 | } | ||
| 80 | |||
| 81 | my @query = (chd => $data); | ||
| 82 | my (@chco, @chdl, @chds); | ||
| 83 | |||
| 84 | my $datasets = $self->datasets; | ||
| 85 | my $max = $self->dataset_count - 1; | ||
| 86 | my $is_text_encoding = $encoding->isa('Google::Chart::Encoding::Text'); | ||
| 87 | for my $i (0..$max) { | ||
| 88 | my $dataset = $datasets->[$i]; | ||
| 89 | if ($dataset->has_legend) { | ||
| 90 | $chdl[$i] = $dataset->legend; | ||
| 91 | } | ||
| 92 | if ($dataset->has_color) { | ||
| 93 | $chco[$i] = $dataset->color; | ||
| 94 | } | ||
| 95 | if ($dataset->has_min_value || $dataset->has_max_value) { | ||
| 96 | $chds[$i] = join(',', $dataset->min_value, $dataset->max_value); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | if (@chds > 0) { | ||
| 100 | push @query, (chds => join(",", map { defined $_ ? $_ : "," } @chds)); | ||
| 101 | } | ||
| 102 | |||
| 103 | if (@chdl) { | ||
| 104 | push @query, (chdl => join('|', map { defined $_ ? $_ : '' } @chdl)); | ||
| 105 | } | ||
| 106 | if (@chco) { | ||
| 107 | push @query, (chco => join(',', map { defined $_ ? $_ : '' } @chco)); | ||
| 108 | } | ||
| 109 | return @query; | ||
| 110 | } | ||
| 111 | |||
| 112 | __PACKAGE__->meta->make_immutable(); | ||
| 113 | |||
| 114 | 1; | ||
| 115 | |||
| 116 | __END__ | ||