| 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::Graph::Plugin::Latest; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Gtk2; | ||
| 22 | use List::Util qw(min max); | ||
| 23 | use List::MoreUtils; | ||
| 24 | use POSIX (); | ||
| 25 | |||
| 26 | use App::Chart::Gtk2::GUI; | ||
| 27 | use App::Chart::Gtk2::Ex::GtkGCBits; | ||
| 28 | |||
| 29 | use base 'App::Chart::Gtk2::Graph::Plugin'; | ||
| 30 | use App::Chart; | ||
| 31 | |||
| 32 | use constant DEBUG => 0; | ||
| 33 | |||
| 34 | use constant LATEST_COLOUR => 'orange'; | ||
| 35 | |||
| 36 | # FIXME: do this by getting latest from series or some such | ||
| 37 | sub _series_want_type { | ||
| 38 | my ($series) = @_; | ||
| 39 | if ($series->isa('App::Chart::Series::Database') | ||
| 40 | || $series->isa('App::Chart::Series::Derived::Adjust')) { | ||
| 41 | return 'ohlc'; | ||
| 42 | } elsif ($series->isa('App::Chart::Series::Derived::Volume')) { | ||
| 43 | return 'volume'; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | sub draw { | ||
| 48 | my ($class, $graph, $region) = @_; | ||
| 49 | |||
| 50 | my $series_list = $graph->{'series_list'}; | ||
| 51 | my $series = $series_list->[0] || return; | ||
| 52 | my $symbol = $series->symbol || return; | ||
| 53 | my $type = _series_want_type($series) || return; | ||
| 54 | |||
| 55 | my $latest = App::Chart::Latest->get($symbol) || return; | ||
| 56 | my $timebase = $series->timebase; | ||
| 57 | |||
| 58 | my $hi = $series->hi; | ||
| 59 | my $win = $graph->window; | ||
| 60 | my ($win_width, $win_height) = $win->get_size; | ||
| 61 | my $x_step = $graph->scale_x_step; | ||
| 62 | my $scale_y = $graph->scale_y_proc; | ||
| 63 | |||
| 64 | my $gc = ($graph->{'latest_gc'} ||= do { | ||
| 65 | my ($colour_str, $color_obj) | ||
| 66 | = App::Chart::Gtk2::GUI::color_object ($graph, LATEST_COLOUR); | ||
| 67 | my $bg_color = $graph->get_style->bg('normal'); | ||
| 68 | my $xor_color = Gtk2::Gdk::Color->new | ||
| 69 | (0,0,0, $color_obj->pixel ^ $bg_color->pixel); | ||
| 70 | |||
| 71 | App::Chart::Gtk2::Ex::GtkGCBits->get_for_widget ($graph, { function => 'xor', | ||
| 72 | foreground => $xor_color }); | ||
| 73 | }); | ||
| 74 | |||
| 75 | $gc->set_clip_region ($region); | ||
| 76 | |||
| 77 | if ($type eq 'ohlc') { | ||
| 78 | |||
| 79 | if (my $quote_date = $latest->{'quote_date'}) { | ||
| 80 | my $t = $timebase->from_iso_ceil ($quote_date); | ||
| 81 | if ($t > $hi) { | ||
| 82 | my $x = $graph->scale_x ($t); | ||
| 83 | if (DEBUG) { print " quote $t ",$timebase->to_iso($t)," x=$x\n"; } | ||
| 84 | if ($region->rect_in (Gtk2::Gdk::Rectangle->new | ||
| 85 | ($x, 0, 2 * $x_step, $win_height)) | ||
| 86 | ne 'out') { | ||
| 87 | |||
| 88 | my $hl_x = $x + POSIX::floor ($x_step/3); | ||
| 89 | my $hl_width = max (1, POSIX::floor ($x_step/3)); | ||
| 90 | foreach my $p ($latest->{'bid'}, $latest->{'offer'}) { | ||
| 91 | next if (! defined $p); | ||
| 92 | my $y = $scale_y->($p); | ||
| 93 | next if ($y < 0 || $y >= $win_height); | ||
| 94 | $win->draw_rectangle ($gc, 1, | ||
| 95 | $hl_x + $hl_width, $y, | ||
| 96 | POSIX::ceil (1.3*$x_step), 1); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | if (my $last_date = $latest->{'last_date'}) { | ||
| 103 | my $t = $timebase->from_iso_floor ($last_date); | ||
| 104 | if ($t > $hi) { | ||
| 105 | my $x = $graph->scale_x ($t); | ||
| 106 | if (DEBUG) { print " last $t ",$timebase->to_iso($t)," x=$x\n"; } | ||
| 107 | |||
| 108 | my $hl_x = $x + POSIX::floor ($x_step/3); | ||
| 109 | my $hl_width = max (1, POSIX::floor ($x_step/3)); | ||
| 110 | |||
| 111 | if ($region->rect_in (Gtk2::Gdk::Rectangle->new | ||
| 112 | ($x, 0, $x_step, $win_height)) ne 'out') { | ||
| 113 | if (my $p = $latest->{'open'}) { | ||
| 114 | my $y = $scale_y-> ($p); | ||
| 115 | if ($y >= 0 && $y < $win_height) { | ||
| 116 | $win->draw_rectangle ($gc, 1, | ||
| 117 | $x, $y, | ||
| 118 | $hl_x - $x, 1); # to the HL bar | ||
| 119 | } | ||
| 120 | } | ||
| 121 | if (my $ph = $latest->{'high'}) { | ||
| 122 | if (my $pl = $latest->{'low'}) { | ||
| 123 | my $yh = $scale_y-> ($ph); | ||
| 124 | my $yl = $scale_y-> ($pl); | ||
| 125 | if ($yl > $yh) { my $tmp = $yl; $yl = $yh; $yh = $tmp; } | ||
| 126 | if ($yh >= 0 && $yl < $win_height) { | ||
| 127 | $yl = max ($yl, 0); | ||
| 128 | $yh = min ($yh, $win_height); | ||
| 129 | $win->draw_rectangle ($gc, 1, | ||
| 130 | $hl_x, $yl, | ||
| 131 | $hl_width, $yh - $yl + 1); | ||
| 132 | } | ||
| 133 | } | ||
| 134 | } | ||
| 135 | if (my $p = $latest->{'last'}) { | ||
| 136 | my $y = $scale_y-> ($p); | ||
| 137 | if ($y >= 0 && $y < $win_height) { | ||
| 138 | $win->draw_rectangle ($gc, 1, | ||
| 139 | $hl_x + $hl_width, $y, | ||
| 140 | POSIX::ceil ($x_step * 0.7), 1); | ||
| 141 | } | ||
| 142 | } | ||
| 143 | } | ||
| 144 | } | ||
| 145 | } | ||
| 146 | |||
| 147 | } else { # $type eq 'volume' | ||
| 148 | |||
| 149 | if (my $last_date = $latest->{'last_date'}) { | ||
| 150 | my $t = $timebase->from_iso_floor ($last_date); | ||
| 151 | if ($t > $hi) { | ||
| 152 | my $x = $graph->scale_x ($t); | ||
| 153 | if (DEBUG) { print " last volume $t ",$timebase->to_iso($t), | ||
| 154 | " x=$x\n"; } | ||
| 155 | if ($region->rect_in (Gtk2::Gdk::Rectangle->new | ||
| 156 | ($x,0, $x_step,$win_height)) ne 'out') { | ||
| 157 | my $volume = $latest->{'volume'}; | ||
| 158 | if (defined $volume) { | ||
| 159 | my $y_zero = $scale_y->(0); | ||
| 160 | my $y_value = $scale_y->($volume); | ||
| 161 | my ($y_low, $y_high) = List::MoreUtils::minmax ($y_zero, $y_value); | ||
| 162 | if ($y_high >= 0 || $y_low <= $win_height) { | ||
| 163 | |||
| 164 | $y_low = max ($y_low, 0); | ||
| 165 | $y_high = min ($y_high, $win_height); | ||
| 166 | |||
| 167 | require App::Chart::Gtk2::LineStyle::Bars; | ||
| 168 | my ($x_offset, $x_width) | ||
| 169 | = App::Chart::Gtk2::LineStyle::Bars->x_offset_and_width ($graph); | ||
| 170 | |||
| 171 | $win->draw_rectangle ($gc, 1, | ||
| 172 | $x, $y_low, | ||
| 173 | $x_width, $y_high - $y_low + 1); | ||
| 174 | } | ||
| 175 | } | ||
| 176 | } | ||
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | } | ||
| 181 | |||
| 182 | $gc->set_clip_region (undef); | ||
| 183 | } | ||
| 184 | |||
| 185 | sub vrange { | ||
| 186 | my ($class, $graph, $series_list) = @_; | ||
| 187 | my $series = $series_list->[0] || return; | ||
| 188 | my $symbol = $series->symbol || return; | ||
| 189 | my $type = _series_want_type ($series) || return; | ||
| 190 | |||
| 191 | if (DEBUG) { print "Graph Latest '$symbol'\n"; } | ||
| 192 | require App::Chart::Latest; | ||
| 193 | my $latest = App::Chart::Latest->get ($symbol); | ||
| 194 | if ($type eq 'ohlc') { | ||
| 195 | return ($latest->{'bid'}, | ||
| 196 | $latest->{'offer'}, | ||
| 197 | $latest->{'open'}, | ||
| 198 | $latest->{'high'}, | ||
| 199 | $latest->{'low'}, | ||
| 200 | $latest->{'close'}); | ||
| 201 | } else { | ||
| 202 | return $latest->{'volume'}; | ||
| 203 | } | ||
| 204 | } | ||
| 205 | |||
| 206 | sub hrange { | ||
| 207 | my ($class, $graph, $series_list) = @_; | ||
| 208 | my $series = $series_list->[0]; | ||
| 209 | if (! $series) { return; } | ||
| 210 | my $symbol = $series->symbol; | ||
| 211 | if (! $symbol) { return; } | ||
| 212 | require App::Chart::Latest; | ||
| 213 | my $latest = App::Chart::Latest->get ($symbol); | ||
| 214 | if (! $latest) { return; } | ||
| 215 | |||
| 216 | my $timebase = $series->timebase; | ||
| 217 | my $q = $latest->{'quote_date'}; | ||
| 218 | my $l = $latest->{'last_date'}; | ||
| 219 | if (! defined $q && ! defined $l) { return; } | ||
| 220 | |||
| 221 | if (defined $q) { $q = $timebase->from_iso_floor ($q); } | ||
| 222 | if (defined $l) { $l = $timebase->from_iso_floor ($l); } | ||
| 223 | |||
| 224 | if (DEBUG) { print "Latest hrange quote ",$q//'undef', | ||
| 225 | " last ",$l//'undef',"\n"; } | ||
| 226 | return (App::Chart::min_maybe ($q,$l), | ||
| 227 | App::Chart::max_maybe ($q,$l)); | ||
| 228 | } | ||
| 229 | |||
| 230 | 1; | ||
| 231 | __END__ |