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

Jifty commits jifty-commit at lists.jifty.org
Tue Sep 16 03:33:34 EDT 2008


Author: sartak
Date: Tue Sep 16 03:33:34 2008
New Revision: 5846

Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/Makefile.PL
   jifty/trunk/lib/Jifty/Plugin/Chart/Renderer/Google.pm

Log:
 r71124 at onn:  sartak | 2008-09-08 15:11:51 -0400
 Support for allowing different min/max for each dataset in the Google renderer


Modified: jifty/trunk/Makefile.PL
==============================================================================
--- jifty/trunk/Makefile.PL	(original)
+++ jifty/trunk/Makefile.PL	Tue Sep 16 03:33:34 2008
@@ -52,6 +52,7 @@
 requires('IPC::PubSub' => '0.23' );
 requires('IPC::Run3');
 requires('Jifty::DBI' => '0.49' );            # Jifty::DBI::Collection Jifty::DBI::Handle Jifty::DBI::Record::Cachable Jifty::DBI::SchemaGenerator
+requires('List::MoreUtils');
 requires('Locale::Maketext::Extract' => '0.20');
 requires('Locale::Maketext::Lexicon' => '0.60');
 requires('Log::Log4perl' => '1.04'); # Log::Log4perl::Appender

Modified: jifty/trunk/lib/Jifty/Plugin/Chart/Renderer/Google.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin/Chart/Renderer/Google.pm	(original)
+++ jifty/trunk/lib/Jifty/Plugin/Chart/Renderer/Google.pm	Tue Sep 16 03:33:34 2008
@@ -6,6 +6,7 @@
 
 use URI::Escape qw(uri_escape);
 use List::Util qw(max min sum);
+use List::MoreUtils qw(mesh);
 use Scalar::Util qw(looks_like_number);
 
 =head1 NAME
@@ -73,6 +74,10 @@
     $args{'width'} =~ s/px$//;
     $args{'height'} =~ s/px$//;
 
+    # a bit of dwim
+    $args{'min_value'} ||= delete $args{'min_values'};
+    $args{'max_value'} ||= delete $args{'max_values'};
+
     # Check size and die if wrong
     for ( qw(width height) ) {
         if ( $type eq 't' ) {
@@ -187,28 +192,43 @@
             }
         }
 
-        my $min = $args{'min_value'} - $args{'min_minus'};
-        my $max = $args{'max_value'} + $args{'max_plus'};
-        
-        $args{'calculated_min'} = $min;
-        $args{'calculated_max'} = $max;
+        for ('min_value', 'max_value') {
+            $args{$_} = [ $args{$_} ] if !ref($args{$_});
+        }
+
+        my @min = map { $_ - $args{'min_minus'} } @{ $args{'min_value'} };
+        my @max = map { $_ - $args{'max_plus'}  } @{ $args{'max_value'} };
+
+        # repeat if necessary
+        #push @min, ($min[-1]) x (@data - @min);
+        #push @max, ($max[-1]) x (@data - @max);
+
+        $args{'calculated_min'} = \@min;
+        $args{'calculated_max'} = \@max;
 
         # Format the min and max for use a few lines down
         unless ( not defined $args{'format'} ) {
-            $min = sprintf $args{'format'}, $min;
-            $max = sprintf $args{'format'}, $max;
+            @min = map { sprintf $args{'format'}, $_ } @min;
+            @max = map { sprintf $args{'format'}, $_ } @max;
         }
 
         # If it's a number, pass it through, otherwise replace it with a
         # number out of range to mark it as undefined
         my @data;
-        for my $data ( @{$args{'data'}} ) {
-            push @data, [map { looks_like_number($_) ? $_ : $min-42 } @$data];
+        for my $data_idx ( 0 .. @{$args{'data'}}-1 ) {
+            push @data, [
+                map {
+                    looks_like_number($_)
+                    ? $_
+                    : $min[$data_idx] - 42
+                } @{ $args{'data'}[$data_idx] }
+            ];
         }
 
         # Let's do text encoding with data scaling
         $url .= "&chd=t:" . join '|', map { join ',', @$_ } @data;
-        $url .= "&chds=$min,$max";
+
+        $url .= "&chds=" . join(',', mesh @min, @max);
     }
 
     # Add a title
@@ -240,7 +260,9 @@
                 }
                 elsif ( not ref $labelset and $labelset eq 'RANGE' ) {
                     push @ranges, sprintf "%d,$args{'format'},$args{'format'}",
-                                           $index, $args{'calculated_min'}, $args{'calculated_max'};
+                                           $index,
+                                           $args{'calculated_min'}[$index],
+                                           $args{'calculated_max'}[$index];
                 }
                 $index++;
             }
@@ -276,6 +298,7 @@
     # Add shape/range markers
     if ( @{ $args{'markers'} } ) {
         my @markers;
+        my $index = 0;
         for my $data ( @{$args{'markers'}} ) {
             my %marker = (
                 type     => 'x',
@@ -290,18 +313,18 @@
             # Calculate where the position should be for horizontal lines
             if ( $marker{'type'} eq 'h' ) {
                 $marker{'position'} = $self->_position_in_range( $marker{'position'},
-                                                                 $args{'calculated_min'},
-                                                                 $args{'calculated_max'} );
+                                                                 $args{'calculated_min'}[$index],
+                                                                 $args{'calculated_max'}[$index] );
             }
             # Calculate where the position should be for ranges
             elsif ( lc($marker{'type'}) eq 'r' ) {
                 for (qw( start end )) {
-                    $marker{$_} = $args{'calculated_min'} if $marker{$_} eq 'MIN';
-                    $marker{$_} = $args{'calculated_max'} if $marker{$_} eq 'MAX';
+                    $marker{$_} = $args{'calculated_min'}[$index] if $marker{$_} eq 'MIN';
+                    $marker{$_} = $args{'calculated_max'}[$index] if $marker{$_} eq 'MAX';
 
                     $marker{$_} = $self->_position_in_range( $marker{$_},
-                                                             $args{'calculated_min'},
-                                                             $args{'calculated_max'} );
+                                                             $args{'calculated_min'}[$index],
+                                                             $args{'calculated_max'}[$index] );
                 }
             }
             # Fix text type
@@ -321,6 +344,7 @@
             push @markers, join(',', @marker{qw( type color dataset position size priority )});
         }
         $url .= "&chm=" . join '|', @markers if @markers;
+        ++$index;
     }
 
     Jifty->web->out( qq{<img src="$url" />} );
@@ -343,21 +367,23 @@
 
 # Borrowed with slight modifications from Google::Chart::Data::SimpleEncoding
 sub _simple_encode_data {
-    my $self = shift;
-    my $max  = shift;
-    my $data = shift;
+    my $self  = shift;
+    my $maxes = shift;
+    my $data  = shift;
 
+    my $i = 0;
     my $result = '';
     my @map = ('A'..'Z', 'a'..'z', 0..9);
     for my $value ( @$data ) {
         if ( looks_like_number($value) ) {
-            my $index = int($value / $max * (@map - 1));
+            my $index = int($value / $maxes->[$i] * (@map - 1));
             $index = 0 if $index < 0;
             $index = @map if $index > @map;
             $result .= $map[$index];
         } else {
             $result .= '_';
         }
+        ++$i;
     }
     return $result;
 }


More information about the Jifty-commit mailing list