1 /*****************************************************************************
6 ** This is part of the CTSim program
7 ** Copyright (C) 1983-2000 Kevin Rosenberg
9 ** $Id: ezplot.cpp,v 1.25 2000/12/27 20:09:19 kevin Exp $
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.
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.
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 ******************************************************************************/
29 int snprintf (char *, size_t, const char*, ...);
33 static const double TICKRATIO = 0.4; // ratio of minor to major tick lengths
34 static const int MAXNUMFMT = 15; // maximum length of a numeric format
35 static const int DEF_CURVE_CLR = C_RED;
38 EZPlotCurve::EZPlotCurve (const double* xData, const double* yData, int n)
44 int copyCount = n * sizeof(double);
45 memcpy (x, xData, copyCount);
46 memcpy (y, yData, copyCount);
51 EZPlotCurve::~EZPlotCurve ()
59 EZPlot::addCurve (const double *y, int n)
61 double* x = new double [n];
63 for (int i = 0; i < n; i++)
72 EZPlot::addCurve (const float *y, int n)
74 double* yDouble = new double [n];
76 for (int i = 0; i < n; i++)
79 addCurve (yDouble, n);
85 EZPlot::addCurve (const float x[], const double y[], int num)
87 double* dx = new double [num];
89 for (int i = 0; i < num; i++)
92 addCurve (dx, y, num);
97 EZPlot::addCurve (const double x[], const float y[], int num)
99 double* dy = new double [num];
101 for (int i = 0; i < num; i++)
104 addCurve (x, dy, num);
111 EZPlot::addCurve (const double x[], const double y[], int num)
116 EZPlotCurve* pCurve = new EZPlotCurve (x, y, num);
117 m_vecCurves.push_back (pCurve);
123 for (EZPlotCurveIterator i = m_vecCurves.begin(); i != m_vecCurves.end(); i++)
128 EZPlot::clearCurves ()
130 for (EZPlotCurveIterator i = m_vecCurves.begin(); i != m_vecCurves.end(); i++)
132 m_vecCurves.erase (m_vecCurves.begin(), m_vecCurves.end());
137 EZPlot::EZPlot (SGP& sgp)
141 m_pol.addSkipWord ("please");
\r
142 m_pol.addSkipWord ("use");
\r
143 m_pol.addSkipWord ("are");
\r
144 m_pol.addSkipWord ("and");
\r
145 m_pol.addSkipWord ("is");
\r
146 m_pol.addSkipWord ("the");
\r
147 m_pol.addSkipWord ("equals");
\r
148 m_pol.addSkipChar ('=');
\r
150 m_pol.usefile (POL::P_USE_STR,"");
\r
151 m_pol.set_inputline ("!eoc ,");
\r
153 m_pol.closefile ();
\r
159 EZPlot::initPlotSettings ()
161 charheight = rSGP.getCharHeight();
162 charwidth = rSGP.getCharWidth();
164 m_iCurrentCurve = -1;
\r
186 o_color = DEF_CURVE_CLR;
189 o_linestyle = SGP::LS_SOLID;
196 o_legendbox = INSIDE;
209 clr_axis = C_BLACK; // set fixed colors
213 clr_number = C_GREEN;
218 EZPlot::setColor (unsigned int iCurve, int iColor)
\r
220 if (m_veciColor.size() <= iCurve) {
\r
221 m_veciColor.resize ((m_iCurrentCurve + 1) * 2);
\r
222 m_vecbColorSet.resize ((m_iCurrentCurve + 1) * 2);
\r
224 m_veciColor [iCurve] = iColor;
\r
225 m_vecbColorSet [iCurve] = true;
\r
229 EZPlot::setSymbol (unsigned int iCurve, int iSymbol)
\r
231 if (m_veciSymbol.size() <= iCurve) {
\r
232 m_veciSymbol.resize ((m_iCurrentCurve + 1) * 2);
\r
233 m_vecbSymbolSet.resize ((m_iCurrentCurve + 1) * 2);
\r
235 m_veciSymbol [iCurve] = iSymbol;
\r
236 m_vecbSymbolSet [iCurve] = true;
\r
240 EZPlot::setSymbolFreq (unsigned int iCurve, int iSymbolFreq)
\r
242 if (m_veciSymbolFreq.size() <= iCurve) {
\r
243 m_veciSymbolFreq.resize ((m_iCurrentCurve + 1) * 2);
\r
244 m_vecbSymbolFreqSet.resize ((m_iCurrentCurve + 1) * 2);
\r
246 m_veciSymbolFreq [iCurve] = iSymbolFreq;
\r
247 m_vecbSymbolFreqSet [iCurve] = true;
\r
251 EZPlot::setLinestyle (unsigned int iCurve, int iLinestyle)
\r
253 if (m_veciLinestyle.size() <= iCurve) {
\r
254 m_veciLinestyle.resize ((m_iCurrentCurve + 1) * 2);
\r
255 m_vecbLinestyleSet.resize ((m_iCurrentCurve + 1) * 2);
\r
257 m_veciLinestyle [iCurve] = iLinestyle;
\r
258 m_vecbLinestyleSet [iCurve] = true;
\r
262 EZPlot::setLegend (unsigned int iCurve, const std::string& strLegend)
\r
264 if (m_vecsLegend.size() <= iCurve) {
\r
265 m_vecsLegend.resize ((m_iCurrentCurve + 1) * 2);
\r
266 m_vecbLegendSet.resize ((m_iCurrentCurve + 1) * 2);
\r
268 m_vecsLegend [iCurve] = strLegend;
\r
269 m_vecbLegendSet [iCurve] = true;
\r
273 EZPlot::setLegend (unsigned int iCurve, const char* const pszLegend)
\r
275 if (m_vecsLegend.size() <= iCurve) {
\r
276 m_vecsLegend.resize ((m_iCurrentCurve + 1) * 2);
\r
277 m_vecbLegendSet.resize ((m_iCurrentCurve + 1) * 2);
\r
279 m_vecsLegend [iCurve] = pszLegend;
\r
280 m_vecbLegendSet [iCurve] = true;
\r
284 EZPlot::getColor (unsigned int iCurve) const
\r
286 if (m_veciColor.size() > iCurve && m_vecbColorSet[iCurve])
\r
287 return m_veciColor[iCurve];
\r
293 EZPlot::getSymbol (unsigned int iCurve) const
\r
295 if (m_veciSymbol.size() > iCurve && m_vecbSymbolSet[iCurve])
\r
296 return m_veciSymbol[iCurve];
\r
302 EZPlot::getSymbolFreq (unsigned int iCurve) const
\r
304 if (m_veciSymbolFreq.size() > iCurve && m_vecbSymbolFreqSet[iCurve])
\r
305 return m_veciSymbolFreq[iCurve];
\r
311 EZPlot::getLinestyle (unsigned int iCurve) const
\r
313 if (m_veciLinestyle.size() > iCurve && m_vecbLinestyleSet[iCurve])
\r
314 return m_veciLinestyle[iCurve];
\r
316 return o_linestyle;
\r
320 EZPlot::getLegend (unsigned int iCurve) const
\r
322 if (m_vecsLegend.size() > iCurve && m_vecbLegendSet[iCurve])
\r
323 return &m_vecsLegend[iCurve];
\r
330 * plot Plots all curves collected by addCurves ()
336 * This routine plots the curves that have stored by addCurves().
339 * drawAxes() & make_numfmt()
345 if (m_vecCurves.size() <= 0)
348 rSGP.setWindow (0., 0., 1., 1.);
350 if (s_textsize == TRUE)
351 rSGP.setTextPointSize (v_textsize);
\r
352 charheight = rSGP.getCharHeight();
353 charwidth = rSGP.getCharWidth();
355 const EZPlotCurve& firstCurve = *m_vecCurves[0];
\r
356 double xmin = firstCurve.x[0]; // extent of curves in world coord
358 double ymin = firstCurve.y[0];
359 double ymax = ymin;
\r
361 for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
362 const EZPlotCurve* const pCurve = m_vecCurves [iCurve];
364 for (int ip = 0; ip < pCurve->m_iPointCount; ip++) {
365 if (pCurve->x[ip] > xmax)
366 xmax = pCurve->x[ip];
367 else if (pCurve->x[ip] < xmin)
368 xmin = pCurve->x[ip];
369 if (pCurve->y[ip] > ymax)
370 ymax = pCurve->y[ip];
371 else if (pCurve->y[ip] < ymin)
372 ymin = pCurve->y[ip];
376 // extend graph limits for user defined axis cross positions
377 if (s_xcross == TRUE) {
380 else if (v_xcross > xmax)
384 if (s_ycross == TRUE) {
387 else if (v_ycross > ymax)
391 // find nice endpoints for axes
392 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))
395 // check if user set x-axis extents
396 if (s_xmin == TRUE) {
398 x_nint = o_xmajortick - 1;
400 if (s_xmax == TRUE) {
402 x_nint = o_xmajortick - 1;
405 // check if user set y-axis extents
406 if (s_ymin == TRUE) {
408 y_nint = o_ymajortick - 1;
410 if (s_ymax == TRUE) {
412 y_nint = o_ymajortick - 1;
415 // calculate increment between major axis in world coordinates
416 xw_tickinc = (xgw_max - xgw_min) / x_nint;
417 yw_tickinc = (ygw_max - ygw_min) / y_nint;
419 // we have now calcuated xgw_min, xyw_max, ygw_min, & ygw_max
421 // set the number of decimal point to users' setting or default
422 // Two formats for numbers: Fixed: -nnn.f and Exponent: -n.fffE+eee
423 if (s_lxfrac == TRUE)
428 if (s_lyfrac == TRUE)
433 make_numfmt (x_numfmt, &x_fldwid, &x_frac, xgw_min, xgw_max, x_nint);
434 make_numfmt (y_numfmt, &y_fldwid, &y_frac, ygw_min, ygw_max, y_nint);
436 xtl_wid = x_fldwid * charwidth; // calc size of tick labels
437 ytl_wid = y_fldwid * charwidth;
438 tl_height = charheight;
440 // calculate the extent of the plot frame
443 xp_max = xp_min + o_xlength;
444 yp_max = yp_min + o_ylength;
446 xp_min = clamp (xp_min, 0., 1.);
447 xp_max = clamp (xp_max, 0., 1.);
448 yp_min = clamp (yp_min, 0., 1.);
449 yp_max = clamp (yp_max, 0., 1.);
451 xa_min = xp_min; // extent of axes
456 // adjust frame for title
457 title_row = ya_max;;
\r
458 if (c_title.length() > 0)
459 ya_max -= 2 * charheight;
\r
461 // calculate legend box boundaries
462 int max_leg = 0; // longest legend in characters
463 int num_leg = 0; // number of legend titles
\r
464 for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
465 const std::string* pstrLegend = getLegend (iCurve);
\r
466 if (pstrLegend && pstrLegend->length() > 0) {
\r
467 int nLegend = pstrLegend->length();
470 if (nLegend > max_leg)
\r
476 if (num_leg > 0 && o_legendbox != NOLEGEND) {
477 double leg_width = (max_leg + 2) * charwidth; // size of legend box
478 double leg_height = num_leg * 3 * charheight;
480 if (s_xlegend == TRUE)
484 if (o_legendbox == OUTSIDE)
485 xa_max -= (leg_width + 0.5 * charwidth);
487 xl_min = xl_max - leg_width;
489 if (s_ylegend == TRUE)
494 yl_min = yl_max - leg_height;
496 rSGP.setColor (clr_legend);
497 rSGP.drawRect (xl_min, yl_min, xl_max, yl_max);
499 int iLegend = 0; // current legend position
\r
500 for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
501 const std::string* pstrLegend = getLegend (iCurve);
502 if (! pstrLegend || pstrLegend->length() == 0)
505 double xmin = xl_min + 1.0 * charwidth;
506 double xmax = xl_max - 1.0 * charwidth;
507 double y = yl_max - (2.0 + iLegend * 3) * charheight;
509 rSGP.moveAbs (xmin, y + 0.5 * charheight);
\r
510 rSGP.drawText (pstrLegend->c_str());
511 rSGP.setColor (getColor (iCurve));
\r
512 int iLS = getLinestyle (iCurve);
513 if (iLS != SGP::LS_NOLINE) {
514 rSGP.setLineStyle (iLS);
515 rSGP.moveAbs (xmin, y);
516 rSGP.lineAbs (xmax, y);
518 int iSymbol = getSymbol (iCurve);
520 double xinc = (xmax - xmin) / (5 - 1);
521 rSGP.setLineStyle (SGP::LS_SOLID);
522 for (int j = 0; j < 5; j++) {
523 rSGP.moveAbs (xmin + j * xinc, y);
524 symbol(iSymbol, 0.5 * charwidth, 0.5 * charheight);
527 ++iLegend; // move to next legend position
529 } // end legend printing
531 // calculate the extent of the axes
533 /*-------------------------*/
534 /* adjust frame for labels */
535 /*-------------------------*/
538 if (c_xlabel.length() > 0)
539 ya_min += 1.5 * charheight;
540 xlbl_row = xp_min; // put x-label on bottom of plot frame
543 if (c_ylabel.length() > 0) {
\r
544 rSGP.setTextAngle (HALFPI);
\r
545 rSGP.setTextSize (1.5 * charheight);
\r
546 double xExtent, yExtent;
\r
547 rSGP.getTextExtent (c_ylabel.c_str(), &xExtent, &yExtent);
\r
548 rSGP.setTextSize (charheight);
\r
550 rSGP.setTextAngle (0.0);
\r
554 /*------------------------------*/
555 /* adjust frame for tick labels */
556 /*------------------------------*/
558 // Calc offset of tick labels from axes
559 if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
561 else if (o_xticks == BELOW)
562 xtl_ofs = -0.5 * charheight;
563 else if (o_xticks == ABOVE)
564 xtl_ofs = 0.5 * charheight;
566 if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
568 else if (o_yticks == LEFT)
569 ytl_ofs = -(2 + y_fldwid) * charwidth;
570 else if (o_yticks == RIGHT)
571 ytl_ofs = 1.5 * charwidth;
573 xa_max -= 0.7 * x_fldwid * charwidth; // make room for last x tick label
\r
580 // see if need to shrink axis extents and/or tick extents
581 if (xtl_ofs != 0.0 && s_ycross == FALSE) {
582 if (o_xticks == BELOW) {
583 ya_min += 1.5 * charheight;
585 } else if (o_xticks == ABOVE) {
587 yt_min = ya_min + 1.5 * charheight;
589 } else // noaxis, no t-labels, or user set cross
592 if (ytl_ofs != 0.0 && s_xcross == FALSE) {
593 if (o_yticks == LEFT) {
594 xa_min += (2 + y_fldwid) * charwidth;
596 } else if (o_yticks == RIGHT) {
598 xt_min = xa_min + ytl_ofs + y_fldwid * charwidth;
603 // decrease size of graph, if necessary, to accommadate space
604 // between axis boundary and boundary of ticks
605 double x_added_ticks = 0; // number of tick spaces added to axis
606 double y_added_ticks = 0;
607 if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
609 if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
612 if (o_grid == TRUE) {
613 if (x_added_ticks < 0)
615 if (y_added_ticks < 0)
619 if (x_added_ticks < 0) {
620 if (o_yticks == LEFT || s_ycross)
626 if (y_added_ticks < 0) {
627 if (o_xticks == BELOW || s_xcross)
633 xn_tickinc = (xt_max - xt_min) / (x_nint + x_added_ticks);
634 yn_tickinc = (yt_max - yt_min) / (y_nint + y_added_ticks);
636 xt_min += 0.5 * x_added_ticks * xn_tickinc;
637 xt_max -= 0.5 * x_added_ticks * xn_tickinc;
638 yt_min += 0.5 * y_added_ticks * yn_tickinc;
639 yt_max -= 0.5 * y_added_ticks * yn_tickinc;
646 //------------------------------------------------------------------------
648 m_xWorldScale = (xgn_max - xgn_min) / (xgw_max - xgw_min);
649 m_yWorldScale = (ygn_max - ygn_min) / (ygw_max - ygw_min);
653 rSGP.setLineStyle (SGP::LS_SOLID);
656 // size of symbol in NDC
657 double symwidth = charwidth;
658 double symheight = charheight;
660 double clipRect[4];
\r
661 clipRect[0] = xgn_min; clipRect[1] = ygn_min; clipRect[2] = xgn_max; clipRect[3] = ygn_max;
\r
663 for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
664 const EZPlotCurve* const pCurve = m_vecCurves [iCurve];
666 rSGP.setColor (getColor (iCurve));
\r
667 int iSym = getSymbol (iCurve);
\r
668 int iLS = getLinestyle (iCurve);
\r
670 if (iLS != SGP::LS_NOLINE) {
671 rSGP.setLineStyle (iLS);
672 double x1 = convertWorldToNDC_X (pCurve->x[0]);
673 double y1 = convertWorldToNDC_Y (pCurve->y[0]);
\r
674 for (int i = 1; i < pCurve->m_iPointCount; i++) {
675 double x2 = convertWorldToNDC_X (pCurve->x[i]);
676 double y2 = convertWorldToNDC_Y (pCurve->y[i]);
\r
677 double x2Clip = x2;
\r
678 double y2Clip = y2;
\r
679 if (clip_rect (x1, y1, x2Clip, y2Clip, clipRect)) {
\r
680 rSGP.moveAbs (x1, y1);
\r
681 rSGP.lineAbs (x2Clip, y2Clip);
\r
688 int iSymFreq = getSymbolFreq (iCurve);
689 rSGP.setLineStyle (SGP::LS_SOLID);
690 double x = convertWorldToNDC_X (pCurve->x[0]);
691 double y = convertWorldToNDC_Y (pCurve->y[0]);
693 symbol (iSym, symwidth, symheight);
694 for (int i = 1; i < pCurve->m_iPointCount; i++)
695 if (i % iSymFreq == 0 || i == pCurve->m_iPointCount - 1) {
696 x = convertWorldToNDC_X (pCurve->x[i]);
697 y = convertWorldToNDC_Y (pCurve->y[i]);
\r
698 if (y >= ygn_min && y <= ygn_max) {
700 symbol (iSym, symwidth, symheight);
\r
710 * drawAxes INTERNAL routine to draw axis & label them
720 double xticklen = 0, yticklen = 0; // length of ticks in NDC
721 double minorinc; // increment between minor axes
722 double xaxispos, yaxispos; // crossing of axes
724 bool axis_near; // TRUE if axis too close to print t-label
729 rSGP.setTextSize (charheight);
730 rSGP.setTextColor (1, -1);
732 if (o_xticks == ABOVE)
733 xticklen = 0.5 * charheight;
734 else if (o_xticks == BELOW)
735 xticklen = -0.5 * charheight;
737 if (o_yticks == RIGHT)
738 yticklen = charwidth;
739 else if (o_yticks == LEFT)
740 yticklen = -charwidth;
742 if (c_title.length() > 0) {
744 rSGP.setTextSize (charheight * 2.0);
745 rSGP.getTextExtent (c_title.c_str(), &wText, &hText);
746 rSGP.moveAbs (xa_min + (xa_max-xa_min)/2 - wText/2, title_row);
747 rSGP.setTextColor (clr_title, -1);
748 rSGP.drawText (c_title);
749 rSGP.setTextSize (charheight);
752 if (o_grid == TRUE || o_box == TRUE) {
753 rSGP.setColor (clr_axis);
754 rSGP.moveAbs (xa_min, ya_min);
755 rSGP.lineAbs (xa_max, ya_min);
756 rSGP.lineAbs (xa_max, ya_max);
757 rSGP.lineAbs (xa_min, ya_max);
758 rSGP.lineAbs (xa_min, ya_min);
761 // calculate position of axes
764 if (s_ycross == TRUE) { // convert users' world-coord
765 xaxispos = convertWorldToNDC_Y (v_ycross);// axis to its position in NDC
766 x = convertWorldToNDC_X (xgw_min);
771 if (s_xcross == TRUE) { // convert users' world-coord
772 yaxispos = convertWorldToNDC_X (v_xcross);// axis to its NDC position
773 y = convertWorldToNDC_Y (ygw_min);
781 if (o_xaxis == LINEAR) {
784 rSGP.setColor (clr_axis);
785 if (o_tag && !o_grid && !o_box && s_xcross) {
786 rSGP.moveAbs (xa_min, xaxispos - charheight);
787 rSGP.lineAbs (xa_min, xaxispos + charheight);
789 rSGP.moveAbs (xa_min, xaxispos);
790 rSGP.lineAbs (xa_max, xaxispos);
791 if (o_tag && !o_grid && !o_box) {
792 rSGP.moveAbs (xa_max, xaxispos - charheight);
793 rSGP.lineAbs (xa_max, xaxispos + charheight);
796 if (o_grid == TRUE) {
797 rSGP.setColor (clr_grid);
798 for (i = 0; i <= x_nint; i++) {
799 rSGP.moveAbs (xt_min + xn_tickinc * i, ya_max);
800 rSGP.lineAbs (xt_min + xn_tickinc * i, ya_min);
803 rSGP.setTextSize (charheight * 1.5);
804 rSGP.setTextColor (clr_label, -1);
\r
805 double wText, hText;
\r
806 rSGP.getTextExtent (c_xlabel.c_str(), &wText, &hText);
807 rSGP.moveAbs (xa_min + (xa_max-xa_min)/2 - wText / 2, xlbl_row + charheight * 1.5);
\r
808 rSGP.drawText (c_xlabel);
809 rSGP.setTextSize (charheight);
810 minorinc = xn_tickinc / (o_xminortick + 1);
812 for (i = 0; i <= x_nint; i++) {
813 x = xt_min + xn_tickinc * i;
814 rSGP.setColor (clr_axis);
815 rSGP.moveAbs (x, xaxispos);
816 rSGP.lineAbs (x, xaxispos + xticklen);
818 for (j = 1; j <= o_xminortick; j++) {
819 x2 = x + minorinc * j;
820 rSGP.moveAbs (x2, xaxispos);
821 rSGP.lineAbs (x2, xaxispos + TICKRATIO * xticklen);
824 if (xaxispos + xtl_ofs > ya_min && o_yaxis != NOAXIS) {
825 double xw = xgw_min + i * xw_tickinc;
826 double x = convertWorldToNDC_X (xw);
827 double d = x - yaxispos;
828 if (o_yticks == RIGHT && d >= 0 && d < 0.9 * xn_tickinc)
830 if (o_yticks == LEFT && d <= 0 && d > -0.9 * xn_tickinc)
834 if (o_xtlabel == TRUE && axis_near == FALSE) {
835 snprintf (str, sizeof(str), x_numfmt, xgw_min + xw_tickinc * i);
836 numstr = str_skip_head (str, " ");
837 rSGP.moveAbs (x-strlen(numstr)*charwidth/2, xaxispos + xtl_ofs);
838 rSGP.setTextColor (clr_number, -1);
839 rSGP.drawText (numstr);
849 if (o_yaxis == LINEAR) {
851 rSGP.setColor (clr_axis);
852 if (o_tag && !o_grid && !o_box && s_ycross) {
853 rSGP.moveAbs (yaxispos - charwidth, ya_min);
854 rSGP.lineAbs (yaxispos + charwidth, ya_min);
856 rSGP.moveAbs (yaxispos, ya_min);
857 rSGP.lineAbs (yaxispos, ya_max);
858 if (o_tag && !o_grid && !o_box) {
859 rSGP.moveAbs (yaxispos - charwidth, ya_max);
860 rSGP.lineAbs (yaxispos + charwidth, ya_max);
863 if (o_grid == TRUE) {
864 rSGP.setColor (clr_grid);
865 for (i = 0; i <= y_nint; i++) {
866 y = yt_min + yn_tickinc * i;
867 rSGP.moveAbs (xa_max, y);
868 rSGP.lineAbs (xa_min, y);
871 rSGP.setTextAngle (HALFPI);
872 rSGP.setTextSize (1.5 * charheight);
873 rSGP.setTextColor (clr_label, -1);
\r
874 double xExtent, yExtent;
\r
875 rSGP.getTextExtent (c_ylabel.c_str(), &xExtent, &yExtent);
\r
876 rSGP.moveAbs (ylbl_col, ya_min + (ya_max-ya_min)/2 - yExtent / 2);
\r
877 rSGP.drawText (c_ylabel);
\r
878 rSGP.setTextAngle (0.0);
879 rSGP.setTextSize (charheight);
880 minorinc = yn_tickinc / (o_yminortick + 1);
882 for (i = 0; i <= y_nint; i++) {
883 y = yt_min + yn_tickinc * i;
884 rSGP.setColor (clr_axis);
885 rSGP.moveAbs (yaxispos, y);
886 rSGP.lineAbs (yaxispos + yticklen, y);
888 for (j = 1; j <= o_yminortick; j++) {
889 y2 = y + minorinc * j;
890 rSGP.moveAbs (yaxispos, y2);
891 rSGP.lineAbs (yaxispos + TICKRATIO * yticklen, y2);
894 if (yaxispos + ytl_ofs > xa_min && o_xaxis != NOAXIS) {
895 double yw = ygw_min + i * yw_tickinc;
896 double y = convertWorldToNDC_Y (yw);
897 double d = y - xaxispos;
898 if (o_xticks == ABOVE && d >= 0 && d < 0.9 * yn_tickinc)
900 if (o_xticks == BELOW && d <= 0 && d > -0.9 * yn_tickinc)
903 if (o_ytlabel == TRUE && axis_near == FALSE) {
904 snprintf (str, sizeof(str), y_numfmt, ygw_min + yw_tickinc * i);
\r
905 rSGP.moveAbs (yaxispos + ytl_ofs, y + 0.5 * charheight);
\r
906 rSGP.setTextColor (clr_number, -1);
\r
915 EZPlot::symbol (int sym, double symwidth, double symheight)
920 if (sym == SB_CROSS) {
921 rSGP.moveRel (-0.5 * symwidth, -0.5 * symheight);
922 rSGP.lineRel (symwidth, symheight);
923 rSGP.moveRel (-symwidth, 0.0);
924 rSGP.lineRel (symwidth, -symheight);
925 rSGP.moveRel (-0.5 * symwidth, 0.5 * symheight);
926 } else if (sym == SB_PLUS) {
927 rSGP.moveRel (-0.5 * symwidth, 0.0);
928 rSGP.lineRel (symwidth, 0.0);
929 rSGP.moveRel (-0.5 * symwidth, -0.5 * symheight);
930 rSGP.lineRel (0.0, symheight);
931 rSGP.moveRel (0.0, -0.5 * symheight);
932 } else if (sym == SB_BOX) {
933 rSGP.moveRel (-0.5 * symwidth, -0.5 * symheight);
934 rSGP.lineRel (symwidth, 0.0);
935 rSGP.lineRel (0.0, symheight);
936 rSGP.lineRel (-symwidth, 0.0);
937 rSGP.lineRel (0.0, -symheight);
938 rSGP.moveRel (0.5 * symwidth, 0.5 * symheight);
939 } else if (sym == SB_CIRCLE) {
940 rSGP.drawCircle (symwidth);
941 } else if (sym == SB_ERRORBAR) {
942 rSGP.moveRel (-0.5 * symwidth, 0.5 * symheight);
943 rSGP.lineRel (symwidth, 0.0);
944 rSGP.moveRel (-0.5 * symwidth, 0.0);
945 rSGP.lineRel (0.0, -symheight);
946 rSGP.moveRel (-0.5 * symwidth, 0.0);
947 rSGP.lineRel (symwidth, 0.0);
948 rSGP.moveRel (-0.5 * symwidth, 0.5 * symheight);
955 * axis_scale calculates graph axis scaling
958 * retval = axis_scale (min, max, nint, minp, maxp, nintp,
959 * rec_total, rec_frac)
962 * double min Smallest value to plot
963 * double max Largest value to plot
964 * int nint Number of intervals desired
967 * int retval FALSE if illegal parameters, else TRUE
968 * double *minp Minimum graph value
969 * double *maxp Maximum graph value
970 * int *nintp Number of intervals for graph
971 * int *rec_total Recommended field width for printing out the number
972 * int *rec_frac Recommended number of digits for print fraction
976 EZPlot::axis_scale (double min, double max, int nint, double *minp, double *maxp, int *nintp)
978 if (min >= max || nint < 1) {
979 sys_error (ERR_WARNING, "Invalid params: min=%lf, max=%lf, num intervals=%d [axis_scale]", min, max, nint);
984 double a = fabs(min);
985 if (fabs(min) < fabs(max))
987 double scale = pow (10.0, floor(log10(a)));
989 double mina = min / scale;
990 double maxa = max / scale;
991 double d = (maxa - mina) / nint;
993 double e = floor (log10(d));
994 double f = d / pow (10.0, e);
998 else if (f < sqrt (10.0))
1000 else if (f < sqrt (50.0))
1002 double wdt = v * pow (10.0, e);
1003 double g = floor (mina / wdt);
1004 if (fabs(g + 1 - mina / wdt) < j)
1011 double h = floor (maxa / wdt) + 1.0;
1012 if (fabs(maxa / wdt + 1 - h) < j)
1018 *nintp = static_cast<int>(h - g);
1019 if (fabs(*maxp) >= 10.0 || fabs(*minp) >= 10.0) {
1020 scale = scale * 10.0;
1032 * make_numfmt Make a numeric format string
1035 * make_numfmt (fmtstr, fldwid, nfrac, min, max, nint)
1036 * char *fmtstr Returned format string for printf()
1037 * int *fldwid Returned field width
1038 * int *nfrac If < 0, then calculate best number of
1039 * fraction places & return that value
1040 * If >= 0, then use that number of places
1041 * double min Minimum value
1042 * double max Maximum value
1043 * int nint Number of intervals between min & max
1046 * This routine is written as an INTERNAL routine for EZPLOT
1049 static inline double
1060 EZPlot::make_numfmt (char *fmtstr, int *fldwid, int *nfrac, double minval, double maxval, int nint)
1062 int wid, frac, expon;
1064 double delta = (maxval - minval) / nint;
1065 double absmin = fabs(minval);
1066 double absmax = fabs(maxval);
\r
1067 if (absmin > absmax)
\r
1069 double logt = log10( absmax );
1071 if (fabs(logt) >= 6) { // use exponential format
1072 if (fabs(logt) > 99)
1077 if (*nfrac < 0) { // calculate frac
1078 delta /= pow (10., floor(logt)); // scale delta
1079 frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
1081 frac = 1; // to be safe, add decimal pt
1082 } else // use users' frac
1085 wid = 2 + frac + expon;
1086 if (minval < 0. || maxval < 0.)
1088 sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "g");
1089 } else { // use fixed format
1090 wid = static_cast<int>(trunc(logt)) + 1;
1093 if (minval < 0. || maxval < 0.)
1096 if (*nfrac < 0) { // calculate frac
1097 if (delta >= 0.999999)
1098 frac = 1; // add a decimal pt to be safe
1100 frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
1101 } else // use users' frac
1105 sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "f");