| 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::Bars; | ||
| 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 Math::Round qw(round); | ||
| 25 | use POSIX (); | ||
| 26 | |||
| 27 | # uncomment this to run the ### lines | ||
| 28 | #use Smart::Comments '###'; | ||
| 29 | |||
| 30 | use App::Chart::Series; | ||
| 31 | |||
| 32 | use constant WIDTH_FRAC => 0.666; | ||
| 33 | |||
| 34 | sub x_offset_and_width { | ||
| 35 | my ($class, $graph) = @_; | ||
| 36 | ### Bars x_offset_and_width() | ||
| 37 | |||
| 38 | my $x_step = $graph->scale_x_step; | ||
| 39 | ### $x_step | ||
| 40 | |||
| 41 | # bars 2/3 of step and at least 1 pixel | ||
| 42 | # offset rounded down so in 2 pixels the bar is to the left | ||
| 43 | my $x_width = max (1, round ($x_step * WIDTH_FRAC)); | ||
| 44 | return (POSIX::floor (($x_step - $x_width) / 2), | ||
| 45 | $x_width); | ||
| 46 | } | ||
| 47 | |||
| 48 | sub draw { | ||
| 49 | my ($class, $graph, $series) = @_; | ||
| 50 | ### Bars draw() | ||
| 51 | |||
| 52 | my $win = $graph->window; | ||
| 53 | my ($win_width, $win_height) = $win->get_size; | ||
| 54 | my $gc = $graph->style->fg_gc ($graph->state); | ||
| 55 | my $ret = 0; | ||
| 56 | |||
| 57 | my $x_step = $graph->scale_x_step; | ||
| 58 | my ($x_offset, $x_width) = $class->x_offset_and_width ($graph); | ||
| 59 | |||
| 60 | my ($lo, $hi) = $graph->draw_t_range; | ||
| 61 | ### draw: "lo=$lo hi=$hi of series 0 to ".$series->hi | ||
| 62 | |||
| 63 | $series->fill ($lo, $hi); | ||
| 64 | my $values = $series->values_array; | ||
| 65 | |||
| 66 | my $scale_y = $graph->scale_y_proc; | ||
| 67 | my $x = $graph->scale_x($lo) + $x_offset; | ||
| 68 | |||
| 69 | my $y_zero = $scale_y->(0); | ||
| 70 | $y_zero = max (-1, min ($win_height, $y_zero)); | ||
| 71 | ### $y_zero | ||
| 72 | |||
| 73 | for (my $t = $lo; $t <= $hi; $t++, $x += $x_step) { | ||
| 74 | ### values[t]: $values->[$t] | ||
| 75 | my $value = $values->[$t] // next; | ||
| 76 | $ret = 1; | ||
| 77 | |||
| 78 | my $y_value = $scale_y->($value); | ||
| 79 | #### $value | ||
| 80 | #### $y_value | ||
| 81 | |||
| 82 | my ($y_low, $y_high) = List::MoreUtils::minmax ($y_zero, $y_value); | ||
| 83 | #### $y_low | ||
| 84 | #### $y_high | ||
| 85 | next if ($y_high < 0); | ||
| 86 | next if ($y_low >= $win_height); | ||
| 87 | |||
| 88 | $y_low = max ($y_low, 0); | ||
| 89 | $y_high = min ($y_high, $win_height); | ||
| 90 | |||
| 91 | $win->draw_rectangle ($gc, 1, | ||
| 92 | $x, $y_low, | ||
| 93 | $x_width, $y_high - $y_low + 1); | ||
| 94 | } | ||
| 95 | ### end: "$hi x=$x" | ||
| 96 | return $ret; | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | 1; | ||
| 101 | __END__ |