1 package MojoFacets::Gnuplot;
6 use base 'Mojolicious::Controller';
8 use Data::Dump qw(dump);
9 use Digest::MD5 qw(md5_hex);
10 use Text::Unaccent::PurePerl;
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';
20 my $gnuplot_hide = $self->every_param('gnuplot_hide');
21 warn "## gnuplot_hide=",dump( $gnuplot_hide );
23 $hide_columns->{$_}++ foreach @$gnuplot_hide;
24 warn "## hide_columns ", dump $hide_columns;
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 );
29 warn "# name $name\n";
31 my $url = "/export/$path/$name";
32 my $dir = $self->app->home->rel_file('public');
34 if ( -e "$dir/$url" ) {
36 my $timefmt = $self->param('timefmt');
38 my $timefmt_x = $timefmt;
39 $timefmt_x =~ s/[ T]%H/\\n%H/;
41 my $spaces = $timefmt;
43 warn "# spaces: [$spaces]",dump( $spaces );
44 $spaces = length( $spaces );
45 warn "# spaces: $spaces";
48 foreach ( 1 .. $#$columns ) {
49 my $title = $columns->[$_];
50 next if $hide_columns->{$title};
52 next if $hide_columns->{ $title };
54 my $n = $_ + 1 + $spaces;
55 push @plot, qq|"$dir/$url" using 1:$n notitle with $with lc $_ pt 7 ps 0.5|, # pt 7 - circle, ps 2 - size 2
56 qq|NaN lc $_ title "$title" with lines|
62 set terminal png size 1000,400
63 set output '$dir/$url.png'
71 set timefmt "$timefmt"
72 #set format x "$timefmt"
73 set format x "$timefmt_x"
78 #set xrange [ "2009-01-01":"2010-01-01" ]
81 $g .= "\n\nplot " . join(',', @plot) . "\n";
86 open(my $gnuplot, '|-', 'gnuplot') || die "gnuplot $!";
90 if ( -e "$dir/$url.png" ) {
91 warn "redirect $url.png";
92 return $self->redirect_to( "$url.png" );
94 $self->render_text( "no $dir/$url.png" );
97 $self->render_text("no graph for $url");