| 1 | package Chart::OFC2::Extremes; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Chart::OFC2::Extremes - OFC2 axis or chart extremes | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use Chart::OFC2::Extremes; | ||
| 10 | |||
| 11 | has 'extremes' => ( | ||
| 12 | is => 'rw', | ||
| 13 | isa => 'Chart::OFC2::Extremes', | ||
| 14 | default => sub { Chart::OFC2::Extremes->new() }, | ||
| 15 | lazy => 1, | ||
| 16 | ); | ||
| 17 | |||
| 18 | $self->extremes->reset(); | ||
| 19 | |||
| 20 | =head1 DESCRIPTION | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | 0 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 25 | use Moose::Util::TypeConstraints; | ||
| 26 | use MooseX::StrictConstructor; | ||
| 27 | use Carp::Clan 'croak'; | ||
| 28 | |||
| 29 | our $VERSION = '0.07'; | ||
| 30 | |||
| 31 | coerce 'Chart::OFC2::Extremes' | ||
| 32 | => from 'HashRef' | ||
| 33 | => via { Chart::OFC2::Extremes->new($_) }; | ||
| 34 | |||
| 35 | =head1 PROPERTIES | ||
| 36 | |||
| 37 | has 'x_axis_max' => (is => 'rw', isa => 'Num|Undef', ); | ||
| 38 | has 'x_axis_min' => (is => 'rw', isa => 'Num|Undef', ); | ||
| 39 | has 'y_axis_max' => (is => 'rw', isa => 'Num|Undef', ); | ||
| 40 | has 'y_axis_min' => (is => 'rw', isa => 'Num|Undef', ); | ||
| 41 | has 'other' => (is => 'rw', isa => 'Num|Undef', ); | ||
| 42 | |||
| 43 | =cut | ||
| 44 | |||
| 45 | has 'x_axis_max' => (is => 'rw', isa => 'Num|Undef', ); | ||
| 46 | has 'x_axis_min' => (is => 'rw', isa => 'Num|Undef', ); | ||
| 47 | has 'y_axis_max' => (is => 'rw', isa => 'Num|Undef', ); | ||
| 48 | has 'y_axis_min' => (is => 'rw', isa => 'Num|Undef', ); | ||
| 49 | has 'other' => (is => 'rw', isa => 'Num|Undef', ); | ||
| 50 | |||
| 51 | |||
| 52 | =head1 METHODS | ||
| 53 | |||
| 54 | =head2 new() | ||
| 55 | |||
| 56 | Object constructor. | ||
| 57 | |||
| 58 | =head2 reset($axis_type, $values) | ||
| 59 | |||
| 60 | Calculate x or y minimal and maximal values and set (x|y)_axis_(min|max) according. | ||
| 61 | |||
| 62 | =cut | ||
| 63 | |||
| 64 | sub reset { | ||
| 65 | my $self = shift; | ||
| 66 | my $axis_type = shift; | ||
| 67 | my $values = shift; | ||
| 68 | |||
| 69 | croak 'pass axis type (x|y) argument' | ||
| 70 | if (($axis_type ne 'y') and ($axis_type ne 'x')); | ||
| 71 | croak 'pass values argument as array ref' | ||
| 72 | if (ref $values ne 'ARRAY'); | ||
| 73 | |||
| 74 | my $axis_min = $axis_type.'_axis_min'; | ||
| 75 | my $axis_max = $axis_type.'_axis_max'; | ||
| 76 | |||
| 77 | my $max; | ||
| 78 | my $min; | ||
| 79 | my @values_to_check = @{$values}; | ||
| 80 | while (scalar @values_to_check) { | ||
| 81 | my $value = shift @values_to_check; | ||
| 82 | |||
| 83 | next if not defined $value; | ||
| 84 | push @values_to_check, @{$value} | ||
| 85 | if ref $value eq 'ARRAY'; | ||
| 86 | |||
| 87 | next if ref $value ne ''; | ||
| 88 | |||
| 89 | $max = $value | ||
| 90 | if ((not defined $max) or ($value > $max)); | ||
| 91 | $min = $value | ||
| 92 | if ((not defined $min) or ($value < $min)); | ||
| 93 | } | ||
| 94 | |||
| 95 | $self->$axis_min($min) | ||
| 96 | if defined $min; | ||
| 97 | $self->$axis_max($max) | ||
| 98 | if defined $max; | ||
| 99 | } | ||
| 100 | |||
| 101 | |||
| 102 | =head2 TO_JSON() | ||
| 103 | |||
| 104 | Returns HashRef that is possible to give to C<encode_json()> function. | ||
| 105 | |||
| 106 | =cut | ||
| 107 | |||
| 108 | sub TO_JSON { | ||
| 109 | my $self = shift; | ||
| 110 | |||
| 111 | return { | ||
| 112 | 'x_axis_max' => $self->x_axis_max, | ||
| 113 | 'x_axis_min' => $self->x_axis_min, | ||
| 114 | 'y_axis_max' => $self->y_axis_max, | ||
| 115 | 'y_axis_min' => $self->y_axis_min, | ||
| 116 | 'other' => $self->other, | ||
| 117 | }; | ||
| 118 | } | ||
| 119 | |||
| 120 | 1; | ||
| 121 | |||
| 122 | |||
| 123 | __END__ | ||