Index: trunk/tools/czartool/Czarplot.pm
===================================================================
--- trunk/tools/czartool/Czarplot.pm	(revision 28752)
+++ trunk/tools/czartool/Czarplot.pm	(revision 28774)
@@ -65,10 +65,27 @@
     my %gnuplotFiles;
     my $stage = undef;
+    my $gnuplotFile = undef;
     foreach $stage (@{$stages}) {
-        $gnuplotFiles{$stage} =  $self->{_czarDb}->createTimeSeriesData($label, $stage, $beginTime, $endTime, \$minX, \$maxX, \$minY, \$maxY, \$timeDiff);
+
+        $gnuplotFile = $self->{_czarDb}->createTimeSeriesData($label, $stage, $beginTime, $endTime, \$minX, \$maxX, \$minY, \$maxY, \$timeDiff);
+        if ($gnuplotFile) {$gnuplotFiles{$stage} = $gnuplotFile;}
+
     }                                                           
 
-    if (!$selectedStage) {plotAllStagesTimeSeries($self, $outputFile, $label, $beginTime, $endTime, $maxX, $minX, $maxY, $minY, $timeDiff, \%gnuplotFiles);}                      
-    else {plotSingleTimeSeries($self, $gnuplotFiles{$selectedStage}, $outputFile, $selectedStage, $label, $beginTime, $endTime, $maxX, $minX, $maxY, $minY, $timeDiff);}
+    my $numOfPlots =  keys(%gnuplotFiles);
+
+    if ($numOfPlots == 0 ) {
+
+        print "Warning: No plots could be generated for '$label' during time period '$beginTime', '$endTime'\n";
+        return;
+    } 
+
+    if ($timeDiff == 0) {
+
+        print "WARNING: Zero time difference for '$label' during time period '$beginTime', '$endTime'\n";
+        return;
+    }
+
+    plotTimeSeries($self, \%gnuplotFiles, $outputFile, $label, $beginTime, $endTime, $maxX, $minX, $maxY, $minY, $timeDiff);
 }                                                    
 
@@ -131,17 +148,48 @@
 ###########################################################################
 #
+# Sorts out plotting max/min x/y
+#
+###########################################################################
+sub sortOutMaxMinY {
+    my ($self, $minY, $maxY) = @_;
+
+    ${$maxY} = ${$maxY} + (${$maxY}/10);
+    ${$minY} = ${$minY} - (${$minY}/10);
+    if (${$maxY} == 0) {${$maxY} = 1;}
+    if (${$minY} == 0) {${$minY} = -1;}
+}
+
+###########################################################################
+#
 # Plots a time-series of pending/faults
 #
 ###########################################################################
