| 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::OHLC; | ||
| 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 | # FIXME: clip to window height in case wild scaling ... | ||
| 34 | |||
| 35 | my $win = $graph->window; | ||
| 36 | my ($win_width, $win_height) = $win->get_size; | ||
| 37 | my $ret = 0; | ||
| 38 | |||
| 39 | my $solid_gc = App::Chart::Gtk2::GUI::gc_for_colour ($graph, undef); | ||
| 40 | my $dash_gc = App::Chart::Gtk2::GUI::gc_for_colour_dashed ($graph, undef); | ||
| 41 | |||
| 42 | my $x_step = $graph->scale_x_step; | ||
| 43 | |||
| 44 | my $open_offset = 0; | ||
| 45 | my $open_width = POSIX::ceil ($x_step * 0.5); | ||
| 46 | |||
| 47 | my $close_width = $open_width; | ||
| 48 | my $close_offset = $x_step - $close_width; | ||
| 49 | |||
| 50 | my $hl_width = max(1, int ($x_step * 0.25)); | ||
| 51 | my $hl_offset = int (($x_step - $hl_width) / 2); | ||
| 52 | |||
| 53 | # line segments centred in step | ||
| 54 | my $line_offset = int ($x_step / 2); | ||
| 55 | |||
| 56 | # if (DEBUG) { | ||
| 57 | # print "shadow $hl_width at $shadow_offset\n"; | ||
| 58 | # print "body $body_width at $body_offset\n"; | ||
| 59 | # } | ||
| 60 | |||
| 61 | my ($lo, $hi) = $graph->draw_t_range; | ||
| 62 | my $scale_y = $graph->scale_y_proc; | ||
| 63 | if (DEBUG) { print "candles: draw $lo to $hi of series 0 to ", | ||
| 64 | $series->hi, "\n"; } | ||
| 65 | |||
| 66 | $series->fill ($lo, $hi); | ||
| 67 | my $opens = $series->array('opens') // []; | ||
| 68 | my $highs = $series->array('highs') // []; | ||
| 69 | my $lows = $series->array('lows') // []; | ||
| 70 | my $closes = $series->array('closes') // $series->values_array; | ||
| 71 | |||
| 72 | # go to the next value before and after the visible window in case we're | ||
| 73 | # showing just lines there, to get going away off the window | ||
| 74 | $lo = $series->find_before ($lo, 1); | ||
| 75 | $hi = $series->find_after ($hi, 1); | ||
| 76 | |||
| 77 | if (DEBUG) { print " extra at ends is $lo to $hi ", | ||
| 78 | $series->timebase->to_iso($lo)," to ", | ||
| 79 | $series->timebase->to_iso($hi),"\n"; } | ||
| 80 | |||
| 81 | my $x = $graph->scale_x ($lo); | ||
| 82 | my $draw = App::Chart::Gtk2::Ex::LineClipper->new (drawable => $win, | ||
| 83 | draw_point => 1); | ||
| 84 | my $prev; | ||
| 85 | my $close; | ||
| 86 | for (my $t = $lo; $t <= $hi; $t++, $x += $x_step, $prev = $close) { | ||
| 87 | $close = $closes->[$t] // next; | ||
| 88 | $ret = 1; | ||
| 89 | my $open = $opens->[$t]; | ||
| 90 | my $high = $highs->[$t]; | ||
| 91 | my $low = $lows->[$t]; | ||
| 92 | |||
| 93 | my $y_close = $scale_y->($close); | ||
| 94 | if (! defined $open && ! defined $high && ! defined $low) { | ||
| 95 | $draw->add (defined $prev ? $solid_gc : $dash_gc, | ||
| 96 | $x + $line_offset, $y_close); | ||
| 97 | } else { | ||
| 98 | $draw->end; | ||
| 99 | |||
| 100 | if (defined $open) { | ||
| 101 | my $y = $scale_y->($open); | ||
| 102 | $win->draw_rectangle ($solid_gc, 1, | ||
| 103 | $x + $open_offset, $y, | ||
| 104 | $open_width, 1); | ||
| 105 | $ret = 1; | ||
| 106 | } | ||
| 107 | |||
| 108 | if (defined $close) { | ||
| 109 | $win->draw_rectangle ($solid_gc, 1, | ||
| 110 | $x + $close_offset, $y_close, | ||
| 111 | $close_width, 1); | ||
| 112 | $ret = 1; | ||
| 113 | } | ||
| 114 | |||
| 115 | if (defined $high && defined $low) { | ||
| 116 | my $y_high = $scale_y->($high); | ||
| 117 | my $y_low = $scale_y->($low); | ||
| 118 | $win->draw_rectangle ($solid_gc, 1, | ||
| 119 | $x + $hl_offset, min ($y_low, $y_high), | ||
| 120 | $hl_width, abs($y_low - $y_high) + 1); | ||
| 121 | $ret = 1; | ||
| 122 | } elsif (defined $high || defined $low) { | ||
| 123 | my $y = $scale_y->($high // $low); | ||
| 124 | $win->draw_rectangle ($solid_gc, 1, | ||
| 125 | $x + $hl_offset, $y, | ||
| 126 | $hl_width, 1); | ||
| 127 | $ret = 1; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | $prev = $close; | ||
| 131 | } | ||
| 132 | return $ret; | ||
| 133 | } | ||
| 134 | |||
| 135 | 1; | ||
| 136 | __END__ |