App-RecordStream

 view release on metacpan or  search on metacpan

lib/App/RecordStream/Operation/tognuplot.pm  view on Meta::CPAN

  $this->{'TEMPFILE'}        = $tempfile;
  $this->{'TITLE'}           = $title;
  $this->{'USING'}           = \@using;
}

sub init_fields {
  my $this   = shift;
  my $record = shift;

  my $specs     = $this->{'KEY_GROUPS'}->get_keyspecs($record);
  my $using     = $this->{'USING'};
  my $bar_graph = $this->{'BAR_GRAPH'};
  my $lines     = $this->{'LINES'};
  my $title     = $this->{'TITLE'};

  if ( ! $bar_graph && !$lines ) {
    die 'Must specify using if more than 2 fields' if ( scalar @$specs > 2 ) && (! scalar @$using > 0);
  }

  if ( ! $title ) {
    $title = join(', ', @$specs);
  }

  if ( scalar @$using == 0 ) {
    if ( $bar_graph || $lines ) {
      my $using_spec = "1 title \"$specs->[0]\"";

      foreach my $idx (2..@$specs) {
        my $title = $specs->[$idx-1];
        $using_spec .= ", '' using $idx title \"$title\"";
      }

      push @$using, $using_spec;
    }
    elsif ( scalar @$specs == 1 ) {
      push @$using, "1";
    }
    elsif ( scalar @$specs == 2 ) {
      push @$using, "1:2";
    }
  }

  $this->{'FIELDS'} = $specs;
  $this->{'TITLE'}  = $title;
}

# hook for additional args
sub site_args {
}

sub accept_record {
  my ($this, $record) = @_;

  if ( $this->{'FIRST_RECORD'} ) {
    $this->{'FIRST_RECORD'} = 0;
    $this->init_fields($record);
  }

  my $line = '';
  foreach my $key (@{$this->{'FIELDS'}}) {
    my $value = ${$record->guess_key_from_spec($key)};
    $value = 0 if not defined $value;
    $line .= "$value ";
  }

  chop $line;
  if ( $this->{'DUMP_TO_SCREEN'} ) {
    $this->push_line($line);
  }
  else {
    my $tempfh = $this->{'TEMPFH'};
    print $tempfh $line . "\n";
  }

  return 1;
}

sub stream_done {
  my ($this) = @_;

  close $this->{'TEMPFH'};

  my $plot_script = '';
  $plot_script .= "set terminal png\n";
  $plot_script .= "set output '" . $this->{'PNG_FILE'} . "'\n";
  $plot_script .= "set title '" . $this->{'TITLE'} . "'\n";

  if ( $this->{'BAR_GRAPH'} ) {
    $plot_script .= <<CMDS;
set style data histogram
set style histogram cluster gap 1
set style fill solid border -1
CMDS
  }
  elsif ( $this->{'LINES'} ) {
    $plot_script .= "set style data linespoints\n";
  }

  foreach my $command (@{$this->{'PRECOMMANDS'}}) {
    $plot_script .= $command . "\n";
  }

  my $plot_cmd = 'plot ';

  my $index = 0;
  my $default_label = join(', ', @{$this->{'FIELDS'}});

  foreach my $use_spec (@{$this->{'USING'}}) {
    if ( $this->{'DUMP_TO_SCREEN'} ) {
      $plot_cmd .= "'screen' using $use_spec ";
    }
    else {
      $plot_cmd .= "'" . $this->{'TEMPFILE'} . "' using $use_spec ";
    }

    if ( not ($use_spec =~ m/title/) ) {
      my $label = $default_label;

      if ( $this->{'LABELS'}->[$index] ) {
        $label = $this->{'LABELS'}->[$index];
      }



( run in 0.500 second using v1.01-cache-2.11-cpan-39bf76dae61 )