stable colors, line in legend, two line x tick timestamp
[MojoFacets.git] / lib / MojoFacets / Gnuplot.pm
index fc79de5..57dd423 100644 (file)
@@ -8,6 +8,7 @@ use base 'Mojolicious::Controller';
 use Data::Dump qw(dump);
 use Digest::MD5 qw(md5_hex);
 use Text::Unaccent::PurePerl;
+use MojoFacets::Data;
 
 sub index {
        my $self = shift;
@@ -16,11 +17,11 @@ sub index {
        my $path    = $self->session('path')    || return $self->redirect_to('/data/load');
        my $with    = $self->param('with') || 'points';
 
+       my $gnuplot_hide = $self->every_param('gnuplot_hide');
+       warn "## gnuplot_hide=",dump( $gnuplot_hide );
        my $hide_columns;
-       if ( $self->param('gnuplot_hide') ) {
-               $hide_columns->{$_}++ foreach $self->param('gnuplot_hide');
-               warn "## hide_columns ", dump $hide_columns;
-       }
+       $hide_columns->{$_}++ foreach @$gnuplot_hide;
+       warn "## hide_columns ", dump $hide_columns;
 
 #      my $name = join('.', 'items', map { my $n = unac_string($_); $n =~ s/\W+/_/g; $n } @$columns );
        my $name = MojoFacets::Data::__export_path_name( $path, 'items', @$columns );
@@ -28,30 +29,46 @@ sub index {
        warn "# name $name\n";
 
        my $url = "/export/$path/$name";
-       my $dir = $self->app->home->rel_dir('public');
+       my $dir = $self->app->home->rel_file('public');
 
        if ( -e "$dir/$url" ) {
 
+               my $timefmt = $self->param('timefmt');
+
+               my $timefmt_x = $timefmt;
+               $timefmt_x =~ s/[ T]%H/\\n%H/;
+
+               my $spaces = $timefmt;
+               $spaces =~ s/\S//g;
+               $spaces = length( $spaces );
+
                my @plot;
                foreach ( 1 .. $#$columns ) {
                        my $title = $columns->[$_];
-                       my $n = $_ + 1;
-                       push @plot, qq|"$dir/$url" using 1:$n title "$title" with $with| unless $hide_columns->{ $title };
+                       next if $hide_columns->{$title};
+                       $title =~ s/_/ /g;
+                       next if $hide_columns->{ $title };
+
+                       my $n = $_ + 1 + $spaces;
+                       push @plot, qq|"$dir/$url" using 1:$n notitle with $with lc $_|,
+                                               qq|NaN lc $_ title "$title" with lines|
+                       ;
                }
 
                my $g = qq|
 
-set terminal png
+set terminal png size 1000,400
 set output '$dir/$url.png'
 
                |;
 
-               if ( my $timefmt = $self->param('timefmt') ) {
+               if ( $timefmt ) {
                        $g .= qq|
 
 set xdata time
 set timefmt "$timefmt"
-set format x "$timefmt"
+#set format x "$timefmt"
+set format x "$timefmt_x"
 
                        |;
                }
@@ -60,8 +77,9 @@ set format x "$timefmt"
 #set yrange [ 0 : ]
 
                $g .= "\n\nplot " . join(',', @plot) . "\n";
+               $g =~ s/\n\n+/\n/sg;
 
-warn "gnuplot $g";
+#warn "gnuplot $g";
 
                open(my $gnuplot, '|-', 'gnuplot') || die "gnuplot $!";
                print $gnuplot $g;