57dd423972044ab8adb32134345bb94b10c934a4
[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 use MojoFacets::Data;
12
13 sub index {
14         my $self = shift;
15
16         my $columns = $self->session('columns') || return $self->redirect_to('/data/columns');
17         my $path    = $self->session('path')    || return $self->redirect_to('/data/load');
18         my $with    = $self->param('with') || 'points';
19
20         my $gnuplot_hide = $self->every_param('gnuplot_hide');
21         warn "## gnuplot_hide=",dump( $gnuplot_hide );
22         my $hide_columns;
23         $hide_columns->{$_}++ foreach @$gnuplot_hide;
24         warn "## hide_columns ", dump $hide_columns;
25
26 #       my $name = join('.', 'items', map { my $n = unac_string($_); $n =~ s/\W+/_/g; $n } @$columns );
27         my $name = MojoFacets::Data::__export_path_name( $path, 'items', @$columns );
28
29         warn "# name $name\n";
30
31         my $url = "/export/$path/$name";
32         my $dir = $self->app->home->rel_file('public');
33
34         if ( -e "$dir/$url" ) {
35
36                 my $timefmt = $self->param('timefmt');
37
38                 my $timefmt_x = $timefmt;
39                 $timefmt_x =~ s/[ T]%H/\\n%H/;
40
41                 my $spaces = $timefmt;
42                 $spaces =~ s/\S//g;
43                 $spaces = length( $spaces );
44
45                 my @plot;
46                 foreach ( 1 .. $#$columns ) {
47                         my $title = $columns->[$_];
48                         next if $hide_columns->{$title};
49                         $title =~ s/_/ /g;
50                         next if $hide_columns->{ $title };
51
52                         my $n = $_ + 1 + $spaces;
53                         push @plot, qq|"$dir/$url" using 1:$n notitle with $with lc $_|,
54                                                 qq|NaN lc $_ title "$title" with lines|
55                         ;
56                 }
57
58                 my $g = qq|
59
60 set terminal png size 1000,400
61 set output '$dir/$url.png'
62
63                 |;
64
65                 if ( $timefmt ) {
66                         $g .= qq|
67
68 set xdata time
69 set timefmt "$timefmt"
70 #set format x "$timefmt"
71 set format x "$timefmt_x"
72
73                         |;
74                 }
75
76 #set xrange [ "2009-01-01":"2010-01-01" ]
77 #set yrange [ 0 : ]
78
79                 $g .= "\n\nplot " . join(',', @plot) . "\n";
80                 $g =~ s/\n\n+/\n/sg;
81
82 #warn "gnuplot $g";
83
84                 open(my $gnuplot, '|-', 'gnuplot') || die "gnuplot $!";
85                 print $gnuplot $g;
86                 close $gnuplot;
87
88                 if ( -e "$dir/$url.png" ) {
89                         warn "redirect $url.png";
90                         return $self->redirect_to( "$url.png" );
91                 } else {
92                         $self->render_text( "no $dir/$url.png" );
93                 }
94         } else {
95                 $self->render_text("no graph for $url");
96         }
97 }
98
99 1;