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