2363fe1c653b06018bb69efec3f5585ee1d56bfe
[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 warn "# spaces: [$spaces]",dump( $spaces );
44                 $spaces = length( $spaces );
45 warn "# spaces: $spaces";
46
47                 my @plot;
48                 foreach ( 1 .. $#$columns ) {
49                         my $title = $columns->[$_];
50                         next if $hide_columns->{$title};
51                         $title =~ s/_/ /g;
52                         next if $hide_columns->{ $title };
53
54                         my $n = $_ + 1 + $spaces;
55                         push @plot, qq|"$dir/$url" using 1:$n notitle with $with lc $_|,
56                                                 qq|NaN lc $_ title "$title" with lines|
57                         ;
58                 }
59
60                 my $g = qq|
61
62 set terminal png size 1000,400
63 set output '$dir/$url.png'
64
65                 |;
66
67                 if ( $timefmt ) {
68                         $g .= qq|
69
70 set xdata time
71 set timefmt "$timefmt"
72 #set format x "$timefmt"
73 set format x "$timefmt_x"
74
75                         |;
76                 }
77
78 #set xrange [ "2009-01-01":"2010-01-01" ]
79 #set yrange [ 0 : ]
80
81                 $g .= "\n\nplot " . join(',', @plot) . "\n";
82                 $g =~ s/\n\n+/\n/sg;
83
84 warn "gnuplot $g";
85
86                 open(my $gnuplot, '|-', 'gnuplot') || die "gnuplot $!";
87                 print $gnuplot $g;
88                 close $gnuplot;
89
90                 if ( -e "$dir/$url.png" ) {
91                         warn "redirect $url.png";
92                         return $self->redirect_to( "$url.png" );
93                 } else {
94                         $self->render_text( "no $dir/$url.png" );
95                 }
96         } else {
97                 $self->render_text("no graph for $url");
98         }
99 }
100
101 1;