Skip to content

Commit

Permalink
merged RT64751
Browse files Browse the repository at this point in the history
  • Loading branch information
robin13 committed Jan 25, 2011
2 parents 828016b + 11dd03a commit 378e4e6
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 8 deletions.
4 changes: 3 additions & 1 deletion lib/Chart/OFC2.pm
Expand Up @@ -76,6 +76,7 @@ use Chart::OFC2::Legend;
use Chart::OFC2::Title;
use Chart::OFC2::Extremes;
use Chart::OFC2::ToolTip;
use Chart::OFC2::Menu;
use List::Util 'min', 'max';
use List::MoreUtils 'any';

Expand Down Expand Up @@ -108,6 +109,7 @@ has 'elements' => (is => 'rw', isa => 'ArrayRef', default => sub{[]}, lazy
has 'extremes' => (is => 'rw', isa => 'Chart::OFC2::Extremes', default => sub { Chart::OFC2::Extremes->new() }, lazy => 1);
has '_json' => (is => 'rw', isa => 'Object', default => sub { JSON::XS->new->pretty(1)->convert_blessed(1) }, lazy => 1);
has 'tooltip' => (is => 'rw', isa => 'Chart::OFC2::ToolTip', coerce => 1);
has 'menu' => (is => 'rw', isa => 'Chart::OFC2::Menu', coerce => 1);
has 'bg_colour' => (is => 'rw', isa => 'Str', default => 'f8f8d8', alias => 'bg_color' );

=head1 METHODS
Expand Down Expand Up @@ -171,7 +173,7 @@ sub render_chart_data {
$self->auto_extremes();

my $data;
foreach my $element( qw/title x_axis y_axis elements bg_colour tooltip y_axis_right y_legend x_legend/ ){
foreach my $element( qw/title menu x_axis y_axis elements bg_colour tooltip y_axis_right y_legend x_legend/ ){
if( $self->$element ){
$data->{$element} = $self->$element;
}
Expand Down
84 changes: 84 additions & 0 deletions lib/Chart/OFC2/Menu.pm
@@ -0,0 +1,84 @@
package Chart::OFC2::Menu;

=head1 NAME
Chart::OFC2::Menu - OFC2 menu object
=head1 SYNOPSIS
use Chart::OFC2;
use Chart::OFC2::Menu;
$chart = Chart::OFC2->new(
'menu' => Chart::OFC2::Menu->new(
values => [
{"type":"camera-icon","text":"Hello","javascript-function":"save_image"},
{"type":"camera-icon","text":"Toggle old data","javascript-function":"toggle"}
],
colour => "#E0E0ff",
outline_colour =>"#707070"
),
);
=head1 DESCRIPTION
=cut

use Moose;
use Moose::Util::TypeConstraints;
use MooseX::StrictConstructor;

our $VERSION = '0.07';

coerce 'Chart::OFC2::Menu'
=> from 'HashRef'
=> via { Chart::OFC2::Menu->new($_) };


=head1 PROPERTIES
has 'values' => ( is => 'rw', isa => 'ArrayRef', 'required' => 1);
has 'colour' => (is => 'rw', isa => 'Str', );
has 'outline_colour' => (is => 'rw', isa => 'Str', );
=cut

has 'values' => ( is => 'rw', isa => 'ArrayRef', 'required' => 1);
has 'colour' => (is => 'rw', isa => 'Str', );
has 'outline_colour' => (is => 'rw', isa => 'Str', );


=head1 METHODS
=head2 new()
Object constructor.
=head2 TO_JSON()
Returns HashRef that is possible to give to C<encode_json()> function.
=cut

sub TO_JSON {
my $self = shift;

return {
map { my $v = $self->$_; (defined $v ? ($_ => $v) : ()) }
map { $_->name }
$self->meta->get_all_attributes
};
}

__PACKAGE__->meta->make_immutable;

1;


__END__
=head1 AUTHOR
Frederik Jung
=cut
31 changes: 24 additions & 7 deletions lib/Chart/OFC2/PieValues.pm
Expand Up @@ -27,12 +27,16 @@ coerce 'Chart::OFC2::PieValues'
has 'values' => ( is => 'rw', isa => 'ArrayRef', );
has 'texts' => ( is => 'rw', isa => 'ArrayRef', );
has 'tips' => ( is => 'rw', isa => 'ArrayRef', );
has 'clicks' => ( is => 'rw', isa => 'ArrayRef', );
=cut

has 'values' => ( is => 'rw', isa => 'ArrayRef', 'required' => 1);
has 'labels' => ( is => 'rw', isa => 'ArrayRef', );
has 'colours' => ( is => 'rw', isa => 'ArrayRef', );
has 'tips' => ( is => 'rw', isa => 'ArrayRef', );
has 'clicks' => ( is => 'rw', isa => 'ArrayRef', );


=head1 METHODS
Expand All @@ -54,24 +58,30 @@ sub _new_from_arrayref {
croak 'pass ArrayRef as argument'
if not ref $arrayref_values ne 'ArrayRef';

my (@values, @labels, @colours);
my (@values, @labels, @colours, @tips, @clicks);
foreach my $value (@{$arrayref_values}) {
if (ref $value eq 'HASH') {
push @values, $value->{'value'};
push @labels, $value->{'label'};
push @colours, $value->{'colour'};
push @tips, $value->{'tip'};
push @clicks, $value->{'click'};
}
else {
push @values, $value;
push @labels, undef;
push @colours, undef;
push @tips, undef;
push @clicks, undef;
}
}

return $class->new(
'values' => \@values,
'labels' => \@labels,
((any { defined $_ } @colours) ? ('colours' => \@colours) : ()),
'tips' => \@tips,
'clicks' => \@clicks,
((any { defined $_ } @colours) ? ('colours' => \@colours) : ())
);
}

Expand All @@ -87,12 +97,19 @@ sub TO_JSON {
my @values_with_labels;
my @values = @{$self->values};
my @labels = @{$self->labels};
my @tips = @{$self->tips};
my @clicks = @{$self->clicks};
for (my $i = 0; $i < @values; $i++) {
push @values_with_labels, (
defined $labels[$i]
? { 'value' => $values[$i], 'label' => $labels[$i] }
: $values[$i]
);
my $value;
if (defined($labels[$i]) || defined($tips[$i]) || defined($clicks[$i])) {
$value = { value => $values[$i] };
$value->{label} = $labels[$i] if defined($labels[$i]);
$value->{tip} = $tips[$i] if defined($tips[$i]);
$value->{'on-click'} = $clicks[$i] if defined($clicks[$i]);
} else {
$value = $values[$i];
}
push(@values_with_labels, $value);
}

return \@values_with_labels;
Expand Down
13 changes: 13 additions & 0 deletions t/03_pie.t
Expand Up @@ -46,6 +46,19 @@ sub main {
],
'type' => 'pie',
'values' => bless( {
'tips' => [ undef,
undef,
undef,
undef,
undef,
],
'clicks' => [
undef,
undef,
undef,
undef,
undef
],
'colours' => [
'#d01f3c',
'#356aa0',
Expand Down

0 comments on commit 378e4e6

Please sign in to comment.