/[simile]/sparklines/create.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /sparklines/create.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 19 by dpavlin, Fri Oct 17 10:59:47 2008 UTC revision 21 by dpavlin, Fri Oct 17 11:16:24 2008 UTC
# Line 10  use strict; Line 10  use strict;
10  use GD::Graph::sparklines;  use GD::Graph::sparklines;
11  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
12  use File::Slurp;  use File::Slurp;
13    use JSON;
14    
15  my $data;  my $json;
16    
17  my $max_month = 0;  my $max_month = 0;
18    
# Line 19  foreach my $file ( @ARGV ) { Line 20  foreach my $file ( @ARGV ) {
20    
21          open(my $fh, '<', $file) or die "$file: $!";          open(my $fh, '<', $file) or die "$file: $!";
22    
23            my $data;
24          my $type = $1 if ($file =~ /monthly-([^\.]+)/);          my $type = $1 if ($file =~ /monthly-([^\.]+)/);
25    
26          while(<$fh>) {          while(<$fh>) {
# Line 27  foreach my $file ( @ARGV ) { Line 29  foreach my $file ( @ARGV ) {
29    
30                  if ( ! defined $data->{$instance} ) {                  if ( ! defined $data->{$instance} ) {
31                          $data->{$instance} = [];                          $data->{$instance} = [];
32                            $json->{$instance}->{$type}->{sum} = 0;
33                            $json->{$instance}->{$type}->{min} = 0;
34                            $json->{$instance}->{$type}->{max} = 0;
35                  }                  }
36    
37                  $max_month = $month if $month > $max_month;                  $max_month = $month if $month > $max_month;
# Line 44  foreach my $file ( @ARGV ) { Line 49  foreach my $file ( @ARGV ) {
49                          push @x, $month;                          push @x, $month;
50                          my $y = $data->{$instance}->[$month] || 0;                          my $y = $data->{$instance}->[$month] || 0;
51                          push @y, $y;                          push @y, $y;
52                            $json->{$instance}->{$type}->{min} = $y if $json->{$instance}->{$type}->{min} > $y;
53                            $json->{$instance}->{$type}->{max} = $y if $json->{$instance}->{$type}->{max} < $y;
54                            $json->{$instance}->{$type}->{sum} += $y;
55                  }                  }
56                  warn "x = ",dump( @x ), "\ny = ", dump( @y );                  warn "x = ",dump( @x ), "\ny = ", dump( @y );
57                  my $gd = $graph->plot( [ \@x, \@y ] ) or die $graph->error;                  my $gd = $graph->plot( [ \@x, \@y ] ) or die $graph->error;
58                  write_file( "s/$instance-$type.png", $gd->png );                          my $path = "s/$instance-$type.png";
59                    write_file( $path, $gd->png );
60                    $json->{$instance}->{$type}->{s} = $path;
61          }          }
62    
63          close($fh);          close($fh);
64  }  }
65    
66    warn "# json = ",dump( $json );
67    my @items;
68    foreach my $instance ( keys %$json ) {
69            foreach my $type ( keys %{ $json->{$instance} } ) {
70                    push @items, {
71                            id => $instance,
72                            label => $instance,
73                            url => "http://www.$instance.skole.hr",
74                            %{ $json->{$instance}->{$type} },
75                    };
76            }
77    }
78    
79    write_file( 'sparklines.js', to_json({ items => \@items }) );
80    

Legend:
Removed from v.19  
changed lines
  Added in v.21

  ViewVC Help
Powered by ViewVC 1.1.26