| 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::AnnLines; | ||
| 18 | use 5.008; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Gtk2 1.190; # auto-release Gtk2::GC | ||
| 22 | use List::MoreUtils; | ||
| 23 | use List::Util qw(min max); | ||
| 24 | |||
| 25 | use App::Chart::Gtk2::Ex::GtkGCBits; | ||
| 26 | |||
| 27 | use base 'App::Chart::Gtk2::Graph::Plugin'; | ||
| 28 | use App::Chart; | ||
| 29 | use App::Chart::Annotation; | ||
| 30 | |||
| 31 | # uncomment this to run the ### lines | ||
| 32 | #use Smart::Comments; | ||
| 33 | |||
| 34 | use constant { ANNLINE_COLOUR => App::Chart::GREY_COLOUR }; | ||
| 35 | |||
| 36 | sub draw { | ||
| 37 | my ($class, $graph, $region, $alines) = @_; | ||
| 38 | |||
| 39 | $alines ||= do { | ||
| 40 | my $series_list = $graph->{'series_list'}; | ||
| 41 | my $series = $series_list->[0] || return; | ||
| 42 | ### Graph-AnnLines: "$series" | ||
| 43 | my $func = $series->can('AnnLines_arrayref') || return; | ||
| 44 | $func->($series); | ||
| 45 | }; | ||
| 46 | ### Graph-AnnLines count: scalar(@$alines) | ||
| 47 | if (! @$alines) { return; } | ||
| 48 | |||
| 49 | my $win = $graph->window; | ||
| 50 | my $scale_x = $graph->scale_x_proc; | ||
| 51 | my $scale_y = $graph->scale_y_proc; | ||
| 52 | |||
| 53 | my $gc = ($graph->{'annline_gc'} ||= do { | ||
| 54 | my ($colour_str, $color_obj) | ||
| 55 | = App::Chart::Gtk2::GUI::color_object ($graph, ANNLINE_COLOUR); | ||
| 56 | my $bg_color = $graph->get_style->bg('normal'); | ||
| 57 | my $xor_color = Gtk2::Gdk::Color->new | ||
| 58 | (0,0,0, $color_obj->pixel ^ $bg_color->pixel); | ||
| 59 | App::Chart::Gtk2::Ex::GtkGCBits->get_for_widget | ||
| 60 | ($graph, { function => 'xor', | ||
| 61 | foreground => $xor_color }); | ||
| 62 | }); | ||
| 63 | |||
| 64 | $gc->set_clip_region ($region); | ||
| 65 | $win->draw_segments | ||
| 66 | ($gc, map { my $x1 = $scale_x->($_->{'t1'}); | ||
| 67 | my $x2 = $scale_x->($_->{'t2'}); | ||
| 68 | my $y1 = $scale_y->($_->{'price1'}); | ||
| 69 | my $y2 = ($_->{'horizontal'} | ||
| 70 | ? $y1 : $scale_y->($_->{'price2'})); | ||
| 71 | |||
| 72 | # if ($region->rect_in (Gtk2::Gdk::Rectangle->new | ||
| 73 | # (min($x1,$x2), abs($x1-$x2), min($y1,$y2), abs($y1-$y2))) eq 'out') { | ||
| 74 | # next; | ||
| 75 | # } | ||
| 76 | ($x1, $y1, $x2, $y2) | ||
| 77 | |||
| 78 | } @$alines); | ||
| 79 | $gc->set_clip_region (undef); | ||
| 80 | } | ||
| 81 | |||
| 82 | sub vrange { | ||
| 83 | my ($class, $graph, $series_list) = @_; | ||
| 84 | my $series = $series_list->[0] || return; | ||
| 85 | $series->can('AnnLines_arrayref') || return; | ||
| 86 | my $aref = $series->AnnLines_arrayref; | ||
| 87 | return map {; ($_->{'price1'}, $_->{'price2'}) } @$aref; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub hrange { | ||
| 91 | my ($class, $graph, $series_list) = @_; | ||
| 92 | my $series = $series_list->[0] || return; | ||
| 93 | $series->can('AnnLines_arrayref') || return; | ||
| 94 | |||
| 95 | my $aref = $series->AnnLines_arrayref; | ||
| 96 | if (! @$aref) { return (); } | ||
| 97 | |||
| 98 | ### Graph-AnnLines hrange: map {; ($_->{'t1'}, $_->{'t2'})} @$aref | ||
| 99 | return List::MoreUtils::minmax (map {; ($_->{'t1'}, $_->{'t2'}) } @$aref); | ||
| 100 | } | ||
| 101 | |||
| 102 | 1; | ||
| 103 | __END__ |