Clicker/Renderer/StackedArea.pm untested

filter: Statementstmt 0.0% 0 / 98 Branchbran 0.0% 0 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod n/a totaltotal 0.0% 0 / 122 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Chart::Clicker::Renderer::StackedArea;
2 $Chart::Clicker::Renderer::StackedArea::VERSION = '2.90';
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__