| 1 | # Copyright 2007, 2008, 2009, 2010, 2011 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Chart. | ||
| 4 | # | ||
| 5 | # Chart is free software; you can redistribute it and/or modify it under the | ||
| 6 | # terms of the GNU General Public License as published by the Free Software | ||
| 7 | # Foundation; either version 3, or (at your option) any later version. | ||
| 8 | # | ||
| 9 | # Chart is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 10 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 12 | # details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU General Public License along | ||
| 15 | # with Chart. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | |||
| 17 | package App::Chart::Gtk2::LineStyle::Candles; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Gtk2; | ||
| 22 | use List::Util qw(min max); | ||
| 23 | |||
| 24 | use App::Chart; | ||
| 25 | use App::Chart::Gtk2::GUI; | ||
| 26 | use App::Chart::Gtk2::Ex::LineClipper; | ||
| 27 | |||
| 28 | use constant DEBUG => 0; | ||
| 29 | |||
| 30 | sub draw { | ||
| 31 | my ($class, $graph, $series) = @_; | ||
| 32 | |||
| 33 | my $win = $graph->window; | ||
| 34 | my ($win_width, $win_height) = $win->get_size; | ||
| 35 | my $ret = 0; | ||
| 36 | |||
| 37 | my $solid_gc = App::Chart::Gtk2::GUI::gc_for_colour ($graph, undef); | ||
| 38 | my $dash_gc = App::Chart::Gtk2::GUI::gc_for_colour_dashed ($graph, undef); | ||
| 39 | my $up_gc = App::Chart::Gtk2::GUI::gc_for_colour($graph,App::Chart::UP_COLOUR); | ||
| 40 | my $down_gc = App::Chart::Gtk2::GUI::gc_for_colour($graph,App::Chart::DOWN_COLOUR); | ||
| 41 | |||
| 42 | my $x_step = $graph->scale_x_step; | ||
| 43 | |||
| 44 | # body is 3/4 of step and shadow 1/4 | ||
| 45 | # shadow at least 1 pixel, and body try to be 3 times shadow | ||
| 46 | my $shadow_width = max(1, int ($x_step / 4)); | ||
| 47 | my $body_width = min ($x_step, $shadow_width * 3); | ||
| 48 | |||
| 49 | # centred in step, and on the left if odd number of pixels | ||
| 50 | my $body_offset = int (($x_step - $body_width) / 2); | ||
| 51 | # centred in body, and on the left if odd number of pixels | ||
| 52 | my $shadow_offset = $body_offset + int (($body_width - $shadow_width) / 2); | ||
| 53 | |||
| 54 | # line segments centred in step | ||
| 55 | my $line_offset = int ($x_step / 2); | ||
| 56 | |||
| 57 | if (DEBUG) { | ||
| 58 | print "shadow $shadow_width at $shadow_offset\n"; | ||
| 59 | print "body $body_width at $body_offset\n"; | ||
| 60 | } | ||
| 61 | |||
| 62 | my ($lo, $hi) = $graph->draw_t_range; | ||
| 63 | my $scale_y = $graph->scale_y_proc; | ||
| 64 | if (DEBUG) { print "candles: draw $lo to $hi of series 0 to ", | ||
| 65 | $series->hi, "\n"; } | ||
| 66 | |||
| 67 | $series->fill ($lo, $hi); | ||
| 68 | my $opens = $series->array('opens') // []; | ||
| 69 | my $highs = $series->array('highs') // []; | ||
| 70 | my $lows = $series->array('lows') // []; | ||
| 71 | my $closes = $series->array('closes') // $series->values_array; | ||
| 72 | |||
| 73 | # go to the next value before and after the visible window in case we're | ||
| 74 | # showing just lines there, to get going away off the window | ||
| 75 | $lo = $series->find_before ($lo, 1); | ||
| 76 | $hi = $series->find_after ($hi, 1); | ||
| 77 | |||
| 78 | if (DEBUG) { print " extra at ends is $lo to $hi ", | ||
| 79 | $series->timebase->to_iso($lo)," to ", | ||
| 80 | $series->timebase->to_iso($hi),"\n"; } | ||
| 81 | |||
| 82 | my $x = $graph->scale_x ($lo); | ||
| 83 | my $draw = App::Chart::Gtk2::Ex::LineClipper->new (drawable => $win, | ||
| 84 | draw_point => 1); | ||
| 85 | my $prev; | ||
| 86 | my $close; | ||
| 87 | for (my $t = $lo; $t <= $hi; $t++, $x += $x_step, $prev = $close) { | ||
| 88 | $close = $closes->[$t] // next; | ||
| 89 | $ret = 1; | ||
| 90 | my $open = $opens->[$t]; | ||
| 91 | my $high = $highs->[$t]; | ||
| 92 | my $low = $lows->[$t]; | ||
| 93 | |||
| 94 | my $y_close = $scale_y->($close); | ||
| 95 | if (! defined $open || ! defined $high || ! defined $low) { | ||
| 96 | $draw->add (defined $prev ? $solid_gc : $dash_gc, | ||
| 97 | $x + $line_offset, $y_close); | ||
| 98 | } else { | ||
| 99 | $draw->end; | ||
| 100 | |||
| 101 | my $y_open = $scale_y->($open); | ||
| 102 | my $y_high = $scale_y->($high); | ||
| 103 | my $y_low = $scale_y->($low); | ||
| 104 | |||
| 105 | my $gc = ($close > $open ? $up_gc | ||
| 106 | : $close == $open ? $solid_gc | ||
| 107 | : $down_gc); | ||
| 108 | |||
| 109 | # high/low should be the right way around from the database, but swap | ||
| 110 | # if necessary to ensure we don't pass a bad height to the server | ||
| 111 | if ($y_low > $y_high) { | ||
| 112 | my $tmp = $y_low; $y_low = $y_high; $y_high = $tmp; | ||
| 113 | } | ||
| 114 | |||
| 115 | # swap to make the "open" the low Y | ||
| 116 | if ($y_open > $y_close) { | ||
| 117 | my $tmp = $y_open; $y_open = $y_close; $y_close = $tmp; | ||
| 118 | } | ||
| 119 | |||
| 120 | if ($y_high >= 0 && $y_low < $win_height) { | ||
| 121 | $y_low = max ($y_low, 0); | ||
| 122 | $y_high = min ($y_high, $win_height); | ||
| 123 | $win->draw_rectangle ($gc, 1, | ||
| 124 | $x + $shadow_offset, $y_low, | ||
| 125 | $shadow_width, $y_high - $y_low + 1); | ||
| 126 | } | ||
| 127 | |||
| 128 | if ($y_close >= 0 && $y_open < $win_height) { | ||
| 129 | $y_open = max ($y_open, 0); | ||
| 130 | $y_close = min ($y_close, $win_height); | ||
| 131 | $win->draw_rectangle ($gc, 1, | ||
| 132 | $x + $body_offset, $y_open, | ||
| 133 | $body_width, $y_close - $y_open + 1); | ||
| 134 | } | ||
| 135 | |||
| 136 | } | ||
| 137 | } | ||
| 138 | return $ret; | ||
| 139 | } | ||
| 140 | |||
| 141 | |||
| 142 | 1; | ||
| 143 | __END__ |