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

« back to all changes in this revision

Viewing changes to lib/Jifty/Plugin/Chart/Renderer/GD/Graph.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::GD::Graph;
 
5
use base qw/ Jifty::Plugin::Chart::Renderer /;
 
6
 
 
7
=head1 NAME
 
8
 
 
9
Jifty::Plugin::Chart::Renderer::GD::Graph - A chart renderer using GD::Graph
 
10
 
 
11
=head1 SYNOPSIS
 
12
 
 
13
In F<config.yml>:
 
14
 
 
15
  Plugins:
 
16
    - Chart:
 
17
        DefaultRenderer: Jifty::Plugin::Chart::Renderer::GD::Graph
 
18
 
 
19
=head1 DESCRIPTION
 
20
 
 
21
This is a chart renderer that uses L<GD::Graph> to build charts.
 
22
 
 
23
=head1 METHODS
 
24
 
 
25
=head2 init
 
26
 
 
27
Adds the F<chart_img_behaviour.js> script to those loaded.
 
28
 
 
29
=cut
 
30
 
 
31
sub init {
 
32
    Jifty->web->add_javascript('chart_img_behaviour.js');
 
33
}
 
34
 
 
35
=head2 render
 
36
 
 
37
Renders an IMG tag referring to the L<GD::Graph> image view.
 
38
 
 
39
=cut
 
40
 
 
41
sub render {
 
42
    my $self = shift;
 
43
    my %args = @_;
 
44
 
 
45
    # GD::Graph types from generic types
 
46
    my %types = (
 
47
        lines          => 'lines',
 
48
        bars           => 'bars',
 
49
        horizontalbars => 'hbars',
 
50
        points         => 'points',
 
51
        linespoints    => 'linespoints', # non-standart
 
52
        area           => 'area',
 
53
        pie            => 'pie',
 
54
        mixed          => 'mixed', # non-standard
 
55
    );
 
56
 
 
57
    # Convert the generic type to a GD::Graph type
 
58
    $args{type} = $types{ $args{type} } || undef;
 
59
 
 
60
    # Save the data for retrieval from the session later
 
61
    my $chart_id   = Jifty->web->serial;
 
62
    my $session_id = 'chart_' . $chart_id;
 
63
    Jifty->web->session->set( $session_id => Jifty::YAML::Dump(\%args) );
 
64
 
 
65
    # Build up the chart tag
 
66
    my $img;
 
67
    $img  = qq{<img};
 
68
    $img .= qq{ src="/chart/gd_graph/$chart_id"};
 
69
    $img .= qq{ class="@{[ join ' ', @{ $args{class} } ]}"};
 
70
 
 
71
    my @styles;
 
72
    push @styles, "width:$args{width}"   if defined $args{width};
 
73
    push @styles, "height:$args{height}" if defined $args{height};
 
74
 
 
75
    $img .= qq{ style="@{[ join ';', @styles ]}"} if @styles;
 
76
    $img .= qq{/>};
 
77
 
 
78
    # Output the <img> tag and include the chart's configuration key
 
79
    Jifty->web->out($img);
 
80
 
 
81
    # Make sure we don't return anything that will get output
 
82
    return;
 
83
}
 
84
 
 
85
=head1 AUTHOR
 
86
 
 
87
Andrew Sterling Hanenkamp C<< <andrew.hanenkamp@boomer.com> >>
 
88
 
 
89
=head1 COPYRIGHT AND LICENSE
 
90
 
 
91
Copyright 2007 Boomer Consulting, Inc.
 
92
 
 
93
This is free software and may be modified and distributed under the same terms as Perl itself.
 
94
 
 
95
=cut
 
96
 
 
97
1;