| 1 | package Chart::OFC2::Element; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Chart::OFC2::Element - OFC2 base module for chart elements | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use Moose; | ||
| 10 | extends 'Chart::OFC2::Element'; | ||
| 11 | |||
| 12 | =head1 DESCRIPTION | ||
| 13 | |||
| 14 | =cut | ||
| 15 | |||
| 16 | 0 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 17 | use Moose::Util::TypeConstraints; | ||
| 18 | use MooseX::StrictConstructor; | ||
| 19 | |||
| 20 | use Chart::OFC2::Extremes; | ||
| 21 | use Scalar::Util 'looks_like_number', 'reftype'; | ||
| 22 | |||
| 23 | our $VERSION = '0.07'; | ||
| 24 | |||
| 25 | =head1 PROPERTIES | ||
| 26 | |||
| 27 | has 'type_name' => (is => 'rw', isa => enum([qw( | ||
| 28 | bar bar_3d bar_filled bar_glass | ||
| 29 | pie | ||
| 30 | hbar | ||
| 31 | line line_dot line_hollow | ||
| 32 | area_hollow | ||
| 33 | scatter | ||
| 34 | )]), required => 1); | ||
| 35 | has 'values' => (is => 'rw', isa => 'ArrayRef', trigger => sub { $_[0]->extremes->reset('y' => $_[1]); } ); | ||
| 36 | has 'extremes' => (is => 'rw', isa => 'Chart::OFC2::Extremes', default => sub { Chart::OFC2::Extremes->new() }, lazy => 1, coerce => 1); | ||
| 37 | has 'use_extremes' => (is => 'rw', isa => 'Bool', default => 0 ); | ||
| 38 | has 'on-click' => (is => 'rw', isa => 'Str', ); | ||
| 39 | has 'tip' => (is => 'rw', isa => 'Str',); | ||
| 40 | has 'alpha' => (is => 'rw', isa => 'Num',); | ||
| 41 | has 'colour' => (is => 'rw', isa => 'Str',); | ||
| 42 | has 'text' => (is => 'rw', isa => 'Str',); | ||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | has 'type_name' => (is => 'rw', isa => enum([qw( | ||
| 47 | bar bar_3d bar_fade bar_filled bar_glass bar_sketch bar_filled bar_stack | ||
| 48 | pie | ||
| 49 | hbar | ||
| 50 | line line_dot line_hollow | ||
| 51 | area_hollow | ||
| 52 | scatter | ||
| 53 | )]), required => 1); | ||
| 54 | has 'values' => (is => 'rw', isa => 'ArrayRef', trigger => sub { $_[0]->extremes->reset('y' => $_[1]); } ); | ||
| 55 | has 'extremes' => (is => 'rw', isa => 'Chart::OFC2::Extremes', default => sub { Chart::OFC2::Extremes->new() }, lazy => 1, coerce => 1); | ||
| 56 | has 'use_extremes' => (is => 'rw', isa => 'Bool',); | ||
| 57 | has 'on-click' => (is => 'rw', isa => 'Str', ); | ||
| 58 | has 'tip' => (is => 'rw', isa => 'Str',); | ||
| 59 | has 'alpha' => (is => 'rw', isa => 'Num',); | ||
| 60 | has 'colour' => (is => 'rw', isa => 'Str',); | ||
| 61 | has 'text' => (is => 'rw', isa => 'Str',); | ||
| 62 | |||
| 63 | |||
| 64 | =head1 METHODS | ||
| 65 | |||
| 66 | =head2 new() | ||
| 67 | |||
| 68 | Object constructor. | ||
| 69 | |||
| 70 | =head2 TO_JSON() | ||
| 71 | |||
| 72 | Returns HashRef that is possible to give to C<encode_json()> function. | ||
| 73 | |||
| 74 | =cut | ||
| 75 | |||
| 76 | sub TO_JSON { | ||
| 77 | my $self = shift; | ||
| 78 | |||
| 79 | my %hash = ( | ||
| 80 | map { my $v = $self->$_; (defined $v ? ($_ => $v) : ()) } | ||
| 81 | grep { $_ ne 'extremes' } | ||
| 82 | grep { $_ ne 'type_name' } | ||
| 83 | grep { $_ ne 'use_extremes' } | ||
| 84 | map { $_->name } | ||
| 85 | $self->meta->get_all_attributes | ||
| 86 | ); | ||
| 87 | $hash{'type'} = $self->type_name; | ||
| 88 | |||
| 89 | _make_numbers_numbers(\$hash{'values'}); | ||
| 90 | |||
| 91 | return \%hash; | ||
| 92 | } | ||
| 93 | |||
| 94 | # finds "looks like numbers" in a structure and makes them really numbers | ||
| 95 | sub _make_numbers_numbers { | ||
| 96 | my $var = shift; | ||
| 97 | my $reftype = reftype($var); | ||
| 98 | |||
| 99 | if ($reftype eq 'REF') { | ||
| 100 | _make_numbers_numbers(${$var}) | ||
| 101 | } | ||
| 102 | elsif ($reftype eq 'HASH') { | ||
| 103 | foreach my $key (keys %{$var}) { | ||
| 104 | _make_numbers_numbers(\${$var}{$key}) | ||
| 105 | } | ||
| 106 | } | ||
| 107 | elsif ($reftype eq 'ARRAY') { | ||
| 108 | my $i = 0; | ||
| 109 | while ($i < @{$var}) { | ||
| 110 | _make_numbers_numbers(\${$var}[$i]); | ||
| 111 | $i++; | ||
| 112 | } | ||
| 113 | } | ||
| 114 | elsif ($reftype eq 'SCALAR') { | ||
| 115 | $$var = $$var+0 | ||
| 116 | if looks_like_number($$var); | ||
| 117 | } | ||
| 118 | else { | ||
| 119 | die "unknown reference type - ".$var; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | 1; | ||
| 124 | |||
| 125 | |||
| 126 | __END__ | ||