| 1 | # Copyright 2008, 2009, 2010 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::Derived::Mul; | ||
| 18 | use 5.008; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Carp; | ||
| 22 | use List::Util qw(min max); | ||
| 23 | use Scalar::Util; | ||
| 24 | |||
| 25 | use App::Chart::Database; | ||
| 26 | use App::Chart::TZ; | ||
| 27 | use base 'App::Chart::Series'; | ||
| 28 | |||
| 29 | |||
| 30 | use constant | ||
| 31 | { longname => 'Multiply', | ||
| 32 | shortname => 'Mul', | ||
| 33 | type => 'overload', | ||
| 34 | }; | ||
| 35 | |||
| 36 | sub new { | ||
| 37 | my ($class, $parent, @factors) = @_; | ||
| 38 | my $factor = 1; | ||
| 39 | foreach (@factors) { | ||
| 40 | if (ref $_) { | ||
| 41 | croak 'Can only multiply a App::Chart::Series by a constant'; | ||
| 42 | } | ||
| 43 | $factor *= $_; | ||
| 44 | } | ||
| 45 | return $class->SUPER::new | ||
| 46 | (parent => $parent, | ||
| 47 | factor => $factor, | ||
| 48 | arrays => { map {; $_ => [] } keys %{$parent->{'arrays'}} }); | ||
| 49 | } | ||
| 50 | |||
| 51 | sub fill_part { | ||
| 52 | my ($self, $lo, $hi) = @_; | ||
| 53 | my $parent = $self->{'parent'}; | ||
| 54 | my $factor = $self->{'factor'}; | ||
| 55 | |||
| 56 | $parent->fill ($lo, $hi); | ||
| 57 | my $arrays = $self->{'arrays'}; | ||
| 58 | while (my ($aname, $s) = each %$arrays) { | ||
| 59 | my $p = $parent->array($aname); | ||
| 60 | |||
| 61 | my $hi = min ($hi, $#$p); | ||
| 62 | if ($#$s < $hi) { $#$s = $hi; } # pre-extend | ||
| 63 | |||
| 64 | foreach my $i ($lo .. $hi) { | ||
| 65 | if (defined $p->[$i]) { | ||
| 66 | $s->[$i] = $p->[$i] * $factor; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | 1; | ||
| 73 | __END__ |