| 1 | package Chart::Clicker::Renderer::StackedArea; | ||
| 2 | $Chart::Clicker::Renderer::StackedArea::VERSION = '2.89'; | ||
| 3 | use Moose; | ||
| 4 | |||
| 5 | # ABSTRACT: Stacked Area renderer | ||
| 6 | |||
| 7 | extends 'Chart::Clicker::Renderer::Area'; | ||
| 8 | |||
| 9 | use Graphics::Primitive::Brush; | ||
| 10 | use Graphics::Primitive::Path; | ||
| 11 | use Graphics::Primitive::Operation::Fill; | ||
| 12 | use Graphics::Primitive::Operation::Stroke; | ||
| 13 | use Graphics::Primitive::Paint::Gradient::Linear; | ||
| 14 | use Graphics::Primitive::Paint::Solid; | ||
| 15 | |||
| 16 | |||
| 17 | has '+additive' => ( default => 1 ); | ||
| 18 | |||
| 19 | |||
| 20 | override('finalize', sub { | ||
| 21 | my ($self) = @_; | ||
| 22 | |||
| 23 | my $height = $self->height; | ||
| 24 | my $width = $self->width; | ||
| 25 | my $clicker = $self->clicker; | ||
| 26 | |||
| 27 | my %accum; | ||
| 28 | |||
| 29 | my $dses = $clicker->get_datasets_for_context($self->context); | ||
| 30 | foreach my $ds (@{ $dses }) { | ||
| 31 | foreach my $series (@{ $ds->series }) { | ||
| 32 | |||
| 33 | # TODO if undef... | ||
| 34 | my $ctx = $clicker->get_context($ds->context); | ||
| 35 | my $domain = $ctx->domain_axis; | ||
| 36 | my $range = $ctx->range_axis; | ||
| 37 | |||
| 38 | my $lastx; # used for completing the path | ||
| 39 | my @vals = @{ $series->values }; | ||
| 40 | my @keys = @{ $series->keys }; | ||
| 41 | |||
| 42 | my $startx; | ||
| 43 | |||
| 44 | my $biggest; | ||
| 45 | my @replays; | ||
| 46 | for(0..($series->key_count - 1)) { | ||
| 47 | |||
| 48 | my $key = $keys[$_]; | ||
| 49 | my $x = $domain->mark($width, $key); | ||
| 50 | next unless defined($x); | ||
| 51 | my $val = $vals[$_]; | ||
| 52 | |||
| 53 | if(exists($accum{$key})) { | ||
| 54 | # Store the previous value | ||
| 55 | push(@replays, [ $x, $accum{$key}]); | ||
| 56 | # Add this value to the accumulator, then replace | ||
| 57 | # it's value with the total | ||
| 58 | $val = $accum{$key} += $val; | ||
| 59 | } else { | ||
| 60 | push(@replays, [ $x, 0 ]); | ||
| 61 | $accum{$key} = $val; | ||
| 62 | } | ||
| 63 | |||
| 64 | my $ymark = $range->mark($height, $val); | ||
| 65 | next unless defined($ymark); | ||
| 66 | my $y = $height - $ymark; | ||
| 67 | |||
| 68 | if(defined($biggest)) { | ||
| 69 | $biggest = $y if $y > $biggest; | ||
| 70 | } else { | ||
| 71 | $biggest = $y; | ||
| 72 | } | ||
| 73 | |||
| 74 | if($_ == 0) { | ||
| 75 | $startx = $x; | ||
| 76 | $self->move_to($x, $y); | ||
| 77 | } else { | ||
| 78 | $self->line_to($x, $y); | ||
| 79 | } | ||
| 80 | $lastx = $x; | ||
| 81 | } | ||
| 82 | my $color = $self->clicker->color_allocator->next; | ||
| 83 | |||
| 84 | my $op = Graphics::Primitive::Operation::Stroke->new; | ||
| 85 | $op->preserve(1); | ||
| 86 | $op->brush($self->brush->clone); | ||
| 87 | $op->brush->color($color); | ||
| 88 | |||
| 89 | $self->do($op); | ||
| 90 | |||
| 91 | while(my $pt = pop(@replays)) { | ||
| 92 | $self->line_to($pt->[0], $height - $range->mark($height, $pt->[1])); | ||
| 93 | } | ||
| 94 | $self->close_path; | ||
| 95 | |||
| 96 | my $paint; | ||
| 97 | if($self->opacity) { | ||
| 98 | |||
| 99 | my $clone = $color->clone; | ||
| 100 | $clone->alpha($self->opacity); | ||
| 101 | $paint = Graphics::Primitive::Paint::Solid->new( | ||
| 102 | color => $clone | ||
| 103 | ); | ||
| 104 | } elsif($self->fade) { | ||
| 105 | |||
| 106 | my $clone = $color->clone; | ||
| 107 | $clone->alpha($self->opacity); | ||
| 108 | |||
| 109 | $paint = Graphics::Primitive::Paint::Gradient::Linear->new( | ||
| 110 | line => Geometry::Primitive::Line->new( | ||
| 111 | start => Geometry::Primitive::Point->new(x => 0, y => 0), | ||
| 112 | end => Geometry::Primitive::Point->new(x => 1, y => $biggest), | ||
| 113 | ), | ||
| 114 | style => 'linear' | ||
| 115 | ); | ||
| 116 | $paint->add_stop(1.0, $color); | ||
| 117 | $paint->add_stop(0, $clone); | ||
| 118 | } else { | ||
| 119 | |||
| 120 | $paint = Graphics::Primitive::Paint::Solid->new( | ||
| 121 | color => $color->clone | ||
| 122 | ); | ||
| 123 | } | ||
| 124 | my $fillop = Graphics::Primitive::Operation::Fill->new( | ||
| 125 | paint => $paint | ||
| 126 | ); | ||
| 127 | |||
| 128 | $self->do($fillop); | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | return 1; | ||
| 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::Renderer::StackedArea - Stacked Area renderer | ||
| 148 | |||
| 149 | =head1 VERSION | ||
| 150 | |||
| 151 | version 2.89 | ||
| 152 | |||
| 153 | =head1 SYNOPSIS | ||
| 154 | |||
| 155 | my $ar = Chart::Clicker::Renderer::StackedArea->new({ | ||
| 156 | fade => 1, | ||
| 157 | brush => Graphics::Primitive::Brush->new({ | ||
| 158 | width => 2 | ||
| 159 | }) | ||
| 160 | }); | ||
| 161 | |||
| 162 | =head1 DESCRIPTION | ||
| 163 | |||
| 164 | Chart::Clicker::Renderer::StackedArea renders a dataset as line-like | ||
| 165 | polygons stacked atop one another. | ||
| 166 | |||
| 167 | =head1 NOTES | ||
| 168 | |||
| 169 | Note that series with varying keys (e.g. Series 1 has keys 1, 2, 3 but Series | ||
| 170 | 2 only has 1 and 3) will cause you some problems. Since the keys don't match | ||
| 171 | up, neither will the accumulation of area. This may be addressed in the | ||
| 172 | future. | ||
| 173 | |||
| 174 | =for HTML <p><img src="http://gphat.github.com/chart-clicker/static/images/examples/stacked-area.png" width="500" height="250" alt="Stacked Area Chart" /></p> | ||
| 175 | |||
| 176 | =head1 ATTRIBUTES | ||
| 177 | |||
| 178 | =head2 brush | ||
| 179 | |||
| 180 | Set/Get the brush that informs the line surrounding the area renders | ||
| 181 | individual segments. | ||
| 182 | |||
| 183 | =head2 fade | ||
| 184 | |||
| 185 | Set/Get the fade flag, which turns on or off a gradient in the area renderer. | ||
| 186 | |||
| 187 | =head2 opacity | ||
| 188 | |||
| 189 | Set the alpha value for the renderer, which makes things more or less opaque. | ||
| 190 | |||
| 191 | =head1 AUTHOR | ||
| 192 | |||
| 193 | Cory G Watson <gphat@cpan.org> | ||
| 194 | |||
| 195 | =head1 COPYRIGHT AND LICENSE | ||
| 196 | |||
| 197 | This software is copyright (c) 2016 by Cory G Watson. | ||
| 198 | |||
| 199 | This is free software; you can redistribute it and/or modify it under | ||
| 200 | the same terms as the Perl 5 programming language system itself. | ||
| 201 | |||
| 202 | =cut |