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