[Bps-public-commit] r11199 - WebChart/lib/WebChart/Renderer

sunnavy at bestpractical.com sunnavy at bestpractical.com
Wed Mar 26 12:37:03 EDT 2008


Author: sunnavy
Date: Wed Mar 26 12:37:03 2008
New Revision: 11199

Added:
   WebChart/lib/WebChart/Renderer/Chart.pm

Log:
added Chart renderer

Added: WebChart/lib/WebChart/Renderer/Chart.pm
==============================================================================
--- (empty file)
+++ WebChart/lib/WebChart/Renderer/Chart.pm	Wed Mar 26 12:37:03 2008
@@ -0,0 +1,83 @@
+package WebChart::Renderer::Chart;
+use strict;
+use warnings;
+use Carp;
+
+package WebChart::Renderer::Chart;
+use base qw/ WebChart::Renderer /;
+
+=head2 render
+
+accept the same args as WebChart::render
+in scalar context, returns html segment like <img src="..." />
+in list context, return html segment and the actual file path of the chart.
+
+=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} } if $types{ $args{type} };
+
+    my $class = "Chart::$args{type}";
+    $class->require or die "can't require $class: $!";
+
+    my $chart = $class->new( $args{width}, $args{height} );
+    $chart->set( %{ $args{options} } ) if $args{options};
+
+    require File::Temp;
+    my ( $fd, $filename ) = File::Temp::tempfile(
+        'wc_XXXXXX',
+        SUFFIX  => '.png',
+        DIR     => $args{img_dir},
+        CLEANUP => 0,
+    );
+    $chart->png( $fd, $args{data} );
+
+    require File::Spec;
+
+    my $seg = qq[<img src="$args{web_img_dir}/]
+      . ( File::Spec->splitpath($filename) )[2] . q{"};
+
+    if ( @{ $args{css_classes} } ) {
+        $seg .= q{ class="} . join( ' ', @{ $args{css_classes} } ) . q{"};
+    }
+
+    $seg .= q[ />];
+
+    return wantarray ? ( $seg, $filename ) : $seg;
+}
+
+1;
+
+__END__
+
+=head1 AUTHOR
+
+sunnavy  C<< <sunnavy at bestpractical.com> >>
+
+=head1 LICENCE AND COPYRIGHT
+
+Copyright 2008 Best Practical Solutions.
+
+This program is free software; you can redistribute it and/or modify it
+under the same terms as Perl itself.



More information about the Bps-public-commit mailing list