Fix plot initialization, improve frame titles
[ctsim.git] / libctgraphics / ezplot.cpp
index ca32b8fea239fee9e86f3897afc696336e863689..4f32d63769a84413c23b69c9d45dc2e283691fc6 100644 (file)
@@ -1,12 +1,10 @@
 /*****************************************************************************
 ** FILE IDENTIFICATION
 **
-**    EZPLOT
+**  ezplot.cpp
 **
 **  This is part of the CTSim program
-**  Copyright (c) 1983-2001 Kevin Rosenberg
-**
-**  $Id$
+**  Copyright (c) 1983-2009 Kevin Rosenberg
 **
 **  This program is free software; you can redistribute it and/or modify
 **  it under the terms of the GNU General Public License (version 2) as
@@ -217,6 +215,10 @@ EZPlot::initPlotSettings ()
   s_xlegend  = FALSE;
   s_ylegend  = FALSE;
   s_textsize = FALSE;
+  s_xmin     = FALSE;
+  s_xmax     = FALSE;
+  s_ymin     = FALSE;
+  s_ymax     = FALSE;
 
   clr_axis   = C_LTGRAY;                // set fixed colors
   clr_title  = C_RED;
@@ -481,15 +483,28 @@ EZPlot::plot (SGP* pSGP)
   double symheight = charheight * 0.3;       // size of symbol in NDC
   double symwidth = symheight;
 
-  const EZPlotCurve& firstCurve = *m_vecCurves[0];
+  if (m_vecCurves.size() < 1)
+    return; // can't plot if there are no curves to plot
+
+  // find a curve with at least one point to get base point
+  double xmin, xmax, ymin, ymax;   // extent of curves in world coord
+  bool found = false;
 
-  double xmin = firstCurve.x[0];   // extent of curves in world coord
-  double xmax = xmin;
-  double ymin = firstCurve.y[0];
-  double ymax = ymin;
+  for (unsigned int iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
+    const EZPlotCurve* const pCurve = m_vecCurves [iCurve];
+    if (pCurve->m_iPointCount > 0) {
+      xmin = pCurve->x[0];
+      xmax = xmin;
+      ymin = pCurve->y[0];
+      ymax = ymin;
+      found = true;
+      break;
+    }
+  }
+  if (! found)
+    return; // curve(s) are empty, so no plotting
 
-  unsigned int iCurve;
-  for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
+  for (unsigned int iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
     const EZPlotCurve* const pCurve = m_vecCurves [iCurve];
 
     for (int ip = 0; ip < pCurve->m_iPointCount; ip++) {
@@ -523,6 +538,7 @@ EZPlot::plot (SGP* pSGP)
   if (! axis_scale (xmin, xmax, o_xmajortick - 1, &xgw_min, &xgw_max, &x_nint) || ! axis_scale (ymin, ymax, o_ymajortick - 1, &ygw_min, &ygw_max, &y_nint))
     return;
 
+
   // check if user set x-axis extents
   if (s_xmin == TRUE) {
     xgw_min = v_xmin;
@@ -599,7 +615,7 @@ EZPlot::plot (SGP* pSGP)
   int max_leg = 0;                      // longest legend in characters
   int num_leg = 0;                      // number of legend titles
 
-  for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
+  for (unsigned int iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
     const std::string* pstrLegend = getLegend (iCurve);
 
     if (pstrLegend && pstrLegend->length() > 0) {
@@ -640,7 +656,7 @@ EZPlot::plot (SGP* pSGP)
 
     int iLegend = 0;                    // current legend position
 
-    for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
+    for (unsigned int iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
       const std::string* pstrLegend = getLegend (iCurve);
       if (! pstrLegend || pstrLegend->length() == 0)
         continue;
@@ -822,18 +838,13 @@ EZPlot::plot (SGP* pSGP)
 
 
 
-  for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
+  for (unsigned int iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
     const EZPlotCurve* const pCurve = m_vecCurves [iCurve];
 
-
     m_pSGP->setColor (getColor (iCurve));
-
     int iSym = getSymbol (iCurve);
-
     int iLS = getLinestyle (iCurve);
 
-
-
     if (iLS != SGP::LS_NOLINE) {
       m_pSGP->setLineStyle (iLS);
       double x1 = convertWorldToNDC_X (pCurve->x[0]);