r189: *** empty log message ***
[ctsim.git] / libctgraphics / ezplot.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **    EZPLOT                                    
5 **
6 **  This is part of the CTSim program
7 **  Copyright (C) 1983-2000 Kevin Rosenberg
8 **
9 **  $Id: ezplot.cpp,v 1.14 2000/09/04 09:06:46 kevin Exp $
10 **
11 **  This program is free software; you can redistribute it and/or modify
12 **  it under the terms of the GNU General Public License (version 2) as
13 **  published by the Free Software Foundation.
14 **
15 **  This program is distributed in the hope that it will be useful,
16 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 **  GNU General Public License for more details.
19 **
20 **  You should have received a copy of the GNU General Public License
21 **  along with this program; if not, write to the Free Software
22 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 ******************************************************************************/
24
25 #include "ezplot.h"
26 #include <algorithm>
27
28
29 // Defaults
30 static const double TICKRATIO = 0.4;    // ratio of minor to major tick lengths
31 static const int MAXNUMFMT = 15;        // maximum length of a numeric format 
32 static const double DEF_CHARHEIGHT = (1./43.); //size of characters in NDC 
33 static const double DEF_CHARWIDTH = (1./80.); // size of characters in NDC 
34 static const int DEF_CURVE_CLR = C_RED;
35
36
37 EZPlotCurve::EZPlotCurve (const double* xData, const double* yData, int n, int color, int linestyle, int symbol, int symfreq, const string& legend)
38   : x(NULL), y(NULL), m_sLegend (legend)
39 {
40   x = new double [n];
41   y = new double [n];
42
43   int copyCount = n * sizeof(double);
44   memcpy (x, xData, copyCount);
45   memcpy (y, yData, copyCount);
46
47   m_iPointCount = n;
48   m_iColor = color;
49   m_iLineStyle = linestyle;
50   m_iSymbol = symbol;
51   m_iSymbolFreq = symfreq;
52 }
53
54 EZPlotCurve::~EZPlotCurve ()
55 {
56   delete x;
57   delete y;
58 }
59
60
61 void 
62 EZPlot::addCurve (const double *y, int n)
63 {
64   double x [n];
65
66   for (int i = 0; i < n; i++)
67     x[i] = i;
68
69   addCurve (x, y, n);
70 }
71
72
73 void 
74 EZPlot::addCurve (const float *y, int n)
75 {
76   double yDouble [n];
77
78   for (int i = 0; i < n; i++)
79     yDouble[i] = y[i];
80
81   addCurve (yDouble, n);
82 }
83
84
85 void
86 EZPlot::addCurve (const float x[], const double y[], int num)
87 {
88   double dx [num];
89
90   for (int i = 0; i < num; i++)
91     dx[i] = x[i];
92
93   addCurve (dx, y, num);
94 }
95
96 void
97 EZPlot::addCurve (const double x[], const float y[], int num)
98 {
99   double dy [num];
100
101   for (int i = 0; i < num; i++)
102     dy[i] = y[i];
103
104   addCurve (x, dy, num);
105 }
106
107
108 void
109 EZPlot::addCurve (const double x[], const double y[], int num)
110 {
111   if (num < 1)
112     return;
113
114   EZPlotCurve* pCurve = new EZPlotCurve (x, y, num, o_color, o_linestyle, o_symbol, o_symfreq, c_legend);
115   m_vecCurves.push_back (pCurve);
116 }
117
118
119 EZPlot::~EZPlot ()
120 {
121   for (EZPlotCurveIterator i = m_vecCurves.begin(); i != m_vecCurves.end(); i++)
122     delete *i;
123 }
124
125 void
126 EZPlot::clearCurves ()
127 {
128   for (EZPlotCurveIterator i = m_vecCurves.begin(); i != m_vecCurves.end(); i++)    
129     delete *i;
130   m_vecCurves.erase (m_vecCurves.begin(), m_vecCurves.end());
131   initPlotSettings();
132 }
133
134
135 EZPlot::EZPlot (SGP& sgp)
136   : rSGP (sgp)
137 {
138     initPlotSettings();
139 }
140
141 void
142 EZPlot::initPlotSettings ()
143 {
144   charheight = DEF_CHARHEIGHT;
145   charwidth = DEF_CHARWIDTH;
146
147   c_xlabel = "";
148   c_ylabel =  "";
149   c_title = "";
150   c_legend = "";
151   
152   o_xporigin = 0.0;
153   o_yporigin = 0.0;
154   o_xlength  = 1.0;
155   o_ylength  = 1.0;
156   
157   o_xaxis = LINEAR;
158   o_yaxis = LINEAR;
159   
160   o_grid = FALSE;
161   o_box = FALSE;
162   
163   o_xmajortick = 10;
164   o_ymajortick =  8;
165   o_xminortick =  4;
166   o_yminortick =  4;
167   
168   o_color = DEF_CURVE_CLR;
169   o_symfreq = 1;
170   o_symbol = -1;
171   o_linestyle = SGP::LS_SOLID;
172   
173   o_xtlabel = TRUE;
174   o_ytlabel = TRUE;
175   o_xticks = BELOW;
176   o_yticks = LEFT;
177   
178   o_legendbox = INSIDE;
179   o_tag = FALSE;
180   
181   s_xtitle   = FALSE;
182   s_ytitle   = FALSE;
183   s_xcross   = FALSE;
184   s_ycross   = FALSE;
185   s_lxfrac   = FALSE;
186   s_lyfrac   = FALSE;
187   s_xlegend  = FALSE;
188   s_ylegend  = FALSE;
189   s_textsize = FALSE;
190   
191   clr_axis   = C_BLACK;         // set fixed colors 
192   clr_title  = C_CYAN;
193   clr_label  = C_CYAN;
194   clr_legend = C_RED;
195   clr_number = C_GREEN;
196   clr_grid   = C_LTGRAY;
197 }
198
199
200 /* NAME
201  *   plot               Plots all curves collected by addCurves ()
202  *
203  * SYNOPSIS
204  *   plot()
205  *
206  * DESCRIPTION
207  *   This routine plots the curves that have stored by addCurves().
208  *
209  * CALLS
210  *   drawAxes() & make_numfmt()
211  */
212
213 void
214 EZPlot::plot ()
215 {
216   if (m_vecCurves.size() <= 0)
217     return;
218
219   rSGP.setWindow (0., 0., 1., 1.);
220
221   if (s_textsize == TRUE) {
222     charheight = v_textsize;
223     charwidth = rSGP.getCharWidth();
224   } else {
225     charheight = rSGP.getCharHeight();
226     charwidth = rSGP.getCharWidth();
227   }
228   
229   const EZPlotCurve& firstCurve = *m_vecCurves[0];
230   double xmin = firstCurve.x[0];   // extent of curves in world coord
231   double xmax = xmin;       
232   double ymin = firstCurve.y[0];
233   double ymax = ymin;
234   
235   for (EZPlotCurveConstIterator iterCurve = m_vecCurves.begin(); iterCurve != m_vecCurves.end(); iterCurve++) {
236     const EZPlotCurve& curve = **iterCurve;
237     
238     for (int ip = 0; ip < curve.m_iPointCount; ip++) {
239       if (curve.x[ip] > xmax)
240         xmax = curve.x[ip];
241       else if (curve.x[ip] < xmin)
242         xmin = curve.x[ip];
243       if (curve.y[ip] > ymax)
244         ymax = curve.y[ip];
245       else if (curve.y[ip] < ymin)
246         ymin = curve.y[ip];
247     }
248   }
249   
250   // extend graph limits for user defined axis cross positions 
251   if (s_xcross == TRUE) {
252     if (v_xcross < xmin)
253       xmin = v_xcross;
254     else if (v_xcross > xmax)
255       xmax = v_xcross;
256   }
257     
258   if (s_ycross == TRUE) {
259     if (v_ycross < ymin)
260       ymin = v_ycross;
261     else if (v_ycross > ymax)
262       ymax = v_ycross;
263   }
264     
265   // find nice endpoints for axes 
266   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))
267     return;
268     
269   // check if user set x-axis extents 
270   if (s_xmin == TRUE) {
271     xgw_min = v_xmin;
272     x_nint = o_xmajortick - 1;
273   }
274   if (s_xmax == TRUE) {
275     xgw_max = v_xmax;
276     x_nint = o_xmajortick - 1;
277   }
278   
279   // check if user set y-axis extents 
280   if (s_ymin == TRUE) {
281     ygw_min = v_ymin;
282     y_nint = o_ymajortick - 1;
283   }
284   if (s_ymax == TRUE) {
285     ygw_max = v_ymax;
286     y_nint = o_ymajortick - 1;
287   }
288     
289   // calculate increment between major axis in world coordinates 
290   xw_tickinc = (xgw_max - xgw_min) / x_nint;
291   yw_tickinc = (ygw_max - ygw_min) / y_nint;
292     
293   // we have now calcuated xgw_min, xyw_max, ygw_min, & ygw_max 
294   
295   // set the number of decimal point to users' setting or default 
296   // Two formats for numbers: Fixed:    -nnn.f and  Exponent: -n.fffE+eee
297   if (s_lxfrac == TRUE)
298     x_frac = v_lxfrac;
299   else
300     x_frac = -1;
301     
302   if (s_lyfrac == TRUE)
303     y_frac = v_lyfrac;
304   else
305     y_frac = -1;
306     
307   make_numfmt (x_numfmt, &x_fldwid, &x_frac, xgw_min, xgw_max, x_nint);
308   make_numfmt (y_numfmt, &y_fldwid, &y_frac, ygw_min, ygw_max, y_nint);
309     
310   xtl_wid = x_fldwid * charwidth;               // calc size of tick labels 
311   ytl_wid = y_fldwid * charwidth;
312   tl_height = charheight;
313
314   // calculate the extent of the plot frame 
315   xp_min = o_xporigin;
316   yp_min = o_yporigin;
317   xp_max = xp_min + o_xlength;
318   yp_max = yp_min + o_ylength;
319
320   xp_min = clamp (xp_min, 0., 1.);
321   xp_max = clamp (xp_max, 0., 1.);
322   yp_min = clamp (yp_min, 0., 1.);
323   yp_max = clamp (yp_max, 0., 1.);
324   
325   xa_min = xp_min;              // extent of axes 
326   xa_max = xp_max;
327   ya_min = yp_min;
328   ya_max = yp_max;
329
330   // adjust frame for title 
331   if (c_title.length() > 0)
332     ya_max -= 2 * charheight;
333   title_row = ya_max + 2 * charheight;
334
335   // calculate legend box boundaries 
336   int max_leg = 0;                      // longest legend in characters 
337   int num_leg = 0;                      // number of legend titles 
338   for (EZPlotCurveConstIterator iterCurve = m_vecCurves.begin(); iterCurve != m_vecCurves.end(); iterCurve++) {
339     const EZPlotCurve& curve = **iterCurve;
340     int nLegend = curve.m_sLegend.length();
341     if (nLegend > 0) {
342       ++num_leg;
343       max_leg = max (max_leg, nLegend);
344     }
345   }
346
347   if (num_leg > 0 && o_legendbox != NOLEGEND) {
348     double leg_width  = (max_leg + 2) * charwidth;      // size of legend box 
349     double leg_height = num_leg * 3 * charheight;
350     
351     if (s_xlegend == TRUE)
352       xl_max = v_xlegend;
353     else {
354       xl_max = xa_max;
355       if (o_legendbox == OUTSIDE)
356         xa_max -= (leg_width + 0.5 * charwidth);
357     }
358     xl_min = xl_max - leg_width;
359     
360     if (s_ylegend == TRUE)
361       yl_max = v_ylegend;
362     else
363       yl_max = ya_max;
364     
365     yl_min = yl_max - leg_height;
366
367     rSGP.setColor (clr_legend);
368     rSGP.drawRect (xl_min, yl_min, xl_max, yl_max);
369
370     int iLegend = 0;                    // current legend position 
371     for (EZPlotCurveIterator iterCurve = m_vecCurves.begin(); iterCurve != m_vecCurves.end(); iterCurve++) {
372         const EZPlotCurve& curve = **iterCurve;
373         
374         if (curve.m_sLegend.length() == 0)
375             continue;
376
377         double xmin = xl_min + 1.0 * charwidth;
378         double xmax = xl_max - 1.0 * charwidth;
379         double y = yl_max - (2.0 + iLegend * 3) * charheight;
380         
381         rSGP.moveAbs (xmin, y + 0.5 * charheight);
382         rSGP.drawText (curve.m_sLegend);
383         rSGP.setColor (curve.m_iColor);
384         if (curve.m_iLineStyle != SGP::LS_NOLINE) {
385             rSGP.setLineStyle (curve.m_iLineStyle);
386             rSGP.moveAbs (xmin, y);
387             rSGP.lineAbs (xmax, y);
388         }
389         if (curve.m_iSymbol > 0) {
390             double xinc = (xmax - xmin) / (5 - 1);
391             rSGP.setLineStyle (SGP::LS_SOLID);
392             for (int j = 0; j < 5; j++) {
393               rSGP.moveAbs (xmin + j * xinc, y);
394               symbol(curve.m_iSymbol, 0.5 * charwidth, 0.5 * charheight);
395             }
396         }
397         ++iLegend;      // move to next legend position 
398     }
399   }   // end legend printing 
400
401   // calculate the extent of the axes 
402
403   /*-------------------------*/
404   /* adjust frame for labels */
405   /*-------------------------*/
406   
407   // X-Label 
408   if (c_xlabel.length() > 0)
409     ya_min += 3.0 * charheight;
410   xlbl_row = xp_min;            // put x-label on bottom of plot frame 
411
412   // Y-Label 
413   if (c_ylabel.length() > 0)
414     xa_min += 3.0 * charwidth;  // reverse rSGP.setTextSize because writing text sideways 
415   ylbl_col = xp_min + 2 * charwidth;
416
417   /*------------------------------*/
418   /* adjust frame for tick labels */
419   /*------------------------------*/
420
421   // Calc offset of tick labels from axes 
422   if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
423     xtl_ofs = 0.0;
424   else if (o_xticks == BELOW)
425     xtl_ofs = -1.5 * charheight;  // kr
426   else if (o_xticks == ABOVE)
427     xtl_ofs = 1.5 * charheight;
428   
429   if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
430     ytl_ofs = 0.0;
431   else if (o_yticks == LEFT)
432     ytl_ofs = -(1 + y_fldwid) * charwidth;
433   else if (o_yticks == RIGHT)
434     ytl_ofs = 1.5 * charwidth;
435
436   xt_min = xa_min;
437   yt_min = ya_min;
438   xt_max = xa_max;
439   yt_max = ya_max;
440
441   // see if need to shrink axis extents and/or tick extents 
442   if (xtl_ofs != 0.0 && s_ycross == FALSE) {
443     if (o_xticks == BELOW) {
444       ya_min += 2.5 * charheight;
445       yt_min = ya_min;
446     } else if (o_xticks == ABOVE) {
447       ya_min += 0.0;
448       yt_min = ya_min + 2.5 * charheight;
449     }
450   } else   // noaxis, no t-labels, or user set cross 
451     yt_min = ya_min;
452   
453   if (ytl_ofs != 0.0 && s_xcross == FALSE) {
454     if (o_yticks == LEFT) {
455       xa_min += (1 + y_fldwid) * charwidth;
456       xt_min = xa_min;
457     } else if (o_yticks == RIGHT) {
458       xa_min += 0.0;
459       xt_min = xa_min + ytl_ofs + y_fldwid * charwidth;
460     }
461   } else
462     xt_min = xa_min;
463
464   // decrease size of graph, if necessary, to accommadate space 
465   // between axis boundary and boundary of ticks 
466   double x_added_ticks = -1; // number of tick spaces added to axis 
467   double y_added_ticks = -1;
468   if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
469     x_added_ticks = 0;
470   if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
471     y_added_ticks = 0;
472   
473   if (o_grid == TRUE) {
474     if (x_added_ticks < 0)
475       x_added_ticks = 2;
476     if (y_added_ticks < 0)
477       y_added_ticks = 2;
478   }
479   
480   if (x_added_ticks < 0) {
481     if (o_yticks == LEFT || s_ycross)
482       x_added_ticks = 1;
483     else
484       x_added_ticks = 2;
485   }
486   
487   if (y_added_ticks < 0) {
488     if (o_xticks == BELOW || s_xcross)
489       y_added_ticks = 1;
490     else
491       y_added_ticks = 2;
492   }
493   
494   xn_tickinc = (xt_max - xt_min) / (x_nint + x_added_ticks);
495   yn_tickinc = (yt_max - yt_min) / (y_nint + y_added_ticks);
496   
497   xt_min += 0.5 * x_added_ticks * xn_tickinc;
498   xt_max -= 0.5 * x_added_ticks * xn_tickinc;
499   yt_min += 0.5 * y_added_ticks * yn_tickinc;
500   yt_max -= 0.5 * y_added_ticks * yn_tickinc;
501   
502   xgn_min = xt_min;
503   xgn_max = xt_max;
504   ygn_min = yt_min;
505   ygn_max = yt_max;
506
507   //------------------------------------------------------------------------
508   
509   m_xWorldScale = (xgn_max - xgn_min) / (xgw_max - xgw_min);
510   m_yWorldScale = (ygn_max - ygn_min) / (ygw_max - ygw_min);
511
512   // PLOT CURVES 
513   
514   rSGP.setLineStyle (SGP::LS_SOLID);
515   drawAxes();
516
517   // size of symbol in NDC 
518   double symwidth = charwidth;
519   double symheight = charheight;
520   
521   for (EZPlotCurveIterator iterCurve = m_vecCurves.begin(); iterCurve != m_vecCurves.end(); iterCurve++) {
522       const EZPlotCurve& curve = **iterCurve;
523
524       rSGP.setColor (curve.m_iColor);
525
526       if (curve.m_iLineStyle != SGP::LS_NOLINE) {
527         rSGP.setLineStyle (curve.m_iLineStyle);
528         double x = convertWorldToNDC_X (curve.x[0]);
529         double y = convertWorldToNDC_Y (curve.y[0]);
530         rSGP.moveAbs (x, y);
531         for (int i = 1; i < curve.m_iPointCount; i++) {
532             x = convertWorldToNDC_X (curve.x[i]);
533             y = convertWorldToNDC_Y (curve.y[i]);
534             rSGP.lineAbs (x, y);
535         }
536       }
537       if (curve.m_iSymbol > 0) {
538         rSGP.setLineStyle (SGP::LS_SOLID);
539         double x = convertWorldToNDC_X (curve.x[0]);
540         double y = convertWorldToNDC_Y (curve.y[0]);
541         rSGP.moveAbs (x, y);
542         symbol (curve.m_iSymbol, symwidth, symheight);
543         for (int i = 1; i < curve.m_iPointCount; i++)
544           if (i % curve.m_iSymbolFreq == 0 || i == curve.m_iPointCount - 1) {
545             x = convertWorldToNDC_X (curve.x[i]);
546             y = convertWorldToNDC_Y (curve.y[i]);
547             rSGP.moveAbs (x, y);
548             symbol (curve.m_iSymbol, symwidth, symheight);
549           }
550       }
551   }
552
553 }
554
555
556 /* NAME
557  *   drawAxes                   INTERNAL routine to draw axis & label them
558  *
559  * SYNOPSIS
560  *   drawAxes()
561  */
562
563
564 void 
565 EZPlot::drawAxes()
566 {
567   double xticklen = 0, yticklen = 0; // length of ticks in NDC 
568   double minorinc;              // increment between minor axes 
569   double xaxispos, yaxispos;    // crossing of axes
570   double x, y, x2, y2;
571   bool axis_near;       // TRUE if axis too close to print t-label 
572   int i, j;
573   char str[256];
574   char *numstr;
575   
576   rSGP.setTextSize (charheight);
577   rSGP.setTextColor (1, -1);
578   
579   if (o_xticks == ABOVE)
580     xticklen = charheight;
581   else if (o_xticks == BELOW)
582     xticklen = -charheight;
583   
584   if (o_yticks == RIGHT)
585     yticklen = charwidth;
586   else if (o_yticks == LEFT)
587     yticklen = -charwidth;
588   
589   if (c_title.length() > 0) {
590     double wText, hText;
591     rSGP.setTextSize (charheight * 2.0);
592     rSGP.getTextExtent (c_title.c_str(), &wText, &hText);
593     rSGP.moveAbs (xa_min + (xa_max-xa_min)/2 - wText/2, title_row);
594     rSGP.setTextColor (clr_title, -1);
595     rSGP.drawText (c_title);
596     rSGP.setTextSize (charheight);
597   }
598   
599   if (o_grid == TRUE || o_box == TRUE) {
600     rSGP.setColor (clr_axis);
601     rSGP.moveAbs (xa_min, ya_min);
602     rSGP.lineAbs (xa_max, ya_min);
603     rSGP.lineAbs (xa_max, ya_max);
604     rSGP.lineAbs (xa_min, ya_max);
605     rSGP.lineAbs (xa_min, ya_min);
606   }
607   
608   // calculate position of axes 
609   
610   // x-axis 
611   if (s_ycross == TRUE) {       // convert users' world-coord 
612     xaxispos = convertWorldToNDC_Y (v_ycross);// axis to its position in NDC 
613     x = convertWorldToNDC_X (xgw_min);
614   } else
615     xaxispos = ya_min;
616   
617   // y-axis 
618   if (s_xcross == TRUE) {       // convert users' world-coord 
619     yaxispos = convertWorldToNDC_X (v_xcross);// axis to its NDC position 
620     y = convertWorldToNDC_Y (ygw_min);
621   } else
622     yaxispos = xa_min;
623   
624   /*-------------*/
625   /* draw x-axis */
626   /*-------------*/
627   
628   if (o_xaxis == LINEAR) {
629     // draw axis line 
630     
631     rSGP.setColor (clr_axis);
632     if (o_tag && !o_grid && !o_box && s_xcross) {
633       rSGP.moveAbs (xa_min, xaxispos - charheight);
634       rSGP.lineAbs (xa_min, xaxispos + charheight);
635     }
636     rSGP.moveAbs (xa_min, xaxispos);
637     rSGP.lineAbs (xa_max, xaxispos);
638     if (o_tag && !o_grid && !o_box) {
639       rSGP.moveAbs (xa_max, xaxispos - charheight);
640       rSGP.lineAbs (xa_max, xaxispos + charheight);
641     }
642     
643     if (o_grid == TRUE) {
644       rSGP.setColor (clr_grid);
645       for (i = 0; i <= x_nint; i++) {
646         rSGP.moveAbs (xt_min + xn_tickinc * i, ya_max);
647         rSGP.lineAbs (xt_min + xn_tickinc * i, ya_min);
648       }
649     }
650     rSGP.moveAbs (xa_min + (xa_max-xa_min)/2 - c_xlabel.length()*charwidth, xlbl_row);
651     rSGP.setTextSize (charheight * 2.0);
652     rSGP.setTextColor (clr_label, -1);
653     rSGP.drawText (c_xlabel);
654     rSGP.setTextSize (charheight);
655     minorinc = xn_tickinc / (o_xminortick + 1);
656
657     for (i = 0; i <= x_nint; i++) {
658       x = xt_min + xn_tickinc * i;
659       rSGP.setColor (clr_axis);
660       rSGP.moveAbs (x, xaxispos);
661       rSGP.lineAbs (x, xaxispos + xticklen);
662       if (i != x_nint)
663         for (j = 1; j <= o_xminortick; j++) {
664           x2 = x + minorinc * j;
665           rSGP.moveAbs (x2, xaxispos);
666           rSGP.lineAbs (x2, xaxispos + TICKRATIO * xticklen);
667         }
668       axis_near = FALSE;
669       if (xaxispos + xtl_ofs > ya_min && o_yaxis != NOAXIS) {
670         double xw = xgw_min + i * xw_tickinc;
671         double x = convertWorldToNDC_X (xw);
672         double d = x - yaxispos;
673         if (o_yticks == RIGHT && d >= 0  && d < 0.9 * xn_tickinc)
674           axis_near = TRUE;
675         if (o_yticks == LEFT && d <= 0 && d > -0.9 * xn_tickinc)
676           axis_near = TRUE;
677       }
678
679       if (o_xtlabel == TRUE && axis_near == FALSE) {
680         snprintf (str, sizeof(str), x_numfmt, xgw_min + xw_tickinc * i);
681         numstr = str_skip_head (str, " ");
682         rSGP.moveAbs (x-strlen(numstr)*charwidth/2, xaxispos + xtl_ofs);
683         rSGP.setTextColor (clr_number, -1);
684         rSGP.drawText (numstr);
685       }
686     }
687   }             // X - Axis 
688   
689   
690   /*--------*/
691   /* y-axis */
692   /*--------*/
693   
694   if (o_yaxis == LINEAR) {
695     
696     rSGP.setColor (clr_axis);
697     if (o_tag && !o_grid && !o_box && s_ycross) {
698       rSGP.moveAbs (yaxispos - charwidth, ya_min);
699       rSGP.lineAbs (yaxispos + charwidth, ya_min);
700     }
701     rSGP.moveAbs (yaxispos, ya_min);
702     rSGP.lineAbs (yaxispos, ya_max);
703     if (o_tag && !o_grid && !o_box) {
704       rSGP.moveAbs (yaxispos - charwidth, ya_max);
705       rSGP.lineAbs (yaxispos + charwidth, ya_max);
706     }
707     
708     if (o_grid == TRUE) {
709       rSGP.setColor (clr_grid);
710       for (i = 0; i <= y_nint; i++) {
711         y = yt_min + yn_tickinc * i;
712         rSGP.moveAbs (xa_max, y);
713         rSGP.lineAbs (xa_min, y);
714       }
715     }
716     rSGP.moveAbs (ylbl_col, ya_min + (ya_max-ya_min)/2 - c_ylabel.length()*charheight);
717     rSGP.setTextAngle (HALFPI);
718     rSGP.setTextSize (2 * charheight);
719     rSGP.setTextColor (clr_label, -1);
720     rSGP.drawText (c_ylabel);
721     rSGP.setTextAngle (0.0);
722     rSGP.setTextSize (charheight);
723     minorinc = yn_tickinc / (o_yminortick + 1);
724     
725     for (i = 0; i <= y_nint; i++) {
726       y = yt_min + yn_tickinc * i;
727       rSGP.setColor (clr_axis);
728       rSGP.moveAbs (yaxispos, y);
729       rSGP.lineAbs (yaxispos + yticklen, y);
730       if (i != y_nint)
731         for (j = 1; j <= o_yminortick; j++) {
732           y2 = y + minorinc * j;
733           rSGP.moveAbs (yaxispos, y2);
734           rSGP.lineAbs (yaxispos + TICKRATIO * yticklen, y2);
735         }
736       axis_near = FALSE;
737       if (yaxispos + ytl_ofs > xa_min && o_xaxis != NOAXIS) {
738         double yw = ygw_min + i * yw_tickinc;
739         double y = convertWorldToNDC_Y (yw);
740         double d = y - xaxispos;
741         if (o_xticks == ABOVE && d >= 0 && d < 0.9 * yn_tickinc)
742           axis_near = TRUE;
743         if (o_xticks == BELOW && d <= 0 && d > -0.9 * yn_tickinc)
744           axis_near = TRUE;
745       }
746       if (o_ytlabel == TRUE && axis_near == FALSE) {
747         snprintf (str, sizeof(str), y_numfmt, ygw_min + yw_tickinc * i);
748         rSGP.moveAbs (yaxispos + ytl_ofs, y + 0.5 * charheight);
749         rSGP.setTextColor (clr_number, -1);
750         rSGP.drawText (str);
751       }
752     }
753   }             // Y - Axis
754 }
755
756
757 void 
758 EZPlot::symbol (int sym, double symwidth, double symheight)
759 {
760   if (sym <= 0)
761     return;
762   
763   if (sym == SB_CROSS) {
764     rSGP.moveRel (-0.5 * symwidth, -0.5 * symheight);
765     rSGP.lineRel (symwidth, symheight);
766     rSGP.moveRel (-symwidth, 0.0);
767     rSGP.lineRel (symwidth, -symheight);
768     rSGP.moveRel (-0.5 * symwidth, 0.5 * symheight);
769   } else if (sym == SB_PLUS) {
770     rSGP.moveRel (-0.5 * symwidth, 0.0);
771     rSGP.lineRel (symwidth, 0.0);
772     rSGP.moveRel (-0.5 * symwidth, -0.5 * symheight);
773     rSGP.lineRel (0.0, symheight);
774     rSGP.moveRel (0.0, -0.5 * symheight);
775   } else if (sym == SB_BOX) {
776     rSGP.moveRel (-0.5 * symwidth, -0.5 * symheight);
777     rSGP.lineRel (symwidth, 0.0);
778     rSGP.lineRel (0.0, symheight);
779     rSGP.lineRel (-symwidth, 0.0);
780     rSGP.lineRel (0.0, -symheight);
781     rSGP.moveRel (0.5 * symwidth, 0.5 * symheight);
782   } else if (sym == SB_CIRCLE) {
783     rSGP.drawCircle (symwidth);
784   } else if (sym == SB_ERRORBAR) {
785     rSGP.moveRel (-0.5 * symwidth, 0.5 * symheight);
786     rSGP.lineRel (symwidth, 0.0);
787     rSGP.moveRel (-0.5 * symwidth, 0.0);
788     rSGP.lineRel (0.0, -symheight);
789     rSGP.moveRel (-0.5 * symwidth, 0.0);
790     rSGP.lineRel (symwidth, 0.0);
791     rSGP.moveRel (-0.5 * symwidth, 0.5 * symheight);
792   }
793 }
794
795
796
797 /* NAME
798  *    axis_scale                        calculates graph axis scaling
799  *
800  *  SYNOPSIS:
801  *    retval = axis_scale (min, max, nint, minp, maxp, nintp, 
802  *                         rec_total, rec_frac)
803  *
804  *    INPUT:
805  *      double min         Smallest value to plot
806  *      double max         Largest value to plot
807  *      int    nint        Number of intervals desired
808  *
809  *    OUTPUT:
810  *      int   retval       FALSE if illegal parameters, else TRUE
811  *      double *minp       Minimum graph value
812  *      double *maxp       Maximum graph value
813  *      int    *nintp      Number of intervals for graph
814  *      int    *rec_total  Recommended field width for printing out the number
815  *      int    *rec_frac   Recommended number of digits for print fraction
816  */
817
818 int 
819 EZPlot::axis_scale (double min, double max, int nint, double *minp, double *maxp, int *nintp)
820 {
821   if (min >= max || nint < 1) {
822     sys_error (ERR_WARNING, "Invalid params: min=%lf, max=%lf, num intervals=%d [axis_scale]", min, max, nint);
823     return (FALSE);
824   }
825   
826   double eps = 0.025;
827   double a = fabs(min);
828   if (fabs(min) < fabs(max))
829     a = fabs(max);
830   double scale = pow (10.0, floor(log10(a)));
831  loop:
832   double mina = min / scale;
833   double maxa = max / scale;
834   double d = (maxa - mina) / nint;
835   double j = d * eps;
836   double e = floor (log10(d));
837   double f = d / pow (10.0, e);
838   double v = 10.0;
839   if (f < sqrt(2.0))
840     v = 1.0;
841   else if (f < sqrt (10.0))
842     v = 2.0;
843   else if (f < sqrt (50.0))
844     v = 5.0;
845   double wdt = v * pow (10.0, e);
846   double g = floor (mina / wdt);
847     if (fabs(g + 1 - mina / wdt) < j)
848       g = g + 1;
849 #if 1
850     g++;
851 #endif
852   *minp = wdt * g;
853   double h = floor (maxa / wdt) + 1.0;
854   if (fabs(maxa / wdt + 1 - h) < j)
855      h = h - 1;
856 #if 1
857     h--;
858 #endif
859   *maxp = wdt * h;
860   *nintp = static_cast<int>(h - g);
861   if (fabs(*maxp) >= 10.0 || fabs(*minp) >= 10.0) {
862     scale = scale * 10.0;
863     goto loop;
864   }
865   
866   *minp *= scale;
867   *maxp *= scale;
868   
869   return (TRUE);
870 }
871
872
873 /* NAME
874  *   make_numfmt                Make a numeric format string
875  *
876  * SYNOPSIS
877  *   make_numfmt (fmtstr, fldwid, nfrac, min, max, nint)
878  *   char *fmtstr               Returned format string for printf()
879  *   int  *fldwid               Returned field width
880  *   int  *nfrac                If < 0, then calculate best number of
881  *                              fraction places & return that value
882  *                              If >= 0, then use that number of places
883  *   double min                 Minimum value
884  *   double max                 Maximum value
885  *   int nint                   Number of intervals between min & max
886  *
887  * DESCRIPTION
888  *   This  routine is written as an INTERNAL routine for EZPLOT
889  */
890
891 static inline double 
892 trunc (double x)
893 {
894   double integer;
895   
896   modf (x, &integer);
897   
898   return (integer);
899 }
900
901 void 
902 EZPlot::make_numfmt (char *fmtstr, int *fldwid, int *nfrac, double minval, double maxval, int nint)
903 {
904   int wid, frac, expon;
905
906   double delta = (maxval - minval) / nint;
907   double absmin = fabs(minval);
908   double absmax = fabs(maxval);
909   double logt = log10( max(absmin, absmax) );
910
911   if (fabs(logt) >= 6) {                // use exponential format 
912     if (fabs(logt) > 99)
913       expon = 5;                //  E+102 
914     else
915       expon = 4;                //  E+00 
916     
917     if (*nfrac < 0) {           // calculate frac 
918       delta /= pow (10., floor(logt));  // scale delta 
919       frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
920       if (frac < 1)
921         frac = 1;               // to be safe, add decimal pt 
922     } else                      // use users' frac 
923       frac = *nfrac;
924     
925     wid = 2 + frac + expon;
926     if (minval < 0. || maxval < 0.)
927       ++wid;
928     sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "g");
929   } else {                      // use fixed format 
930     wid = static_cast<int>(trunc(logt)) + 1;
931     if (wid < 1)
932       wid = 1;
933     if (minval < 0. || maxval < 0.)
934       ++wid;
935     
936     if (*nfrac < 0) {           // calculate frac 
937       if (delta >= 0.999999)
938         frac = 1;               // add a decimal pt to be safe 
939       else
940         frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
941     } else                      // use users' frac 
942       frac = *nfrac;
943     
944     wid += 1 + frac;
945     sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "f");
946   }
947
948   *fldwid = wid;
949   *nfrac = frac;
950 }
951