1 /*****************************************************************************
6 ** This is part of the CTSim program
7 ** Copyright (c) 1983-2009 Kevin Rosenberg
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.
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.
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 ******************************************************************************/
27 int snprintf (char *, size_t, const char*, ...);
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;
36 EZPlotCurve::EZPlotCurve (const double* xData, const double* yData, int n)
37 : x(new double[n]), y(new double[n])
39 for (int i = 0; i < n; i++) {
47 EZPlotCurve::~EZPlotCurve ()
55 EZPlot::addCurve (const double *y, int n)
57 double* x = new double [n];
59 for (int i = 0; i < n; i++)
68 EZPlot::addCurve (const float *y, int n)
70 double* yDouble = new double [n];
72 for (int i = 0; i < n; i++)
75 addCurve (yDouble, n);
81 EZPlot::addCurve (const float x[], const double y[], int num)
83 double* dx = new double [num];
85 for (int i = 0; i < num; i++)
88 addCurve (dx, y, num);
93 EZPlot::addCurve (const double* const x, const float* const y, int num)
95 double* dy = new double [num];
97 for (int i = 0; i < num; i++)
100 addCurve (x, dy, num);
107 EZPlot::addCurve (const double* const x, const double* const y, int num)
112 EZPlotCurve* pCurve = new EZPlotCurve (x, y, num);
113 m_vecCurves.push_back (pCurve);
119 for (EZPlotCurveIterator i = m_vecCurves.begin(); i != m_vecCurves.end(); i++)
124 EZPlot::clearCurves ()
126 for (EZPlotCurveIterator i = m_vecCurves.begin(); i != m_vecCurves.end(); i++)
128 m_vecCurves.erase (m_vecCurves.begin(), m_vecCurves.end());
137 m_pol.addSkipWord ("please");
139 m_pol.addSkipWord ("use");
141 m_pol.addSkipWord ("are");
143 m_pol.addSkipWord ("and");
145 m_pol.addSkipWord ("is");
147 m_pol.addSkipWord ("the");
149 m_pol.addSkipWord ("equals");
151 m_pol.addSkipChar ('=');
155 m_pol.usefile (POL::P_USE_STR,"");
157 m_pol.set_inputline ("!eoc ,");
169 EZPlot::initPlotSettings ()
171 m_iCurrentCurve = -1;
196 o_color = DEF_CURVE_CLR;
199 o_linestyle = SGP::LS_SOLID;
206 o_legendbox = INSIDE;
219 clr_axis = C_LTGRAY; // set fixed colors
223 clr_number = C_GREEN;
229 EZPlot::setColor (unsigned int iCurve, int iColor)
233 if (m_veciColor.size() <= iCurve) {
235 m_veciColor.resize ((m_iCurrentCurve + 1) * 2);
237 m_vecbColorSet.resize ((m_iCurrentCurve + 1) * 2);
241 m_veciColor [iCurve] = iColor;
243 m_vecbColorSet [iCurve] = true;
251 EZPlot::setSymbol (unsigned int iCurve, int iSymbol)
255 if (m_veciSymbol.size() <= iCurve) {
257 m_veciSymbol.resize ((m_iCurrentCurve + 1) * 2);
259 m_vecbSymbolSet.resize ((m_iCurrentCurve + 1) * 2);
263 m_veciSymbol [iCurve] = iSymbol;
265 m_vecbSymbolSet [iCurve] = true;
273 EZPlot::setSymbolFreq (unsigned int iCurve, int iSymbolFreq)
277 if (m_veciSymbolFreq.size() <= iCurve) {
279 m_veciSymbolFreq.resize ((m_iCurrentCurve + 1) * 2);
281 m_vecbSymbolFreqSet.resize ((m_iCurrentCurve + 1) * 2);
285 m_veciSymbolFreq [iCurve] = iSymbolFreq;
287 m_vecbSymbolFreqSet [iCurve] = true;
295 EZPlot::setLinestyle (unsigned int iCurve, int iLinestyle)
299 if (m_veciLinestyle.size() <= iCurve) {
301 m_veciLinestyle.resize ((m_iCurrentCurve + 1) * 2);
303 m_vecbLinestyleSet.resize ((m_iCurrentCurve + 1) * 2);
307 m_veciLinestyle [iCurve] = iLinestyle;
309 m_vecbLinestyleSet [iCurve] = true;
317 EZPlot::setLegend (unsigned int iCurve, const std::string& strLegend)
321 if (m_vecsLegend.size() <= iCurve) {
323 m_vecsLegend.resize ((m_iCurrentCurve + 1) * 2);
325 m_vecbLegendSet.resize ((m_iCurrentCurve + 1) * 2);
329 m_vecsLegend [iCurve] = strLegend;
331 m_vecbLegendSet [iCurve] = true;
339 EZPlot::setLegend (unsigned int iCurve, const char* const pszLegend)
343 if (m_vecsLegend.size() <= iCurve) {
345 m_vecsLegend.resize ((m_iCurrentCurve + 1) * 2);
347 m_vecbLegendSet.resize ((m_iCurrentCurve + 1) * 2);
351 m_vecsLegend [iCurve] = pszLegend;
353 m_vecbLegendSet [iCurve] = true;
361 EZPlot::getColor (unsigned int iCurve) const
365 if (m_veciColor.size() > iCurve && m_vecbColorSet[iCurve])
367 return m_veciColor[iCurve];
379 EZPlot::getSymbol (unsigned int iCurve) const
383 if (m_veciSymbol.size() > iCurve && m_vecbSymbolSet[iCurve])
385 return m_veciSymbol[iCurve];
397 EZPlot::getSymbolFreq (unsigned int iCurve) const
401 if (m_veciSymbolFreq.size() > iCurve && m_vecbSymbolFreqSet[iCurve])
403 return m_veciSymbolFreq[iCurve];
415 EZPlot::getLinestyle (unsigned int iCurve) const
419 if (m_veciLinestyle.size() > iCurve && m_vecbLinestyleSet[iCurve])
421 return m_veciLinestyle[iCurve];
433 EZPlot::getLegend (unsigned int iCurve) const
437 if (m_vecsLegend.size() > iCurve && m_vecbLegendSet[iCurve])
439 return &m_vecsLegend[iCurve];
452 * plot Plots all curves collected by addCurves ()
458 * This routine plots the curves that have stored by addCurves().
461 * drawAxes() & make_numfmt()
465 EZPlot::plot (SGP* pSGP)
467 if (m_vecCurves.size() <= 0)
472 m_pSGP->setWindow (0., 0., 1., 1.);
474 if (s_textsize == TRUE)
475 m_pSGP->setTextPointSize (v_textsize);
477 charheight = m_pSGP->getCharHeight();
478 charwidth = m_pSGP->getCharWidth();
479 double symheight = charheight * 0.3; // size of symbol in NDC
480 double symwidth = symheight;
482 const EZPlotCurve& firstCurve = *m_vecCurves[0];
484 double xmin = firstCurve.x[0]; // extent of curves in world coord
486 double ymin = firstCurve.y[0];
490 for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
491 const EZPlotCurve* const pCurve = m_vecCurves [iCurve];
493 for (int ip = 0; ip < pCurve->m_iPointCount; ip++) {
494 if (pCurve->x[ip] > xmax)
495 xmax = pCurve->x[ip];
496 else if (pCurve->x[ip] < xmin)
497 xmin = pCurve->x[ip];
498 if (pCurve->y[ip] > ymax)
499 ymax = pCurve->y[ip];
500 else if (pCurve->y[ip] < ymin)
501 ymin = pCurve->y[ip];
505 // extend graph limits for user defined axis cross positions
506 if (s_xcross == TRUE) {
509 else if (v_xcross > xmax)
513 if (s_ycross == TRUE) {
516 else if (v_ycross > ymax)
520 // find nice endpoints for axes
521 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))
524 // check if user set x-axis extents
525 if (s_xmin == TRUE) {
527 x_nint = o_xmajortick - 1;
529 if (s_xmax == TRUE) {
531 x_nint = o_xmajortick - 1;
534 // check if user set y-axis extents
535 if (s_ymin == TRUE) {
537 y_nint = o_ymajortick - 1;
539 if (s_ymax == TRUE) {
541 y_nint = o_ymajortick - 1;
544 // calculate increment between major axis in world coordinates
545 xw_tickinc = (xgw_max - xgw_min) / x_nint;
546 yw_tickinc = (ygw_max - ygw_min) / y_nint;
548 // we have now calcuated xgw_min, xyw_max, ygw_min, & ygw_max
550 // set the number of decimal point to users' setting or default
551 // Two formats for numbers: Fixed: -nnn.f and Exponent: -n.fffE+eee
552 if (s_lxfrac == TRUE)
557 if (s_lyfrac == TRUE)
562 make_numfmt (x_numfmt, &x_fldwid, &x_frac, xgw_min, xgw_max, x_nint);
563 make_numfmt (y_numfmt, &y_fldwid, &y_frac, ygw_min, ygw_max, y_nint);
565 xtl_wid = x_fldwid * charwidth; // calc size of tick labels
566 ytl_wid = y_fldwid * charwidth;
567 tl_height = charheight;
569 // calculate the extent of the plot frame
572 xp_max = xp_min + o_xlength;
573 yp_max = yp_min + o_ylength;
575 xp_min = clamp (xp_min, 0., 1.);
576 xp_max = clamp (xp_max, 0., 1.);
577 yp_min = clamp (yp_min, 0., 1.);
578 yp_max = clamp (yp_max, 0., 1.);
580 xa_min = xp_min; // extent of axes
585 // adjust frame for title
588 if (c_title.length() > 0)
589 ya_max -= 2 * charheight;
593 ya_max -= 0.7 * charheight; // allow room for yaxis ticklabel
596 // calculate legend box boundaries
597 int max_leg = 0; // longest legend in characters
598 int num_leg = 0; // number of legend titles
600 for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
601 const std::string* pstrLegend = getLegend (iCurve);
603 if (pstrLegend && pstrLegend->length() > 0) {
605 int nLegend = pstrLegend->length();
608 if (nLegend > max_leg)
616 if (num_leg > 0 && o_legendbox != NOLEGEND) {
617 double leg_width = (max_leg + 2) * charwidth; // size of legend box
618 double leg_height = num_leg * 3 * charheight;
620 if (s_xlegend == TRUE)
624 if (o_legendbox == OUTSIDE)
625 xa_max -= (leg_width + 0.5 * charwidth);
627 xl_min = xl_max - leg_width;
629 if (s_ylegend == TRUE)
634 yl_min = yl_max - leg_height;
636 m_pSGP->setColor (clr_legend);
637 m_pSGP->drawRect (xl_min, yl_min, xl_max, yl_max);
639 int iLegend = 0; // current legend position
641 for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
642 const std::string* pstrLegend = getLegend (iCurve);
643 if (! pstrLegend || pstrLegend->length() == 0)
646 double xmin = xl_min + 1.0 * charwidth;
647 double xmax = xl_max - 1.0 * charwidth;
648 double y = yl_max - (2.0 + iLegend * 3) * charheight;
650 m_pSGP->moveAbs (xmin, y + 0.5 * charheight);
652 m_pSGP->drawText (pstrLegend->c_str());
653 m_pSGP->setColor (getColor (iCurve));
655 int iLS = getLinestyle (iCurve);
656 if (iLS != SGP::LS_NOLINE) {
657 m_pSGP->setLineStyle (iLS);
658 m_pSGP->moveAbs (xmin, y);
659 m_pSGP->lineAbs (xmax, y);
662 int iSymbol = getSymbol (iCurve);
664 double xinc = (xmax - xmin) / (5 - 1);
665 m_pSGP->setLineStyle (SGP::LS_SOLID);
666 for (int j = 0; j < 5; j++) {
667 m_pSGP->moveAbs (xmin + j * xinc, y);
668 symbol (iSymbol, symwidth, symheight);
671 ++iLegend; // move to next legend position
673 } // end legend printing
675 // calculate the extent of the axes
677 /*-------------------------*/
678 /* adjust frame for labels */
679 /*-------------------------*/
682 if (c_xlabel.length() > 0)
683 ya_min += 1.5 * charheight;
684 xlbl_row = xp_min; // put x-label on bottom of plot frame
687 if (c_ylabel.length() > 0) {
689 m_pSGP->setTextAngle (HALFPI);
691 m_pSGP->setTextSize (1.5 * charheight);
693 double xExtent, yExtent;
695 m_pSGP->getTextExtent (c_ylabel.c_str(), &xExtent, &yExtent);
697 m_pSGP->setTextSize (charheight);
701 m_pSGP->setTextAngle (0.0);
706 /*------------------------------*/
707 /* adjust frame for tick labels */
708 /*------------------------------*/
710 // Calc offset of tick labels from axes
711 if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
713 else if (o_xticks == BELOW)
714 xtl_ofs = -0.5 * charheight;
715 else if (o_xticks == ABOVE)
716 xtl_ofs = 0.5 * charheight;
718 if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
720 else if (o_yticks == LEFT) {
721 double xExtentMin, xExtentMax, yExtent;
723 snprintf (s, sizeof(s), y_numfmt, ymin);
724 m_pSGP->getTextExtent (s, &xExtentMin, &yExtent);
725 snprintf (s, sizeof(s), y_numfmt, ymax);
726 m_pSGP->getTextExtent (s, &xExtentMax, &yExtent);
727 if (xExtentMin > xExtentMax)
728 xExtentMax = xExtentMin;
729 ytl_ofs = -xExtentMax;
730 } else if (o_yticks == RIGHT)
731 ytl_ofs = 1.5 * charwidth;
733 xa_max -= 0.7 * x_fldwid * charwidth; // make room for last x tick label
740 // see if need to shrink axis extents and/or tick extents
741 if (xtl_ofs != 0.0 && s_ycross == FALSE) {
742 if (o_xticks == BELOW) {
743 ya_min += 1.5 * charheight;
745 } else if (o_xticks == ABOVE) {
747 yt_min = ya_min + 1.5 * charheight;
749 } else // noaxis, no t-labels, or user set cross
752 if (ytl_ofs != 0.0 && s_xcross == FALSE) {
753 if (o_yticks == LEFT) {
754 xa_min += 2*charwidth - ytl_ofs; // (2 + y_fldwid) * charwidth;
756 } else if (o_yticks == RIGHT) {
758 xt_min = xa_min + ytl_ofs; // + y_fldwid * charwidth;
763 // decrease size of graph, if necessary, to accommadate space
764 // between axis boundary and boundary of ticks
765 double x_added_ticks = 0; // number of tick spaces added to axis
766 double y_added_ticks = 0;
767 if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
769 if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
772 if (o_grid == TRUE) {
773 if (x_added_ticks < 0)
775 if (y_added_ticks < 0)
779 if (x_added_ticks < 0) {
780 if (o_yticks == LEFT || s_ycross)
786 if (y_added_ticks < 0) {
787 if (o_xticks == BELOW || s_xcross)
793 xn_tickinc = (xt_max - xt_min) / (x_nint + x_added_ticks);
794 yn_tickinc = (yt_max - yt_min) / (y_nint + y_added_ticks);
796 xt_min += 0.5 * x_added_ticks * xn_tickinc;
797 xt_max -= 0.5 * x_added_ticks * xn_tickinc;
798 yt_min += 0.5 * y_added_ticks * yn_tickinc;
799 yt_max -= 0.5 * y_added_ticks * yn_tickinc;
806 //------------------------------------------------------------------------
808 m_xWorldScale = (xgn_max - xgn_min) / (xgw_max - xgw_min);
809 m_yWorldScale = (ygn_max - ygn_min) / (ygw_max - ygw_min);
813 m_pSGP->setLineStyle (SGP::LS_SOLID);
819 clipRect[0] = xgn_min; clipRect[1] = ygn_min; clipRect[2] = xgn_max; clipRect[3] = ygn_max;
823 for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
824 const EZPlotCurve* const pCurve = m_vecCurves [iCurve];
827 m_pSGP->setColor (getColor (iCurve));
829 int iSym = getSymbol (iCurve);
831 int iLS = getLinestyle (iCurve);
835 if (iLS != SGP::LS_NOLINE) {
836 m_pSGP->setLineStyle (iLS);
837 double x1 = convertWorldToNDC_X (pCurve->x[0]);
838 double y1 = convertWorldToNDC_Y (pCurve->y[0]);
840 for (int i = 1; i < pCurve->m_iPointCount; i++) {
841 double x2 = convertWorldToNDC_X (pCurve->x[i]);
842 double y2 = convertWorldToNDC_Y (pCurve->y[i]);
846 if (clip_rect (x1, y1, x2Clip, y2Clip, clipRect)) {
847 m_pSGP->moveAbs (x1, y1);
848 m_pSGP->lineAbs (x2Clip, y2Clip);
855 int iSymFreq = getSymbolFreq (iCurve);
856 m_pSGP->setLineStyle (SGP::LS_SOLID);
857 double x = convertWorldToNDC_X (pCurve->x[0]);
858 double y = convertWorldToNDC_Y (pCurve->y[0]);
859 m_pSGP->moveAbs (x, y);
860 symbol (iSym, symwidth, symheight);
861 for (int i = 1; i < pCurve->m_iPointCount; i++)
862 if (i % iSymFreq == 0 || i == pCurve->m_iPointCount - 1) {
863 x = convertWorldToNDC_X (pCurve->x[i]);
864 y = convertWorldToNDC_Y (pCurve->y[i]);
866 if (y >= ygn_min && y <= ygn_max) {
867 m_pSGP->moveAbs (x, y);
868 symbol (iSym, symwidth, symheight);
877 * drawAxes INTERNAL routine to draw axis & label them
887 double xticklen = 0, yticklen = 0; // length of ticks in NDC
888 double minorinc; // increment between minor axes
889 double xaxispos, yaxispos; // crossing of axes
891 bool axis_near; // TRUE if axis too close to print t-label
896 m_pSGP->setTextSize (charheight);
897 m_pSGP->setTextColor (1, -1);
899 if (o_xticks == ABOVE)
900 xticklen = 0.5 * charheight;
901 else if (o_xticks == BELOW)
902 xticklen = -0.5 * charheight;
904 if (o_yticks == RIGHT)
905 yticklen = charwidth;
906 else if (o_yticks == LEFT)
907 yticklen = -charwidth;
909 if (c_title.length() > 0) {
911 m_pSGP->setTextSize (charheight * 2.0);
912 m_pSGP->getTextExtent (c_title.c_str(), &wText, &hText);
913 m_pSGP->moveAbs (xa_min + (xa_max-xa_min)/2 - wText/2, title_row);
914 m_pSGP->setTextColor (clr_title, -1);
915 m_pSGP->drawText (c_title);
916 m_pSGP->setTextSize (charheight);
919 if (o_grid == TRUE || o_box == TRUE) {
920 m_pSGP->setColor (clr_axis);
921 m_pSGP->moveAbs (xa_min, ya_min);
922 m_pSGP->lineAbs (xa_max, ya_min);
923 m_pSGP->lineAbs (xa_max, ya_max);
924 m_pSGP->lineAbs (xa_min, ya_max);
925 m_pSGP->lineAbs (xa_min, ya_min);
928 // calculate position of axes
931 if (s_ycross == TRUE) { // convert users' world-coord
932 xaxispos = convertWorldToNDC_Y (v_ycross);// axis to its position in NDC
933 x = convertWorldToNDC_X (xgw_min);
938 if (s_xcross == TRUE) { // convert users' world-coord
939 yaxispos = convertWorldToNDC_X (v_xcross);// axis to its NDC position
940 y = convertWorldToNDC_Y (ygw_min);
948 if (o_xaxis == LINEAR) {
951 m_pSGP->setColor (clr_axis);
952 if (o_tag && !o_grid && !o_box && s_xcross) {
953 m_pSGP->moveAbs (xa_min, xaxispos - charheight);
954 m_pSGP->lineAbs (xa_min, xaxispos + charheight);
956 m_pSGP->moveAbs (xa_min, xaxispos);
957 m_pSGP->lineAbs (xa_max, xaxispos);
958 if (o_tag && !o_grid && !o_box) {
959 m_pSGP->moveAbs (xa_max, xaxispos - charheight);
960 m_pSGP->lineAbs (xa_max, xaxispos + charheight);
963 if (o_grid == TRUE) {
964 m_pSGP->setColor (clr_grid);
965 for (i = 0; i <= x_nint; i++) {
966 m_pSGP->moveAbs (xt_min + xn_tickinc * i, ya_max);
967 m_pSGP->lineAbs (xt_min + xn_tickinc * i, ya_min);
970 m_pSGP->setTextSize (charheight * 1.5);
971 m_pSGP->setTextColor (clr_label, -1);
975 m_pSGP->getTextExtent (c_xlabel.c_str(), &wText, &hText);
976 m_pSGP->moveAbs (xa_min + (xa_max-xa_min)/2 - wText / 2, xlbl_row + charheight * 1.5);
978 m_pSGP->drawText (c_xlabel);
979 m_pSGP->setTextSize (charheight);
980 minorinc = xn_tickinc / (o_xminortick + 1);
982 for (i = 0; i <= x_nint; i++) {
983 x = xt_min + xn_tickinc * i;
984 m_pSGP->setColor (clr_axis);
985 m_pSGP->moveAbs (x, xaxispos);
986 m_pSGP->lineAbs (x, xaxispos + xticklen);
988 for (j = 1; j <= o_xminortick; j++) {
989 x2 = x + minorinc * j;
990 m_pSGP->moveAbs (x2, xaxispos);
991 m_pSGP->lineAbs (x2, xaxispos + TICKRATIO * xticklen);
994 if (xaxispos + xtl_ofs > ya_min && o_yaxis != NOAXIS) {
995 double xw = xgw_min + i * xw_tickinc;
996 double x = convertWorldToNDC_X (xw);
997 double d = x - yaxispos;
998 if (o_yticks == RIGHT && d >= 0 && d < 0.9 * xn_tickinc)
1000 if (o_yticks == LEFT && d <= 0 && d > -0.9 * xn_tickinc)
1004 if (o_xtlabel == TRUE && axis_near == FALSE) {
1005 snprintf (str, sizeof(str), x_numfmt, xgw_min + xw_tickinc * i);
1006 numstr = str_skip_head (str, " ");
1007 double xExtent, yExtent;
1008 m_pSGP->getTextExtent (numstr, &xExtent, &yExtent);
1009 m_pSGP->moveAbs (x - xExtent/2, xaxispos + xtl_ofs);
1010 m_pSGP->setTextColor (clr_number, -1);
1011 m_pSGP->drawText (numstr);
1021 if (o_yaxis == LINEAR) {
1023 m_pSGP->setColor (clr_axis);
1024 if (o_tag && !o_grid && !o_box && s_ycross) {
1025 m_pSGP->moveAbs (yaxispos - charwidth, ya_min);
1026 m_pSGP->lineAbs (yaxispos + charwidth, ya_min);
1028 m_pSGP->moveAbs (yaxispos, ya_min);
1029 m_pSGP->lineAbs (yaxispos, ya_max);
1030 if (o_tag && !o_grid && !o_box) {
1031 m_pSGP->moveAbs (yaxispos - charwidth, ya_max);
1032 m_pSGP->lineAbs (yaxispos + charwidth, ya_max);
1035 if (o_grid == TRUE) {
1036 m_pSGP->setColor (clr_grid);
1037 for (i = 0; i <= y_nint; i++) {
1038 y = yt_min + yn_tickinc * i;
1039 m_pSGP->moveAbs (xa_max, y);
1040 m_pSGP->lineAbs (xa_min, y);
1044 m_pSGP->setTextAngle (HALFPI);
1045 m_pSGP->setTextSize (1.5 * charheight);
1046 m_pSGP->setTextColor (clr_label, -1);
1048 double xExtent, yExtent;
1050 m_pSGP->getTextExtent (c_ylabel.c_str(), &xExtent, &yExtent);
1051 m_pSGP->moveAbs (ylbl_col, ya_min + (ya_max-ya_min)/2 - yExtent / 2);
1052 m_pSGP->drawText (c_ylabel);
1054 m_pSGP->setTextAngle (0.0);
1055 m_pSGP->setTextSize (charheight);
1056 minorinc = yn_tickinc / (o_yminortick + 1);
1058 for (i = 0; i <= y_nint; i++) {
1059 y = yt_min + yn_tickinc * i;
1060 m_pSGP->setColor (clr_axis);
1061 m_pSGP->moveAbs (yaxispos, y);
1062 m_pSGP->lineAbs (yaxispos + yticklen, y);
1064 for (j = 1; j <= o_yminortick; j++) {
1065 y2 = y + minorinc * j;
1066 m_pSGP->moveAbs (yaxispos, y2);
1067 m_pSGP->lineAbs (yaxispos + TICKRATIO * yticklen, y2);
1070 if (yaxispos + ytl_ofs > xa_min && o_xaxis != NOAXIS) {
1071 double yw = ygw_min + i * yw_tickinc;
1072 double y = convertWorldToNDC_Y (yw);
1073 double d = y - xaxispos;
1074 if (o_xticks == ABOVE && d >= 0 && d < 0.9 * yn_tickinc)
1076 if (o_xticks == BELOW && d <= 0 && d > -0.9 * yn_tickinc)
1079 if (o_ytlabel == TRUE && axis_near == FALSE) {
1080 snprintf (str, sizeof(str), y_numfmt, ygw_min + yw_tickinc * i);
1081 double xExtent, yExtent;
1082 m_pSGP->getTextExtent (str, &xExtent, &yExtent);
1083 if (o_yticks == LEFT)
1084 m_pSGP->moveAbs (yaxispos - 1.5 * charwidth - xExtent, y + 0.5 * yExtent);
1086 m_pSGP->moveAbs (yaxispos + 1.5 * charwidth, y + 0.5 * yExtent);
1087 m_pSGP->setTextColor (clr_number, -1);
1088 m_pSGP->drawText (str);
1096 EZPlot::symbol (int sym, double symwidth, double symheight)
1101 if (sym == SB_CROSS) {
1102 m_pSGP->markerRel (0, 0);
1103 // m_pSGP->moveRel (-0.5 * symwidth, -0.5 * symheight);
1104 // m_pSGP->lineRel (symwidth, symheight);
1105 // m_pSGP->moveRel (-symwidth, 0.0);
1106 // m_pSGP->lineRel (symwidth, -symheight);
1107 // m_pSGP->moveRel (-0.5 * symwidth, 0.5 * symheight);
1108 } else if (sym == SB_PLUS) {
1109 m_pSGP->moveRel (-0.5 * symwidth, 0.0);
1110 m_pSGP->lineRel (symwidth, 0.0);
1111 m_pSGP->moveRel (-0.5 * symwidth, -0.5 * symheight);
1112 m_pSGP->lineRel (0.0, symheight);
1113 m_pSGP->moveRel (0.0, -0.5 * symheight);
1114 } else if (sym == SB_BOX) {
1115 m_pSGP->moveRel (-0.5 * symwidth, -0.5 * symheight);
1116 m_pSGP->lineRel (symwidth, 0.0);
1117 m_pSGP->lineRel (0.0, symheight);
1118 m_pSGP->lineRel (-symwidth, 0.0);
1119 m_pSGP->lineRel (0.0, -symheight);
1120 m_pSGP->moveRel (0.5 * symwidth, 0.5 * symheight);
1121 } else if (sym == SB_CIRCLE) {
1122 m_pSGP->drawCircle (symwidth);
1123 } else if (sym == SB_ERRORBAR) {
1124 m_pSGP->moveRel (-0.5 * symwidth, 0.5 * symheight);
1125 m_pSGP->lineRel (symwidth, 0.0);
1126 m_pSGP->moveRel (-0.5 * symwidth, 0.0);
1127 m_pSGP->lineRel (0.0, -symheight);
1128 m_pSGP->moveRel (-0.5 * symwidth, 0.0);
1129 m_pSGP->lineRel (symwidth, 0.0);
1130 m_pSGP->moveRel (-0.5 * symwidth, 0.5 * symheight);
1131 } else if (sym == SB_POINT) {
1132 m_pSGP->pointRel (0, 0);
1139 * axis_scale calculates graph axis scaling
1142 * retval = axis_scale (min, max, nint, minp, maxp, nintp,
1143 * rec_total, rec_frac)
1146 * double min Smallest value to plot
1147 * double max Largest value to plot
1148 * int nint Number of intervals desired
1151 * int retval FALSE if illegal parameters, else TRUE
1152 * double *minp Minimum graph value
1153 * double *maxp Maximum graph value
1154 * int *nintp Number of intervals for graph
1155 * int *rec_total Recommended field width for printing out the number
1156 * int *rec_frac Recommended number of digits for print fraction
1160 EZPlot::axis_scale (double min, double max, int nint, double *minp, double *maxp, int *nintp)
1163 sys_error (ERR_WARNING, "No intervals to plot: num intervals=%d [axis_scale]", nint);
1167 double scaled = fabs(max) / 10;
1170 *minp = min - scaled;
1171 *maxp = max + scaled;
1177 double a = fabs(min);
1178 if (fabs(min) < fabs(max))
1180 double scale = pow (10.0, floor(log10(a)));
1182 double mina = min / scale;
1183 double maxa = max / scale;
1184 double d = (maxa - mina) / nint;
1186 double e = floor (log10(d));
1187 double f = d / pow (10.0, e);
1191 else if (f < sqrt (10.0))
1193 else if (f < sqrt (50.0))
1195 double wdt = v * pow (10.0, e);
1196 double g = floor (mina / wdt);
1197 if (fabs(g + 1 - mina / wdt) < j)
1205 double h = floor (maxa / wdt) + 1.0;
1206 if (fabs(maxa / wdt + 1 - h) < j)
1212 *nintp = static_cast<int>(h - g);
1213 if (fabs(*maxp) >= 10.0 || fabs(*minp) >= 10.0) {
1214 scale = scale * 10.0;
1226 * make_numfmt Make a numeric format string
1229 * make_numfmt (fmtstr, fldwid, nfrac, min, max, nint)
1230 * char *fmtstr Returned format string for printf()
1231 * int *fldwid Returned field width
1232 * int *nfrac If < 0, then calculate best number of
1233 * fraction places & return that value
1234 * If >= 0, then use that number of places
1235 * double min Minimum value
1236 * double max Maximum value
1237 * int nint Number of intervals between min & max
1240 * This routine is written as an INTERNAL routine for EZPLOT
1243 static inline double
1254 EZPlot::make_numfmt (char *fmtstr, int *fldwid, int *nfrac, double minval, double maxval, int nint)
1256 int wid, frac, expon;
1258 double delta = (maxval - minval) / nint;
1259 double absmin = fabs(minval);
1260 double absmax = fabs(maxval);
1262 if (absmin > absmax)
1265 double logt = log10( absmax );
1267 if (fabs(logt) >= 6) { // use exponential format
1268 if (fabs(logt) > 99)
1273 if (*nfrac < 0) { // calculate frac
1274 delta /= pow (10., floor(logt)); // scale delta
1275 frac = static_cast<int>(fabs(my_trunc(log10(delta)))) + 1;
1277 frac = 1; // to be safe, add decimal pt
1278 } else // use users' frac
1281 wid = 2 + frac + expon;
1282 if (minval < 0. || maxval < 0.)
1284 sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "g");
1285 } else { // use fixed format
1286 wid = static_cast<int>(my_trunc(logt)) + 1;
1289 if (minval < 0. || maxval < 0.)
1292 if (*nfrac < 0) { // calculate frac
1293 if (delta >= 0.999999)
1294 frac = 1; // add a decimal pt to be safe
1296 frac = static_cast<int>(fabs(my_trunc(log10(delta)))) + 1;
1297 } else // use users' frac
1301 sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "f");