| 1 | package Chart::Clicker::Decoration::OverAxis; | ||
| 2 | $Chart::Clicker::Decoration::OverAxis::VERSION = '2.89'; | ||
| 3 | use Moose; | ||
| 4 | |||
| 5 | extends 'Chart::Clicker::Container'; | ||
| 6 | |||
| 7 | # ABSTRACT: An axis drawn over data | ||
| 8 | |||
| 9 | use Graphics::Color::RGB; | ||
| 10 | use Graphics::Primitive::Operation::Fill; | ||
| 11 | use Graphics::Primitive::Paint::Solid; | ||
| 12 | use Layout::Manager::Flow; | ||
| 13 | |||
| 14 | |||
| 15 | has 'axis_height' => ( | ||
| 16 | is => 'rw', | ||
| 17 | isa => 'Num', | ||
| 18 | default => 20 | ||
| 19 | ); | ||
| 20 | |||
| 21 | |||
| 22 | has '+background_color' => ( | ||
| 23 | # is => 'rw', | ||
| 24 | # isa => 'Graphics::Color::RGB', | ||
| 25 | default => sub { | ||
| 26 | Graphics::Color::RGB->new( | ||
| 27 | red => .18, green => .17, blue => .17, alpha => 1 | ||
| 28 | ) | ||
| 29 | } | ||
| 30 | ); | ||
| 31 | |||
| 32 | |||
| 33 | has 'border_color' => ( | ||
| 34 | is => 'rw', | ||
| 35 | isa => 'Graphics::Color::RGB', | ||
| 36 | default => sub { | ||
| 37 | Graphics::Color::RGB->new( | ||
| 38 | red => 1, green => 1, blue => 1, alpha => 1 | ||
| 39 | ) | ||
| 40 | } | ||
| 41 | ); | ||
| 42 | |||
| 43 | |||
| 44 | has 'border_width' => ( | ||
| 45 | is => 'rw', | ||
| 46 | isa => 'Num', | ||
| 47 | default => 2 | ||
| 48 | ); | ||
| 49 | |||
| 50 | |||
| 51 | has 'context' => ( | ||
| 52 | is => 'rw', | ||
| 53 | isa => 'Str', | ||
| 54 | required => 1 | ||
| 55 | ); | ||
| 56 | |||
| 57 | |||
| 58 | has 'font' => ( | ||
| 59 | is => 'rw', | ||
| 60 | isa => 'Graphics::Primitive::Font', | ||
| 61 | default => sub { Graphics::Primitive::Font->new } | ||
| 62 | ); | ||
| 63 | |||
| 64 | |||
| 65 | has '+layout_manager' => ( | ||
| 66 | default => sub { Layout::Manager::Compass->new } | ||
| 67 | ); | ||
| 68 | |||
| 69 | |||
| 70 | has 'text_color' => ( | ||
| 71 | is => 'rw', | ||
| 72 | isa => 'Graphics::Color::RGB', | ||
| 73 | default => sub { | ||
| 74 | Graphics::Color::RGB->new( | ||
| 75 | red => 1, green => 1, blue => 1, alpha => 1 | ||
| 76 | ) | ||
| 77 | } | ||
| 78 | ); | ||
| 79 | |||
| 80 | override('prepare', sub { | ||
| 81 | my ($self) = @_; | ||
| 82 | |||
| 83 | $self->height($self->axis_height); | ||
| 84 | |||
| 85 | my $ctx = $self->clicker->get_context($self->context); | ||
| 86 | |||
| 87 | my $ticks = $ctx->domain_axis->tick_values; | ||
| 88 | foreach my $tick (@{ $ticks }) { | ||
| 89 | |||
| 90 | my $tb = Graphics::Primitive::TextBox->new( | ||
| 91 | text => $tick, | ||
| 92 | color => $self->text_color, | ||
| 93 | horizontal_alignment => 'center', | ||
| 94 | vertical_alignment => 'center', | ||
| 95 | font => $self->font | ||
| 96 | ); | ||
| 97 | |||
| 98 | $self->add_component($tb, 'w'); | ||
| 99 | } | ||
| 100 | super; | ||
| 101 | }); | ||
| 102 | |||
| 103 | override('finalize', sub { | ||
| 104 | my ($self) = @_; | ||
| 105 | |||
| 106 | my $ctx = $self->clicker->get_context($self->context); | ||
| 107 | my $domain = $ctx->domain_axis; | ||
| 108 | my $range = $ctx->range_axis; | ||
| 109 | |||
| 110 | my $y = $range->mark($self->height, $range->baseline); | ||
| 111 | |||
| 112 | my $axis_y = $y - ($self->axis_height / 2); | ||
| 113 | |||
| 114 | $self->origin->x(0); | ||
| 115 | $self->origin->y($axis_y); | ||
| 116 | $self->border->top->width($self->border_width); | ||
| 117 | $self->border->bottom->width($self->border_width); | ||
| 118 | $self->border->color($self->border_color); | ||
| 119 | $self->minimum_height($self->axis_height); | ||
| 120 | $self->height($self->axis_height); | ||
| 121 | |||
| 122 | my $ticks = $domain->tick_values; | ||
| 123 | for my $i (0 .. $#{ $ticks }) { | ||
| 124 | my $tick = $ticks->[$i]; | ||
| 125 | |||
| 126 | my $comp = $self->get_component($i); | ||
| 127 | $comp->width($self->width / scalar(@{ $ticks })); | ||
| 128 | $comp->origin->x( | ||
| 129 | $domain->mark($self->width, $tick) - $comp->width / 2 | ||
| 130 | ); | ||
| 131 | $comp->height($self->axis_height); | ||
| 132 | } | ||
| 133 | }); | ||
| 134 | |||
| 135 | __PACKAGE__->meta->make_immutable; | ||
| 136 | |||
| 137 | no Moose; | ||
| 138 | |||
| 139 | 1; | ||
| 140 | |||
| 141 | __END__ | ||
| 142 | |||
| 143 | =pod | ||
| 144 | |||
| 145 | =head1 NAME | ||
| 146 | |||
| 147 | Chart::Clicker::Decoration::OverAxis - An axis drawn over data | ||
| 148 | |||
| 149 | =head1 VERSION | ||
| 150 | |||
| 151 | version 2.89 | ||
| 152 | |||
| 153 | =head1 DESCRIPTION | ||
| 154 | |||
| 155 | An axis that is meant to be drawn "over" a chart. You can find an example | ||
| 156 | of an OverAxis at L<http://gphat.github.com/chart-clicker/static/images/examples/overaxis.png>. | ||
| 157 | |||
| 158 | =head1 ATTRIBUTES | ||
| 159 | |||
| 160 | =head2 axis_height | ||
| 161 | |||
| 162 | Set/Get the height of the OverAxis that will be drawn. | ||
| 163 | |||
| 164 | =head2 background_color | ||
| 165 | |||
| 166 | Set/Get the background L<color|Graphics::Color::RGB> for this OverAxis. | ||
| 167 | |||
| 168 | =head2 border_color | ||
| 169 | |||
| 170 | Set/Get the border L<color|Graphics::Color::RGB> for this OverAxis. | ||
| 171 | |||
| 172 | =head2 border_width | ||
| 173 | |||
| 174 | Set/Get the width of the border for this OverAxis | ||
| 175 | |||
| 176 | =head2 context | ||
| 177 | |||
| 178 | Set/Get the context that this OverAxis should use. | ||
| 179 | |||
| 180 | =head2 font | ||
| 181 | |||
| 182 | The L<font|Graphics::Primitive::Font> to use for the OverAxis. | ||
| 183 | |||
| 184 | =head2 layout_manager | ||
| 185 | |||
| 186 | The layout manager to use for this overaxis. Defaults to a | ||
| 187 | L<Layout::Manager::Compass>. | ||
| 188 | |||
| 189 | =head2 text_color | ||
| 190 | |||
| 191 | Set/Get the L<color|Graphics::Color::RGB> of the text labels dawn for the ticks. | ||
| 192 | |||
| 193 | =head1 AUTHOR | ||
| 194 | |||
| 195 | Cory G Watson <gphat@cpan.org> | ||
| 196 | |||
| 197 | =head1 COPYRIGHT AND LICENSE | ||
| 198 | |||
| 199 | This software is copyright (c) 2016 by Cory G Watson. | ||
| 200 | |||
| 201 | This is free software; you can redistribute it and/or modify it under | ||
| 202 | the same terms as the Perl 5 programming language system itself. | ||
| 203 | |||
| 204 | =cut |