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