| 1 | # Copyright 2007, 2008, 2009, 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::HighLow; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Gtk2; | ||
| 22 | use List::Util qw(min max); | ||
| 23 | use Math::Round qw(round); | ||
| 24 | |||
| 25 | use App::Chart; | ||
| 26 | use App::Chart::Series; | ||
| 27 | |||
| 28 | sub draw { | ||
| 29 | my ($class, $graph, $series) = @_; | ||
| 30 | |||
| 31 | my $win = $graph->window; | ||
| 32 | my ($win_width, $win_height) = $win->get_size; | ||
| 33 | my $gc = $graph->style->fg_gc ($graph->state); | ||
| 34 | my $ret = 0; | ||
| 35 | |||
| 36 | my $x_step = $graph->scale_x_step; | ||
| 37 | # bars 2/3 of step and at least 1 pixel | ||
| 38 | my $x_width = max (1, round ($x_step * 0.666)); | ||
| 39 | # centred in step, but preferring the left | ||
| 40 | my $x_offset = int (($x_step - $x_width) / 2); | ||
| 41 | |||
| 42 | my ($t_lo, $t_hi) = $graph->draw_t_range(); | ||
| 43 | my $scale_y = $graph->scale_y_proc(); | ||
| 44 | my $x = $graph->scale_x($t_lo) + $x_offset - $x_step; | ||
| 45 | |||
| 46 | $series->fill ($t_lo, $t_hi); | ||
| 47 | my $values = $series->values_array; | ||
| 48 | my $highs = $series->array('highs') // $values; | ||
| 49 | my $lows = $series->array('lows') // $values; | ||
| 50 | |||
| 51 | foreach my $t ($t_lo .. $t_hi) { | ||
| 52 | $x += $x_step; | ||
| 53 | my $high = $highs->[$t] // $values->[$t]; | ||
| 54 | next if (! defined $high); | ||
| 55 | $ret = 1; | ||
| 56 | my $low = $lows->[$t] // $values->[$t]; | ||
| 57 | |||
| 58 | my $y_high = $scale_y->($high); | ||
| 59 | my $y_low = $scale_y->($low); | ||
| 60 | if ($y_low > $y_high) { my $t = $y_low; $y_low = $y_high; $y_high = $t; } | ||
| 61 | next if ($y_high < 0); | ||
| 62 | next if ($y_low >= $win_height); | ||
| 63 | |||
| 64 | $y_low = max ($y_low, 0); | ||
| 65 | $y_high = min ($y_high, $win_height); | ||
| 66 | |||
| 67 | $win->draw_rectangle ($gc, 1, $x, $y_low, $x_width, $y_high - $y_low + 1); | ||
| 68 | } | ||
| 69 | return $ret; | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | 1; | ||
| 74 | __END__ |