File: Chart.pm

package info (click to toggle)
libjifty-plugin-chart-perl 1.01%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 348 kB
  • ctags: 350
  • sloc: perl: 2,614; sh: 48; makefile: 2
file content (100 lines) | stat: -rw-r--r-- 2,776 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use strict;
use warnings;

package Jifty::Plugin::Chart::Renderer::Chart;
use base qw/ Jifty::Plugin::Chart::Renderer /;

use Jifty::YAML;

=head1 NAME

Jifty::Plugin::Chart::Renderer::Chart - A chart renderer using PNG charts

=head1 DESCRIPTION

This is the default chart renderer used by the L<Jifty::Plugin::Chart> plugin. It works by rendering an IMG tag in the HTML output, which then points to a URL which, when dispatched, retrieves the stored configuration and renders the chart using the L<Chart> package.

=head1 METHODS

=head2 init

Adds the F<chart_img_behaviour.js> script to those loaded.

=cut

sub init {
    Jifty->web->add_javascript('chart_img_behaviour.js');
}

=head2 render

Implemented the L<Jifty::Plugin::Chart::Renderer/render> method interface.

=cut

sub render {
    my $self = shift;
    my %args = @_;

    # Conversion from generic types to Chart types
    my %types = (
        'bars'           => 'Bars',
        'composite'      => 'Composite', # non-standard
        'direction'      => 'Direction', # non-standard
        'errorbars'      => 'ErrorBars', # non-standard
        'horizontalbars' => 'HorizontalBars',
        'lines'          => 'Lines',
        'linespoints'    => 'LinesPoints',
        'mountain'       => 'Mountain',  # non-standard
        'pareto'         => 'Pareto',    # non-standard
        'pie'            => 'Pie',
        'points'         => 'Points',
        'split'          => 'Split',     # non-standard
        'stackedbars'    => 'StackedBars',
    );

    # Make sure the type is ready to be used as a Chart class name
    $args{type} = $types{ $args{type} } || undef;

    # Save the data for retrieval from the session later
    my $chart_id   = Jifty->web->serial;
    my $session_id = 'chart_' . $chart_id;
    Jifty->web->session->set( $session_id => Jifty::YAML::Dump(\%args) );

    # Build up the chart tag
    my $img;
    $img  = qq{<img};
    $img .= qq{ src="/chart/chart/$chart_id"};
    $img .= qq{ class="@{[ join ' ', @{ $args{class} } ]}"};

    my @styles;
    push @styles, "width:$args{width}"   if defined $args{width};
    push @styles, "height:$args{height}" if defined $args{height};

    $img .= qq{ style="@{[ join ';', @styles ]}"} if @styles;
    $img .= qq{/>};
    
    # Output the <img> tag and include the chart's configuration key
    Jifty->web->out($img);

    # Make sure we don't return anything that will get output
    return;
}

=head1 SEE ALSO

L<Jifty::Plugin::Chart>, L<Jifty::Plugin::Chart::Renderer>

=head1 AUTHOR

Andrew Sterling Hanenkamp C<< <andrew.hanenkamp@boomer.com> >>

=head1 COPYRIGHT AND LICENSE

Copyright 2007 Boomer Consulting, Inc.

This is free software and may be modified and distributed under the same terms as Perl itself.

=cut

1;