| 1 | # Copyright 2006, 2007, 2009, 2011, 2024 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::Stops; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Gtk2; | ||
| 22 | use POSIX (); | ||
| 23 | |||
| 24 | use App::Chart; | ||
| 25 | use App::Chart::Gtk2::GUI; | ||
| 26 | use App::Chart::Series; | ||
| 27 | |||
| 28 | use constant DEBUG => 0; | ||
| 29 | |||
| 30 | sub draw { | ||
| 31 | my ($class, $graph, $series) = @_; | ||
| 32 | if (DEBUG) { print "Stops draw\n"; } | ||
| 33 | |||
| 34 | my $win = $graph->window; | ||
| 35 | my ($win_width, $win_height) = $win->get_size; | ||
| 36 | my $gc = App::Chart::Gtk2::GUI::gc_for_colour ($graph, App::Chart::GREY_COLOUR); | ||
| 37 | my $ret = 0; | ||
| 38 | my $last_y = -1; | ||
| 39 | |||
| 40 | my $x_step = $graph->scale_x_step; | ||
| 41 | my $x_width = $x_step - 1; # full width of column | ||
| 42 | |||
| 43 | my ($lo, $hi) = $graph->draw_t_range; | ||
| 44 | if (DEBUG) { print " draw lo=$lo hi=$hi of series 0 to @{[$series->hi]}\n";} | ||
| 45 | |||
| 46 | $series->fill ($lo, $hi); | ||
| 47 | |||
| 48 | my $scale_y = $graph->scale_y_proc; | ||
| 49 | my $x_lo = $graph->scale_x($lo); | ||
| 50 | |||
| 51 | foreach my $arrayname ($series->array_names) { | ||
| 52 | my $values = $series->array($arrayname); | ||
| 53 | |||
| 54 | my @segments; | ||
| 55 | for (my $t = $lo, my $x = $x_lo; | ||
| 56 | $t <= $hi; | ||
| 57 | $t++, $x += $x_step) { | ||
| 58 | if (defined (my $value = $values->[$t])) { | ||
| 59 | $ret = 1; | ||
| 60 | |||
| 61 | my $y = $scale_y->($value); | ||
| 62 | if (DEBUG >= 2) { print " t=$t value=$value y=$y\n"; } | ||
| 63 | if ($y >= 0 && $y <= $win_height) { | ||
| 64 | if ($y == $last_y) { | ||
| 65 | $segments[-2] = $x + $x_width; # extend last entry | ||
| 66 | } else { | ||
| 67 | push @segments, $x,$y, $x+$x_width,$y; | ||
| 68 | } | ||
| 69 | next; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | # undef or outside window, don't extend segment | ||
| 73 | $last_y = -1; | ||
| 74 | } | ||
| 75 | if (DEBUG) { local $,=' ', print " segments", @segments, "\n"; } | ||
| 76 | if (@segments) { # Gtk2 1.220 doesn't allow no segments | ||
| 77 | $win->draw_segments ($gc, @segments); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | return $ret; | ||
| 81 | } | ||
| 82 | |||
| 83 | |||
| 84 | 1; | ||
| 85 | __END__ |