[Jifty-commit] r3750 - in jifty/trunk: . lib/Jifty/Plugin/Chart/Renderer

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Tue Jul 31 22:33:10 EDT 2007


Author: sterling
Date: Tue Jul 31 22:33:09 2007
New Revision: 3750

Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/lib/Jifty/Plugin/Chart/Dispatcher.pm
   jifty/trunk/lib/Jifty/Plugin/Chart/Renderer/Chart.pm
   jifty/trunk/lib/Jifty/Plugin/Chart/View.pm

Log:
 r8270 at dynpc145:  andrew | 2007-07-31 21:31:01 -0500
 Moved the chart/* dispatch to chart/chart/* to make room for alternate charting mechanisms.


Modified: jifty/trunk/lib/Jifty/Plugin/Chart/Dispatcher.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin/Chart/Dispatcher.pm	(original)
+++ jifty/trunk/lib/Jifty/Plugin/Chart/Dispatcher.pm	Tue Jul 31 22:33:09 2007
@@ -12,13 +12,13 @@
 
 =head1 RULES
 
-=head2 chart/*
+=head2 chart/chart/*
 
 Grabs the chart configuration stored in the key indicated in C<$1> and unpacks it using L<YAML>. It then passes it to the L<Jifty::Plugin::Chart::View/chart> template.
 
 =cut
 
-on 'chart/*' => run {
+on 'chart/chart/*' => run {
     # Create a session ID to lookup the chart configuration
     my $session_id = 'chart_' . $1;
 
@@ -40,7 +40,38 @@
 
     # Send them on to chart the chart
     set 'args' => $args;
-    show 'chart';
+    show 'chart/chart'
+};
+
+=head2 chart/gd_graph/*
+
+Grabs the chart configuration stored in the key indicated in C<$1> and unpacks it using L<YAML>. It then passes it to the L<Jifty::Plugin::Chart::View/chart> template.
+
+=cut
+
+on 'chart/gd_graph/*' => run {
+    # Create a session ID to lookup the chart configuration
+    my $session_id = 'chart_' . $1;
+
+    # Unpack the data and then clear it from the session
+    my $args = Jifty::YAML::Load( Jifty->web->session->get( $session_id ) );
+    Jifty->web->session->remove( $session_id );
+
+    # No data? Act like a 404
+    last_rule unless defined $args;
+
+    # Use the "type" to determine which class to use
+    my $class = 'GD::Graph::' . $args->{type};
+
+    # Load that class or die if it does not exist
+    #$class->require;
+
+    # Remember the class name for the view
+    $args->{class} = $class;
+
+    # Send them on to chart the chart
+    set 'args' => $args;
+    show 'chart/gd_graph'
 };
 
 =head1 SEE ALSO

Modified: jifty/trunk/lib/Jifty/Plugin/Chart/Renderer/Chart.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin/Chart/Renderer/Chart.pm	(original)
+++ jifty/trunk/lib/Jifty/Plugin/Chart/Renderer/Chart.pm	Tue Jul 31 22:33:09 2007
@@ -26,11 +26,6 @@
     my $self = shift;
     my %args = @_;
 
-    # Turn any subs into values returned
-    for my $key (keys %args) {
-        $args{$key} = $args{$key}->(\%args) if ref $args{$key} eq 'CODE';
-    }
-
     # Make sure the type is ready to be used as a class name
     $args{type} = ucfirst lc $args{type};
 
@@ -40,7 +35,7 @@
     Jifty->web->session->set( $session_id => Jifty::YAML::Dump(\%args) );
 
     # Output the <img> tag and include the chart's configuration key
-    Jifty->web->out(qq{<img src="/chart/$chart_id" width="$args{width}" height="$args{height}"/>});
+    Jifty->web->out(qq{<img src="/chart/chart/$chart_id" width="$args{width}" height="$args{height}"/>});
 
     # Make sure we don't return anything that will get output
     return;

Modified: jifty/trunk/lib/Jifty/Plugin/Chart/View.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin/Chart/View.pm	(original)
+++ jifty/trunk/lib/Jifty/Plugin/Chart/View.pm	Tue Jul 31 22:33:09 2007
@@ -10,7 +10,7 @@
 
 =head1 TEMPLATES
 
-=head2 chart
+=head2 chart/chart
 
 This shows a chart using L<Chart>. It expects to find the arguments in the C<args> parameter, which is setup for it in L<Jifty::Plugin::Chart::Dispatcher>.
 
@@ -18,11 +18,10 @@
 
 =cut
 
-template 'chart' => sub {
+template 'chart/chart' => sub {
     # Load the arguments
     my $args = get 'args';
 
-
     # Set the output type to the PNG file type
     Jifty->handler->apache->content_type('image/png');
 
@@ -40,6 +39,36 @@
     }
 };
 
+=head2 chart/gd_graph
+
+This shows a chart using L<GD::Graph>. It expects to find the arguments in the C<args> parameter, which is setup for it in L<Jifty::Plugin::Chart::Dispatcher>.
+
+This will output a PNG file unless there is an error building the chart.
+
+=cut
+
+template 'chart/gd_graph' => sub {
+    # Load the arguments
+    my $args = get 'args';
+
+    # Set the output type to the PNG file type
+    Jifty->handler->apache->content_type('image/png');
+
+    # Render the chart and output the PNG file generated
+    eval {
+        my $graph = $args->{class}->new( $args->{width}, $args->{height} );
+        my $gd    = $graph->plot($args->{data})
+            or die $graph->error;
+        outs_raw($gd->png);
+    };
+
+    # Should have thrown an error if bad stuff happened, handle that
+    if ($@) {
+        Jifty->log->error("Failed to render chart: $@");
+        die $@;
+    }
+};
+
 =head1 SEE ALSO
 
 L<Jifty::Plugin::Chart::Dispatcher>


More information about the Jifty-commit mailing list