| 1 | |||
| 2 | package Google::Chart::WithAxis; | ||
| 3 | use Moose::Role; | ||
| 4 | use Google::Chart::Types; | ||
| 5 | use namespace::clean -except => qw(meta); | ||
| 6 | |||
| 7 | has axes => ( | ||
| 8 | traits => ['Array'], | ||
| 9 | is => 'ro', | ||
| 10 | isa => 'ArrayRef[Google::Chart::Axis]', | ||
| 11 | required => 1, | ||
| 12 | lazy_build => 1, | ||
| 13 | ); | ||
| 14 | |||
| 15 | sub _build_axes { [] } | ||
| 16 | |||
| 17 | sub add_axis { | ||
| 18 | my $self = shift; | ||
| 19 | push @{$self->axes}, Google::Chart::Axis->new(@_); | ||
| 20 | } | ||
| 21 | |||
| 22 | around prepare_query => sub { | ||
| 23 | my ($next, $self, @args) = @_; | ||
| 24 | my @query = $next->($self, @args); | ||
| 25 | |||
| 26 | my $count = 0; | ||
| 27 | my %query = ( | ||
| 28 | chxt => [], | ||
| 29 | chxl => [], | ||
| 30 | chxp => [], | ||
| 31 | chxr => [], | ||
| 32 | chxs => [], | ||
| 33 | ); | ||
| 34 | foreach my $axis (@{ $self->axes }) { | ||
| 35 | push @{$query{chxt}}, $axis->location; | ||
| 36 | if (my @labels = $axis->labels) { | ||
| 37 | push @{$query{chxl}}, join('|', "$count:", map { defined $_ ? $_ : '' } @labels); | ||
| 38 | } | ||
| 39 | if (my @label_positions = $axis->label_positions) { | ||
| 40 | push @{$query{chxp}}, join(',', $count, @label_positions); | ||
| 41 | } | ||
| 42 | if (my @range = $axis->range) { | ||
| 43 | push @{$query{chxr}}, join(',', $count, @range); | ||
| 44 | } | ||
| 45 | if ($axis->has_color || $axis->has_font_size || $axis->has_alignment) { | ||
| 46 | my @args = ( $axis->color ); | ||
| 47 | push @args, $axis->font_size if $axis->font_size; | ||
| 48 | push @args, $axis->alignment if $axis->alignment; | ||
| 49 | push @{$query{chxs}}, join(',', $count, @args); | ||
| 50 | } | ||
| 51 | $count++; | ||
| 52 | } | ||
| 53 | |||
| 54 | foreach my $comp (qw( chxt )) { | ||
| 55 | my $list = delete $query{ $comp }; | ||
| 56 | if (scalar @$list > 0) { | ||
| 57 | $query{ $comp } = join(',', @$list); | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | foreach my $comp (qw( chxl chxp chxr chxs )) { | ||
| 62 | my $list = delete $query{ $comp }; | ||
| 63 | if (scalar @$list > 0) { | ||
| 64 | $query{ $comp } = join('|', @$list); | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | push @query, %query; | ||
| 69 | return @query; | ||
| 70 | }; | ||
| 71 | |||
| 72 | package # hide from PAUSE | ||
| 73 | Google::Chart::Axis; | ||
| 74 | use Moose; | ||
| 75 | use Moose::Util::TypeConstraints; | ||
| 76 | use namespace::clean -except => qw(meta); | ||
| 77 | |||
| 78 | has location => ( | ||
| 79 | is => 'ro', | ||
| 80 | isa => enum([ qw( x y r t ) ] ), | ||
| 81 | required => 1, | ||
| 82 | ); | ||
| 83 | |||
| 84 | has labels => ( | ||
| 85 | is => 'ro', | ||
| 86 | isa => 'ArrayRef[Str|Undef]', | ||
| 87 | auto_deref => 1, | ||
| 88 | ); | ||
| 89 | |||
| 90 | has label_positions => ( | ||
| 91 | is => 'ro', | ||
| 92 | isa => 'ArrayRef[Num]', | ||
| 93 | auto_deref => 1, | ||
| 94 | ); | ||
| 95 | |||
| 96 | has range => ( | ||
| 97 | is => 'ro', | ||
| 98 | isa => 'ArrayRef[Num]', # XXX should validate @range == 2 | ||
| 99 | auto_deref => 1, | ||
| 100 | ); | ||
| 101 | |||
| 102 | has color => ( | ||
| 103 | is => 'ro', | ||
| 104 | isa => 'Str', | ||
| 105 | predicate => 'has_color', | ||
| 106 | ); | ||
| 107 | |||
| 108 | has font_size => ( | ||
| 109 | is => 'ro', | ||
| 110 | isa => 'Num', | ||
| 111 | predicate => 'has_font_size', | ||
| 112 | ); | ||
| 113 | |||
| 114 | has alignment => ( | ||
| 115 | is => 'ro', | ||
| 116 | isa => enum([ qw(-1 0 1) ] ), | ||
| 117 | predicate => 'has_alignment', | ||
| 118 | ); | ||
| 119 | |||
| 120 | __PACKAGE__->meta->make_immutable; | ||
| 121 | |||
| 122 | 1; | ||
| 123 | |||
| 124 | __END__ |