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

« back to all changes in this revision

Viewing changes to lib/Jifty/Plugin/Chart/Renderer/SimpleBars.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::SimpleBars;
 
5
use base qw/ Jifty::Plugin::Chart::Renderer /;
 
6
 
 
7
=head1 NAME
 
8
 
 
9
Jifty::Plugin::Chart::Renderer::SimpleBars - a simple horizontal bar chart
 
10
 
 
11
=head1 DESCRIPTION
 
12
 
 
13
This is a simple renderer for charts created both as a dead simple way of rendering horizontal bar charts, which can be a very simple way of rendering data, and as a prototype for some other work I'm thinking of doing with the chart plugin.
 
14
 
 
15
=head1 OPTIONS
 
16
 
 
17
Of the rendering API, this only uses the first dataset given and ignores any others. It also fails if used for any type other than the only one it supports "horizontalbars".
 
18
 
 
19
It takes the following options:
 
20
 
 
21
=over
 
22
 
 
23
=item summary
 
24
 
 
25
To maximize the accessibility of your chart, set this to describe the data. This will set the table's summary attribute.
 
26
 
 
27
=back
 
28
 
 
29
=head1 STYLING
 
30
 
 
31
Please be aware that when using this object, you can change the bar color
 
32
using CSS like so:
 
33
 
 
34
  div.simple_bars span.bar {
 
35
      background-color: black;
 
36
  }
 
37
 
 
38
=head1 METHODS
 
39
 
 
40
=head2 init
 
41
 
 
42
Tell Jifty about the CSS and JS files SimpleBars needs.
 
43
 
 
44
=cut
 
45
 
 
46
sub init {
 
47
    Jifty->web->add_javascript('simple_bars.js');
 
48
    Jifty->web->add_css('simple_bars.css');
 
49
}
 
50
 
 
51
=head2 render
 
52
 
 
53
Renders a horizontal bar chart. This is done by rendering a table of HTML values, which is then converted to a bar chart by the Javascript added to the response during L</init>.
 
54
 
 
55
If JavaScript is not supported by the browser, all the data is presented ina table. They can still read the data, but just not in the most readable form.
 
56
 
 
57
=cut
 
58
 
 
59
sub render {
 
60
    my $self = shift;
 
61
    my %args = @_;
 
62
 
 
63
    # We only handle horizontalbars, fail on all else
 
64
    if ($args{type} ne 'horizontalbars') {
 
65
        die 'Sorry, SimpleBars charts only handle horizontalbars chart types.';
 
66
    }
 
67
 
 
68
    # Create a fresh ID for the chart
 
69
    my $chart_id = 'chart_' . Jifty->web->serial;
 
70
 
 
71
    # Add the simple_bars class for the JavaScript to find
 
72
    push @{ $args{class} }, 'simple_bars';
 
73
 
 
74
    # Build the table
 
75
    my $table;
 
76
    $table  = qq{<table id="$chart_id"};
 
77
    $table .= qq{ class="@{[ join ' ', @{ $args{class} } ]}"};
 
78
    $table .= qq{ summary="$args{summary}"} if $args{summary};
 
79
    $table .= qq{/><tbody>};
 
80
 
 
81
    for my $index (0 .. $#{ $args{data}[0] }) {
 
82
        my $label = $args{data}[0][$index];
 
83
        my $point = $args{data}[1][$index];
 
84
 
 
85
        $table .= '<tr>';
 
86
        $table .= "<td>@{[ Jifty->web->escape($label) ]}</td>";
 
87
        $table .= "<td>@{[ Jifty->web->escape($point) ]}</td>";
 
88
        $table .= '</tr>';
 
89
    }
 
90
 
 
91
    $table .= '</tbody></table>';
 
92
 
 
93
    Jifty->web->out($table);
 
94
 
 
95
    return;
 
96
}
 
97
 
 
98
=head1 AUTHOR
 
99
 
 
100
Andrew Sterling Hanenkamp, C<< <andrew.hanenkamp@boomer.com> >>
 
101
 
 
102
=head1 COPYRIGHT AND LICENSE
 
103
 
 
104
Copyright 2007 Boomer Consulting, Inc.
 
105
 
 
106
This is free software. You may modify and redistribute it under the same terms as Perl itself.
 
107
 
 
108
=cut
 
109
 
 
110
1