| Chart-OFC2 documentation | Contained in the Chart-OFC2 distribution. |
Chart::OFC2::PieValues - OFC2 values for pie charts object
has 'values' => ( is => 'rw', isa => 'ArrayRef', );
has 'texts' => ( is => 'rw', isa => 'ArrayRef', );
Object constructor.
Allow object creation by coerce of ArrayRef.
Returns HashRef that is possible to give to encode_json() function.
Jozef Kutej
| Chart-OFC2 documentation | Contained in the Chart-OFC2 distribution. |
package Chart::OFC2::PieValues;
use Moose; use Moose::Util::TypeConstraints; use MooseX::StrictConstructor; use Carp::Clan 'croak'; use List::MoreUtils 'any'; our $VERSION = '0.07'; coerce 'Chart::OFC2::PieValues' => from 'ArrayRef' => via { Chart::OFC2::PieValues->_new_from_arrayref($_) };
has 'values' => ( is => 'rw', isa => 'ArrayRef', 'required' => 1); has 'labels' => ( is => 'rw', isa => 'ArrayRef', ); has 'colours' => ( is => 'rw', isa => 'ArrayRef', );
sub _new_from_arrayref { my $class = shift; my $arrayref_values = shift; croak 'pass ArrayRef as argument' if not ref $arrayref_values ne 'ArrayRef'; my (@values, @labels, @colours); foreach my $value (@{$arrayref_values}) { if (ref $value eq 'HASH') { push @values, $value->{'value'}; push @labels, $value->{'label'}; push @colours, $value->{'colour'}; } else { push @values, $value; push @labels, undef; push @colours, undef; } } return $class->new( 'values' => \@values, 'labels' => \@labels, ((any { defined $_ } @colours) ? ('colours' => \@colours) : ()), ); }
sub TO_JSON { my ($self) = @_; my @values_with_labels; my @values = @{$self->values}; my @labels = @{$self->labels}; for (my $i = 0; $i < @values; $i++) { push @values_with_labels, ( defined $labels[$i] ? { 'value' => $values[$i], 'label' => $labels[$i] } : $values[$i] ); } return \@values_with_labels; } __PACKAGE__->meta->make_immutable; 1; __END__