Catalyst::View::Chart::Strip - A Catalyst View for Chart::Strip graphics


Catalyst-View-Chart-Strip documentation Contained in the Catalyst-View-Chart-Strip distribution.

Index


Code Index:

NAME

Top

Catalyst::View::Chart::Strip - A Catalyst View for Chart::Strip graphics

SYNOPSIS

Top

  package MyApp::View::ChartStrip;

  use strict;
  use base 'Catalyst::View::Chart::Strip';

  __PACKAGE__->config(
      cs_package => 'Chart::Strip',
      height => 192,
      width => 720,
      limit_factor => 1,
      transparent => 0,
      img_type => 'png',
      palette => [qw/
                     FF0000
                     00CC00
                     0000FF 
                     CC00CC 
                 /],
  );

  1;

  # A controller method which generates a chart:
  sub thechart : Local {
      my ( $self, $c ) = @_;

      [ ... generate $data and $opts somehow or other ... ]
      $c->stash->{chart_opts} = $opts;
      $c->stash->{chart_data} = $data;
      $c->forward('MyApp::View::ChartStrip');
  }

DESCRIPTION

Top

This view allows the serving of Chart::Strip stripchart graphics via Catalyst. The raw numeric data and various chart options are placed in $c->stash.

Instances of Catalyst::View::Chart::Strip, like MyApp::View::ChartStrip shown in the synopsis above, can be thought of as basically a collection of common defaults for the various chart options. You should probably create a seperate View class for each distinct style of charts your application commonly generates.

All of the standard constructor arguments documented by Chart::Strip are supported as ->config parameters in your View class, and are also overrideable at chart generation time via $c->stash->{chart_opts}.

Catalyst::View::Chart::Strip adds a few new options in addition to the ones that are standard in Chart::Strip, which are detailed below.

CONFIGURATION PARAMETERS

Top

(See Chart::Strip for a complete list of options. Any Chart::Strip option can be passed through as a ->config parameter).

All of these options are valid both a ->config time, or at chart generation time via $c->stash->{chart_opts}.

img_type

Sets the output image type. Values currently supported by Chart::Strip and GD beneath it are png and jpeg. The default is png if unspecified.

quality

This is the quality parameter for the output graphics data, as documented in detail by GD's documentation. Valid quality ranges are 0-100 for jpeg and 0-9 for png. Completely optional, and defaults to a reasonably normal value in both cases.

palette

An optional arrayref of colors as six-digit hexidecimal strings, like FFFFFF or 4A5C2D. The various datasets in your graph will be colored with the colors of this array in order, recycling to the top of the list if there are more data items than colors specified. The default is a reasonable 9-color high-contrast palette designed for a white background, which happens to also be the default.

cs_package

This allows choosing an alternative but compatible Chart::Strip implementation, such as Chart::Strip::Stacked. Defaults to the original Chart::Strip.

STASH VARIABLES

Top

As shown in the synopsis at the top, your chart is ultimately defined by the contents of two stash variables: $c->stash->{chart_opts}, and $c->stash->{chart_data}.

chart_opts is analogous to the configuration options described above for the View-wide ->config settings. Valid things here are all of the documented arguments to Chart::Strip's new() method, as well as the configuration parameters specifically details above.

chart_data should be an arrayref of sets of data to be charted. Each item in the arrayref should in turn be a hashref consisting of two keys: data and opts. These two keys are analogous to the two arguments of Chart::Strip's add_data method.

In other words, the following example standard Chart::Strip code:

  my $chart = Chart::Strip‐>new( title   => 'Happiness of our Group' );
  $chart‐>add_data( $davey_data, { style => 'line',
                                   color => 'FF0000',
                                   label => 'Davey' } );

  $chart‐>add_data( $jenna_data, { style => 'line',
                                   color => '00FF88',
                                   label => 'Jenna' } );

Becomes this in terms of stash variables:

   $c->stash->{chart_opts}->{title} = 'Happiness of our Group';
   $c->stash->{chart_data} = [
       { data => $davey_data, opts => { style => 'line',
                                        color => 'FF0000',
                                        label => 'Davey'  }
       },
       { data => $jenna_data, opts => { style => 'line',
                                        color => '00FF88',
                                        label => 'Jenna'  }
       },

   ];

Note that colors are completely optional for us, since we have a reasonable default palette. You need only neccesarily supply the style and label options for a reasonable chart.

See Catalyst::View::Chart::Strip::Example for a full-fledged controller action you can copy and paste as a working example.

METHODS

Top

new

Constructor for these Views. Mainly just defaults the above-documented View-specific options, and loads the selected cs_package package.

process

This does the chart generation itself. The bulk of the code is concerned with applying the palette to your data before constructing the Chart::Strip object and using it to generate the output binary image data.

SEE ALSO

Top

Catalyst, Catalyst::View, Catalyst::Helper::View::Chart::Strip, Catalyst::View::Chart::Strip::Example, Chart::Strip, Chart::Strip::Stacked

AUTHOR

Top

Brandon L Black, blblack@gmail.com

LICENSE

Top

You may distribute this code under the same terms as Perl itself.


Catalyst-View-Chart-Strip documentation Contained in the Catalyst-View-Chart-Strip distribution.
package Catalyst::View::Chart::Strip;

use strict;
use base qw/Catalyst::View/;
use UNIVERSAL::require;
use Carp;
use NEXT;

our $VERSION = '0.05';

# This default palette is hand-tweaked for contrast
# against white background on an RGB monitor for human eyes.
# The first 7 colors are very good, and the last 2 are decent
# enough for most purposes.  There is no perfect 9+ color
# high-constrast palette, and this is probably as good as it gets.

our $def_pal = [qw/
    FF0000
    00CC00
    0000FF 
    CC00CC 
    00BBDD 
    DDBB00
    000000
    666666
    557700
/];

sub new {
    my $self = shift->NEXT::new(@_);

    $self->{cs_package} ||= 'Chart::Strip';
    $self->{img_type} ||= 'png';
    $self->{palette} ||= $def_pal;
    $self->{cs_package}->require
        or croak "Cannot load Chart::Strip module '$self->{cs_package}'";

    $self;
}

sub process {
    my ($self, $c) = @_;

    my $opts = $c->stash->{chart_opts};
    my $data = $c->stash->{chart_data};

    my $chart   = $self->{cs_package}->new( %$self, %$opts );
    my $palette = $chart->{palette};

    # This is all in support of defaulted color palettes
    my $is_stacked = ($data->[0]->{opts}->{style} eq 'stacked');
    if($is_stacked) {
        my $stack = $data->[0];
        if( ! @{ $stack->{opts}->{colors} || [] } ) {
            my @stacked_colors;
            my $cnum = 0;
            my $ncolors_wanted = @{$stack->{data}->[0]->{values}};
            foreach (1..$ncolors_wanted) {
                unshift(@stacked_colors, $palette->[$cnum]);
                $cnum++;
                $cnum = 0 if $cnum > $#$palette;
            }
            $stack->{opts}->{colors} = \@stacked_colors;
            $chart->add_data($stack->{data}, $stack->{opts});
        }
    }
    else {
        my $cnum = 0;
        foreach (@$data) {
            $_->{opts}->{color} ||= $palette->[$cnum];
            $chart->add_data($_->{data}, $_->{opts});
            $cnum++;
            $cnum = 0 if $cnum > $#$palette;
        }
    }

    my $itype = $chart->{img_type};
    $c->response->content_type("image/$itype");
    $c->response->body($chart->$itype($chart->{quality}));
}

1;