Debian Codegraph @

libctgraphics/ezplot.cpp

raw
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 }
1007 axis_near = FALSE;
1008 if (xaxispos + xtl_ofs > ya_min && o_yaxis != NOAXIS) {
1009 double xw = xgw_min + i * xw_tickinc;
1010 double x = convertWorldToNDC_X (xw);
1011 double d = x - yaxispos;
1012 if (o_yticks == RIGHT && d >= 0 && d < 0.9 * xn_tickinc)
1013 axis_near = TRUE;
1014 if (o_yticks == LEFT && d <= 0 && d > -0.9 * xn_tickinc)
1015 axis_near = TRUE;
1016 }
1017
1018 if (o_xtlabel == TRUE && axis_near == FALSE) {
1019 snprintf (str, sizeof(str), x_numfmt, xgw_min + xw_tickinc * i);
1020 numstr = str_skip_head (str, " ");
1021 double xExtent, yExtent;
1022 m_pSGP->getTextExtent (numstr, &xExtent, &yExtent);
1023 m_pSGP->moveAbs (x - xExtent/2, xaxispos + xtl_ofs);
1024 m_pSGP->setTextColor (clr_number, -1);
1025 m_pSGP->drawText (numstr);
1026 }
1027 }
1028 } // X - Axis
1029
1030
1031 /*--------*/
1032 /* y-axis */
1033 /*--------*/
1034
1035 if (o_yaxis == LINEAR) {
1036
1037 m_pSGP->setColor (clr_axis);
1038 if (o_tag && !o_grid && !o_box && s_ycross) {
1039 m_pSGP->moveAbs (yaxispos - charwidth, ya_min);
1040 m_pSGP->lineAbs (yaxispos + charwidth, ya_min);
1041 }
1042 m_pSGP->moveAbs (yaxispos, ya_min);
1043 m_pSGP->lineAbs (yaxispos, ya_max);
1044 if (o_tag && !o_grid && !o_box) {
1045 m_pSGP->moveAbs (yaxispos - charwidth, ya_max);
1046 m_pSGP->lineAbs (yaxispos + charwidth, ya_max);
1047 }
1048
1049 if (o_grid == TRUE) {
1050 m_pSGP->setColor (clr_grid);
1051 for (i = 0; i <= y_nint; i++) {
1052 y = yt_min + yn_tickinc * i;
1053 m_pSGP->moveAbs (xa_max, y);
1054 m_pSGP->lineAbs (xa_min, y);
1055 }
1056 }
1057
1058 m_pSGP->setTextAngle (HALFPI);
1059 m_pSGP->setTextSize (1.5 * charheight);
1060 m_pSGP->setTextColor (clr_label, -1);
1061
1062 double xExtent, yExtent;
1063
1064 m_pSGP->getTextExtent (c_ylabel.c_str(), &xExtent, &yExtent);
1065 m_pSGP->moveAbs (ylbl_col, ya_min + (ya_max-ya_min)/2 - yExtent / 2);
1066 m_pSGP->drawText (c_ylabel);
1067
1068 m_pSGP->setTextAngle (0.0);
1069 m_pSGP->setTextSize (charheight);
1070 minorinc = yn_tickinc / (o_yminortick + 1);
1071
1072 for (i = 0; i <= y_nint; i++) {
1073 y = yt_min + yn_tickinc * i;
1074 m_pSGP->setColor (clr_axis);
1075 m_pSGP->moveAbs (yaxispos, y);
1076 m_pSGP->lineAbs (yaxispos + yticklen, y);
1077 if (i != y_nint) {
1078 for (j = 1; j <= o_yminortick; j++) {
1079 y2 = y + minorinc * j;
1080 m_pSGP->moveAbs (yaxispos, y2);
1081 m_pSGP->lineAbs (yaxispos + TICKRATIO * yticklen, y2);
1082 }
1083 }
1084 axis_near = FALSE;
1085 if (yaxispos + ytl_ofs > xa_min && o_xaxis != NOAXIS) {
1086 double yw = ygw_min + i * yw_tickinc;
1087 double y = convertWorldToNDC_Y (yw);
1088 double d = y - xaxispos;
1089 if (o_xticks == ABOVE && d >= 0 && d < 0.9 * yn_tickinc)
1090 axis_near = TRUE;
1091 if (o_xticks == BELOW && d <= 0 && d > -0.9 * yn_tickinc)
1092 axis_near = TRUE;
1093 }
1094 if (o_ytlabel == TRUE && axis_near == FALSE) {
1095 snprintf (str, sizeof(str), y_numfmt, ygw_min + yw_tickinc * i);
1096 double xExtent, yExtent;
1097 m_pSGP->getTextExtent (str, &xExtent, &yExtent);
1098 if (o_yticks == LEFT)
1099 m_pSGP->moveAbs (yaxispos - 1.5 * charwidth - xExtent, y + 0.5 * yExtent);
1100 else
1101 m_pSGP->moveAbs (yaxispos + 1.5 * charwidth, y + 0.5 * yExtent);
1102 m_pSGP->setTextColor (clr_number, -1);
1103 m_pSGP->drawText (str);
1104 }
1105 }
1106 } // Y - Axis
1107 }
1108
1109
1110 void
1111 EZPlot::symbol (int sym, double symwidth, double symheight)
1112 {
1113 if (sym <= 0)
1114 return;
1115
1116 if (sym == SB_CROSS) {
1117 m_pSGP->markerRel (0, 0);
1118 // m_pSGP->moveRel (-0.5 * symwidth, -0.5 * symheight);
1119 // m_pSGP->lineRel (symwidth, symheight);
1120 // m_pSGP->moveRel (-symwidth, 0.0);
1121 // m_pSGP->lineRel (symwidth, -symheight);
1122 // m_pSGP->moveRel (-0.5 * symwidth, 0.5 * symheight);
1123 } else if (sym == SB_PLUS) {
1124 m_pSGP->moveRel (-0.5 * symwidth, 0.0);
1125 m_pSGP->lineRel (symwidth, 0.0);
1126 m_pSGP->moveRel (-0.5 * symwidth, -0.5 * symheight);
1127 m_pSGP->lineRel (0.0, symheight);
1128 m_pSGP->moveRel (0.0, -0.5 * symheight);
1129 } else if (sym == SB_BOX) {
1130 m_pSGP->moveRel (-0.5 * symwidth, -0.5 * symheight);
1131 m_pSGP->lineRel (symwidth, 0.0);
1132 m_pSGP->lineRel (0.0, symheight);
1133 m_pSGP->lineRel (-symwidth, 0.0);
1134 m_pSGP->lineRel (0.0, -symheight);
1135 m_pSGP->moveRel (0.5 * symwidth, 0.5 * symheight);
1136 } else if (sym == SB_CIRCLE) {
1137 m_pSGP->drawCircle (symwidth);
1138 } else if (sym == SB_ERRORBAR) {
1139 m_pSGP->moveRel (-0.5 * symwidth, 0.5 * symheight);
1140 m_pSGP->lineRel (symwidth, 0.0);
1141 m_pSGP->moveRel (-0.5 * symwidth, 0.0);
1142 m_pSGP->lineRel (0.0, -symheight);
1143 m_pSGP->moveRel (-0.5 * symwidth, 0.0);
1144 m_pSGP->lineRel (symwidth, 0.0);
1145 m_pSGP->moveRel (-0.5 * symwidth, 0.5 * symheight);
1146 } else if (sym == SB_POINT) {
1147 m_pSGP->pointRel (0, 0);
1148 }
1149 }
1150
1151
1152
1153 /* NAME
1154 * axis_scale calculates graph axis scaling
1155 *
1156 * SYNOPSIS:
1157 * retval = axis_scale (min, max, nint, minp, maxp, nintp,
1158 * rec_total, rec_frac)
1159 *
1160 * INPUT:
1161 * double min Smallest value to plot
1162 * double max Largest value to plot
1163 * int nint Number of intervals desired
1164 *
1165 * OUTPUT:
1166 * int retval FALSE if illegal parameters, else TRUE
1167 * double *minp Minimum graph value
1168 * double *maxp Maximum graph value
1169 * int *nintp Number of intervals for graph
1170 * int *rec_total Recommended field width for printing out the number
1171 * int *rec_frac Recommended number of digits for print fraction
1172 */
1173
1174 int
1175 EZPlot::axis_scale (double min, double max, int nint, double *minp, double *maxp, int *nintp)
1176 {
1177 if (nint < 1) {
1178 sys_error (ERR_WARNING, "No intervals to plot: num intervals=%d [axis_scale]", nint);
1179 return (FALSE);
1180 }
1181 if (min >= max) {
1182 double scaled = fabs(max) / 10;
1183 if (scaled == 0)
1184 scaled = 0.1;
1185 *minp = min - scaled;
1186 *maxp = max + scaled;
1187 *nintp = 2;
1188 return (TRUE);
1189 }
1190
1191 double eps = 0.025;
1192 double a = fabs(min);
1193 if (fabs(min) < fabs(max))
1194 a = fabs(max);
1195 double scale = pow (10.0, floor(log10(a)));
1196 loop:
1197 double mina = min / scale;
1198 double maxa = max / scale;
1199 double d = (maxa - mina) / nint;
1200 double j = d * eps;
1201 double e = floor (log10(d));
1202 double f = d / pow (10.0, e);
1203 double v = 10.0;
1204 if (f < sqrt(2.0))
1205 v = 1.0;
1206 else if (f < sqrt (10.0))
1207 v = 2.0;
1208 else if (f < sqrt (50.0))
1209 v = 5.0;
1210 double wdt = v * pow (10.0, e);
1211 double g = floor (mina / wdt);
1212 if (fabs(g + 1 - mina / wdt) < j)
1213 g = g + 1;
1214 #undef TEST1
1215
1216 #ifdef TEST1
1217 g++;
1218 #endif
1219 *minp = wdt * g;
1220 double h = floor (maxa / wdt) + 1.0;
1221 if (fabs(maxa / wdt + 1 - h) < j)
1222 h = h - 1;
1223 #ifdef TEST1
1224 h--;
1225 #endif
1226 *maxp = wdt * h;
1227 *nintp = static_cast<int>(h - g);
1228 if (fabs(*maxp) >= 10.0 || fabs(*minp) >= 10.0) {
1229 scale = scale * 10.0;
1230 goto loop;
1231 }
1232
1233 *minp *= scale;
1234 *maxp *= scale;
1235
1236 return (TRUE);
1237 }
1238
1239
1240 /* NAME
1241 * make_numfmt Make a numeric format string
1242 *
1243 * SYNOPSIS
1244 * make_numfmt (fmtstr, fldwid, nfrac, min, max, nint)
1245 * char *fmtstr Returned format string for printf()
1246 * int *fldwid Returned field width
1247 * int *nfrac If < 0, then calculate best number of
1248 * fraction places & return that value
1249 * If >= 0, then use that number of places
1250 * double min Minimum value
1251 * double max Maximum value
1252 * int nint Number of intervals between min & max
1253 *
1254 * DESCRIPTION
1255 * This routine is written as an INTERNAL routine for EZPLOT
1256 */
1257
1258 static inline double
1259 my_trunc (double x)
1260 {
1261 double integer;
1262
1263 modf (x, &integer);
1264
1265 return (integer);
1266 }
1267
1268 void
1269 EZPlot::make_numfmt (char *fmtstr, int *fldwid, int *nfrac, double minval, double maxval, int nint)
1270 {
1271 int wid, frac, expon;
1272
1273 double delta = (maxval - minval) / nint;
1274 double absmin = fabs(minval);
1275 double absmax = fabs(maxval);
1276
1277 if (absmin > absmax)
1278
1279 absmax = absmin;
1280 double logt = log10( absmax );
1281
1282 if (fabs(logt) >= 6) { // use exponential format
1283 if (fabs(logt) > 99)
1284 expon = 5; // E+102
1285 else
1286 expon = 4; // E+00
1287
1288 if (*nfrac < 0) { // calculate frac
1289 delta /= pow (10., floor(logt)); // scale delta
1290 frac = static_cast<int>(fabs(my_trunc(log10(delta)))) + 1;
1291 if (frac < 1)
1292 frac = 1; // to be safe, add decimal pt
1293 } else // use users' frac
1294 frac = *nfrac;
1295
1296 wid = 2 + frac + expon;
1297 if (minval < 0. || maxval < 0.)
1298 ++wid;
1299 sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "g");
1300 } else { // use fixed format
1301 wid = static_cast<int>(my_trunc(logt)) + 1;
1302 if (wid < 1)
1303 wid = 1;
1304 if (minval < 0. || maxval < 0.)
1305 ++wid;
1306
1307 if (*nfrac < 0) { // calculate frac
1308 if (delta >= 0.999999)
1309 frac = 1; // add a decimal pt to be safe
1310 else
1311 frac = static_cast<int>(fabs(my_trunc(log10(delta)))) + 1;
1312 } else // use users' frac
1313 frac = *nfrac;
1314
1315 wid += 1 + frac;
1316 sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "f");
1317 }
1318
1319 *fldwid = wid;
1320 *nfrac = frac;
1321 }
1322