r187: *** empty log message ***
[ctsim.git] / libctgraphics / ezplot.cpp
index 6ffd0b05b2f2fc9bc2b9f090b64987dd3cd26774..fb823b505f15feb1c7f0bbdee9ec052fab08190b 100644 (file)
@@ -6,7 +6,7 @@
 **  This is part of the CTSim program
 **  Copyright (C) 1983-2000 Kevin Rosenberg
 **
-**  $Id: ezplot.cpp,v 1.7 2000/07/28 08:28:08 kevin Exp $
+**  $Id: ezplot.cpp,v 1.13 2000/09/02 05:10:39 kevin Exp $
 **
 **  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
@@ -31,7 +31,7 @@ static const double TICKRATIO = 0.4;  // ratio of minor to major tick lengths
 static const int MAXNUMFMT = 15;       // maximum length of a numeric format 
 static const double DEF_CHARHEIGHT = (1./43.); //size of characters in NDC 
 static const double DEF_CHARWIDTH = (1./80.); // size of characters in NDC 
-static const int DEF_CURVE_CLR = C_YELLOW;
+static const int DEF_CURVE_CLR = C_RED;
 
 
 
@@ -41,16 +41,10 @@ EZPlotCurve::EZPlotCurve (const double* xData, const double* yData, int n, int c
   x = new double [n];
   y = new double [n];
 
-#if 1
-  for (int i = 0; i < n; i++) {
-    x[i] = xData[i];
-    y[i] = yData[i];
-  }
-#else
   int copyCount = n * sizeof(double);
   memcpy (x, xData, copyCount);
   memcpy (y, yData, copyCount);
-#endif
+
   m_iPointCount = n;
   m_iColor = color;
   m_iLineStyle = linestyle;
@@ -77,6 +71,18 @@ EZPlot::addCurve (const double *y, int n)
 }
 
 
+void 
+EZPlot::addCurve (const float *y, int n)
+{
+  double yDouble [n];
+
+  for (int i = 0; i < n; i++)
+    yDouble[i] = y[i];
+
+  addCurve (yDouble, n);
+}
+
+
 void
 EZPlot::addCurve (const float x[], const double y[], int num)
 {
@@ -88,6 +94,17 @@ EZPlot::addCurve (const float x[], const double y[], int num)
   addCurve (dx, y, num);
 }
 
+void
+EZPlot::addCurve (const double x[], const float y[], int num)
+{
+  double dy [num];
+
+  for (int i = 0; i < num; i++)
+    dy[i] = y[i];
+
+  addCurve (x, dy, num);
+}
+
 
 void
 EZPlot::addCurve (const double x[], const double y[], int num)
@@ -95,21 +112,8 @@ EZPlot::addCurve (const double x[], const double y[], int num)
   if (num < 1)
     return;
 
-  int iNumCurves = m_vecCurves.size();
-
-  if (o_unknowncurves == FALSE && iNumCurves >= o_reqcurves)
-    clearCurves ();
-
-  if (o_unknowncurves == TRUE || iNumCurves < o_reqcurves) {
-    EZPlotCurve* pCurve = new EZPlotCurve (x, y, num, o_color, o_linestyle, o_symbol, o_symfreq, c_legend);
-    m_vecCurves.push_back (pCurve);
-  }
-    
-  iNumCurves = m_vecCurves.size();  // recalculate
-  if (o_unknowncurves == FALSE && iNumCurves >= o_reqcurves) {
-    plot ();
-    clearCurves ();
-  }
+  EZPlotCurve* pCurve = new EZPlotCurve (x, y, num, o_color, o_linestyle, o_symbol, o_symfreq, c_legend);
+  m_vecCurves.push_back (pCurve);
 }
 
 
@@ -122,27 +126,30 @@ EZPlot::~EZPlot ()
 void
 EZPlot::clearCurves ()
 {
-  for (EZPlotCurveIterator i = m_vecCurves.begin(); i != m_vecCurves.end(); i++)    delete *i;
+  for (EZPlotCurveIterator i = m_vecCurves.begin(); i != m_vecCurves.end(); i++)    
+    delete *i;
   m_vecCurves.erase (m_vecCurves.begin(), m_vecCurves.end());
+  initPlotSettings();
 }
 
 
 EZPlot::EZPlot (SGP& sgp)
   : rSGP (sgp)
 {
-  curveinteract = -1;
+    initPlotSettings();
+}
 