-sub plotAllStagesTimeSeries {
-    my ($self, $outputFile, $label, $fromTime, $toTime, $maxX, $minX, $maxY, $minY, $timeDiff, $gnuplotFiles) = @_;
-
-    $maxY = $maxY + ($maxY/10);
-    $minY = $minY - ($minY/10);
-    if ($maxY == 0) {$maxY = 1;}
-    if ($maxY == 0) {$maxY = 1;}
-    if ($minY == 0) {$minY = -1;}
-
-    my $divX = $timeDiff/2;
+sub plotTimeSeries {
+    my ($self, $gnuplotFiles, $outputFile, $label, $fromTime, $toTime, $maxX, $minX, $maxY, $minY, $timeDiff) = @_;
+
+    $self->sortOutMaxMinY(\$minY, \$maxY);
+
+    my $timeFormat = undef;
+
+    my $divX = $timeDiff/4;
+
+    # if less than a couple of hours data plotted, show 30 mins tics
+    if ($timeDiff < 7200) {
+        $timeFormat = "%H:%M";
+        $divX = 1800;
+    }
+    # if less than one days data plotted, show hourly tics
+    elsif ($timeDiff < 86400) {
+        $timeFormat = "%H:%M";
+        $divX = 3600;
+    }
+    # if more than one days data plotted, show daisy tics
+    else {
+        
+        $timeFormat = "%m/%d %H:%M";
+        $divX = 86400;
+    }
+
+    my $numOfPlots = keys %$gnuplotFiles;
 
     open (GP, "|/usr/bin/gnuplot -persist") or die "no gnuplot";
@@ -156,7 +204,6 @@
         "set xdata time;" .
         "set timefmt \"$self->{_dateFormat}\";" .
-        #"set xrange [\"$minX\":\"$maxX\"];" .
-        #"set yrange [$minY:$maxY];" .
-        "set format x \"%m/%d %H:%M\";" .
+        "set xrange [\"$minX\":\"$maxX\"];" .
+        "set format x \"$timeFormat\";" .
         "set xtics \"$minX\", $divX, \"$maxX\";" .
         "set grid xtics;" .
@@ -168,49 +215,20 @@
     foreach my $stage (keys %$gnuplotFiles) {
         if (!$firstIn) {print GP ",";}
-        print GP "'" . $gnuplotFiles->{$stage} . "' using 1:4 title \"$stage\" with lines";
+
+        # for single stage plots, show pending/processed/faults
+        if ($numOfPlots == 1) {
+
+            print GP "'" . $gnuplotFiles->{$stage} . "' using 1:4 title \"processed\" with lines lt 2,";
+            print GP "'" . $gnuplotFiles->{$stage} . "' using 1:2 title \"pending\" with lines lt 3,";
+            print GP "'" . $gnuplotFiles->{$stage} . "' using 1:3 title \"faults\" with lines lt 1";
+        }
+        # when plotting multiple stages, show only processed
+        else {
+            print GP "'" . $gnuplotFiles->{$stage} . "' using 1:4 title \"$stage\" with lines";
+        }
         $firstIn = 0;
     }
 
     print GP "\n";
-    close GP;
-}
-
-###########################################################################
-#
-# Plots a time-series of pending/faults
-#
-###########################################################################
-sub plotSingleTimeSeries {
-    my ($self, $inputFile, $outputFile, $stage, $label, $fromTime, $toTime, $maxX, $minX, $maxY, $minY, $timeDiff) = @_;
-
-    $maxY = $maxY + ($maxY/10);
-    $minY = $minY - ($minY/10);
-    if ($maxY == 0) {$maxY = 1;}
-    if ($maxY == 0) {$maxY = 1;}
-    if ($minY == 0) {$minY = -1;}
-
-    my $divX = $timeDiff/2;
-
-    open (GP, "|/usr/bin/gnuplot -persist") or die "no gnuplot";
-    use FileHandle;
-    GP->autoflush(1);
-
-    print GP
-        "set term $self->{_outputFormat};" .
-        "set output \"$outputFile\";" .
-        "set title \"'$stage' stage for '$label' since $fromTime\";" .
-        "set key left top;" .
-        "set xdata time;" .
-        "set timefmt \"$self->{_dateFormat}\";" .
-        "set xrange [\"$minX\":\"$maxX\"];" .
-        "set yrange [$minY:$maxY];" .
-        "set format x \"%m/%d %H:%M\";" .
-        "set xtics \"$minX\", $divX, \"$maxX\";" .
-        "set grid;" .
-        "set xlabel \"Time\";" .
-        "set ylabel \"Exposures\";" .
-        "plot '$inputFile' using 1:4 title \"processed\" with lines lt 2," .
-        "'$inputFile' using 1:2 title \"pending\" with lines lt 3," .
-        "'$inputFile' using 1:3 title \"faults\" with lines lt 1\n";
     close GP;
 }
@@ -237,8 +255,5 @@
         "set style histogram cluster gap 3;" .
         "set style fill solid border -1;" .
-#        "set boxwidth 1.5;" .
         "set bmargin 5;" .
-#        "set xtic rotate by -90;" .
-#        "plot '$inputFile' using 2:xtic(1) notitle\n";
         "set ylabel \"Exposures\";" .
         "plot '$inputFile' using 2:xtic(1) title \"Processed\" lt 2, '' using 3 title \"Pending\" lt 3, '' using 4 title \"Faults\" lt 1\n";
