Changeset 10638

Show
Ignore:
Timestamp:
04/28/08 13:36:06 (4 months ago)
Author:
yusukebe
Message:

interface を変更, おそらくこの方が汎用性がある

Location:
lang/perl/WebService-Simple-Google-Chart/trunk
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/WebService-Simple-Google-Chart/trunk/example/chart01.pl

    r10561 r10638  
    66 
    77my $chart = WebService::Simple::Google::Chart->new; 
    8 my $data  = { foo => 200, bar => 130, hoge => 70 }; 
    9 my $url   = $chart->get_url( 
     8my $url = $chart->get_url( 
    109    { 
    11         size => "250x100", 
    12         type => "p3", 
    13         data => $data, 
    14     } 
     10        chs => "250x100", 
     11        cht => "p3", 
     12   }, 
     13    { foo => 200, bar => 130, hoge => 70 } 
    1514); 
     15 
    1616print $url . "\n"; 
    17 #$chart->render_to_file("foo.png"); 
     17$chart->render_to_file("hoge.png"); 
  • lang/perl/WebService-Simple-Google-Chart/trunk/lib/WebService/Simple/Google/Chart.pm

    r10585 r10638  
    1212 
    1313sub get_url { 
    14     my ( $self, $param ) = @_; 
    15     my ( @label, @value, $data, $total_count ); 
    16     $data        = $param->{data}; 
    17     $total_count = 0; 
    18     map { $total_count += $data->{$_} } keys %$data; 
    19     foreach my $key ( keys %{ $param->{data} } ) { 
    20         push( @label, $key ); 
    21         my $percent = int( $param->{data}->{$key} / $total_count * 100 + 0.5 ); 
    22         push( @value, $percent ); 
    23     } 
    24     $self->{request_param}->{chs} = $param->{size}; 
    25     $self->{request_param}->{cht} = $param->{type}; 
    26     $self->{request_param}->{chl} = join( "|", @label ); 
    27     $self->{request_param}->{chd} = "t:" . join( ",", @value ); 
     14    my ( $self, $param, $data ) = @_; 
     15    $self->{request_param} = $param; 
     16    $self->_set_data_param($data); 
    2817    return $self->request_url( $self->{request_param} ); 
    2918} 
    3019 
    3120sub render_to_file { 
    32     my $self  = shift; 
    33     my $param = shift; 
    34     my $filename; 
    35     if ( ref $param eq 'Hash' ) { 
    36         $self->{request_param} = $param; 
     21    my ($self,$filename,$param,$data)  = @_; 
     22    if($param){ 
     23        $self->{request_param} = $param; 
     24        $self->_set_data_param($data); 
    3725    } 
    38     else { 
    39         $filename = $param; 
    40     } 
    41     $self->SUPER::get( $self->{request_param}, ":content_file" => $filename ); 
     26    $self->SUPER::get( $self->{request_param} , ":content_file" => $filename ); 
    4227} 
    4328 
    44 sub _total_value { 
    45     my ( $self, $data ) = @_; 
    46     my @values; 
    47     map { push( @values, $data->{$_} ) } keys %$data; 
    48     @values = sort { $b <=> $a } @values; 
    49     return $values[0]; 
     29sub _set_data_param { 
     30    my ($self, $data) = @_; 
     31    my ( @label, @value, $total_count ); 
     32    $total_count = 0; 
     33    map { $total_count += $data->{$_} } keys %$data; 
     34    foreach my $key ( keys %$data ) { 
     35        push( @label, $key ); 
     36        my $percent = int( $data->{$key} / $total_count * 100 + 0.5 ); 
     37        push( @value, $percent ); 
     38    } 
     39    my $data_param = {}; 
     40    $self->{request_param}->{chl} = join( "|", @label ); 
     41    $self->{request_param}->{chd} = "t:" . join( ",", @value ); 
    5042} 
    5143 
     
    5648=head1 NAME 
    5749 
    58 WebService::Simple::Google::Chart - Get Google Chart URL and File 
     50WebService::Simple::Google::Chart - Get Google Chart URL and image file 
    5951 
    6052=head1 SYNOPSIS 
     
    6355 
    6456  my $chart = WebService::Simple::Google::Chart->new; 
    65   my $data  = { foo => 200, bar => 130, hoge => 70 }; 
    6657  my $url   = $chart->get_url( 
    6758      { 
    6859          size => "250x100", 
    6960          type => "p3", 
    70           data => $data, 
    71       } 
     61      }, 
     62      { foo => 200, bar => 130, hoge => 70 }, 
    7263  ); 
    7364  print $url; 
  • lang/perl/WebService-Simple-Google-Chart/trunk/t/01_basic.t

    r10560 r10638  
    1212{ 
    1313    my $chart = WebService::Simple::Google::Chart->new; 
    14     ok($chart, "object created ok"); 
    15     isa_ok( $chart, "WebService::Simple::Google::Chart", "object isa WebService::Simple::Google::Chart" ); 
    16     my $data  = { foo => 200, bar => 130, hoge => 70 }; 
    17     my $url   = $chart->get_url( 
    18                             { 
    19                              size => "250x100", 
    20                              type => "p3", 
    21                              data => $data, 
    22                          } 
    23                             ); 
     14    ok( $chart, "object created ok" ); 
     15    isa_ok( 
     16        $chart, 
     17        "WebService::Simple::Google::Chart", 
     18        "object isa WebService::Simple::Google::Chart" 
     19    ); 
     20    my $data = { foo => 200, bar => 130, hoge => 70 }; 
     21    my $url = $chart->get_url( 
     22        { 
     23            chs  => "250x100", 
     24            cht => "p3", 
     25        }, 
     26        $data 
     27    ); 
    2428    my $uri = URI->new($url); 
    25     ok($uri->query_param("chs") eq "250x100", "chs parameter ok"); 
    26     ok($uri->query_param("cht") eq "p3", "cht parameter ok"); 
    27     ok($uri->query_param("chl") eq "bar|foo|hoge", "chl parameter ok"); 
    28     ok($uri->query_param("chd") eq "t:33,50,18", "chd parameter ok"); 
     29    ok( $uri->query_param("chs") eq "250x100",      "chs parameter ok" ); 
     30    ok( $uri->query_param("cht") eq "p3",           "cht parameter ok" ); 
     31    ok( $uri->query_param("chl") eq "bar|foo|hoge", "chl parameter ok" ); 
     32    ok( $uri->query_param("chd") eq "t:33,50,18",   "chd parameter ok" ); 
    2933}