File: Series.pm

package info (click to toggle)
libchart-clicker-perl 2.90-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 696 kB
  • sloc: perl: 4,379; makefile: 2
file content (222 lines) | stat: -rw-r--r-- 4,315 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package Chart::Clicker::Data::Series;
$Chart::Clicker::Data::Series::VERSION = '2.90';
use Moose;

# ABSTRACT: A series of key, value pairs representing chart data

use List::Util qw(max min);
use Chart::Clicker::Data::Range;


has 'keys' => (
    traits => [ 'Array' ],
    is => 'rw',
    isa => 'ArrayRef[Num]',
    default => sub { [] },
    handles => {
        'add_to_keys' => 'push',
        'key_count' => 'count'
    }
);


has 'name' => (
    is => 'rw',
    isa => 'Str',
    predicate => 'has_name'
);


has 'range' => (
    is => 'rw',
    isa => 'Chart::Clicker::Data::Range',
    lazy_build => 1
);


has 'values' => (
    traits => [ 'Array' ],
    is => 'rw',
    isa => 'ArrayRef[Num|Undef]',
    default => sub { [] },
    handles => {
        'add_to_values' => 'push',
        'value_count' => 'count'
    }
);

sub _build_range {
    my ($self) = @_;

    my $values = $self->values;

    confess('A series must have values before it can be charted')
        unless scalar(@{ $values });

    return Chart::Clicker::Data::Range->new(
        lower => min(grep { defined } @{ $values }),
        upper => max(grep { defined } @{ $values })
    );
}

sub BUILDARGS {
    my ($class, @args) = @_;

    if(@args == 1 && (ref($args[0]) eq 'HASH') && !exists($args[0]->{keys})) {
        my @keys = sort { $a <=> $b } keys %{ $args[0] };
        my @values = ();
        foreach my $k (@keys) {
            push(@values, $args[0]->{$k})
        }
        return { keys => \@keys, values => \@values }
    } elsif(@args % 2 == 0) {
        return { @args };
    }

    return $args[0];
}


sub add_pair {
    my ($self, $key, $value) = @_;

    $self->add_to_keys($key);
    $self->add_to_values($value);
}


sub get_value_for_key {
    my ($self, $key) = @_;

    for(0..$self->key_count) {
        my $ikey = $self->keys->[$_];
        return $self->values->[$_] if defined($ikey) && $ikey == $key;
    }

    # We found nothing, return undef
    return undef;
}

sub prepare {
    my ($self) = @_;

    if($self->key_count != $self->value_count) {
        die('Series key/value counts dont match: '.$self->key_count.' != '.$self->value_count.'.');
    }

    return 1;
}

__PACKAGE__->meta->make_immutable;

no Moose;

1;

__END__

=pod

=head1 NAME

Chart::Clicker::Data::Series - A series of key, value pairs representing chart data

=head1 VERSION

version 2.90

=head1 SYNOPSIS

  use Chart::Clicker::Data::Series;

  my @keys = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
  my @values = (42, 25, 86, 23, 2, 19, 103, 12, 54, 9);

  my $series = Chart::Clicker::Data::Series->new({
    keys    => \@keys,
    value   => \@values
  });

  # Alternately, if you prefer

  my $series2 = Chart::Clicker::Data::Series->new({
    1  => 42,
    2  => 25,
    3  => 85,
    4  => 23,
    5  => 2,
    6  => 19,
    7  => 102,
    8  => 12,
    9  => 54,
    10 => 9
  });

=head1 DESCRIPTION

Chart::Clicker::Data::Series represents a series of values to be charted.

Despite the name (keys and values) it is expected that all keys and values
will be numeric.  Values is pretty obvious, but it is important that keys
also be numeric, as otherwise we'd have no idea how to order the data.

If you want to use text labels for your domain's see L<Chart::Clicker::Axis>'s
L<tick_labels> method.

=head1 ATTRIBUTES

=head2 keys

Set/Get the keys for this series.

=head2 add_to_keys

Adds a key to this series.

=head2 name

Set/Get the name for this Series

=head2 range

Returns the L<range|Chart::Clicker::Data::Range> for this series.

=head2 values

Set/Get the values for this series.

=head1 METHODS

=head2 key_count

Get the count of keys in this series.

=head2 add_to_values

Add a value to this series.

=head2 value_count

Get the count of values in this series.

=head2 add_pair ($key, $value)

Convenience method to add a single key and a single value to the series.

=head2 get_value_for_key ($key)

Returns the value associated with the specified key.  This is necessary
because not all series will have values for every key.

=head1 AUTHOR

Cory G Watson <gphat@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2016 by Cory G Watson.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut