Subject: | Patch for feature: Text with custom scaling in Google::Chart 0.05014 |
To implement:
http://code.google.com/apis/chart/docs/data_formats.html#data_scaling
Used Google::Chart::Data::Text as a template. Much code is redundant and
would be be better factored out, but maybe this provides a starting
point.
Google::Chart 0.05014
Moose 1.09
perl, v5.10.0 built for i386-linux-thread-multi
Fedora release 11 (Leonidas)
-Chad
Subject: | 12_textscaled.t |
use strict;
use Test::More (tests => 8);
use Test::Exception;
BEGIN
{
use_ok("Google::Chart::Data::TextScaled");
}
{
my $data = Google::Chart::Data::TextScaled->new([ -2.1, 1.0, 1.2, 1.3, 50, 105.5 ]);
ok($data);
isa_ok($data, "Google::Chart::Data::TextScaled");
my $query = $data->as_query;
note($query);
is( $query, "chd=t%3A-2.1%2C1.0%2C1.2%2C1.3%2C50.0%2C105.5" );
}
{
my $data = Google::Chart::Data::TextScaled->new(
[
[ -2.1, 1.2, 1.3, 50, 105.5 ],
[ 100, 50, 1.3, 1.2, 1.0 ],
]
);
ok($data);
isa_ok($data, "Google::Chart::Data::TextScaled");
my $query = $data->as_query;
note($query);
is( $query, "chd=t%3A-2.1%2C1.2%2C1.3%2C50.0%2C105.5%7C100.0%2C50.0%2C1.3%2C1.2%2C1.0" );
}
{
dies_ok { Google::Chart::Data::TextScaled->new([ 'A' ]) } "bad args"
}
Subject: | TextScaled.pm |
# $Id$
package Google::Chart::Data::TextScaled;
use Moose;
use URI::Escape ();
with 'Google::Chart::Data';
# Left over from QueryComponent::Simple (from Google::Chart::Data)
sub parameter_value { }
has '+dataset' => (
isa => 'ArrayRef[Google::Chart::Data::TextScaled::DataSet]',
);
has 'scaling' => (
is => 'rw',
isa => 'Maybe[ArrayRef]',
);
__PACKAGE__->meta->make_immutable;
no Moose;
sub BUILDARGS {
my $self = shift;
# A dataset must be an array of arrays or array of values
my @dataset;
my @dataargs;
my %args;
if (@_ == 1 && ref $_[0] eq 'ARRAY') {
@dataargs = @{$_[0]};
} else {
%args = @_;
@dataargs = @{ delete $args{dataset} || [] };
}
if (! ref $dataargs[0] ) {
@dataargs = ([ @dataargs]);
}
foreach my $dataset ( @dataargs ) {
if (! Scalar::Util::blessed $dataset) {
$dataset = Google::Chart::Data::TextScaled::DataSet->new(data => $dataset)
}
push @dataset, $dataset;
}
return { %args, dataset => \@dataset }
}
sub as_query {
my $self = shift;
my %data;
$data{'chd'} = URI::Escape::uri_escape
't:' . join('|',map { $_->as_string } @{$self->dataset});
if ($self->scaling) {
$data{'chds'} = URI::Escape::uri_escape
join(',', map {join ',', @$_} @{$self->scaling});
}
return wantarray ? %data :
join('&', map { "$_=$data{$_}" } keys %data );
}
package # hide from PAUSE
Google::Chart::Data::TextScaled::DataSet;
use Moose;
use Moose::Util::TypeConstraints;
subtype 'Google::Chart::Data::TextScaled::DataSet::Value'
=> as 'Num'
;
has 'data' => (
is => 'rw',
isa => 'ArrayRef[Maybe[Google::Chart::Data::TextScaled::DataSet::Value]]',
required => 1,
default => sub { +[] }
);
__PACKAGE__->meta->make_immutable;
no Moose;
no Moose::Util::TypeConstraints;
sub as_string {
my $self = shift;
return join(',', map { sprintf('%0.1f', $_) } @{$self->data});
}
1;
__END__
=head1 NAME
Google::Chart::Data::TextScaled - Google::Chart Scaled Text Encoding
=head1 SYNOPSIS
Google::Chart->new(
data => {
type => "TextScaled",
dataset => [ .... ],
scaled => [[0,5],[-12.2,222.2]],
}
);
=head1 METHODS
=head2 parameter_value
=cut