Fix plot initialization, improve frame titles
[ctsim.git] / libctgraphics / ezplot.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **  ezplot.cpp
5 **
6 **  This is part of the CTSim program
7 **  Copyright (c) 1983-2009 Kevin Rosenberg
8 **
9 **  This program is free software; you can redistribute it and/or modify
10 **  it under the terms of the GNU General Public License (version 2) as
11 **  published by the Free Software Foundation.
12 **
13 **  This program is distributed in the hope that it will be useful,
14 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 **  GNU General Public License for more details.
17 **
18 **  You should have received a copy of the GNU General Public License
19 **  along with this program; if not, write to the Free Software
20 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21 ******************************************************************************/
22
23 #include "ezplot.h"
24 #include <algorithm>
25
26 #ifdef __GNUWIN32__
27 int snprintf (char *, size_t, const char*, ...);
28 #endif
29
30 // Defaults
31 static const double TICKRATIO = 0.4;    // ratio of minor to major tick lengths
32 static const int MAXNUMFMT = 15;        // maximum length of a numeric format
33 static const int DEF_CURVE_CLR = C_RED;
34
35
36 EZPlotCurve::EZPlotCurve (const double* xData, const double* yData, int n)
37 : x(new double[n]), y(new double[n])
38 {
39   for (int i = 0; i < n; i++) {
40     x[i] = xData[i];
41     y[i] = yData[i];
42   }
43
44   m_iPointCount = n;
45 }
46
47 EZPlotCurve::~EZPlotCurve ()
48 {
49   delete x;
50   delete y;
51 }
52
53
54 void
55 EZPlot::addCurve (const double *y, int n)
56 {
57   double* x = new double [n];
58
59   for (int i = 0; i < n; i++)
60     x[i] = i;
61
62   addCurve (x, y, n);
63   delete x;
64 }
65
66
67 void
68 EZPlot::addCurve (const float *y, int n)
69 {
70   double* yDouble = new double [n];
71
72   for (int i = 0; i < n; i++)
73     yDouble[i] = y[i];
74
75   addCurve (yDouble, n);
76   delete yDouble;
77 }
78
79
80 void
81 EZPlot::addCurve (const float x[], const double y[], int num)
82 {
83   double* dx = new double [num];
84
85   for (int i = 0; i < num; i++)
86     dx[i] = x[i];
87
88   addCurve (dx, y, num);
89   delete dx;
90 }
91
92 void
93 EZPlot::addCurve (const double* const x, const float* const y, int num)
94 {
95   double* dy = new double [num];
96
97   for (int i = 0; i < num; i++)
98     dy[i] = y[i];
99
100   addCurve (x, dy, num);
101
102   delete dy;
103 }
104
105
106 void
107 EZPlot::addCurve (const double* const x, const double* const y, int num)
108 {
109   if (num < 1)
110     return;
111
112   EZPlotCurve* pCurve = new EZPlotCurve (x, y, num);
113   m_vecCurves.push_back (pCurve);
114 }
115
116
117 EZPlot::~EZPlot ()
118 {
119   for (EZPlotCurveIterator i = m_vecCurves.begin(); i != m_vecCurves.end(); i++)
120     delete *i;
121 }
122
123 void
124 EZPlot::clearCurves ()
125 {
126   for (EZPlotCurveIterator i = m_vecCurves.begin(); i != m_vecCurves.end(); i++)
127     delete *i;
128   m_vecCurves.erase (m_vecCurves.begin(), m_vecCurves.end());
129   initPlotSettings();
130 }
131
132
133 EZPlot::EZPlot ()
134 {
135     initKeywords();
136
137     m_pol.addSkipWord ("please");
138
139     m_pol.addSkipWord ("use");
140
141     m_pol.addSkipWord ("are");
142
143     m_pol.addSkipWord ("and");
144
145     m_pol.addSkipWord ("is");
146
147     m_pol.addSkipWord ("the");
148
149     m_pol.addSkipWord ("equals");
150
151     m_pol.addSkipChar ('=');
152
153
154
155     m_pol.usefile (POL::P_USE_STR,"");
156
157     m_pol.set_inputline ("!eoc ,");
158
159     m_pol.reader ();
160
161     m_pol.closefile ();
162
163
164
165     initPlotSettings();
166 }
167
168 void
169 EZPlot::initPlotSettings ()
170 {
171   m_iCurrentCurve = -1;
172
173   m_pSGP = NULL;
174
175
176   c_xlabel = "";
177   c_ylabel =  "";
178   c_title = "";
179
180   o_xporigin = 0.0;
181   o_yporigin = 0.0;
182   o_xlength  = 1.0;
183   o_ylength  = 1.0;
184
185   o_xaxis = LINEAR;
186   o_yaxis = LINEAR;
187
188   o_grid = FALSE;
189   o_box = FALSE;
190
191   o_xmajortick = 10;
192   o_ymajortick =  8;
193   o_xminortick =  4;
194   o_yminortick =  4;
195
196   o_color = DEF_CURVE_CLR;
197   o_symfreq = 1;
198   o_symbol = -1;
199   o_linestyle = SGP::LS_SOLID;
200
201   o_xtlabel = TRUE;
202   o_ytlabel = TRUE;
203   o_xticks = BELOW;
204   o_yticks = LEFT;
205
206   o_legendbox = INSIDE;
207   o_tag = FALSE;
208
209   s_xtitle   = FALSE;
210   s_ytitle   = FALSE;
211   s_xcross   = FALSE;
212   s_ycross   = FALSE;
213   s_lxfrac   = FALSE;
214   s_lyfrac   = FALSE;
215   s_xlegend  = FALSE;
216   s_ylegend  = FALSE;
217   s_textsize = FALSE;
218   s_xmin     = FALSE;
219   s_xmax     = FALSE;
220   s_ymin     = FALSE;
221   s_ymax     = FALSE;
222
223   clr_axis   = C_LTGRAY;                // set fixed colors
224   clr_title  = C_RED;
225   clr_label  = C_BLUE;
226   clr_legend = C_CYAN;
227   clr_number = C_GREEN;
228   clr_grid   = C_LTGRAY;
229 }
230
231 void
232
233 EZPlot::setColor (unsigned int iCurve, int iColor)
234
235 {
236
237   if (m_veciColor.size() <= iCurve) {
238
239     m_veciColor.resize ((m_iCurrentCurve + 1) * 2);
240
241     m_vecbColorSet.resize ((m_iCurrentCurve + 1) * 2);
242
243   }
244
245   m_veciColor [iCurve] = iColor;
246
247   m_vecbColorSet [iCurve] = true;
248
249 }
250
251
252
253 void
254
255 EZPlot::setSymbol (unsigned int iCurve, int iSymbol)
256
257 {
258
259   if (m_veciSymbol.size() <= iCurve) {
260
261     m_veciSymbol.resize ((m_iCurrentCurve + 1) * 2);
262
263     m_vecbSymbolSet.resize ((m_iCurrentCurve + 1) * 2);
264
265   }
266
267   m_veciSymbol [iCurve] = iSymbol;
268
269   m_vecbSymbolSet [iCurve] = true;
270
271 }
272
273
274
275 void
276
277 EZPlot::setSymbolFreq (unsigned int iCurve, int iSymbolFreq)
278
279 {
280
281   if (m_veciSymbolFreq.size() <= iCurve) {
282
283     m_veciSymbolFreq.resize ((m_iCurrentCurve + 1) * 2);
284
285     m_vecbSymbolFreqSet.resize ((m_iCurrentCurve + 1) * 2);
286
287   }
288
289   m_veciSymbolFreq [iCurve] = iSymbolFreq;
290
291   m_vecbSymbolFreqSet [iCurve] = true;
292
293 }
294
295
296
297 void
298
299 EZPlot::setLinestyle (unsigned int iCurve, int iLinestyle)
300
301 {
302
303   if (m_veciLinestyle.size() <= iCurve) {
304
305     m_veciLinestyle.resize ((m_iCurrentCurve + 1) * 2);
306
307     m_vecbLinestyleSet.resize ((m_iCurrentCurve + 1) * 2);
308
309   }
310
311   m_veciLinestyle [iCurve] = iLinestyle;
312
313   m_vecbLinestyleSet [iCurve] = true;
314
315 }
316
317
318
319 void
320
321 EZPlot::setLegend (unsigned int iCurve, const std::string& strLegend)
322
323 {
324
325   if (m_vecsLegend.size() <= iCurve) {
326
327     m_vecsLegend.resize ((m_iCurrentCurve + 1) * 2);
328
329     m_vecbLegendSet.resize ((m_iCurrentCurve + 1) * 2);
330
331   }
332
333   m_vecsLegend [iCurve] = strLegend;
334
335   m_vecbLegendSet [iCurve] = true;
336
337 }
338
339
340
341 void
342
343 EZPlot::setLegend (unsigned int iCurve, const char* const pszLegend)
344
345 {
346
347   if (m_vecsLegend.size() <= iCurve) {
348
349     m_vecsLegend.resize ((m_iCurrentCurve + 1) * 2);
350
351     m_vecbLegendSet.resize ((m_iCurrentCurve + 1) * 2);
352
353   }
354
355   m_vecsLegend [iCurve] = pszLegend;
356
357   m_vecbLegendSet [iCurve] = true;
358
359 }
360
361
362
363 int
364
365 EZPlot::getColor (unsigned int iCurve) const
366
367 {
368
369   if (m_veciColor.size() > iCurve && m_vecbColorSet[iCurve])
370
371     return m_veciColor[iCurve];
372
373   else
374
375     return o_color;
376
377 }
378
379
380
381 int
382
383 EZPlot::getSymbol (unsigned int iCurve) const
384
385 {
386
387   if (m_veciSymbol.size() > iCurve && m_vecbSymbolSet[iCurve])
388
389     return m_veciSymbol[iCurve];
390
391   else
392
393     return o_symbol;
394
395 }
396
397
398
399 int
400
401 EZPlot::getSymbolFreq (unsigned int iCurve) const
402
403 {
404
405   if (m_veciSymbolFreq.size() > iCurve && m_vecbSymbolFreqSet[iCurve])
406
407     return m_veciSymbolFreq[iCurve];
408
409   else
410
411     return o_symfreq;
412
413 }
414
415
416
417 int
418
419 EZPlot::getLinestyle (unsigned int iCurve) const
420
421 {
422
423   if (m_veciLinestyle.size() > iCurve && m_vecbLinestyleSet[iCurve])
424
425     return m_veciLinestyle[iCurve];
426
427   else
428
429     return o_linestyle;
430
431 }
432
433
434
435 const std::string*
436
437 EZPlot::getLegend (unsigned int iCurve) const
438
439 {
440
441   if (m_vecsLegend.size() > iCurve && m_vecbLegendSet[iCurve])
442
443     return &m_vecsLegend[iCurve];
444
445   else
446
447     return NULL;
448
449 }
450
451
452
453
454
455 /* NAME
456 *   plot                Plots all curves collected by addCurves ()
457 *
458 * SYNOPSIS
459 *   plot()
460 *
461 * DESCRIPTION
462 *   This routine plots the curves that have stored by addCurves().
463 *
464 * CALLS
465 *   drawAxes() & make_numfmt()
466 */
467
468 void
469 EZPlot::plot (SGP* pSGP)
470 {
471   if (m_vecCurves.size() <= 0)
472     return;
473
474   m_pSGP = pSGP;
475
476   m_pSGP->setWindow (0., 0., 1., 1.);
477
478   if (s_textsize == TRUE)
479     m_pSGP->setTextPointSize (v_textsize);
480
481   charheight = m_pSGP->getCharHeight();
482   charwidth = m_pSGP->getCharWidth();
483   double symheight = charheight * 0.3;       // size of symbol in NDC
484   double symwidth = symheight;
485
486   if (m_vecCurves.size() < 1)
487     return; // can't plot if there are no curves to plot
488
489   // find a curve with at least one point to get base point
490   double xmin, xmax, ymin, ymax;   // extent of curves in world coord
491   bool found = false;
492
493   for (unsigned int iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
494     const EZPlotCurve* const pCurve = m_vecCurves [iCurve];
495     if (pCurve->m_iPointCount > 0) {
496       xmin = pCurve->x[0];
497       xmax = xmin;
498       ymin = pCurve->y[0];
499       ymax = ymin;
500       found = true;
501       break;
502     }
503   }
504   if (! found)
505     return; // curve(s) are empty, so no plotting
506
507   for (unsigned int iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
508     const EZPlotCurve* const pCurve = m_vecCurves [iCurve];
509
510     for (int ip = 0; ip < pCurve->m_iPointCount; ip++) {
511       if (pCurve->x[ip] > xmax)
512         xmax = pCurve->x[ip];
513       else if (pCurve->x[ip] < xmin)
514         xmin = pCurve->x[ip];
515       if (pCurve->y[ip] > ymax)
516         ymax = pCurve->y[ip];
517       else if (pCurve->y[ip] < ymin)
518         ymin = pCurve->y[ip];
519     }
520   }
521
522   // extend graph limits for user defined axis cross positions
523   if (s_xcross == TRUE) {
524     if (v_xcross < xmin)
525       xmin = v_xcross;
526     else if (v_xcross > xmax)
527       xmax = v_xcross;
528   }
529
530   if (s_ycross == TRUE) {
531     if (v_ycross < ymin)
532       ymin = v_ycross;
533     else if (v_ycross > ymax)
534       ymax = v_ycross;
535   }
536
537   // find nice endpoints for axes
538   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))
539     return;
540
541
542   // check if user set x-axis extents
543   if (s_xmin == TRUE) {
544     xgw_min = v_xmin;
545     x_nint = o_xmajortick - 1;
546   }
547   if (s_xmax == TRUE) {
548     xgw_max = v_xmax;
549     x_nint = o_xmajortick - 1;
550   }
551
552   // check if user set y-axis extents
553   if (s_ymin == TRUE) {
554     ygw_min = v_ymin;
555     y_nint = o_ymajortick - 1;
556   }
557   if (s_ymax == TRUE) {
558     ygw_max = v_ymax;
559     y_nint = o_ymajortick - 1;
560   }
561
562   // calculate increment between major axis in world coordinates
563   xw_tickinc = (xgw_max - xgw_min) / x_nint;
564   yw_tickinc = (ygw_max - ygw_min) / y_nint;
565
566   // we have now calcuated xgw_min, xyw_max, ygw_min, & ygw_max
567
568   // set the number of decimal point to users' setting or default
569   // Two formats for numbers: Fixed:    -nnn.f and  Exponent: -n.fffE+eee
570   if (s_lxfrac == TRUE)
571     x_frac = v_lxfrac;
572   else
573     x_frac = -1;
574
575   if (s_lyfrac == TRUE)
576     y_frac = v_lyfrac;
577   else
578     y_frac = -1;
579
580   make_numfmt (x_numfmt, &x_fldwid, &x_frac, xgw_min, xgw_max, x_nint);
581   make_numfmt (y_numfmt, &y_fldwid, &y_frac, ygw_min, ygw_max, y_nint);
582
583   xtl_wid = x_fldwid * charwidth;               // calc size of tick labels
584   ytl_wid = y_fldwid * charwidth;
585   tl_height = charheight;
586
587   // calculate the extent of the plot frame
588   xp_min = o_xporigin;
589   yp_min = o_yporigin;
590   xp_max = xp_min + o_xlength;
591   yp_max = yp_min + o_ylength;
592
593   xp_min = clamp (xp_min, 0., 1.);
594   xp_max = clamp (xp_max, 0., 1.);
595   yp_min = clamp (yp_min, 0., 1.);
596   yp_max = clamp (yp_max, 0., 1.);
597
598   xa_min = xp_min;              // extent of axes
599   xa_max = xp_max;
600   ya_min = yp_min;
601   ya_max = yp_max;
602
603   // adjust frame for title
604   title_row = ya_max;;
605
606   if (c_title.length() > 0)
607     ya_max -= 2 * charheight;
608
609   else
610
611     ya_max -= 0.7 * charheight;  // allow room for yaxis ticklabel
612
613
614   // calculate legend box boundaries
615   int max_leg = 0;                      // longest legend in characters
616   int num_leg = 0;                      // number of legend titles
617
618   for (unsigned int iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
619     const std::string* pstrLegend = getLegend (iCurve);
620
621     if (pstrLegend && pstrLegend->length() > 0) {
622
623       int nLegend = pstrLegend->length();
624       if (nLegend > 0) {
625         ++num_leg;
626         if (nLegend > max_leg)
627
628           nLegend = max_leg;
629
630       }
631     }
632   }
633
634   if (num_leg > 0 && o_legendbox != NOLEGEND) {
635     double leg_width  = (max_leg + 2) * charwidth;      // size of legend box
636     double leg_height = num_leg * 3 * charheight;
637
638     if (s_xlegend == TRUE)
639       xl_max = v_xlegend;
640     else {
641       xl_max = xa_max;
642       if (o_legendbox == OUTSIDE)
643         xa_max -= (leg_width + 0.5 * charwidth);
644     }
645     xl_min = xl_max - leg_width;
646
647     if (s_ylegend == TRUE)
648       yl_max = v_ylegend;
649     else
650       yl_max = ya_max;
651
652     yl_min = yl_max - leg_height;
653
654     m_pSGP->setColor (clr_legend);
655     m_pSGP->drawRect (xl_min, yl_min, xl_max, yl_max);
656
657     int iLegend = 0;                    // current legend position
658
659     for (unsigned int iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
660       const std::string* pstrLegend = getLegend (iCurve);
661       if (! pstrLegend || pstrLegend->length() == 0)
662         continue;
663
664       double xmin = xl_min + 1.0 * charwidth;
665       double xmax = xl_max - 1.0 * charwidth;
666       double y = yl_max - (2.0 + iLegend * 3) * charheight;
667
668       m_pSGP->moveAbs (xmin, y + 0.5 * charheight);
669
670       m_pSGP->drawText (pstrLegend->c_str());
671       m_pSGP->setColor (getColor (iCurve));
672
673       int iLS = getLinestyle (iCurve);
674       if (iLS != SGP::LS_NOLINE) {
675         m_pSGP->setLineStyle (iLS);
676         m_pSGP->moveAbs (xmin, y);
677         m_pSGP->lineAbs (xmax, y);
678       }
679
680       int iSymbol = getSymbol (iCurve);
681       if (iSymbol > 0) {
682         double xinc = (xmax - xmin) / (5 - 1);
683         m_pSGP->setLineStyle (SGP::LS_SOLID);
684         for (int j = 0; j < 5; j++) {
685           m_pSGP->moveAbs (xmin + j * xinc, y);
686           symbol (iSymbol, symwidth, symheight);
687         }
688       }
689       ++iLegend;        // move to next legend position
690     }
691   }   // end legend printing
692
693   // calculate the extent of the axes
694
695   /*-------------------------*/
696   /* adjust frame for labels */
697   /*-------------------------*/
698
699   // X-Label
700   if (c_xlabel.length() > 0)
701     ya_min += 1.5 * charheight;
702   xlbl_row = xp_min;            // put x-label on bottom of plot frame
703
704   // Y-Label
705   if (c_ylabel.length() > 0) {
706
707     m_pSGP->setTextAngle (HALFPI);
708
709     m_pSGP->setTextSize (1.5 * charheight);
710
711     double xExtent, yExtent;
712
713     m_pSGP->getTextExtent (c_ylabel.c_str(), &xExtent, &yExtent);
714
715     m_pSGP->setTextSize (charheight);
716
717     xa_min += xExtent;
718
719     m_pSGP->setTextAngle (0.0);
720
721   }
722   ylbl_col = xp_min;
723
724   /*------------------------------*/
725   /* adjust frame for tick labels */
726   /*------------------------------*/
727
728   // Calc offset of tick labels from axes
729   if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
730     xtl_ofs = 0.0;
731   else if (o_xticks == BELOW)
732     xtl_ofs = -0.5 * charheight;
733   else if (o_xticks == ABOVE)
734     xtl_ofs = 0.5 * charheight;
735
736   if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
737     ytl_ofs = 0.0;
738   else if (o_yticks == LEFT) {
739     double xExtentMin, xExtentMax, yExtent;
740     char s[1024];
741     snprintf (s, sizeof(s), y_numfmt, ymin);
742     m_pSGP->getTextExtent (s, &xExtentMin, &yExtent);
743     snprintf (s, sizeof(s), y_numfmt, ymax);
744     m_pSGP->getTextExtent (s, &xExtentMax, &yExtent);
745     if (xExtentMin > xExtentMax)
746       xExtentMax = xExtentMin;
747     ytl_ofs = -xExtentMax;
748   } else if (o_yticks == RIGHT)
749     ytl_ofs = 1.5 * charwidth;
750
751   xa_max -= 0.7 * x_fldwid * charwidth; // make room for last x tick label
752
753   xt_min = xa_min;
754   yt_min = ya_min;
755   xt_max = xa_max;
756   yt_max = ya_max;
757
758   // see if need to shrink axis extents and/or tick extents
759   if (xtl_ofs != 0.0 && s_ycross == FALSE) {
760     if (o_xticks == BELOW) {
761       ya_min += 1.5 * charheight;
762       yt_min = ya_min;
763     } else if (o_xticks == ABOVE) {
764       ya_min += 0.0;
765       yt_min = ya_min + 1.5 * charheight;
766     }
767   } else   // noaxis, no t-labels, or user set cross
768     yt_min = ya_min;
769
770   if (ytl_ofs != 0.0 && s_xcross == FALSE) {
771     if (o_yticks == LEFT) {
772       xa_min += 2*charwidth - ytl_ofs; // (2 + y_fldwid) * charwidth;
773       xt_min = xa_min;
774     } else if (o_yticks == RIGHT) {
775       xa_min += 0.0;
776       xt_min = xa_min + ytl_ofs; // + y_fldwid * charwidth;
777     }
778   } else
779     xt_min = xa_min;
780
781   // decrease size of graph, if necessary, to accommadate space
782   // between axis boundary and boundary of ticks
783   double x_added_ticks = 0; // number of tick spaces added to axis
784   double y_added_ticks = 0;
785   if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
786     x_added_ticks = 0;
787   if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
788     y_added_ticks = 0;
789
790   if (o_grid == TRUE) {
791     if (x_added_ticks < 0)
792       x_added_ticks = 2;
793     if (y_added_ticks < 0)
794       y_added_ticks = 2;
795   }
796
797   if (x_added_ticks < 0) {
798     if (o_yticks == LEFT || s_ycross)
799       x_added_ticks = 1;
800     else
801       x_added_ticks = 2;
802   }
803
804   if (y_added_ticks < 0) {
805     if (o_xticks == BELOW || s_xcross)
806       y_added_ticks = 1;
807     else
808       y_added_ticks = 2;
809   }
810
811   xn_tickinc = (xt_max - xt_min) / (x_nint + x_added_ticks);
812   yn_tickinc = (yt_max - yt_min) / (y_nint + y_added_ticks);
813
814   xt_min += 0.5 * x_added_ticks * xn_tickinc;
815   xt_max -= 0.5 * x_added_ticks * xn_tickinc;
816   yt_min += 0.5 * y_added_ticks * yn_tickinc;
817   yt_max -= 0.5 * y_added_ticks * yn_tickinc;
818
819   xgn_min = xt_min;
820   xgn_max = xt_max;
821   ygn_min = yt_min;
822   ygn_max = yt_max;
823
824   //------------------------------------------------------------------------
825
826   m_xWorldScale = (xgn_max - xgn_min) / (xgw_max - xgw_min);
827   m_yWorldScale = (ygn_max - ygn_min) / (ygw_max - ygw_min);
828
829   // PLOT CURVES
830
831   m_pSGP->setLineStyle (SGP::LS_SOLID);
832   drawAxes();
833
834
835   double clipRect[4];
836
837   clipRect[0] = xgn_min; clipRect[1] = ygn_min; clipRect[2] = xgn_max; clipRect[3] = ygn_max;
838
839
840
841   for (unsigned int iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
842     const EZPlotCurve* const pCurve = m_vecCurves [iCurve];
843
844     m_pSGP->setColor (getColor (iCurve));
845     int iSym = getSymbol (iCurve);
846     int iLS = getLinestyle (iCurve);
847
848     if (iLS != SGP::LS_NOLINE) {
849       m_pSGP->setLineStyle (iLS);
850       double x1 = convertWorldToNDC_X (pCurve->x[0]);
851       double y1 = convertWorldToNDC_Y (pCurve->y[0]);
852
853       for (int i = 1; i < pCurve->m_iPointCount; i++) {
854         double x2 = convertWorldToNDC_X (pCurve->x[i]);
855         double y2 = convertWorldToNDC_Y (pCurve->y[i]);
856         double x2Clip = x2;
857         double y2Clip = y2;
858
859         if (clip_rect (x1, y1, x2Clip, y2Clip, clipRect)) {
860           m_pSGP->moveAbs (x1, y1);
861           m_pSGP->lineAbs (x2Clip, y2Clip);
862         }
863         x1 = x2;
864         y1 = y2;
865       }
866     }
867     if (iSym > 0) {
868       int iSymFreq = getSymbolFreq (iCurve);
869       m_pSGP->setLineStyle (SGP::LS_SOLID);
870       double x = convertWorldToNDC_X (pCurve->x[0]);
871       double y = convertWorldToNDC_Y (pCurve->y[0]);
872       m_pSGP->moveAbs (x, y);
873       symbol (iSym, symwidth, symheight);
874       for (int i = 1; i < pCurve->m_iPointCount; i++)
875         if (i % iSymFreq == 0 || i == pCurve->m_iPointCount - 1) {
876           x = convertWorldToNDC_X (pCurve->x[i]);
877           y = convertWorldToNDC_Y (pCurve->y[i]);
878
879           if (y >= ygn_min && y <= ygn_max) {
880             m_pSGP->moveAbs (x, y);
881             symbol (iSym, symwidth, symheight);
882           }
883         }
884     }
885   }
886 }
887
888
889 /* NAME
890 *   drawAxes                    INTERNAL routine to draw axis & label them
891 *
892 * SYNOPSIS
893 *   drawAxes()
894 */
895
896
897 void
898 EZPlot::drawAxes()
899 {
900   double xticklen = 0, yticklen = 0; // length of ticks in NDC
901   double minorinc;              // increment between minor axes
902   double xaxispos, yaxispos;    // crossing of axes
903   double x, y, x2, y2;
904   bool axis_near;       // TRUE if axis too close to print t-label
905   int i, j;
906   char str[256];
907   char *numstr;
908
909   m_pSGP->setTextSize (charheight);
910   m_pSGP->setTextColor (1, -1);
911
912   if (o_xticks == ABOVE)
913     xticklen = 0.5 * charheight;
914   else if (o_xticks == BELOW)
915     xticklen = -0.5 * charheight;
916
917   if (o_yticks == RIGHT)
918     yticklen = charwidth;
919   else if (o_yticks == LEFT)
920     yticklen = -charwidth;
921
922   if (c_title.length() > 0) {
923     double wText, hText;
924     m_pSGP->setTextSize (charheight * 2.0);
925     m_pSGP->getTextExtent (c_title.c_str(), &wText, &hText);
926     m_pSGP->moveAbs (xa_min + (xa_max-xa_min)/2 - wText/2, title_row);
927     m_pSGP->setTextColor (clr_title, -1);
928     m_pSGP->drawText (c_title);
929     m_pSGP->setTextSize (charheight);
930   }
931
932   if (o_grid == TRUE || o_box == TRUE) {
933     m_pSGP->setColor (clr_axis);
934     m_pSGP->moveAbs (xa_min, ya_min);
935     m_pSGP->lineAbs (xa_max, ya_min);
936     m_pSGP->lineAbs (xa_max, ya_max);
937     m_pSGP->lineAbs (xa_min, ya_max);
938     m_pSGP->lineAbs (xa_min, ya_min);
939   }
940
941   // calculate position of axes
942
943   // x-axis
944   if (s_ycross == TRUE) {       // convert users' world-coord
945     xaxispos = convertWorldToNDC_Y (v_ycross);// axis to its position in NDC
946     x = convertWorldToNDC_X (xgw_min);
947   } else
948     xaxispos = ya_min;
949
950   // y-axis
951   if (s_xcross == TRUE) {       // convert users' world-coord
952     yaxispos = convertWorldToNDC_X (v_xcross);// axis to its NDC position
953     y = convertWorldToNDC_Y (ygw_min);
954   } else
955     yaxispos = xa_min;
956
957   /*-------------*/
958   /* draw x-axis */
959   /*-------------*/
960
961   if (o_xaxis == LINEAR) {
962     // draw axis line
963
964     m_pSGP->setColor (clr_axis);
965     if (o_tag && !o_grid && !o_box && s_xcross) {
966       m_pSGP->moveAbs (xa_min, xaxispos - charheight);
967       m_pSGP->lineAbs (xa_min, xaxispos + charheight);
968     }
969     m_pSGP->moveAbs (xa_min, xaxispos);
970     m_pSGP->lineAbs (xa_max, xaxispos);
971     if (o_tag && !o_grid && !o_box) {
972       m_pSGP->moveAbs (xa_max, xaxispos - charheight);
973       m_pSGP->lineAbs (xa_max, xaxispos + charheight);
974     }
975
976     if (o_grid == TRUE) {
977       m_pSGP->setColor (clr_grid);
978       for (i = 0; i <= x_nint; i++) {
979         m_pSGP->moveAbs (xt_min + xn_tickinc * i, ya_max);
980         m_pSGP->lineAbs (xt_min + xn_tickinc * i, ya_min);
981       }
982     }
983     m_pSGP->setTextSize (charheight * 1.5);
984     m_pSGP->setTextColor (clr_label, -1);
985
986     double wText, hText;
987
988     m_pSGP->getTextExtent (c_xlabel.c_str(), &wText, &hText);
989     m_pSGP->moveAbs (xa_min + (xa_max-xa_min)/2 - wText / 2, xlbl_row +  charheight * 1.5);
990
991     m_pSGP->drawText (c_xlabel);
992     m_pSGP->setTextSize (charheight);
993     minorinc = xn_tickinc / (o_xminortick + 1);
994
995     for (i = 0; i <= x_nint; i++) {
996       x = xt_min + xn_tickinc * i;
997       m_pSGP->setColor (clr_axis);
998       m_pSGP->moveAbs (x, xaxispos);
999       m_pSGP->lineAbs (x, xaxispos + xticklen);
1000       if (i != x_nint)
1001         for (j = 1; j <= o_xminortick; j++) {
1002           x2 = x + minorinc * j;
1003           m_pSGP->moveAbs (x2, xaxispos);
1004           m_pSGP->lineAbs (x2, xaxispos + TICKRATIO * xticklen);
1005         }
1006         axis_near = FALSE;
1007         if (xaxispos + xtl_ofs > ya_min && o_yaxis != NOAXIS) {
1008           double xw = xgw_min + i * xw_tickinc;
1009           double x = convertWorldToNDC_X (xw);
1010           double d = x - yaxispos;
1011           if (o_yticks == RIGHT && d >= 0  && d < 0.9 * xn_tickinc)
1012             axis_near = TRUE;
1013           if (o_yticks == LEFT && d <= 0 && d > -0.9 * xn_tickinc)
1014             axis_near = TRUE;
1015         }
1016
1017         if (o_xtlabel == TRUE && axis_near == FALSE) {
1018           snprintf (str, sizeof(str), x_numfmt, xgw_min + xw_tickinc * i);
1019           numstr = str_skip_head (str, " ");
1020           double xExtent, yExtent;
1021           m_pSGP->getTextExtent (numstr, &xExtent, &yExtent);
1022           m_pSGP->moveAbs (x - xExtent/2, xaxispos + xtl_ofs);
1023           m_pSGP->setTextColor (clr_number, -1);
1024           m_pSGP->drawText (numstr);
1025         }
1026     }
1027   }             // X - Axis
1028
1029
1030   /*--------*/
1031   /* y-axis */
1032   /*--------*/
1033
1034   if (o_yaxis == LINEAR) {
1035
1036     m_pSGP->setColor (clr_axis);
1037     if (o_tag && !o_grid && !o_box && s_ycross) {
1038       m_pSGP->moveAbs (yaxispos - charwidth, ya_min);
1039       m_pSGP->lineAbs (yaxispos + charwidth, ya_min);
1040     }
1041     m_pSGP->moveAbs (yaxispos, ya_min);
1042     m_pSGP->lineAbs (yaxispos, ya_max);
1043     if (o_tag && !o_grid && !o_box) {
1044       m_pSGP->moveAbs (yaxispos - charwidth, ya_max);
1045       m_pSGP->lineAbs (yaxispos + charwidth, ya_max);
1046     }
1047
1048     if (o_grid == TRUE) {
1049       m_pSGP->setColor (clr_grid);
1050       for (i = 0; i <= y_nint; i++) {
1051         y = yt_min + yn_tickinc * i;
1052         m_pSGP->moveAbs (xa_max, y);
1053         m_pSGP->lineAbs (xa_min, y);
1054       }
1055     }
1056
1057     m_pSGP->setTextAngle (HALFPI);
1058     m_pSGP->setTextSize (1.5 * charheight);
1059     m_pSGP->setTextColor (clr_label, -1);
1060
1061     double xExtent, yExtent;
1062
1063     m_pSGP->getTextExtent (c_ylabel.c_str(), &xExtent, &yExtent);
1064     m_pSGP->moveAbs (ylbl_col, ya_min + (ya_max-ya_min)/2 - yExtent / 2);
1065     m_pSGP->drawText (c_ylabel);
1066
1067     m_pSGP->setTextAngle (0.0);
1068     m_pSGP->setTextSize (charheight);
1069     minorinc = yn_tickinc / (o_yminortick + 1);
1070
1071     for (i = 0; i <= y_nint; i++) {
1072       y = yt_min + yn_tickinc * i;
1073       m_pSGP->setColor (clr_axis);
1074       m_pSGP->moveAbs (yaxispos, y);
1075       m_pSGP->lineAbs (yaxispos + yticklen, y);
1076       if (i != y_nint)
1077         for (j = 1; j <= o_yminortick; j++) {
1078           y2 = y + minorinc * j;
1079           m_pSGP->moveAbs (yaxispos, y2);
1080           m_pSGP->lineAbs (yaxispos + TICKRATIO * yticklen, y2);
1081         }
1082         axis_near = FALSE;
1083         if (yaxispos + ytl_ofs > xa_min && o_xaxis != NOAXIS) {
1084           double yw = ygw_min + i * yw_tickinc;
1085           double y = convertWorldToNDC_Y (yw);
1086           double d = y - xaxispos;
1087           if (o_xticks == ABOVE && d >= 0 && d < 0.9 * yn_tickinc)
1088             axis_near = TRUE;
1089           if (o_xticks == BELOW && d <= 0 && d > -0.9 * yn_tickinc)
1090             axis_near = TRUE;
1091         }
1092         if (o_ytlabel == TRUE && axis_near == FALSE) {
1093           snprintf (str, sizeof(str), y_numfmt, ygw_min + yw_tickinc * i);
1094           double xExtent, yExtent;
1095           m_pSGP->getTextExtent (str, &xExtent, &yExtent);
1096           if (o_yticks == LEFT)
1097             m_pSGP->moveAbs (yaxispos - 1.5 * charwidth - xExtent, y + 0.5 * yExtent);
1098           else
1099             m_pSGP->moveAbs (yaxispos + 1.5 * charwidth, y + 0.5 * yExtent);
1100           m_pSGP->setTextColor (clr_number, -1);
1101           m_pSGP->drawText (str);
1102         }
1103     }
1104   }             // Y - Axis
1105 }
1106
1107
1108 void
1109 EZPlot::symbol (int sym, double symwidth, double symheight)
1110 {
1111   if (sym <= 0)
1112     return;
1113
1114   if (sym == SB_CROSS) {
1115     m_pSGP->markerRel (0, 0);
1116 //    m_pSGP->moveRel (-0.5 * symwidth, -0.5 * symheight);
1117 //    m_pSGP->lineRel (symwidth, symheight);
1118 //    m_pSGP->moveRel (-symwidth, 0.0);
1119 //    m_pSGP->lineRel (symwidth, -symheight);
1120 //    m_pSGP->moveRel (-0.5 * symwidth, 0.5 * symheight);
1121   } else if (sym == SB_PLUS) {
1122     m_pSGP->moveRel (-0.5 * symwidth, 0.0);
1123     m_pSGP->lineRel (symwidth, 0.0);
1124     m_pSGP->moveRel (-0.5 * symwidth, -0.5 * symheight);
1125     m_pSGP->lineRel (0.0, symheight);
1126     m_pSGP->moveRel (0.0, -0.5 * symheight);
1127   } else if (sym == SB_BOX) {
1128     m_pSGP->moveRel (-0.5 * symwidth, -0.5 * symheight);
1129     m_pSGP->lineRel (symwidth, 0.0);
1130     m_pSGP->lineRel (0.0, symheight);
1131     m_pSGP->lineRel (-symwidth, 0.0);
1132     m_pSGP->lineRel (0.0, -symheight);
1133     m_pSGP->moveRel (0.5 * symwidth, 0.5 * symheight);
1134   } else if (sym == SB_CIRCLE) {
1135     m_pSGP->drawCircle (symwidth);
1136   } else if (sym == SB_ERRORBAR) {
1137     m_pSGP->moveRel (-0.5 * symwidth, 0.5 * symheight);
1138     m_pSGP->lineRel (symwidth, 0.0);
1139     m_pSGP->moveRel (-0.5 * symwidth, 0.0);
1140     m_pSGP->lineRel (0.0, -symheight);
1141     m_pSGP->moveRel (-0.5 * symwidth, 0.0);
1142     m_pSGP->lineRel (symwidth, 0.0);
1143     m_pSGP->moveRel (-0.5 * symwidth, 0.5 * symheight);
1144   } else if (sym == SB_POINT) {
1145     m_pSGP->pointRel (0, 0);
1146   }
1147 }
1148
1149
1150
1151 /* NAME
1152 *    axis_scale                 calculates graph axis scaling
1153 *
1154 *  SYNOPSIS:
1155 *    retval = axis_scale (min, max, nint, minp, maxp, nintp,
1156 *                          rec_total, rec_frac)
1157 *
1158 *    INPUT:
1159 *       double min         Smallest value to plot
1160 *       double max         Largest value to plot
1161 *       int    nint        Number of intervals desired
1162 *
1163 *    OUTPUT:
1164 *       int   retval       FALSE if illegal parameters, else TRUE
1165 *       double *minp       Minimum graph value
1166 *       double *maxp       Maximum graph value
1167 *       int    *nintp      Number of intervals for graph
1168 *      int    *rec_total  Recommended field width for printing out the number
1169 *       int    *rec_frac   Recommended number of digits for print fraction
1170 */
1171
1172 int
1173 EZPlot::axis_scale (double min, double max, int nint, double *minp, double *maxp, int *nintp)
1174 {
1175   if (nint < 1) {
1176     sys_error (ERR_WARNING, "No intervals to plot: num intervals=%d [axis_scale]", nint);
1177     return (FALSE);
1178   }
1179   if (min >= max) {
1180     double scaled = fabs(max) / 10;
1181     if (scaled == 0)
1182       scaled = 0.1;
1183     *minp = min - scaled;
1184     *maxp = max + scaled;
1185     *nintp = 2;
1186     return (TRUE);
1187   }
1188
1189   double eps = 0.025;
1190   double a = fabs(min);
1191   if (fabs(min) < fabs(max))
1192     a = fabs(max);
1193   double scale = pow (10.0, floor(log10(a)));
1194 loop:
1195   double mina = min / scale;
1196   double maxa = max / scale;
1197   double d = (maxa - mina) / nint;
1198   double j = d * eps;
1199   double e = floor (log10(d));
1200   double f = d / pow (10.0, e);
1201   double v = 10.0;
1202   if (f < sqrt(2.0))
1203     v = 1.0;
1204   else if (f < sqrt (10.0))
1205     v = 2.0;
1206   else if (f < sqrt (50.0))
1207     v = 5.0;
1208   double wdt = v * pow (10.0, e);
1209   double g = floor (mina / wdt);
1210   if (fabs(g + 1 - mina / wdt) < j)
1211     g = g + 1;
1212 #undef TEST1
1213
1214 #ifdef TEST1
1215   g++;
1216 #endif
1217   *minp = wdt * g;
1218   double h = floor (maxa / wdt) + 1.0;
1219   if (fabs(maxa / wdt + 1 - h) < j)
1220     h = h - 1;
1221 #ifdef TEST1
1222   h--;
1223 #endif
1224   *maxp = wdt * h;
1225   *nintp = static_cast<int>(h - g);
1226   if (fabs(*maxp) >= 10.0 || fabs(*minp) >= 10.0) {
1227     scale = scale * 10.0;
1228     goto loop;
1229   }
1230
1231   *minp *= scale;
1232   *maxp *= scale;
1233
1234   return (TRUE);
1235 }
1236
1237
1238 /* NAME
1239 *   make_numfmt         Make a numeric format string
1240 *
1241 * SYNOPSIS
1242 *   make_numfmt (fmtstr, fldwid, nfrac, min, max, nint)
1243 *   char *fmtstr                Returned format string for printf()
1244 *   int  *fldwid                Returned field width
1245 *   int  *nfrac         If < 0, then calculate best number of
1246 *                               fraction places & return that value
1247 *                               If >= 0, then use that number of places
1248 *   double min                  Minimum value
1249 *   double max                  Maximum value
1250 *   int nint                    Number of intervals between min & max
1251 *
1252 * DESCRIPTION
1253 *   This  routine is written as an INTERNAL routine for EZPLOT
1254 */
1255
1256 static inline double
1257 my_trunc (double x)
1258 {
1259   double integer;
1260
1261   modf (x, &integer);
1262
1263   return (integer);
1264 }
1265
1266 void
1267 EZPlot::make_numfmt (char *fmtstr, int *fldwid, int *nfrac, double minval, double maxval, int nint)
1268 {
1269   int wid, frac, expon;
1270
1271   double delta = (maxval - minval) / nint;
1272   double absmin = fabs(minval);
1273   double absmax = fabs(maxval);
1274
1275   if (absmin > absmax)
1276
1277     absmax = absmin;
1278   double logt = log10( absmax );
1279
1280   if (fabs(logt) >= 6) {                // use exponential format
1281     if (fabs(logt) > 99)
1282       expon = 5;                //  E+102
1283     else
1284       expon = 4;                //  E+00
1285
1286     if (*nfrac < 0) {           // calculate frac
1287       delta /= pow (10., floor(logt));  // scale delta
1288       frac = static_cast<int>(fabs(my_trunc(log10(delta)))) + 1;
1289       if (frac < 1)
1290         frac = 1;               // to be safe, add decimal pt
1291     } else                      // use users' frac
1292       frac = *nfrac;
1293
1294     wid = 2 + frac + expon;
1295     if (minval < 0. || maxval < 0.)
1296       ++wid;
1297     sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "g");
1298   } else {                      // use fixed format
1299     wid = static_cast<int>(my_trunc(logt)) + 1;
1300     if (wid < 1)
1301       wid = 1;
1302     if (minval < 0. || maxval < 0.)
1303       ++wid;
1304
1305     if (*nfrac < 0) {           // calculate frac
1306       if (delta >= 0.999999)
1307         frac = 1;               // add a decimal pt to be safe
1308       else
1309         frac = static_cast<int>(fabs(my_trunc(log10(delta)))) + 1;
1310     } else                      // use users' frac
1311       frac = *nfrac;
1312
1313     wid += 1 + frac;
1314     sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "f");
1315   }
1316
1317   *fldwid = wid;
1318   *nfrac = frac;
1319 }
1320