| 1 | package Chart::Clicker::Renderer::PolarArea; | ||
| 2 | $Chart::Clicker::Renderer::PolarArea::VERSION = '2.89'; | ||
| 3 | use Moose; | ||
| 4 | |||
| 5 | extends 'Chart::Clicker::Renderer'; | ||
| 6 | |||
| 7 | # ABSTRACT: Polar Area renderer | ||
| 8 | |||
| 9 | use Graphics::Color::RGB; | ||
| 10 | use Geometry::Primitive::Arc; | ||
| 11 | use Graphics::Primitive::Brush; | ||
| 12 | |||
| 13 | use Scalar::Util qw(refaddr); | ||
| 14 | |||
| 15 | |||
| 16 | has 'border_color' => ( | ||
| 17 | is => 'rw', | ||
| 18 | isa => 'Graphics::Color::RGB', | ||
| 19 | default => sub { Graphics::Color::RGB->new }, | ||
| 20 | ); | ||
| 21 | |||
| 22 | |||
| 23 | has 'brush' => ( | ||
| 24 | is => 'rw', | ||
| 25 | isa => 'Graphics::Primitive::Brush', | ||
| 26 | default => sub { Graphics::Primitive::Brush->new } | ||
| 27 | ); | ||
| 28 | has '_accum' => ( | ||
| 29 | is => 'rw', | ||
| 30 | isa => 'ArrayRef', | ||
| 31 | default => sub { [] } | ||
| 32 | ); | ||
| 33 | has '_colors' => ( | ||
| 34 | is => 'rw', | ||
| 35 | isa => 'ArrayRef', | ||
| 36 | default => sub { [] } | ||
| 37 | ); | ||
| 38 | has '_largest' => ( | ||
| 39 | is => 'rw', | ||
| 40 | isa => 'Num', | ||
| 41 | default => 0 | ||
| 42 | ); | ||
| 43 | |||
| 44 | my $TO_RAD = (4 * atan2(1, 1)) / 180; | ||
| 45 | |||
| 46 | override('prepare', sub { | ||
| 47 | my $self = shift; | ||
| 48 | |||
| 49 | super; | ||
| 50 | |||
| 51 | my $clicker = $self->clicker; | ||
| 52 | |||
| 53 | # TODO | ||
| 54 | # This implementation is dumb... the better way would be: | ||
| 55 | # draw the first slice | ||
| 56 | # remember once of the corner points | ||
| 57 | # when drawing the next slice, start at the remembered point and arc-neg with no radius | ||
| 58 | # then arc pos again | ||
| 59 | # or something like that | ||
| 60 | |||
| 61 | # This is really hinky, basically since figuring out the arcs and whatnot | ||
| 62 | # is a pain in the ass, we employ the painter's algorithm and draw | ||
| 63 | # the last series first, then paint the next one over it. As such, we have | ||
| 64 | # to know the total "accumulated" value for each series' position so that | ||
| 65 | # we can decrement it on each run through the series... this finds the | ||
| 66 | # totals | ||
| 67 | my $dses = $clicker->get_datasets_for_context($self->context); | ||
| 68 | foreach my $ds (@{ $dses }) { | ||
| 69 | |||
| 70 | my $lvs = $ds->largest_value_slice; | ||
| 71 | if($lvs > $self->_largest) { | ||
| 72 | $self->_largest($lvs) | ||
| 73 | } | ||
| 74 | |||
| 75 | my $count = $ds->max_key_count; | ||
| 76 | for my $pos (0..$count - 1) { | ||
| 77 | my $vals = $ds->get_series_values($pos); | ||
| 78 | my $total = 0; | ||
| 79 | foreach my $v (@{ $vals }) { | ||
| 80 | $total += $v; | ||
| 81 | } | ||
| 82 | $self->_accum->[$pos] += $total; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | }); | ||
| 87 | |||
| 88 | override('finalize', sub { | ||
| 89 | my $self = shift; | ||
| 90 | |||
| 91 | my $clicker = $self->clicker; | ||
| 92 | |||
| 93 | my $radius = $self->height; | ||
| 94 | if($self->width < $self->height) { | ||
| 95 | $radius = $self->width; | ||
| 96 | } | ||
| 97 | |||
| 98 | $radius = $radius / 2; | ||
| 99 | |||
| 100 | # Take into account the line around the edge when working out the radius | ||
| 101 | $radius -= $self->brush->width; | ||
| 102 | |||
| 103 | my $height = $self->height; | ||
| 104 | my $linewidth = 1; | ||
| 105 | my $midx = $self->width / 2; | ||
| 106 | my $midy = $height / 2; | ||
| 107 | |||
| 108 | my $per = $radius / $self->_largest; | ||
| 109 | |||
| 110 | my $dses = $clicker->get_datasets_for_context($self->context); | ||
| 111 | |||
| 112 | foreach my $ds (@{ $dses }) { | ||
| 113 | foreach my $series (@{ $ds->series }) { | ||
| 114 | push(@{ $self->_colors }, $clicker->color_allocator->next); | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | # my $dses = $clicker->get_datasets_for_context($self->context); | ||
| 119 | foreach my $ds (reverse @{ $dses }) { | ||
| 120 | |||
| 121 | my $ctx = $clicker->get_context($ds->context); | ||
| 122 | my $domain = $ctx->domain_axis; | ||
| 123 | my $range = $ctx->range_axis; | ||
| 124 | |||
| 125 | foreach my $series (reverse @{ $ds->series }) { | ||
| 126 | |||
| 127 | my $pos = 0; | ||
| 128 | |||
| 129 | my $degs = 360 / scalar(@{ $series->values }); | ||
| 130 | |||
| 131 | my $color = pop(@{ $self->_colors }); | ||
| 132 | |||
| 133 | my $v = 0; | ||
| 134 | foreach my $val (@{ $series->values }) { | ||
| 135 | |||
| 136 | my $foo = $self->_accum->[$v]; | ||
| 137 | |||
| 138 | # Remove this value worth of accumulate from the accumulator | ||
| 139 | # so that our size is properly pushed out, see above comment | ||
| 140 | # in prepare... | ||
| 141 | $self->_accum->[$v] -= $val; | ||
| 142 | |||
| 143 | $self->move_to($midx, $midy); | ||
| 144 | $self->arc(($self->_accum->[$v] + $val) * $per, ($pos - $degs) * $TO_RAD, $pos * $TO_RAD); | ||
| 145 | $self->close_path; | ||
| 146 | |||
| 147 | my $fop = Graphics::Primitive::Operation::Fill->new( | ||
| 148 | preserve => 1, | ||
| 149 | paint => Graphics::Primitive::Paint::Solid->new( | ||
| 150 | color => $color, | ||
| 151 | ) | ||
| 152 | ); | ||
| 153 | $self->do($fop); | ||
| 154 | |||
| 155 | my $op = Graphics::Primitive::Operation::Stroke->new; | ||
| 156 | $op->brush($self->brush->clone); | ||
| 157 | $op->brush->color($self->border_color); | ||
| 158 | $self->do($op); | ||
| 159 | |||
| 160 | $pos += $degs; | ||
| 161 | $v++; | ||
| 162 | } | ||
| 163 | } | ||
| 164 | } | ||
| 165 | |||
| 166 | return 1; | ||
| 167 | }); | ||
| 168 | |||
| 169 | __PACKAGE__->meta->make_immutable; | ||
| 170 | |||
| 171 | no Moose; | ||
| 172 | |||
| 173 | 1; | ||
| 174 | |||
| 175 | __END__ | ||
| 176 | |||
| 177 | =pod | ||
| 178 | |||
| 179 | =head1 NAME | ||
| 180 | |||
| 181 | Chart::Clicker::Renderer::PolarArea - Polar Area renderer | ||
| 182 | |||
| 183 | =head1 VERSION | ||
| 184 | |||
| 185 | version 2.89 | ||
| 186 | |||
| 187 | =head1 SYNOPSIS | ||
| 188 | |||
| 189 | my $par = Chart::Clicker::Renderer::PolarArea->new; | ||
| 190 | # Optionally set the stroke | ||
| 191 | $par->brush->width(2); | ||
| 192 | # and color | ||
| 193 | $par->border_color(Graphics::Color::RGB->new(red => 1, green => 1, blue => 1)); | ||
| 194 | |||
| 195 | =head1 DESCRIPTION | ||
| 196 | |||
| 197 | Chart::Clicker::Renderer::PolarArea renders each series as a slice of a pie. | ||
| 198 | The values of the series determine the radius of each slice, with larger | ||
| 199 | values making the slices longer. The 360 degrees of pie is divided equally. | ||
| 200 | |||
| 201 | =for HTML <p><img src=http://gphat.github.com/chart-clicker/static/images/examples/polararea.png" width="300" height="250" alt="Polar Area Chart" /></p> | ||
| 202 | |||
| 203 | =head1 ATTRIBUTES | ||
| 204 | |||
| 205 | =head2 border_color | ||
| 206 | |||
| 207 | Set/Get the L<color|Graphics::Color::RGB> to use for the border. | ||
| 208 | |||
| 209 | =head2 brush | ||
| 210 | |||
| 211 | Set/Get a L<brush|Graphics::Primitive::Brush> to be used for the polar area's borders, which are around | ||
| 212 | each slice. | ||
| 213 | |||
| 214 | =head1 AUTHOR | ||
| 215 | |||
| 216 | Cory G Watson <gphat@cpan.org> | ||
| 217 | |||
| 218 | =head1 COPYRIGHT AND LICENSE | ||
| 219 | |||
| 220 | This software is copyright (c) 2016 by Cory G Watson. | ||
| 221 | |||
| 222 | This is free software; you can redistribute it and/or modify it under | ||
| 223 | the same terms as the Perl 5 programming language system itself. | ||
| 224 | |||
| 225 | =cut |