~ubuntu-branches/ubuntu/karmic/libjifty-plugin-chart-perl/karmic

« back to all changes in this revision

Viewing changes to lib/Jifty/Plugin/Chart/Renderer/PlotKit.pm

  • Committer: Bazaar Package Importer
  • Author(s): AGOSTINI Yves
  • Date: 2009-06-11 12:23:49 UTC
  • Revision ID: james.westby@ubuntu.com-20090611122349-s1ss1jf2bw9mtvjn
Tags: upstream-0.90000
ImportĀ upstreamĀ versionĀ 0.90000

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
use strict;
 
2
use warnings;
 
3
 
 
4
package Jifty::Plugin::Chart::Renderer::PlotKit;
 
5
use base qw/Jifty::Plugin::Chart::Renderer /;
 
6
 
 
7
use Jifty::YAML;
 
8
 
 
9
=head1 NAME
 
10
 
 
11
Jifty::Plugin::Chart::Renderer::PlotKit - A chart renderer using PlotKit
 
12
 
 
13
=head1 DESCRIPTION
 
14
 
 
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.
 
16
 
 
17
=head1 METHODS
 
18
 
 
19
=head2 init
 
20
 
 
21
Adds the various JavaScript files required to use PlotKit.
 
22
 
 
23
=cut
 
24
 
 
25
sub init {
 
26
 
 
27
    Jifty->web->add_external_javascript(qw(
 
28
        /static/js/mochikit.noexport.js
 
29
        /static/js/MochiKit/MochiKit.js
 
30
    ));
 
31
    Jifty->web->add_javascript(qw(
 
32
        PlotKit/excanvas.js
 
33
        PlotKit/PlotKit_Packed-20060807-custom.js
 
34
    ));
 
35
}
 
36
 
 
37
=head2 render
 
38
 
 
39
Implemented the L<Jifty::Plugin::Chart::Renderer/render> method interface.
 
40
 
 
41
=cut
 
42
 
 
43
sub render {
 
44
    my $self = shift;
 
45
    my %args = ( options => {}, @_ );
 
46
 
 
47
    # translations from generic type to PlotKit types
 
48
    my %types = (
 
49
        lines          => { type => 'line' },
 
50
        bars           => { type => 'bar', barOrientation => 'vertical' },
 
51
        pie            => { type => 'pie' },
 
52
        horizontalbars => { type => 'bar', barOrientation => 'horizontal' },
 
53
    );
 
54
 
 
55
    # save it for error reporting
 
56
    my $orig_type = $args{type};
 
57
 
 
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;
 
62
 
 
63
    # Bad stuff, not a supported type
 
64
    if ( not defined $args{type} ) {
 
65
        $self->log->warn("Unsupported chart type: $orig_type!");
 
66
        return;
 
67
    }
 
68
 
 
69
    # Kill the "px" unit
 
70
    $args{width} =~ s/px$//;
 
71
    $args{height} =~ s/px$//;
 
72
 
 
73
    $self->_transform_data( \%args );
 
74
 
 
75
    # Save the data for retrieval from the session later
 
76
    my $chart_id   = 'chart_' . Jifty->web->serial;
 
77
 
 
78
    # Output the <canvas> tag and include the chart's JS
 
79
    my $div;
 
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};
 
84
    $div .= qq{></div>};
 
85
 
 
86
    Jifty->web->out(<<"    END_OF_HTML");
 
87
$div
 
88
 
 
89
<script type="text/javascript">
 
90
var plot = function() {
 
91
    var plotter = PlotKit.EasyPlot(
 
92
        "$args{type}",
 
93
        @{[Jifty::JSON::objToJson( $args{options} )]},
 
94
        \$("$chart_id"),
 
95
        @{[Jifty::JSON::objToJson( $args{data} )]}
 
96
    );
 
97
};
 
98
YAHOO.util.Event.onAvailable( "$chart_id", plot );
 
99
</script>
 
100
    END_OF_HTML
 
101
 
 
102
    # Make sure we don't return anything that will get output
 
103
    return;
 
104
}
 
105
 
 
106
sub _transform_data {
 
107
    my $self = shift;
 
108
    my $args = shift;
 
109
 
 
110
    my @data;
 
111
    my $labels = shift @{ $args->{data} };
 
112
 
 
113
    for ( my $i = 0; $i < @$labels; $i++ ) {
 
114
        push @{$args->{options}{xTicks}}, { v => $i, label => $labels->[$i] }
 
115
            if defined $labels->[$i];
 
116
    }
 
117
    
 
118
    for my $dataset ( @{ $args->{data} } ) {
 
119
        my @ds;
 
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' ];
 
124
            } else {
 
125
                push @ds, $dataset->[$i];
 
126
            }
 
127
        }
 
128
        push @data, \@ds;
 
129
    }
 
130
 
 
131
    $args->{data} = \@data;
 
132
}
 
133
 
 
134
sub prereq_plugins {
 
135
  return ("Prototypism");
 
136
}
 
137
 
 
138
=head1 SEE ALSO
 
139
 
 
140
L<Jifty::Plugin::Chart>, L<Jifty::Plugin::Chart::Renderer>
 
141
 
 
142
=head1 AUTHOR
 
143
 
 
144
Thomas Sibley
 
145
 
 
146
=head1 COPYRIGHT AND LICENSE
 
147
 
 
148
Copyright 2007 Best Practical Solutions, LLC
 
149
 
 
150
This is free software and may be modified and distributed under the same terms as Perl itself.
 
151
 
 
152
=cut
 
153
 
 
154
1;