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