| 1 | package Chart::Clicker::Renderer::Bar; | ||||||||||||||||||||||||||||
| 2 | 2 | $Chart::Clicker::Renderer::Bar::VERSION = '2.89'; | |||||||||||||||||||||||||||
| 3 | 2 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | extends 'Chart::Clicker::Renderer'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | # ABSTRACT: Bar renderer | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Graphics::Primitive::Brush; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Graphics::Primitive::Operation::Fill; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Graphics::Primitive::Operation::Stroke; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Graphics::Primitive::Paint::Solid; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | has 'bar_padding' => ( | |||||||||||||||||||||||||||
| 17 | is => 'rw', | ||||||||||||||||||||||||||||
| 18 | isa => 'Int', | ||||||||||||||||||||||||||||
| 19 | default => 0 | ||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 2 | has 'bar_width' => ( | |||||||||||||||||||||||||||
| 24 | is => 'rw', | ||||||||||||||||||||||||||||
| 25 | isa => 'Num', | ||||||||||||||||||||||||||||
| 26 | predicate => 'has_bar_width' | ||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | has 'brush' => ( | ||||||||||||||||||||||||||||
| 31 | is => 'rw', | ||||||||||||||||||||||||||||
| 32 | isa => 'Graphics::Primitive::Brush', | ||||||||||||||||||||||||||||
| 33 | 3 | ▶ | default => sub { Graphics::Primitive::Brush->new } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 2 | ); | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 2 | has 'opacity' => ( | |||||||||||||||||||||||||||
| 38 | is => 'rw', | ||||||||||||||||||||||||||||
| 39 | isa => 'Num', | ||||||||||||||||||||||||||||
| 40 | default => 1 | ||||||||||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | override('prepare', sub { | ||||||||||||||||||||||||||||
| 44 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | super; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | my $datasets = $self->clicker->get_datasets_for_context($self->context); | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | $self->{KEYCOUNT} = 0; | |||||||||||||||||||||||||||
| 51 | 0 | foreach my $ds (@{ $datasets }) { | |||||||||||||||||||||||||||
| 52 | 0 | $self->{SCOUNT} += $ds->count; | |||||||||||||||||||||||||||
| 53 | 0 | ▶ | if($ds->max_key_count > $self->{KEYCOUNT}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | $self->{KEYCOUNT} = $ds->max_key_count; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | return 1; | |||||||||||||||||||||||||||
| 59 | 2 | }); | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | override('finalize', sub { | ||||||||||||||||||||||||||||
| 62 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=15 SCAR=54.8
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | my $clicker = $self->clicker; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | my $height = $self->height; | |||||||||||||||||||||||||||
| 67 | 0 | my $width = $self->width; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | my $dses = $clicker->get_datasets_for_context($self->context); | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | my $brwidth = $self->brush->width; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | my $padding = $self->bar_padding + $brwidth * 2; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | my $offset = 1; | |||||||||||||||||||||||||||
| 76 | 0 | foreach my $ds (@{ $dses }) { | |||||||||||||||||||||||||||
| 77 | 0 | foreach my $series (@{ $ds->series }) { | |||||||||||||||||||||||||||
| 78 | # TODO if undef... | ||||||||||||||||||||||||||||
| 79 | 0 | my $ctx = $clicker->get_context($ds->context); | |||||||||||||||||||||||||||
| 80 | 0 | my $domain = $ctx->domain_axis; | |||||||||||||||||||||||||||
| 81 | 0 | my $range = $ctx->range_axis; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | my $owidth = $width - ($width * $domain->fudge_amount); | |||||||||||||||||||||||||||
| 84 | 0 | my $bwidth; | |||||||||||||||||||||||||||
| 85 | 0 | ▶ | if($self->has_bar_width) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 0 | $bwidth = $self->bar_width; | |||||||||||||||||||||||||||
| 87 | } else { | ||||||||||||||||||||||||||||
| 88 | 0 | $bwidth = int(($owidth / $self->{KEYCOUNT})) - $padding; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 0 | my $hbwidth = int($bwidth / 2); | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # Fudge amounts mess up the calculation of bar widths, so | ||||||||||||||||||||||||||||
| 93 | # we compensate for them here. | ||||||||||||||||||||||||||||
| 94 | 0 | my $cbwidth = $bwidth / $self->{SCOUNT}; | |||||||||||||||||||||||||||
| 95 | 0 | my $chbwidth = int($cbwidth / 2); | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | my $color = $clicker->color_allocator->next; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | my $base = $range->baseline; | |||||||||||||||||||||||||||
| 100 | 0 | my $basey; | |||||||||||||||||||||||||||
| 101 | 0 | ▶ | if(defined($base)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | $basey = $height - $range->mark($height, $base); | |||||||||||||||||||||||||||
| 103 | } else { | ||||||||||||||||||||||||||||
| 104 | 0 | $basey = $height; | |||||||||||||||||||||||||||
| 105 | 0 | $base = $range->range->lower; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | my @vals = @{ $series->values }; | |||||||||||||||||||||||||||
| 109 | 0 | my @keys = @{ $series->keys }; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | my $sksent = $series->key_count; | |||||||||||||||||||||||||||
| 112 | 0 | for(0..($sksent - 1)) { | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # Skip drawing anything if the value is equal to the baseline | ||||||||||||||||||||||||||||
| 115 | 0 | ▶ | next if $vals[$_] == $range->baseline; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | my $x = $domain->mark($width, $keys[$_]); | |||||||||||||||||||||||||||
| 118 | 0 | my $y = $range->mark($height, $vals[$_]); | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | if($vals[$_] >= $base) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | if($self->{SCOUNT} == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | $self->move_to($x + $chbwidth, $basey); | |||||||||||||||||||||||||||
| 123 | 0 | $self->rectangle( | |||||||||||||||||||||||||||
| 124 | -int($cbwidth), -int($y - ($height - $basey)) | ||||||||||||||||||||||||||||
| 125 | ); | ||||||||||||||||||||||||||||
| 126 | } else { | ||||||||||||||||||||||||||||
| 127 | 0 | $self->move_to( | |||||||||||||||||||||||||||
| 128 | $x - $hbwidth + ($offset * $cbwidth), $basey | ||||||||||||||||||||||||||||
| 129 | ); | ||||||||||||||||||||||||||||
| 130 | 0 | $self->rectangle( | |||||||||||||||||||||||||||
| 131 | -int($cbwidth) + $brwidth, -int($y - ($height - $basey)) | ||||||||||||||||||||||||||||
| 132 | ); | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | } else { | ||||||||||||||||||||||||||||
| 135 | 0 | ▶ | if($self->{SCOUNT} == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 0 | $self->move_to($x + $chbwidth, $basey); | |||||||||||||||||||||||||||
| 137 | 0 | $self->rectangle( | |||||||||||||||||||||||||||
| 138 | -int($cbwidth), -int($y - ($height - $basey)) | ||||||||||||||||||||||||||||
| 139 | ); | ||||||||||||||||||||||||||||
| 140 | } else { | ||||||||||||||||||||||||||||
| 141 | 0 | $self->move_to( | |||||||||||||||||||||||||||
| 142 | $x - $hbwidth + ($offset * $cbwidth), $basey | ||||||||||||||||||||||||||||
| 143 | ); | ||||||||||||||||||||||||||||
| 144 | 0 | $self->rectangle( | |||||||||||||||||||||||||||
| 145 | -int($cbwidth) + $brwidth, int($height - $basey - $y) | ||||||||||||||||||||||||||||
| 146 | ); | ||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | my $fillop = Graphics::Primitive::Operation::Fill->new( | |||||||||||||||||||||||||||
| 152 | paint => Graphics::Primitive::Paint::Solid->new | ||||||||||||||||||||||||||||
| 153 | ); | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 0 | ▶ | if($self->opacity < 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 0 | my $fillcolor = $color->clone; | |||||||||||||||||||||||||||
| 157 | 0 | $fillcolor->alpha($self->opacity); | |||||||||||||||||||||||||||
| 158 | 0 | $fillop->paint->color($fillcolor); | |||||||||||||||||||||||||||
| 159 | # Since we're going to stroke this, we want to preserve it. | ||||||||||||||||||||||||||||
| 160 | 0 | ▶ | $fillop->preserve(1) if $brwidth; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | } else { | ||||||||||||||||||||||||||||
| 162 | 0 | $fillop->paint->color($color); | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | $self->do($fillop); | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 0 | ▶ | if(($self->opacity < 1) && ($brwidth > 0)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->opacity < 1 and $brwidth > 0
MC/DC0%0/2
$self->opacity < 1 and $brwidth > 0
$self->opacity < 1
$brwidth > 0
Truth table0%0/3
A and B
A= $self->opacity < 1B= $brwidth > 0
| |||||||||||||||||||||||||||||
| 168 | 0 | my $strokeop = Graphics::Primitive::Operation::Stroke->new; | |||||||||||||||||||||||||||
| 169 | 0 | $strokeop->brush($self->brush->clone); | |||||||||||||||||||||||||||
| 170 | 0 | ▶ | unless(defined($self->brush->color)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 0 | $strokeop->brush->color($color); | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | 0 | $self->do($strokeop); | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 0 | $offset++; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 0 | return 1; | |||||||||||||||||||||||||||
| 181 | 2 | }); | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 2 | ▶ | no Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 2 | 1; | |||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | __END__ | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =pod | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =head1 NAME | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | Chart::Clicker::Renderer::Bar - Bar renderer | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =head1 VERSION | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | version 2.89 | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | my $br = Chart::Clicker::Renderer::Bar->new; | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | Chart::Clicker::Renderer::Bar renders a dataset as bars. | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | =head1 NEGATIVE BARS | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | If you'd like to render both "negative and positive" bars, look at | ||||||||||||||||||||||||||||
| 212 | L<Chart::Clicker::Axis>'s C<baseline> attribute. Setting it will result in | ||||||||||||||||||||||||||||
| 213 | something like this: | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | =for HTML <p><img src="http://gphat.github.com/chart-clicker/static/images/examples/bar-baseline.png" width="500" height="250" alt="Base (Baseline) Chart" /></p> | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | =for HTML <p><img src="http://gphat.github.com/chart-clicker/static/images/examples/bar.png" width="500" height="250" alt="Bar Chart" /></p> | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | =head2 bar_padding | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | How much padding to put around a bar. A padding of 4 will result in 2 pixels | ||||||||||||||||||||||||||||
| 222 | on each side. | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =head2 bar_width | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | Allows you to override the calculation that determines the optimal width for | ||||||||||||||||||||||||||||
| 229 | bars. Be careful using this as it can making things look terrible. Note that | ||||||||||||||||||||||||||||
| 230 | this number is divided evenly between all the values in a series when charting | ||||||||||||||||||||||||||||
| 231 | multiple series. | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | =head2 brush | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | Set/Get the L<brush|Graphics::Primitive::Brush> to use around each bar. | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | =head2 opacity | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | Set/Get the alpha value to make each bar more or less opaque. | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | Cory G Watson <gphat@cpan.org> | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | This software is copyright (c) 2016 by Cory G Watson. | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 250 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | =cut | ||||||||||||||||||||||||||||