File Coverage

blib/lib/SVG/Graph/Data.pm
Criterion Covered Total %
statement 45 112 40.1
branch 11 20 55.0
condition n/a
subroutine 13 38 34.2
pod 9 34 26.4
total 78 204 38.2


line stmt bran cond sub pod time code
1             package SVG::Graph::Data;
2              
3 7     7   58 use strict;
  7         15  
  7         323  
4 7     7   8587 use Statistics::Descriptive;
  7         168539  
  7         534  
5 7     7   9780 use Data::Dumper;
  7         93630  
  7         12645  
6              
7             =head2 new
8              
9             Title : new
10             Usage : my $data = SVG::Graph::Data->new
11             Function: creates a new SVG::Graph::Data object
12             Returns : a SVG::Graph::Data object
13             Args : (optional) array of SVG::Graph::Data::Datum objects
14              
15              
16             =cut
17              
18             sub new {
19 9     9 1 778 my($class, %args) = @_;
20 9         31 my $self = bless {}, $class;
21 9         40 $self->init(%args);
22 9         27 return $self;
23             }
24              
25             =head2 init
26              
27             Title : init
28             Usage :
29             Function:
30             Example :
31             Returns :
32             Args :
33              
34              
35             =cut
36              
37             sub init {
38 9     9 1 23 my($self, %args) = @_;
39 9         25 foreach my $arg (keys %args){
40 9         21 my $meth = 'add_'.$arg;
41 9         44 $self->$meth($args{$arg});
42             }
43 9         33 $self->is_changed(1);
44             }
45              
46             =head2 data
47              
48             Title : data
49             Usage :
50             Function:
51             Example :
52             Returns :
53             Args :
54              
55              
56             =cut
57              
58             sub data {
59 34     34 1 56 my $self = shift;
60              
61 34 50       85 return $self->{data} ? @{$self->{data}} : ();
  34         323  
62             }
63              
64             =head2 add_data
65              
66             Title : add_data
67             Usage : $data->add_data($datum)
68             Function: adds a Datum object to the current Data object
69             Returns : none
70             Args : SVG::Graph::Data::Datum object
71              
72              
73             =cut
74              
75             sub add_data {
76 9     9 1 29 my($self,@data) = @_;
77              
78 9         25 my $epitaph = "only SVG::Graph::Data::Datum objects accepted";
79              
80 9         26 foreach my $data (@data){
81 9 50       43 if(ref $data eq 'ARRAY'){
82 9         19 foreach my $d (@$data){
83 180 50       345 die $epitaph unless ref $d eq 'SVG::Graph::Data::Datum';
84 180         163 push @{$self->{data}}, $d;
  180         365  
85             }
86             } else {
87 0 0       0 die $epitaph unless ref $data eq 'SVG::Graph::Data::Datum';
88 0         0 push @{$self->{data}}, $data;
  0         0  
89             }
90             }
91              
92 9         36 $self->is_changed(1);
93             }
94              
95             =head2 _recalculate_stats
96              
97             Title : _recalculate_stats
98             Usage :
99             Function:
100             Example :
101             Returns :
102             Args :
103              
104              
105             =cut
106              
107             sub _recalculate_stats{
108 0     0   0 my ($self,@args) = @_;
109 0 0       0 return undef unless $self->is_changed;
110              
111             #x
112 0         0 my $xstat = Statistics::Descriptive::Full->new();
113 0         0 $xstat->add_data(map {$_->x} $self->data);
  0         0  
114 0         0 $self->xstat($xstat);
115              
116             #y
117 0         0 my $ystat = Statistics::Descriptive::Full->new();
118 0         0 $ystat->add_data(map {$_->y} $self->data);
  0         0  
119 0         0 $self->ystat($ystat);
120              
121             #z
122 0         0 my $zstat = Statistics::Descriptive::Full->new();
123 0         0 $zstat->add_data(map {$_->z} $self->data);
  0         0  
124 0         0 $self->zstat($zstat);
125              
126 0         0 $self->is_changed(0);
127             }
128              
129             =head2 xstat
130              
131             Title : xstat
132             Usage : $obj->xstat($newval)
133             Function:
134             Example :
135             Returns : value of xstat (a scalar)
136             Args : on set, new value (a scalar or undef, optional)
137              
138              
139             =cut
140              
141             sub xstat{
142 249     249 1 308 my $self = shift;
143              
144 249 100       497 return $self->{'xstat'} = shift if @_;
145 241         836 return $self->{'xstat'};
146             }
147              
148             =head2 ystat
149              
150             Title : ystat
151             Usage : $obj->ystat($newval)
152             Function:
153             Example :
154             Returns : value of ystat (a scalar)
155             Args : on set, new value (a scalar or undef, optional)
156              
157              
158             =cut
159              
160             sub ystat{
161 249     249 1 279 my $self = shift;
162              
163 249 100       488 return $self->{'ystat'} = shift if @_;
164 241         781 return $self->{'ystat'};
165             }
166              
167             =head2 zstat
168              
169             Title : zstat
170             Usage : $obj->zstat($newval)
171             Function:
172             Example :
173             Returns : value of zstat (a scalar)
174             Args : on set, new value (a scalar or undef, optional)
175              
176              
177             =cut
178              
179             sub zstat{
180 13     13 1 23 my $self = shift;
181              
182 13 100       52 return $self->{'zstat'} = shift if @_;
183 5         19 return $self->{'zstat'};
184             }
185              
186              
187 0     0 0 0 sub xmean {$_[0]->_recalculate_stats; return $_[0]->xstat->mean}
  0         0  
