| 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::Alerts; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Gtk2; | ||
| 22 | use App::Chart::Annotation; | ||
| 23 | use List::Util qw(min max); | ||
| 24 | |||
| 25 | use App::Chart::Gtk2::Ex::GtkGCBits; | ||
| 26 | use base 'App::Chart::Gtk2::Graph::Plugin'; | ||
| 27 | |||
| 28 | # uncomment this to run the ### lines | ||
| 29 | #use Smart::Comments; | ||
| 30 | |||
| 31 | use constant ALERTS_COLOUR => 'red'; | ||
| 32 | |||
| 33 | use constant { ELEM_PRICE => 0, | ||
| 34 | ELEM_ABOVE => 1 }; | ||
| 35 | |||
| 36 | use constant { ALERT_COLOUR => 'red', | ||
| 37 | ALERT_WIDTH => 20, | ||
| 38 | ALERT_HEIGHT => 8 }; | ||
| 39 | |||
| 40 | sub draw_t { | ||
| 41 | my ($graph) = @_; | ||
| 42 | my $series = $graph->get('series_list')->[0] || return undef; | ||
| 43 | my $timebase = $series->timebase; | ||
| 44 | return 1 + min ($timebase->today, $series->hi); | ||
| 45 | } | ||
| 46 | |||
| 47 | sub draw { | ||
| 48 | my ($class, $graph, $region, $alerts) = @_; | ||
| 49 | ### Graph-Alerts draw() | ||
| 50 | |||
| 51 | $alerts ||= do { | ||
| 52 | require App::Chart::Annotation; | ||
| 53 | # FIXME: series autoloads not so good | ||
| 54 | # $series->isa('App::Chart::Series::Database') || return; | ||
| 55 | # $series->Alerts_arrayref; | ||
| 56 | my @alerts = _alerts_for_series_list($graph->{'series_list'}); | ||
| 57 | \@alerts | ||
| 58 | }; | ||
| 59 | if (! @$alerts) { | ||
| 60 | ### none | ||
| 61 | return; | ||
| 62 | } | ||
| 63 | |||
| 64 | my $win = $graph->window; | ||
| 65 | my $scale_y = $graph->scale_y_proc; | ||
| 66 | |||
| 67 | my $gc = ($graph->{'alert_gc'} ||= do { | ||
| 68 | my ($colour_str, $color_obj) | ||
| 69 | = App::Chart::Gtk2::GUI::color_object ($graph, ALERT_COLOUR); | ||
| 70 | my $bg_color = $graph->get_style->bg('normal'); | ||
| 71 | my $xor_color = Gtk2::Gdk::Color->new | ||
| 72 | (0,0,0, $color_obj->pixel ^ $bg_color->pixel); | ||
| 73 | |||
| 74 | App::Chart::Gtk2::Ex::GtkGCBits->get_for_widget ($graph, { function => 'xor', | ||
| 75 | foreground => $xor_color }); | ||
| 76 | }); | ||
| 77 | |||
| 78 | my $t = draw_t ($graph); | ||
| 79 | my $x = $graph->scale_x ($t); | ||
| 80 | |||
| 81 | $gc->set_clip_region ($region); | ||
| 82 | foreach my $elem (@$alerts) { | ||
| 83 | my $y = $scale_y->($elem->{'price'}); | ||
| 84 | my $y_offset = ($elem->{'above'} ? - ALERT_HEIGHT : ALERT_HEIGHT); | ||
| 85 | ### price: $elem->{'price'} | ||
| 86 | ### y: $y | ||
| 87 | $win->draw_lines ($gc, | ||
| 88 | $x - ALERT_WIDTH, $y, | ||
| 89 | $x, $y, | ||
| 90 | $x, $y + $y_offset); | ||
| 91 | } | ||
| 92 | $gc->set_clip_region (undef); | ||
| 93 | } | ||
| 94 | |||
| 95 | sub vrange { | ||
| 96 | my ($class, $graph, $series_list) = @_; | ||
| 97 | ### Graph-Alerts vrange(): "@$series_list" | ||
| 98 | ### values: map {$_->price} _alerts_for_series_list($series_list) | ||
| 99 | return map {$_->price} _alerts_for_series_list($series_list); | ||
| 100 | } | ||
| 101 | |||
| 102 | sub _alerts_for_series_list { | ||
| 103 | my ($series_list) = @_; | ||
| 104 | my @alerts; | ||
| 105 | foreach my $series (@$series_list) { | ||
| 106 | if (my $func = $series->can('Alerts_arrayref')) { | ||
| 107 | push @alerts, @{$series->$func}; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | return @alerts; | ||
| 111 | } | ||
| 112 | |||
| 113 | 1; | ||
| 114 | __END__ |