use param to trasfer timefmt to gnuplot image
[MojoFacets.git] / lib / MojoFacets / Gnuplot.pm
1 package MojoFacets::Gnuplot;
2
3 use warnings;
4 use strict;
5
6 use base 'Mojolicious::Controller';
7
8 use Data::Dump qw(dump);
9 use Digest::MD5 qw(md5_hex);
10 use Text::Unaccent::PurePerl;
11
12 sub index {
13         my $self = shift;
14
15         my $columns = $self->session('columns') || $self->redirect_to('/data/columns');
16
17         my $url = '/export/' . $self->session('path') . '/' . unac_string( join('.', 'items', @$columns) );
18         my $dir = $self->app->home->rel_dir('public');
19
20         if ( -e "$dir/$url" ) {
21
22                 my @plot;
23                 foreach ( 1 .. $#$columns ) {
24                         my $n = $_ + 1;
25                         push @plot, qq|"$dir/$url" using 1:$n title "$columns->[$_]" with points|;
26                 }
27
28                 my $g = qq|
29
30 set terminal png
31 set output '$dir/$url.png'
32
33                 |;
34
35                 if ( my $timefmt = $self->param('timefmt') ) {
36                         $g .= qq|
37
38 set xdata time
39 set timefmt "$timefmt"
40 set format x "$timefmt"
41
42                         |;
43                 }
44
45 #set xrange [ "2009-01-01":"2010-01-01" ]
46 #set yrange [ 0 : ]
47
48                 $g .= "\n\nplot " . join(',', @plot) . "\n";
49
50 warn "gnuplot $g";
51
52                 open(my $gnuplot, '|-', 'gnuplot') || die "gnuplot $!";
53                 print $gnuplot $g;
54                 close $gnuplot;
55
56                 $self->redirect_to( "$url.png" );
57                 #$self->render_text( "$url.png" );
58         } else {
59                 $self->render_text("no graph for $url");
60         }
61 }
62
63 1;