シンプルに
package Highcharts::Column; use strict; use warnings; our $VERSION = '0.0.2'; sub new { my $class = shift; my $self = $class->_init(); return bless $self, $class; } sub set_data{ my $self = shift; my %properties = @_; while (my($key, $value) = each(%properties)) { $self->{$key} = $value ; } } sub draw { my $self = shift; # draw header my $header = &_header($self); my $story; my $cnt; # draw data foreach my $data(@{$self->{y_values}}) { if( $cnt++ ne 0 ) { $story .= " ,\n"} $story .= &_story ($data); } # drawa footer my $footer = &_footer; my $js = $header.$story.$footer; return($js); } sub _init { my $class = shift; my @y_data =( { name=> 'y1' , data=> "1,2,3"},{ name=> 'y2' , data=> "3,2,1"} ); my $self = { id => 'Sample', title => 'Title', subtitle => 'Sub Title', x_values => "1,2,3", xaxis_title => "xaxis title", yaxis_title => "yaxis title", y_values => \@y_data, }; return bless $self, $class; } sub _header { my $self = shift; my $header = ' '; $header = <<_HEADER_; var char; \$(document).ready(function() { chart = new Highcharts.Chart( { chart: { renderTo: 'chart_column_$self->{id}', defaultSeriesType: 'column' }, title: { text: '$self->{title}' }, subtitle: { text: '$self->{subtitle}' }, xAxis: { categories: [ $self->{x_values} ], title: { text : '$self->{xaxis_title}' } }, yAxis: { title: { text: '$self->{yaxis_title}' } }, series: [ _HEADER_ return($header); } sub _story { my $data = shift; my $story =<<_STORY_; { name : '$data->{name}', data : [$data->{data}] } _STORY_ return($story); } sub _footer { my $footer = <<_FOOTER_; ] }); }); _FOOTER_ }
ま、このライブラリの需要自体はあるのかと言えば、少ないだろうなーとは思う。Highcharts、Perlで発言してる人捜すとなんか見当たらないしw
use Highcharts::Column; my $foo = Highcharts::Column->new(); my @y_data = ({ name=> 'y1' , data=> "10,3,2"}, { name=> 'y2' , data=> "1,3,2"} ); my %config = id => 'Test', title => 'TiTle', subtitle => 'Sub TiTle', x_values => "10,12,1", xaxis_title =>"xaxis_title", yaxis_title =>"yaxis_title", y_values => \@y_data, ); my $result = Highcharts::Column->new(); $result->set_data(%sample); my $js = $result->draw(); print $js;
こんな感じで中身を表示できる。外部JSに保存して読み出すとかそのままソースに書き込むかは利用者による感じで。
04 /
01 / 03 / 05 / 06 /
07 / 08 / 09 / 10 /
01 / 02 / 03 / 04 / 06 /
07 / 08 / 09 / 10 / 11 / 12 /
01 / 02 / 03 / 05 / 09 / 10 / 11 / 12 /
01 / 02 / 03 / 04 / 05 / 06 /
07 / 08 / 09 / 10 / 11 /
01 / 02 / 03 / 04 / 05 / 06 /
07 / 08 / 09 / 10 / 11 /
01 / 02 / 03 / 04 / 05 / 06 /
07 / 11 / 12 /
01 / 02 / 03 / 04 / 05 / 06 /
07 / 08 / 09 / 10 / 11 / 12 /
01 / 02 / 03 / 04 / 05 / 06 /
07 / 08 / 09 / 10 / 11 / 12 /
01 / 02 / 03 / 04 / 05 / 06 /
07 / 08 / 09 / 10 / 11 / 12 /
01 / 02 / 03 / 04 / 05 / 06 /
07 / 08 / 09 / 10 / 11 / 12 /
01 / 02 / 03 / 04 / 05 / 06 /
07 / 08 / 09 / 10 / 11 / 12 /