| 1 | package Chart::Clicker::Renderer::CandleStick; | ||||||||||
| 2 | 1 | $Chart::Clicker::Renderer::CandleStick::VERSION = '2.89'; | |||||||||
| 3 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 1 | extends 'Chart::Clicker::Renderer'; | |||||||||
| 6 | |||||||||||
| 7 | # ABSTRACT: CandleStick renderer | ||||||||||
| 8 | |||||||||||
| 9 | 1 | ▶ | use Graphics::Primitive::Brush; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use Graphics::Primitive::Operation::Fill; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use Graphics::Primitive::Operation::Stroke; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | ▶ | use Graphics::Primitive::Paint::Solid; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 1 | ▶ | use List::Util qw(max min); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | |||||||||||
| 17 | 1 | has 'bar_padding' => ( | |||||||||
| 18 | is => 'rw', | ||||||||||
| 19 | isa => 'Int', | ||||||||||
| 20 | default => 0 | ||||||||||
| 21 | ); | ||||||||||
| 22 | |||||||||||
| 23 | |||||||||||
| 24 | has 'brush' => ( | ||||||||||
| 25 | is => 'rw', | ||||||||||
| 26 | isa => 'Graphics::Primitive::Brush', | ||||||||||
| 27 | 1 | ▶ | default => sub { Graphics::Primitive::Brush->new(width => 2) } | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 1 | ); | |||||||||
| 29 | |||||||||||
| 30 | override('prepare', sub { | ||||||||||
| 31 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||
| 32 | |||||||||||
| 33 | 0 | super; | |||||||||
| 34 | |||||||||||
| 35 | 0 | my $datasets = $self->clicker->get_datasets_for_context($self->context); | |||||||||
| 36 | |||||||||||
| 37 | 0 | $self->{SCOUNT} = 1; | |||||||||
| 38 | 0 | $self->{KEYCOUNT} = 0; | |||||||||
| 39 | 0 | foreach my $ds (@{ $datasets }) { | |||||||||
| 40 | 0 | $self->{SCOUNT} += $ds->count; | |||||||||
| 41 | 0 | ▶ | if($ds->max_key_count > $self->{KEYCOUNT}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 0 | $self->{KEYCOUNT} = $ds->max_key_count; | |||||||||
| 43 | } | ||||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | 0 | return 1; | |||||||||
| 47 | 1 | }); | |||||||||
| 48 | |||||||||||
| 49 | override('finalize', sub { | ||||||||||
| 50 | 0 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||
| 51 | |||||||||||
| 52 | 0 | my $clicker = $self->clicker; | |||||||||
| 53 | |||||||||||
| 54 | 0 | my $width = $self->width; | |||||||||
| 55 | 0 | my $height = $self->height; | |||||||||
| 56 | |||||||||||
| 57 | 0 | my $dses = $clicker->get_datasets_for_context($self->context); | |||||||||
| 58 | |||||||||||
| 59 | 0 | my $padding = $self->bar_padding + $self->brush->width; | |||||||||
| 60 | |||||||||||
| 61 | 0 | my $bwidth = int(($width / $dses->[0]->max_key_count)) - $self->bar_padding; | |||||||||
| 62 | 0 | my $hbwidth = int($bwidth / 2); | |||||||||
| 63 | |||||||||||
| 64 | 0 | my $scounter = $self->{SCOUNT}; | |||||||||
| 65 | 0 | foreach my $ds (@{ $dses }) { | |||||||||
| 66 | 0 | foreach my $series (@{ $ds->series }) { | |||||||||
| 67 | |||||||||||
| 68 | 0 | my $color = $clicker->color_allocator->next; | |||||||||
| 69 | |||||||||||
| 70 | # TODO if undef... | ||||||||||
| 71 | 0 | my $ctx = $clicker->get_context($ds->context); | |||||||||
| 72 | 0 | my $domain = $ctx->domain_axis; | |||||||||
| 73 | 0 | my $range = $ctx->range_axis; | |||||||||
| 74 | |||||||||||
| 75 | 0 | my $ocbwidth = $bwidth - ($bwidth * $domain->fudge_amount); | |||||||||
| 76 | 0 | my $cbwidth = $ocbwidth / $self->{SCOUNT}; | |||||||||
| 77 | 0 | my $hcbwidth = $cbwidth / 2; | |||||||||
| 78 | 0 | my $offset = $bwidth - ($bwidth / $self->{SCOUNT}); | |||||||||
| 79 | |||||||||||
| 80 | 0 | my $min = $range->range->lower; | |||||||||
| 81 | |||||||||||
| 82 | 0 | my @highs = @{ $series->highs }; | |||||||||
| 83 | 0 | my @lows = @{ $series->lows }; | |||||||||
| 84 | 0 | my @opens = @{ $series->opens }; | |||||||||
| 85 | 0 | my @vals = @{ $series->values }; | |||||||||
| 86 | |||||||||||
| 87 | 0 | my @keys = @{ $series->keys }; | |||||||||
| 88 | 0 | for(0..($series->key_count - 1)) { | |||||||||
| 89 | 0 | my $x = $domain->mark($width, $keys[$_]); | |||||||||
| 90 | |||||||||||
| 91 | 0 | $x -= $cbwidth * $scounter; | |||||||||
| 92 | 0 | $x += $offset; | |||||||||
| 93 | |||||||||||
| 94 | 0 | my $openy = $height - $range->mark($height, $opens[$_]); | |||||||||
| 95 | 0 | my $closey = $height - $range->mark($height, $vals[$_]); | |||||||||
| 96 | 0 | my $highy = $height - $range->mark($height, $highs[$_]); | |||||||||
| 97 | 0 | my $lowy = $height - $range->mark($height, $lows[$_]); | |||||||||
| 98 | |||||||||||
| 99 | 0 | my $_height = $closey - $openy; | |||||||||
| 100 | |||||||||||
| 101 | 0 | $self->move_to($x - $hcbwidth, $openy); | |||||||||
| 102 | 0 | $self->rectangle( | |||||||||
| 103 | $cbwidth, $_height | ||||||||||
| 104 | ); | ||||||||||
| 105 | |||||||||||
| 106 | 0 | my $op; | |||||||||
| 107 | 0 | ▶ | if($_height < 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 108 | # We fill the bar if it closed higher | ||||||||||
| 109 | 0 | $op = Graphics::Primitive::Operation::Fill->new( | |||||||||
| 110 | paint => Graphics::Primitive::Paint::Solid->new( | ||||||||||
| 111 | color => $color | ||||||||||
| 112 | ) | ||||||||||
| 113 | ); | ||||||||||
| 114 | } else { | ||||||||||
| 115 | # We stroke the bar if it closed lower | ||||||||||
| 116 | 0 | $op = Graphics::Primitive::Operation::Stroke->new( | |||||||||
| 117 | brush => $self->brush->clone | ||||||||||
| 118 | ); | ||||||||||
| 119 | 0 | $op->brush->color($color); | |||||||||
| 120 | 0 | $op->brush->width(2); | |||||||||
| 121 | } | ||||||||||
| 122 | 0 | $self->do($op); | |||||||||
| 123 | |||||||||||
| 124 | 0 | $self->move_to($x, min($openy, $closey)); | |||||||||
| 125 | 0 | $self->line_to($x, $highy); | |||||||||
| 126 | |||||||||||
| 127 | 0 | $self->move_to($x, max($openy, $closey)); | |||||||||
| 128 | 0 | $self->line_to($x, $lowy); | |||||||||
| 129 | |||||||||||
| 130 | 0 | my $lineop = Graphics::Primitive::Operation::Stroke->new( | |||||||||
| 131 | brush => $self->brush->clone | ||||||||||
| 132 | ); | ||||||||||
| 133 | 0 | $lineop->brush->color($color); | |||||||||
| 134 | |||||||||||
| 135 | 0 | $self->do($lineop); | |||||||||
| 136 | |||||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | 0 | $scounter--; | |||||||||
| 140 | } | ||||||||||
| 141 | } | ||||||||||
| 142 | |||||||||||
| 143 | 0 | return 1; | |||||||||
| 144 | 1 | }); | |||||||||
| 145 | |||||||||||
| 146 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||
| 147 | |||||||||||
| 148 | 1 | ▶ | no Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 149 | |||||||||||
| 150 | 1 | 1; | |||||||||
| 151 | |||||||||||
| 152 | __END__ | ||||||||||
| 153 | |||||||||||
| 154 | =pod | ||||||||||
| 155 | |||||||||||
| 156 | =head1 NAME | ||||||||||
| 157 | |||||||||||
| 158 | Chart::Clicker::Renderer::CandleStick - CandleStick renderer | ||||||||||
| 159 | |||||||||||
| 160 | =head1 VERSION | ||||||||||
| 161 | |||||||||||
| 162 | version 2.89 | ||||||||||
| 163 | |||||||||||
| 164 | =head1 SYNOPSIS | ||||||||||
| 165 | |||||||||||
| 166 | my $br = Chart::Clicker::Renderer::CandleStick->new; | ||||||||||
| 167 | |||||||||||
| 168 | =head1 DESCRIPTION | ||||||||||
| 169 | |||||||||||
| 170 | Chart::Clicker::Renderer::CandleStick renders a dataset as a candlestick style | ||||||||||
| 171 | bar chart. | ||||||||||
| 172 | |||||||||||
| 173 | =for HTML <p><img src="http://gphat.github.com/chart-clicker/static/images/examples/candlestick.png" width="500" height="250" alt="Candlestick Chart" /></p> | ||||||||||
| 174 | |||||||||||
| 175 | =head1 ATTRIBUTES | ||||||||||
| 176 | |||||||||||
| 177 | =head2 bar_padding | ||||||||||
| 178 | |||||||||||
| 179 | How much padding to put around a bar. A padding of 4 will result in 2 pixels | ||||||||||
| 180 | on each side. | ||||||||||
| 181 | |||||||||||
| 182 | =head2 brush | ||||||||||
| 183 | |||||||||||
| 184 | Set/Get the L<brush|Graphics::Primitive::Brush> to use around each bar and on each line. | ||||||||||
| 185 | |||||||||||
| 186 | =head1 AUTHOR | ||||||||||
| 187 | |||||||||||
| 188 | Cory G Watson <gphat@cpan.org> | ||||||||||
| 189 | |||||||||||
| 190 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 191 | |||||||||||
| 192 | This software is copyright (c) 2016 by Cory G Watson. | ||||||||||
| 193 | |||||||||||
| 194 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 195 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 196 | |||||||||||
| 197 | =cut | ||||||||||