| 1 | # Copyright 2008, 2009 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::Series::AddSub; | ||
| 18 | use 5.008; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Carp; | ||
| 22 | use List::Util qw(min max); | ||
| 23 | |||
| 24 | use App::Chart::Database; | ||
| 25 | use App::Chart::TZ; | ||
| 26 | use base 'App::Chart::Series'; | ||
| 27 | |||
| 28 | use constant DEBUG => 0; | ||
| 29 | |||
| 30 | sub new { | ||
| 31 | if (DEBUG) { require Data::Dumper; | ||
| 32 | print "AddSub new ",Data::Dumper::Dumper(\@_); } | ||
| 33 | my ($class, $x, $y, @more) = @_; | ||
| 34 | $y->isa('App::Chart::Series') | ||
| 35 | or croak 'Can only add two App::Chart::Series objects'; | ||
| 36 | |||
| 37 | my $x_timebase = $x->timebase; | ||
| 38 | my $y_timebase = $y->timebase; | ||
| 39 | if (ref $x_timebase ne ref $y_timebase) { | ||
| 40 | croak 'Can only add Series with same timebase scale'; | ||
| 41 | } | ||
| 42 | |||
| 43 | my $timebase; | ||
| 44 | if ($x_timebase->to_iso(0) ge $y_timebase->to_iso(0)) { | ||
| 45 | $timebase = $x_timebase; | ||
| 46 | } else { | ||
| 47 | $timebase = $y_timebase; | ||
| 48 | } | ||
| 49 | |||
| 50 | my $x_offset = $timebase->convert_from_floor ($x_timebase, 0); | ||
| 51 | my $y_offset = $timebase->convert_from_floor ($y_timebase, 0); | ||
| 52 | |||
| 53 | my $hi = min ($timebase->convert_from_floor ($x_timebase, $x->hi), | ||
| 54 | $timebase->convert_from_floor ($y_timebase, $y->hi)); | ||
| 55 | |||
| 56 | return $class->SUPER::new | ||
| 57 | (timebase => $timebase, | ||
| 58 | hi => $hi, | ||
| 59 | parent => $x, | ||
| 60 | parent2 => $y, | ||
| 61 | x_offset => $x_offset, | ||
| 62 | y_offset => $y_offset, | ||
| 63 | arrays => { map {; $_ => [] } keys %{$x->{'arrays'}} }, | ||
| 64 | @more); | ||
| 65 | } | ||
| 66 | |||
| 67 | sub fill_part { | ||
| 68 | my ($self, $lo, $hi) = @_; | ||
| 69 | |||
| 70 | my $xs = $self->{'parent'}; | ||
| 71 | my $ys = $self->{'parent2'}; | ||
| 72 | my $negate = $self->{'negate'}; | ||
| 73 | my $x_offset = $self->{'x_offset'}; | ||
| 74 | my $y_offset = $self->{'y_offset'}; | ||
| 75 | |||
| 76 | $xs->fill ($lo - $x_offset, $hi - $x_offset); | ||
| 77 | $ys->fill ($lo - $y_offset, $hi - $y_offset); | ||
| 78 | |||
| 79 | my $arrays = $self->{'arrays'}; | ||
| 80 | while (my ($aname, $sa) = each %$arrays) { | ||
| 81 | my $xa = $xs->array($aname); | ||
| 82 | my $ya = $ys->array($aname); | ||
| 83 | |||
| 84 | my $hi = min ($hi, $#$xa - $x_offset, $#$ya - $x_offset); | ||
| 85 | if ($#$sa < $hi) { $#$sa = $hi; } # pre-extend | ||
| 86 | |||
| 87 | foreach my $i ($lo .. $hi) { | ||
| 88 | my $x = $xa->[$i - $x_offset]; | ||
| 89 | if (defined $x) { | ||
| 90 | my $y = $ya->[$i - $y_offset]; | ||
| 91 | if (defined $y) { | ||
| 92 | $sa->[$i] = $x + ($negate ? - $y : $y); | ||
| 93 | if (DEBUG) {print "$i $x ",($negate ? '-' : '+')," $y $sa->[$i]\n";} | ||
| 94 | } | ||
| 95 | } | ||
| 96 | } | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | 1; | ||
| 101 | __END__ |