From bcf002fcf22fa952040ba53ba4f07d7f863226a8 Mon Sep 17 00:00:00 2001 From: "Peter J. Holzer" Date: Mon, 8 Aug 2016 13:21:54 +0200 Subject: [PATCH] Add new script tsplotsql --- tsplotsql | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100755 tsplotsql diff --git a/tsplotsql b/tsplotsql new file mode 100755 index 0000000..f4038d4 --- /dev/null +++ b/tsplotsql @@ -0,0 +1,109 @@ +#!/usr/bin/perl -w + +=head1 NAME + +tsplotv - plot time series given in vertical format + +=head1 SYNOPSIS + +tsplotv +[--finalresolution dpi] +[--legend-position pos] +[--log-y] +[--output-format format ] +[--stacked] +[--style style] +[file ...] + +=head1 DESCRIPTION + +This program expects time series data in vertical format, I.e., +each line contains a tab-separated tripel . + +The default legend position is "top right", same as with gnuplot. +Another frequently useful position (especially if you have lots of series) +is "below". Note that positions which consist of several words (such as +"top right" need to be passed to tsplotv as a single argument, so the +space needs to be hidden from the shell by use of quotes or a backslash. + +The default output format is "png", the default style is "lines". + +See L for a description of possible legend positions, +output formats, and styles. + +The --stacked option causes the time series to be stacked on top of each +other. + +=cut + +use strict; +use TimeSeries; +use Getopt::Long; +use Pod::Usage; +use DBIx::SimpleConnect; + +my $help; +my $legend_position = 'top right'; +my $output_format = 'png'; +my $stacked = 0; +my $style = "lines"; +my $log_y = 0; +my $finalresolution; +my $time_t = 0; +my $colors; +my $dbname; + +GetOptions('help|?' => \$help, + 'legend_position|legend-position=s' => \$legend_position, + 'output_format|output-format=s' => \$output_format, + 'stacked' => \$stacked, + 'style:s' => \$style, + 'log_y|log-y' => \$log_y, + 'finalresolution' => \$finalresolution, + 'time_t' => \$time_t, + 'colors=s' => \$colors, + 'dbname=s' => \$dbname, + ) or pod2usage(2); +pod2usage(1) if $help; + + +binmode STDOUT, ':raw'; + +my %series; +my $ns; +my %data; + +my $ts = TimeSeries->new(output_format => $output_format); +my $dbh = DBIx::SimpleConnect->connect($dbname); + +for my $q (@ARGV) { + my $qdata = $dbh->selectall_arrayref($q); + for my $r (@$qdata) { + my ($timestamp, $series, $value) = @$r; + $series{$series} = ++$ns unless ($series{$series}); + $data{$timestamp}{$series} = $value; + } +} +my @series = sort { $series{$a} <=> $series{$b} } keys %series; +$ts->legend(@series); +$ts->legend_position($legend_position); +$ts->stacked($stacked); +$ts->style($style); +$ts->log_y($log_y); +$ts->finalresolution($finalresolution) if $finalresolution; +if ($colors) { + $ts->colors(split(/,/, $colors)); +} + +for my $timestamp (sort keys %data) { + my %d = %{$data{$timestamp}}; + my @values = @d{@series}; + if ($time_t) { + $ts->add($timestamp, @values); + } else { + $ts->add_timestring($timestamp, @values); + } +} + +my $g = $ts->plot(); +print $g