+void
+EZPlot::initPlotSettings ()
+{
   charwidth = DEF_CHARWIDTH;   
   charheight = DEF_CHARHEIGHT;
   
-  c_xlabel = "X axis";
-  c_ylabel =  "Y axis";
+  c_xlabel = "";
+  c_ylabel =  "";
   c_title = "";
   c_legend = "";
   
-  o_reqcurves = 1;
-  o_unknowncurves = FALSE;
-  
   o_xporigin = 0.0;
   o_yporigin = 0.0;
   o_xlength  = 1.0;
@@ -182,26 +189,23 @@ EZPlot::EZPlot (SGP& sgp)
   s_ylegend  = FALSE;
   s_textsize = FALSE;
   
-  clr_axis   = C_WHITE;                /* set fixed colors */
-  clr_title  = (C_CYAN+8);
-  clr_label  = (C_CYAN+8);
-  clr_legend = (C_RED+8);
-  clr_number = (C_GREEN+8);
-  clr_grid   = (C_BLACK+8);
+  clr_axis   = C_BLACK;                /* set fixed colors */
+  clr_title  = C_CYAN;
+  clr_label  = C_CYAN;
+  clr_legend = C_RED;
+  clr_number = C_GREEN;
+  clr_grid   = C_LTGRAY;
 }
 
 
 /* NAME
- *   plot              INTERNAL: Plots all curves collected by ezplot()
+ *   plot              Plots all curves collected by addCurves ()
  *
  * SYNOPSIS
  *   plot()
  *
  * DESCRIPTION
- *   This routine plots the curves that have stored by ezplot().
- *
- * CALLED BY
- *   EZPLOT() only
+ *   This routine plots the curves that have stored by addCurves().
  *
  * CALLS
  *   drawAxes() & make_numfmt()
@@ -261,8 +265,8 @@ EZPlot::plot ()
   }
     
   /* find nice endpoints for axes */
-  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);
+  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) {
@@ -308,7 +312,8 @@ EZPlot::plot ()
   xtl_wid = x_fldwid * charwidth;              /* calc size of tick labels */
   ytl_wid = y_fldwid * charwidth;
   tl_height = charheight;
-  
+
+  //  rSGP.getViewport (xp_min, yp_min, xp_max, yp_max);
   /* calculate the extent of the plot frame */
   xp_min = o_xporigin;
   yp_min = o_yporigin;
@@ -328,7 +333,7 @@ EZPlot::plot ()
   /* adjust frame for title */
   if (c_title.length() > 0)
     ya_max -= 2.5 * charheight;
-  title_row = ya_max + 0.5 * charheight;
+  title_row = ya_max + charheight;
 
   /* calculate legend box boundaries */
   int max_leg = 0;                     /* longest legend in characters */
@@ -513,13 +518,11 @@ EZPlot::plot ()
   /* Convert WC in graph boundary to axis boundary */
   rSGP.setWindow  (xgw_min, ygw_min, xgw_max, ygw_max);        /* Graph boundary */
   rSGP.setViewport (xgn_min, ygn_min, xgn_max, ygn_max);
   double xminTemp = xa_min, xmaxTemp = xa_max;
   double yminTemp = ya_min, ymaxTemp = ya_max;
   rSGP.transformNDCtoMC (&xminTemp, &yminTemp); // calc WC to axis boundaries
-  rSGP.transformNDCtoMC (&yminTemp, &ymaxTemp);
-    
-  rSGP.setWindow  (xminTemp, yminTemp, xmaxTemp, ymaxTemp); // Set window to axis boundaries
+  rSGP.transformNDCtoMC (&xmaxTemp, &ymaxTemp);
+  rSGP.setWindow (xminTemp, yminTemp, xmaxTemp, ymaxTemp); // Set window to axis boundaries
   rSGP.setViewport (xa_min, ya_min, xa_max, ya_max);   
   
   symwidth = charwidth * (xgw_max - xgw_min);
@@ -584,7 +587,9 @@ EZPlot::drawAxes(void)
   rSGP.setViewport (xp_min, yp_min, xp_max, yp_max);
   
   if (c_title.length() > 0) {
-    rSGP.moveAbs (xa_min + (xa_max-xa_min)/2 - c_title.length()*charwidth, title_row);
+    double wText, hText;
+    rSGP.getTextExtent (c_title.c_str(), &wText, &hText);
+    rSGP.moveAbs (xa_min + (xa_max-xa_min)/2 - wText/2, title_row);
     rSGP.setTextSize (charheight * 2.0);
     rSGP.setTextColor (clr_title, -1);
     rSGP.drawText (c_title);
@@ -837,7 +842,7 @@ int
 EZPlot::axis_scale (double min, double max, int nint, double *minp, double *maxp, int *nintp)
 {
   if (min >= max || nint < 1) {
-    sys_error (ERR_WARNING, "Invalid params: min=%lf, min=%lf, num intervals=%d [axis_scale]", min, max, nint);
+    sys_error (ERR_WARNING, "Invalid params: min=%lf, max=%lf, num intervals=%d [axis_scale]", min, max, nint);
     return (FALSE);
   }