even more agressicly script export path
[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 $name = unac_string( join('.', 'items', @$columns) );
18         $name =~ s/\W+/_/g;
19
20         warn "# name $name\n";
21
22         my $url = '/export/' . $self->session('path') . '/' . $name;
23         my $dir = $self->app->home->rel_dir('public');
24
25         if ( -e "$dir/$url" ) {
26
27                 my @plot;
28                 foreach ( 1 .. $#$columns ) {
29                         my $n = $_ + 1;
30                         push @plot, qq|"$dir/$url" using 1:$n title "$columns->[$_]" with points|;
31                 }
32
33                 my $g = qq|
34
35 set terminal png
36 set output '$dir/$url.png'
37
38                 |;
39
40                 if ( my $timefmt = $self->param('timefmt') ) {
41                         $g .= qq|
42
43 set xdata time
44 set timefmt "$timefmt"
45 set format x "$timefmt"
46
47                         |;
48                 }
49
50 #set xrange [ "2009-01-01":"2010-01-01" ]
51 #set yrange [ 0 : ]
52
53                 $g .= "\n\nplot " . join(',', @plot) . "\n";
54
55 warn "gnuplot $g";
56
57                 open(my $gnuplot, '|-', 'gnuplot') || die "gnuplot $!";
58                 print $gnuplot $g;
59                 close $gnuplot;
60
61                 $self->redirect_to( "$url.png" );
62                 #$self->render_text( "$url.png" );
63         } else {
64                 $self->render_text("no graph for $url");
65         }
66 }
67
68 1;