4
package Jifty::Plugin::Chart::Renderer::PlotKit;
5
use base qw/Jifty::Plugin::Chart::Renderer /;
11
Jifty::Plugin::Chart::Renderer::PlotKit - A chart renderer using PlotKit
15
This is an alternate chart renderer used by the L<Jifty::Plugin::Chart> plugin. It works by rendering a <div> tag in the HTML output and some JavaScript in a <script> tag.
21
Adds the various JavaScript files required to use PlotKit.
27
Jifty->web->add_external_javascript(qw(
28
/static/js/mochikit.noexport.js
29
/static/js/MochiKit/MochiKit.js
31
Jifty->web->add_javascript(qw(
33
PlotKit/PlotKit_Packed-20060807-custom.js
39
Implemented the L<Jifty::Plugin::Chart::Renderer/render> method interface.
45
my %args = ( options => {}, @_ );
47
# translations from generic type to PlotKit types
49
lines => { type => 'line' },
50
bars => { type => 'bar', barOrientation => 'vertical' },
51
pie => { type => 'pie' },
52
horizontalbars => { type => 'bar', barOrientation => 'horizontal' },
55
# save it for error reporting
56
my $orig_type = $args{type};
58
# Make sure the type is ready to be used
59
my $options = $types{ $args{type} } || {};
60
$args{type} = delete $options->{type};
61
$args{options}{$_} = $options->{$_} foreach keys %$options;
63
# Bad stuff, not a supported type
64
if ( not defined $args{type} ) {
65
$self->log->warn("Unsupported chart type: $orig_type!");
70
$args{width} =~ s/px$//;
71
$args{height} =~ s/px$//;
73
$self->_transform_data( \%args );
75
# Save the data for retrieval from the session later
76
my $chart_id = 'chart_' . Jifty->web->serial;
78
# Output the <canvas> tag and include the chart's JS
80
$div = qq{<div id="$chart_id"};
81
$div .= qq{ class="@{[ join ' ', @{ $args{class} } ]}"};
82
$div .= qq{ height="$args{height}"} if $args{height};
83
$div .= qq{ width="$args{width}"} if $args{width};
86
Jifty->web->out(<<" END_OF_HTML");
89
<script type="text/javascript">
90
var plot = function() {
91
var plotter = PlotKit.EasyPlot(
93
@{[Jifty::JSON::objToJson( $args{options} )]},
95
@{[Jifty::JSON::objToJson( $args{data} )]}
98
YAHOO.util.Event.onAvailable( "$chart_id", plot );
102
# Make sure we don't return anything that will get output
106
sub _transform_data {
111
my $labels = shift @{ $args->{data} };
113
for ( my $i = 0; $i < @$labels; $i++ ) {
114
push @{$args->{options}{xTicks}}, { v => $i, label => $labels->[$i] }
115
if defined $labels->[$i];
118
for my $dataset ( @{ $args->{data} } ) {
120
for ( my $i = 0; $i < @$dataset; $i++ ) {
121
# PlotKit can't deal with undefined values
122
if ( not ref $dataset->[$i] ) {
123
push @ds, [ $i, defined $dataset->[$i] ? $dataset->[$i] : '0' ];
125
push @ds, $dataset->[$i];
131
$args->{data} = \@data;
135
return ("Prototypism");
140
L<Jifty::Plugin::Chart>, L<Jifty::Plugin::Chart::Renderer>
146
=head1 COPYRIGHT AND LICENSE
148
Copyright 2007 Best Practical Solutions, LLC
150
This is free software and may be modified and distributed under the same terms as Perl itself.