188 0     0 0 0 sub xmode {$_[0]->_recalculate_stats; return $_[0]->xstat->mode}
  0         0  
189 0     0 0 0 sub xmedian {$_[0]->_recalculate_stats; return $_[0]->xstat->median}
  0         0  
190 0     0 0 0 sub xmin {$_[0]->_recalculate_stats; return $_[0]->xstat->min}
  0         0  
191 0     0 0 0 sub xmax {$_[0]->_recalculate_stats; return $_[0]->xstat->max}
  0         0  
192 0     0 0 0 sub xrange {$_[0]->_recalculate_stats; return $_[0]->xstat->sample_range}
  0         0  
193 0     0 0 0 sub xstdv {$_[0]->_recalculate_stats; return $_[0]->xstat->standard_deviation}
  0         0  
194 0     0 0 0 sub xpercentile {$_[0]->_recalculate_stats; return $_[0]->xstat->percentile($_[1])}
  0         0  
195              
196 0     0 0 0 sub ymean {$_[0]->_recalculate_stats; return $_[0]->ystat->mean}
  0         0  
197 0     0 0 0 sub ymode {$_[0]->_recalculate_stats; return $_[0]->ystat->mode}
  0         0  
198 0     0 0 0 sub ymedian {$_[0]->_recalculate_stats; return $_[0]->ystat->median}
  0         0  
199 0     0 0 0 sub ymin {$_[0]->_recalculate_stats; return $_[0]->ystat->min}
  0         0  
200 0     0 0 0 sub ymax {$_[0]->_recalculate_stats; return $_[0]->ystat->max}
  0         0  
201 0     0 0 0 sub yrange {$_[0]->_recalculate_stats; return $_[0]->ystat->sample_range}
  0         0  
202 0     0 0 0 sub ystdv {$_[0]->_recalculate_stats; return $_[0]->ystat->standard_deviation}
  0         0  
203 0     0 0 0 sub ypercentile {$_[0]->_recalculate_stats; return $_[0]->ystat->percentile($_[1])}
  0         0  
204              
205 0     0 0 0 sub zmean {$_[0]->_recalculate_stats; return $_[0]->zstat->mean}
  0         0  
206 0     0 0 0 sub zmode {$_[0]->_recalculate_stats; return $_[0]->zstat->mode}
  0         0  
207 0     0 0 0 sub zmedian {$_[0]->_recalculate_stats; return $_[0]->zstat->median}
  0         0  
208 0     0 0 0 sub zmin {$_[0]->_recalculate_stats; return $_[0]->zstat->min}
  0         0  
209 0     0 0 0 sub zmax {$_[0]->_recalculate_stats; return $_[0]->zstat->max}
  0         0  
210 5     5 0 24 sub zrange {$_[0]->_recalculate_stats; return $_[0]->zstat->sample_range}
  5         14  
211 0     0 0 0 sub zstdv {$_[0]->_recalculate_stats; return $_[0]->zstat->standard_deviation}
  0         0  
212 0     0 0 0 sub zpercentile {$_[0]->_recalculate_stats; return $_[0]->zstat->percentile($_[1])}
  0         0  
213              
214             =head2 is_changed
215              
216             Title : is_changed
217             Usage : $obj->is_changed($newval)
218             Function:
219             Example :
220             Returns : value of is_changed (a scalar)
221             Args : on set, new value (a scalar or undef, optional)
222              
223              
224             =cut
225              
226             sub is_changed{
227 533     533 1 659 my $self = shift;
228              
229 533 100       1151 return $self->{'is_changed'} = shift if @_;
230 487         1740 return $self->{'is_changed'};
231             }
232              
233             =head2 svg
234              
235             Title : svg
236             Usage : $obj->svg($newval)
237             Function:
238             Example :
239             Returns : value of svg (a scalar)
240             Args : on set, new value (a scalar or undef, optional)
241              
242              
243             =cut
244              
245 10     10 0 53 sub add_svg {return shift->svg(@_)};
246             sub svg{
247 0     0 1   my $self = shift;
248              
249 0 0         return $self->{'svg'} = shift if @_;
250 0           return $self->{'svg'};
251             }
252              
253              
254             1;