| 1 | # Copyright 2007, 2008, 2009, 2010, 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::Line; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Gtk2; | ||
| 22 | use List::Util qw(min max); | ||
| 23 | use POSIX (); | ||
| 24 | |||
| 25 | use App::Chart::Gtk2::GUI; | ||
| 26 | use App::Chart::Gtk2::Ex::LineClipper; | ||
| 27 | |||
| 28 | use constant DEBUG => 0; | ||
| 29 | |||
| 30 | sub draw { | ||
| 31 | my ($class, $graph, $series) = @_; | ||
| 32 | if (DEBUG) { print "LineStyle::Line draw $graph $series\n"; } | ||
| 33 | |||
| 34 | my $win = $graph->window; | ||
| 35 | my $ret = 0; | ||
| 36 | |||
| 37 | my $x_step = $graph->scale_x_step; | ||
| 38 | my $x_offset = POSIX::floor ($x_step / 2); # centred in step | ||
| 39 | |||
| 40 | my ($lo, $hi) = $graph->draw_t_range; | ||
| 41 | $series->fill ($lo, $hi); | ||
| 42 | |||
| 43 | # go to the next value before and after the visible window, so as to show | ||
| 44 | # lines going away off the window (esp if a big gap) | ||
| 45 | $lo = $series->find_before ($lo, 1); | ||
| 46 | $hi = $series->find_after ($hi, 1); | ||
| 47 | |||
| 48 | my $scale_y = $graph->scale_y_proc; | ||
| 49 | my $draw = App::Chart::Gtk2::Ex::LineClipper->new (drawable => $win); | ||
| 50 | my $initial_x = $graph->scale_x($lo) + $x_offset; | ||
| 51 | |||
| 52 | foreach my $arrayname ($series->array_names) { | ||
| 53 | my $values = $series->array($arrayname); | ||
| 54 | |||
| 55 | my $colour_name = ($series->can('line_colours') | ||
| 56 | ? $series->line_colours->{$arrayname} | ||
| 57 | : undef); | ||
| 58 | my ($solid_gc, $dash_gc); | ||
| 59 | if (($colour_name//'') eq 'solid') { # hack for ZigZag | ||
| 60 | $solid_gc = $dash_gc = App::Chart::Gtk2::GUI::gc_for_colour ($graph, undef); | ||
| 61 | } else { | ||
| 62 | $solid_gc = App::Chart::Gtk2::GUI::gc_for_colour ($graph, $colour_name); | ||
| 63 | $dash_gc = App::Chart::Gtk2::GUI::gc_for_colour_dashed ($graph, $colour_name); | ||
| 64 | } | ||
| 65 | |||
| 66 | if (DEBUG) { local $,=' '; | ||
| 67 | print "Line values ", | ||
| 68 | map {$_//'undef'} @{$values}[$lo .. $hi]; | ||
| 69 | print "\n"; } | ||
| 70 | |||
| 71 | my $x = $initial_x; | ||
| 72 | my $value; | ||
| 73 | my $gc = $solid_gc; | ||
| 74 | for (my $t = $lo; $t <= $hi; $t++, $x += $x_step) { | ||
| 75 | $value = $values->[$t]; | ||
| 76 | if (defined $value) { | ||
| 77 | $ret = 1; | ||
| 78 | $draw->add ($gc, $x, $scale_y->($value)); | ||
| 79 | $gc = $solid_gc; | ||
| 80 | } else { | ||
| 81 | $gc = $dash_gc; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | $draw->end; | ||
| 85 | } | ||
| 86 | return $ret; | ||
| 87 | } | ||
| 88 | |||
| 89 | 1; | ||
| 90 | __END__ |