Jifty::Plugin::Chart::Renderer::SimpleBars - a simple horizontal bar chart


Jifty-Plugin-Chart documentation Contained in the Jifty-Plugin-Chart distribution.

Index


Code Index:

NAME

Top

Jifty::Plugin::Chart::Renderer::SimpleBars - a simple horizontal bar chart

DESCRIPTION

Top

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.

OPTIONS

Top

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".

It takes the following options:

summary

To maximize the accessibility of your chart, set this to describe the data. This will set the table's summary attribute.

STYLING

Top

Please be aware that when using this object, you can change the bar color using CSS like so:

  div.simple_bars span.bar {
      background-color: black;
  }

METHODS

Top

init

Tell Jifty about the CSS and JS files SimpleBars needs.

render

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 init.

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.

AUTHOR

Top

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

COPYRIGHT AND LICENSE

Top


Jifty-Plugin-Chart documentation Contained in the Jifty-Plugin-Chart distribution.
use strict;
use warnings;

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

sub init {
    Jifty->web->add_javascript('simple_bars.js');
    Jifty->web->add_css('simple_bars.css');
}

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

    # We only handle horizontalbars, fail on all else
    if ($args{type} ne 'horizontalbars') {
        die 'Sorry, SimpleBars charts only handle horizontalbars chart types.';
    }

    # Create a fresh ID for the chart
    my $chart_id = 'chart_' . Jifty->web->serial;

    # Add the simple_bars class for the JavaScript to find
    push @{ $args{class} }, 'simple_bars';

    # Build the table
    my $table;
    $table  = qq{<table id="$chart_id"};
    $table .= qq{ class="@{[ join ' ', @{ $args{class} } ]}"};
    $table .= qq{ summary="$args{summary}"} if $args{summary};
    $table .= qq{/><tbody>};

    for my $index (0 .. $#{ $args{data}[0] }) {
        my $label = $args{data}[0][$index];
        my $point = $args{data}[1][$index];

        $table .= '<tr>';
        $table .= "<td>@{[ Jifty->web->escape($label) ]}</td>";
        $table .= "<td>@{[ Jifty->web->escape($point) ]}</td>";
        $table .= '</tr>';
    }

    $table .= '</tbody></table>';

    Jifty->web->out($table);

    return;
}

1