1 /*****************************************************************************
6 ** This is part of the CTSim program
7 ** Copyright (c) 1983-2001 Kevin Rosenberg
9 ** $Id: ezplot.cpp,v 1.31 2001/02/20 04:48:45 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)
39 : x(new double[n]), y(new double[n])
41 for (int i = 0; i < n; i++) {
49 EZPlotCurve::~EZPlotCurve ()
57 EZPlot::addCurve (const double *y, int n)
59 double* x = new double [n];
61 for (int i = 0; i < n; i++)
70 EZPlot::addCurve (const float *y, int n)
72 double* yDouble = new double [n];
74 for (int i = 0; i < n; i++)
77 addCurve (yDouble, n);
83 EZPlot::addCurve (const float x[], const double y[], int num)
85 double* dx = new double [num];
87 for (int i = 0; i < num; i++)
90 addCurve (dx, y, num);
95 EZPlot::addCurve (const double* const x, const float* const y, int num)
97 double* dy = new double [num];
99 for (int i = 0; i < num; i++)
102 addCurve (x, dy, num);
109 EZPlot::addCurve (const double* const x, const double* const y, int num)
114 EZPlotCurve* pCurve = new EZPlotCurve (x, y, num);
115 m_vecCurves.push_back (pCurve);
121 for (EZPlotCurveIterator i = m_vecCurves.begin(); i != m_vecCurves.end(); i++)
126 EZPlot::clearCurves ()
128 for (EZPlotCurveIterator i = m_vecCurves.begin(); i != m_vecCurves.end(); i++)
130 m_vecCurves.erase (m_vecCurves.begin(), m_vecCurves.end());
139 m_pol.addSkipWord ("please");
141 m_pol.addSkipWord ("use");
143 m_pol.addSkipWord ("are");
145 m_pol.addSkipWord ("and");
147 m_pol.addSkipWord ("is");
149 m_pol.addSkipWord ("the");
151 m_pol.addSkipWord ("equals");
153 m_pol.addSkipChar ('=');
157 m_pol.usefile (POL::P_USE_STR,"");
159 m_pol.set_inputline ("!eoc ,");
171 EZPlot::initPlotSettings ()
173 m_iCurrentCurve = -1;
198 o_color = DEF_CURVE_CLR;
201 o_linestyle = SGP::LS_SOLID;
208 o_legendbox = INSIDE;
221 clr_axis = C_LTGRAY; // set fixed colors
225 clr_number = C_GREEN;
231 EZPlot::setColor (unsigned int iCurve, int iColor)
235 if (m_veciColor.size() <= iCurve) {
237 m_veciColor.resize ((m_iCurrentCurve + 1) * 2);
239 m_vecbColorSet.resize ((m_iCurrentCurve + 1) * 2);
243 m_veciColor [iCurve] = iColor;
245 m_vecbColorSet [iCurve] = true;
253 EZPlot::setSymbol (unsigned int iCurve, int iSymbol)
257 if (m_veciSymbol.size() <= iCurve) {
259 m_veciSymbol.resize ((m_iCurrentCurve + 1) * 2);
261 m_vecbSymbolSet.resize ((m_iCurrentCurve + 1) * 2);
265 m_veciSymbol [iCurve] = iSymbol;
267 m_vecbSymbolSet [iCurve] = true;
275 EZPlot::setSymbolFreq (unsigned int iCurve, int iSymbolFreq)
279 if (m_veciSymbolFreq.size() <= iCurve) {
281 m_veciSymbolFreq.resize ((m_iCurrentCurve + 1) * 2);
283 m_vecbSymbolFreqSet.resize ((m_iCurrentCurve + 1) * 2);
287 m_veciSymbolFreq [iCurve] = iSymbolFreq;
289 m_vecbSymbolFreqSet [iCurve] = true;
297 EZPlot::setLinestyle (unsigned int iCurve, int iLinestyle)
301 if (m_veciLinestyle.size() <= iCurve) {
303 m_veciLinestyle.resize ((m_iCurrentCurve + 1) * 2);
305 m_vecbLinestyleSet.resize ((m_iCurrentCurve + 1) * 2);
309 m_veciLinestyle [iCurve] = iLinestyle;
311 m_vecbLinestyleSet [iCurve] = true;
319 EZPlot::setLegend (unsigned int iCurve, const std::string& strLegend)
323 if (m_vecsLegend.size() <= iCurve) {
325 m_vecsLegend.resize ((m_iCurrentCurve + 1) * 2);
327 m_vecbLegendSet.resize ((m_iCurrentCurve + 1) * 2);
331 m_vecsLegend [iCurve] = strLegend;
333 m_vecbLegendSet [iCurve] = true;
341 EZPlot::setLegend (unsigned int iCurve, const char* const pszLegend)
345 if (m_vecsLegend.size() <= iCurve) {
347 m_vecsLegend.resize ((m_iCurrentCurve + 1) * 2);
349 m_vecbLegendSet.resize ((m_iCurrentCurve + 1) * 2);
353 m_vecsLegend [iCurve] = pszLegend;
355 m_vecbLegendSet [iCurve] = true;
363 EZPlot::getColor (unsigned int iCurve) const
367 if (m_veciColor.size() > iCurve && m_vecbColorSet[iCurve])
369 return m_veciColor[iCurve];
381 EZPlot::getSymbol (unsigned int iCurve) const
385 if (m_veciSymbol.size() > iCurve && m_vecbSymbolSet[iCurve])
387 return m_veciSymbol[iCurve];
399 EZPlot::getSymbolFreq (unsigned int iCurve) const
403 if (m_veciSymbolFreq.size() > iCurve && m_vecbSymbolFreqSet[iCurve])
405 return m_veciSymbolFreq[iCurve];
417 EZPlot::getLinestyle (unsigned int iCurve) const
421 if (m_veciLinestyle.size() > iCurve && m_vecbLinestyleSet[iCurve])
423 return m_veciLinestyle[iCurve];
435 EZPlot::getLegend (unsigned int iCurve) const
439 if (m_vecsLegend.size() > iCurve && m_vecbLegendSet[iCurve])
441 return &m_vecsLegend[iCurve];
454 * plot Plots all curves collected by addCurves ()
460 * This routine plots the curves that have stored by addCurves().
463 * drawAxes() & make_numfmt()
467 EZPlot::plot (SGP* pSGP)
470 if (m_vecCurves.size() <= 0)
477 m_pSGP->setWindow (0., 0., 1., 1.);
479 if (s_textsize == TRUE)
480 m_pSGP->setTextPointSize (v_textsize);
482 charheight = m_pSGP->getCharHeight();
483 charwidth = m_pSGP->getCharWidth();
485 const EZPlotCurve& firstCurve = *m_vecCurves[0];
487 double xmin = firstCurve.x[0]; // extent of curves in world coord
489 double ymin = firstCurve.y[0];
493 for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
494 const EZPlotCurve* const pCurve = m_vecCurves [iCurve];
496 for (int ip = 0; ip < pCurve->m_iPointCount; ip++) {
497 if (pCurve->x[ip] > xmax)
498 xmax = pCurve->x[ip];
499 else if (pCurve->x[ip] < xmin)
500 xmin = pCurve->x[ip];
501 if (pCurve->y[ip] > ymax)
502 ymax = pCurve->y[ip];
503 else if (pCurve->y[ip] < ymin)
504 ymin = pCurve->y[ip];
508 // extend graph limits for user defined axis cross positions
509 if (s_xcross == TRUE) {
512 else if (v_xcross > xmax)
516 if (s_ycross == TRUE) {
519 else if (v_ycross > ymax)
523 // find nice endpoints for axes
524 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))
527 // check if user set x-axis extents
528 if (s_xmin == TRUE) {
530 x_nint = o_xmajortick - 1;
532 if (s_xmax == TRUE) {
534 x_nint = o_xmajortick - 1;
537 // check if user set y-axis extents
538 if (s_ymin == TRUE) {
540 y_nint = o_ymajortick - 1;
542 if (s_ymax == TRUE) {
544 y_nint = o_ymajortick - 1;
547 // calculate increment between major axis in world coordinates
548 xw_tickinc = (xgw_max - xgw_min) / x_nint;
549 yw_tickinc = (ygw_max - ygw_min) / y_nint;
551 // we have now calcuated xgw_min, xyw_max, ygw_min, & ygw_max
553 // set the number of decimal point to users' setting or default
554 // Two formats for numbers: Fixed: -nnn.f and Exponent: -n.fffE+eee
555 if (s_lxfrac == TRUE)
560 if (s_lyfrac == TRUE)
565 make_numfmt (x_numfmt, &x_fldwid, &x_frac, xgw_min, xgw_max, x_nint);
566 make_numfmt (y_numfmt, &y_fldwid, &y_frac, ygw_min, ygw_max, y_nint);
568 xtl_wid = x_fldwid * charwidth; // calc size of tick labels
569 ytl_wid = y_fldwid * charwidth;
570 tl_height = charheight;
572 // calculate the extent of the plot frame
575 xp_max = xp_min + o_xlength;
576 yp_max = yp_min + o_ylength;
578 xp_min = clamp (xp_min, 0., 1.);
579 xp_max = clamp (xp_max, 0., 1.);
580 yp_min = clamp (yp_min, 0., 1.);
581 yp_max = clamp (yp_max, 0., 1.);
583 xa_min = xp_min; // extent of axes
588 // adjust frame for title
591 if (c_title.length() > 0)
592 ya_max -= 2 * charheight;
596 ya_max -= 0.7 * charheight; // allow room for yaxis ticklabel
599 // calculate legend box boundaries
600 int max_leg = 0; // longest legend in characters
601 int num_leg = 0; // number of legend titles
603 for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
604 const std::string* pstrLegend = getLegend (iCurve);
606 if (pstrLegend && pstrLegend->length() > 0) {
608 int nLegend = pstrLegend->length();
611 if (nLegend > max_leg)
619 if (num_leg > 0 && o_legendbox != NOLEGEND) {
620 double leg_width = (max_leg + 2) * charwidth; // size of legend box
621 double leg_height = num_leg * 3 * charheight;
623 if (s_xlegend == TRUE)
627 if (o_legendbox == OUTSIDE)
628 xa_max -= (leg_width + 0.5 * charwidth);
630 xl_min = xl_max - leg_width;
632 if (s_ylegend == TRUE)
637 yl_min = yl_max - leg_height;
639 m_pSGP->setColor (clr_legend);
640 m_pSGP->drawRect (xl_min, yl_min, xl_max, yl_max);
642 int iLegend = 0; // current legend position
644 for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
645 const std::string* pstrLegend = getLegend (iCurve);
646 if (! pstrLegend || pstrLegend->length() == 0)
649 double xmin = xl_min + 1.0 * charwidth;
650 double xmax = xl_max - 1.0 * charwidth;
651 double y = yl_max - (2.0 + iLegend * 3) * charheight;
653 m_pSGP->moveAbs (xmin, y + 0.5 * charheight);
655 m_pSGP->drawText (pstrLegend->c_str());
656 m_pSGP->setColor (getColor (iCurve));
658 int iLS = getLinestyle (iCurve);
659 if (iLS != SGP::LS_NOLINE) {
660 m_pSGP->setLineStyle (iLS);
661 m_pSGP->moveAbs (xmin, y);
662 m_pSGP->lineAbs (xmax, y);
665 int iSymbol = getSymbol (iCurve);
667 double xinc = (xmax - xmin) / (5 - 1);
668 m_pSGP->setLineStyle (SGP::LS_SOLID);
669 for (int j = 0; j < 5; j++) {
670 m_pSGP->moveAbs (xmin + j * xinc, y);
671 symbol(iSymbol, 0.5 * charwidth, 0.5 * charheight);
674 ++iLegend; // move to next legend position
676 } // end legend printing
678 // calculate the extent of the axes
680 /*-------------------------*/
681 /* adjust frame for labels */
682 /*-------------------------*/
685 if (c_xlabel.length() > 0)
686 ya_min += 1.5 * charheight;
687 xlbl_row = xp_min; // put x-label on bottom of plot frame
690 if (c_ylabel.length() > 0) {
692 m_pSGP->setTextAngle (HALFPI);
694 m_pSGP->setTextSize (1.5 * charheight);
696 double xExtent, yExtent;
698 m_pSGP->getTextExtent (c_ylabel.c_str(), &xExtent, &yExtent);
700 m_pSGP->setTextSize (charheight);
704 m_pSGP->setTextAngle (0.0);
709 /*------------------------------*/
710 /* adjust frame for tick labels */
711 /*------------------------------*/
713 // Calc offset of tick labels from axes
714 if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
716 else if (o_xticks == BELOW)
717 xtl_ofs = -0.5 * charheight;
718 else if (o_xticks == ABOVE)
719 xtl_ofs = 0.5 * charheight;
721 if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
723 else if (o_yticks == LEFT) {
724 double xExtentMin, xExtentMax, yExtent;
726 snprintf (s, sizeof(s), y_numfmt, ymin);
727 m_pSGP->getTextExtent (s, &xExtentMin, &yExtent);
728 snprintf (s, sizeof(s), y_numfmt, ymax);
729 m_pSGP->getTextExtent (s, &xExtentMax, &yExtent);
730 if (xExtentMin > xExtentMax)
731 xExtentMax = xExtentMin;
732 ytl_ofs = -xExtentMax;
733 } else if (o_yticks == RIGHT)
734 ytl_ofs = 1.5 * charwidth;
736 xa_max -= 0.7 * x_fldwid * charwidth; // make room for last x tick label
743 // see if need to shrink axis extents and/or tick extents
744 if (xtl_ofs != 0.0 && s_ycross == FALSE) {
745 if (o_xticks == BELOW) {
746 ya_min += 1.5 * charheight;
748 } else if (o_xticks == ABOVE) {
750 yt_min = ya_min + 1.5 * charheight;
752 } else // noaxis, no t-labels, or user set cross
755 if (ytl_ofs != 0.0 && s_xcross == FALSE) {
756 if (o_yticks == LEFT) {
757 xa_min += 2*charwidth - ytl_ofs; // (2 + y_fldwid) * charwidth;
759 } else if (o_yticks == RIGHT) {
761 xt_min = xa_min + ytl_ofs; // + y_fldwid * charwidth;
766 // decrease size of graph, if necessary, to accommadate space
767 // between axis boundary and boundary of ticks
768 double x_added_ticks = 0; // number of tick spaces added to axis
769 double y_added_ticks = 0;
770 if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
772 if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
775 if (o_grid == TRUE) {
776 if (x_added_ticks < 0)
778 if (y_added_ticks < 0)
782 if (x_added_ticks < 0) {
783 if (o_yticks == LEFT || s_ycross)
789 if (y_added_ticks < 0) {
790 if (o_xticks == BELOW || s_xcross)
796 xn_tickinc = (xt_max - xt_min) / (x_nint + x_added_ticks);
797 yn_tickinc = (yt_max - yt_min) / (y_nint + y_added_ticks);
799 xt_min += 0.5 * x_added_ticks * xn_tickinc;
800 xt_max -= 0.5 * x_added_ticks * xn_tickinc;
801 yt_min += 0.5 * y_added_ticks * yn_tickinc;
802 yt_max -= 0.5 * y_added_ticks * yn_tickinc;
809 //------------------------------------------------------------------------
811 m_xWorldScale = (xgn_max - xgn_min) / (xgw_max - xgw_min);
812 m_yWorldScale = (ygn_max - ygn_min) / (ygw_max - ygw_min);
816 m_pSGP->setLineStyle (SGP::LS_SOLID);
819 // size of symbol in NDC
820 double symwidth = charwidth;
821 double symheight = charheight;
826 clipRect[0] = xgn_min; clipRect[1] = ygn_min; clipRect[2] = xgn_max; clipRect[3] = ygn_max;
830 for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
831 const EZPlotCurve* const pCurve = m_vecCurves [iCurve];
834 m_pSGP->setColor (getColor (iCurve));
836 int iSym = getSymbol (iCurve);
838 int iLS = getLinestyle (iCurve);
842 if (iLS != SGP::LS_NOLINE) {
843 m_pSGP->setLineStyle (iLS);
844 double x1 = convertWorldToNDC_X (pCurve->x[0]);
845 double y1 = convertWorldToNDC_Y (pCurve->y[0]);
847 for (int i = 1; i < pCurve->m_iPointCount; i++) {
848 double x2 = convertWorldToNDC_X (pCurve->x[i]);
849 double y2 = convertWorldToNDC_Y (pCurve->y[i]);
855 if (clip_rect (x1, y1, x2Clip, y2Clip, clipRect)) {
857 m_pSGP->moveAbs (x1, y1);
859 m_pSGP->lineAbs (x2Clip, y2Clip);
871 int iSymFreq = getSymbolFreq (iCurve);
872 m_pSGP->setLineStyle (SGP::LS_SOLID);
873 double x = convertWorldToNDC_X (pCurve->x[0]);
874 double y = convertWorldToNDC_Y (pCurve->y[0]);
875 m_pSGP->moveAbs (x, y);
876 symbol (iSym, symwidth, symheight);
877 for (int i = 1; i < pCurve->m_iPointCount; i++)
878 if (i % iSymFreq == 0 || i == pCurve->m_iPointCount - 1) {
879 x = convertWorldToNDC_X (pCurve->x[i]);
880 y = convertWorldToNDC_Y (pCurve->y[i]);
882 if (y >= ygn_min && y <= ygn_max) {
883 m_pSGP->moveAbs (x, y);
884 symbol (iSym, symwidth, symheight);
894 * drawAxes INTERNAL routine to draw axis & label them
904 double xticklen = 0, yticklen = 0; // length of ticks in NDC
905 double minorinc; // increment between minor axes
906 double xaxispos, yaxispos; // crossing of axes
908 bool axis_near; // TRUE if axis too close to print t-label
913 m_pSGP->setTextSize (charheight);
914 m_pSGP->setTextColor (1, -1);
916 if (o_xticks == ABOVE)
917 xticklen = 0.5 * charheight;
918 else if (o_xticks == BELOW)
919 xticklen = -0.5 * charheight;
921 if (o_yticks == RIGHT)
922 yticklen = charwidth;
923 else if (o_yticks == LEFT)
924 yticklen = -charwidth;
926 if (c_title.length() > 0) {
928 m_pSGP->setTextSize (charheight * 2.0);
929 m_pSGP->getTextExtent (c_title.c_str(), &wText, &hText);
930 m_pSGP->moveAbs (xa_min + (xa_max-xa_min)/2 - wText/2, title_row);
931 m_pSGP->setTextColor (clr_title, -1);
932 m_pSGP->drawText (c_title);
933 m_pSGP->setTextSize (charheight);
936 if (o_grid == TRUE || o_box == TRUE) {
937 m_pSGP->setColor (clr_axis);
938 m_pSGP->moveAbs (xa_min, ya_min);
939 m_pSGP->lineAbs (xa_max, ya_min);
940 m_pSGP->lineAbs (xa_max, ya_max);
941 m_pSGP->lineAbs (xa_min, ya_max);
942 m_pSGP->lineAbs (xa_min, ya_min);
945 // calculate position of axes
948 if (s_ycross == TRUE) { // convert users' world-coord
949 xaxispos = convertWorldToNDC_Y (v_ycross);// axis to its position in NDC
950 x = convertWorldToNDC_X (xgw_min);
955 if (s_xcross == TRUE) { // convert users' world-coord
956 yaxispos = convertWorldToNDC_X (v_xcross);// axis to its NDC position
957 y = convertWorldToNDC_Y (ygw_min);
965 if (o_xaxis == LINEAR) {
968 m_pSGP->setColor (clr_axis);
969 if (o_tag && !o_grid && !o_box && s_xcross) {
970 m_pSGP->moveAbs (xa_min, xaxispos - charheight);
971 m_pSGP->lineAbs (xa_min, xaxispos + charheight);
973 m_pSGP->moveAbs (xa_min, xaxispos);
974 m_pSGP->lineAbs (xa_max, xaxispos);
975 if (o_tag && !o_grid && !o_box) {
976 m_pSGP->moveAbs (xa_max, xaxispos - charheight);
977 m_pSGP->lineAbs (xa_max, xaxispos + charheight);
980 if (o_grid == TRUE) {
981 m_pSGP->setColor (clr_grid);
982 for (i = 0; i <= x_nint; i++) {
983 m_pSGP->moveAbs (xt_min + xn_tickinc * i, ya_max);
984 m_pSGP->lineAbs (xt_min + xn_tickinc * i, ya_min);
987 m_pSGP->setTextSize (charheight * 1.5);
988 m_pSGP->setTextColor (clr_label, -1);
992 m_pSGP->getTextExtent (c_xlabel.c_str(), &wText, &hText);
993 m_pSGP->moveAbs (xa_min + (xa_max-xa_min)/2 - wText / 2, xlbl_row + charheight * 1.5);
995 m_pSGP->drawText (c_xlabel);
996 m_pSGP->setTextSize (charheight);
997 minorinc = xn_tickinc / (o_xminortick + 1);
999 for (i = 0; i <= x_nint; i++) {
1000 x = xt_min + xn_tickinc * i;
1001 m_pSGP->setColor (clr_axis);
1002 m_pSGP->moveAbs (x, xaxispos);
1003 m_pSGP->lineAbs (x, xaxispos + xticklen);
1005 for (j = 1; j <= o_xminortick; j++) {
1006 x2 = x + minorinc * j;
1007 m_pSGP->moveAbs (x2, xaxispos);
1008 m_pSGP->lineAbs (x2, xaxispos + TICKRATIO * xticklen);
1011 if (xaxispos + xtl_ofs > ya_min && o_yaxis != NOAXIS) {
1012 double xw = xgw_min + i * xw_tickinc;
1013 double x = convertWorldToNDC_X (xw);
1014 double d = x - yaxispos;
1015 if (o_yticks == RIGHT && d >= 0 && d < 0.9 * xn_tickinc)
1017 if (o_yticks == LEFT && d <= 0 && d > -0.9 * xn_tickinc)
1021 if (o_xtlabel == TRUE && axis_near == FALSE) {
1022 snprintf (str, sizeof(str), x_numfmt, xgw_min + xw_tickinc * i);
1023 numstr = str_skip_head (str, " ");
1024 double xExtent, yExtent;
1025 m_pSGP->getTextExtent (numstr, &xExtent, &yExtent);
1026 m_pSGP->moveAbs (x - xExtent/2, xaxispos + xtl_ofs);
1027 m_pSGP->setTextColor (clr_number, -1);
1028 m_pSGP->drawText (numstr);
1038 if (o_yaxis == LINEAR) {
1040 m_pSGP->setColor (clr_axis);
1041 if (o_tag && !o_grid && !o_box && s_ycross) {
1042 m_pSGP->moveAbs (yaxispos - charwidth, ya_min);
1043 m_pSGP->lineAbs (yaxispos + charwidth, ya_min);
1045 m_pSGP->moveAbs (yaxispos, ya_min);
1046 m_pSGP->lineAbs (yaxispos, ya_max);
1047 if (o_tag && !o_grid && !o_box) {
1048 m_pSGP->moveAbs (yaxispos - charwidth, ya_max);
1049 m_pSGP->lineAbs (yaxispos + charwidth, ya_max);
1052 if (o_grid == TRUE) {
1053 m_pSGP->setColor (clr_grid);
1054 for (i = 0; i <= y_nint; i++) {
1055 y = yt_min + yn_tickinc * i;
1056 m_pSGP->moveAbs (xa_max, y);
1057 m_pSGP->lineAbs (xa_min, y);
1061 m_pSGP->setTextAngle (HALFPI);
1062 m_pSGP->setTextSize (1.5 * charheight);
1063 m_pSGP->setTextColor (clr_label, -1);
1065 double xExtent, yExtent;
1067 m_pSGP->getTextExtent (c_ylabel.c_str(), &xExtent, &yExtent);
1068 m_pSGP->moveAbs (ylbl_col, ya_min + (ya_max-ya_min)/2 - yExtent / 2);
1069 m_pSGP->drawText (c_ylabel);
1071 m_pSGP->setTextAngle (0.0);
1072 m_pSGP->setTextSize (charheight);
1073 minorinc = yn_tickinc / (o_yminortick + 1);
1075 for (i = 0; i <= y_nint; i++) {
1076 y = yt_min + yn_tickinc * i;
1077 m_pSGP->setColor (clr_axis);
1078 m_pSGP->moveAbs (yaxispos, y);
1079 m_pSGP->lineAbs (yaxispos + yticklen, y);
1081 for (j = 1; j <= o_yminortick; j++) {
1082 y2 = y + minorinc * j;
1083 m_pSGP->moveAbs (yaxispos, y2);
1084 m_pSGP->lineAbs (yaxispos + TICKRATIO * yticklen, y2);
1087 if (yaxispos + ytl_ofs > xa_min && o_xaxis != NOAXIS) {
1088 double yw = ygw_min + i * yw_tickinc;
1089 double y = convertWorldToNDC_Y (yw);
1090 double d = y - xaxispos;
1091 if (o_xticks == ABOVE && d >= 0 && d < 0.9 * yn_tickinc)
1093 if (o_xticks == BELOW && d <= 0 && d > -0.9 * yn_tickinc)
1096 if (o_ytlabel == TRUE && axis_near == FALSE) {
1097 snprintf (str, sizeof(str), y_numfmt, ygw_min + yw_tickinc * i);
1098 double xExtent, yExtent;
1099 m_pSGP->getTextExtent (str, &xExtent, &yExtent);
1100 if (o_yticks == LEFT)
1101 m_pSGP->moveAbs (yaxispos - 1.5 * charwidth - xExtent, y + 0.5 * yExtent);
1103 m_pSGP->moveAbs (yaxispos + 1.5 * charwidth, y + 0.5 * yExtent);
1104 m_pSGP->setTextColor (clr_number, -1);
1105 m_pSGP->drawText (str);
1113 EZPlot::symbol (int sym, double symwidth, double symheight)
1118 if (sym == SB_CROSS) {
1119 m_pSGP->moveRel (-0.5 * symwidth, -0.5 * symheight);
1120 m_pSGP->lineRel (symwidth, symheight);
1121 m_pSGP->moveRel (-symwidth, 0.0);
1122 m_pSGP->lineRel (symwidth, -symheight);
1123 m_pSGP->moveRel (-0.5 * symwidth, 0.5 * symheight);
1124 } else if (sym == SB_PLUS) {
1125 m_pSGP->moveRel (-0.5 * symwidth, 0.0);
1126 m_pSGP->lineRel (symwidth, 0.0);
1127 m_pSGP->moveRel (-0.5 * symwidth, -0.5 * symheight);
1128 m_pSGP->lineRel (0.0, symheight);
1129 m_pSGP->moveRel (0.0, -0.5 * symheight);
1130 } else if (sym == SB_BOX) {
1131 m_pSGP->moveRel (-0.5 * symwidth, -0.5 * symheight);
1132 m_pSGP->lineRel (symwidth, 0.0);
1133 m_pSGP->lineRel (0.0, symheight);
1134 m_pSGP->lineRel (-symwidth, 0.0);
1135 m_pSGP->lineRel (0.0, -symheight);
1136 m_pSGP->moveRel (0.5 * symwidth, 0.5 * symheight);
1137 } else if (sym == SB_CIRCLE) {
1138 m_pSGP->drawCircle (symwidth);
1139 } else if (sym == SB_ERRORBAR) {
1140 m_pSGP->moveRel (-0.5 * symwidth, 0.5 * symheight);
1141 m_pSGP->lineRel (symwidth, 0.0);
1142 m_pSGP->moveRel (-0.5 * symwidth, 0.0);
1143 m_pSGP->lineRel (0.0, -symheight);
1144 m_pSGP->moveRel (-0.5 * symwidth, 0.0);
1145 m_pSGP->lineRel (symwidth, 0.0);
1146 m_pSGP->moveRel (-0.5 * symwidth, 0.5 * symheight);
1153 * axis_scale calculates graph axis scaling
1156 * retval = axis_scale (min, max, nint, minp, maxp, nintp,
1157 * rec_total, rec_frac)
1160 * double min Smallest value to plot
1161 * double max Largest value to plot
1162 * int nint Number of intervals desired
1165 * int retval FALSE if illegal parameters, else TRUE
1166 * double *minp Minimum graph value
1167 * double *maxp Maximum graph value
1168 * int *nintp Number of intervals for graph
1169 * int *rec_total Recommended field width for printing out the number
1170 * int *rec_frac Recommended number of digits for print fraction
1174 EZPlot::axis_scale (double min, double max, int nint, double *minp, double *maxp, int *nintp)
1176 if (min >= max || nint < 1) {
1177 sys_error (ERR_WARNING, "Invalid params: min=%lf, max=%lf, num intervals=%d [axis_scale]", min, max, nint);
1182 double a = fabs(min);
1183 if (fabs(min) < fabs(max))
1185 double scale = pow (10.0, floor(log10(a)));
1187 double mina = min / scale;
1188 double maxa = max / scale;
1189 double d = (maxa - mina) / nint;
1191 double e = floor (log10(d));
1192 double f = d / pow (10.0, e);
1196 else if (f < sqrt (10.0))
1198 else if (f < sqrt (50.0))
1200 double wdt = v * pow (10.0, e);
1201 double g = floor (mina / wdt);
1202 if (fabs(g + 1 - mina / wdt) < j)
1210 double h = floor (maxa / wdt) + 1.0;
1211 if (fabs(maxa / wdt + 1 - h) < j)
1217 *nintp = static_cast<int>(h - g);
1218 if (fabs(*maxp) >= 10.0 || fabs(*minp) >= 10.0) {
1219 scale = scale * 10.0;
1231 * make_numfmt Make a numeric format string
1234 * make_numfmt (fmtstr, fldwid, nfrac, min, max, nint)
1235 * char *fmtstr Returned format string for printf()
1236 * int *fldwid Returned field width
1237 * int *nfrac If < 0, then calculate best number of
1238 * fraction places & return that value
1239 * If >= 0, then use that number of places
1240 * double min Minimum value
1241 * double max Maximum value
1242 * int nint Number of intervals between min & max
1245 * This routine is written as an INTERNAL routine for EZPLOT
1248 static inline double
1259 EZPlot::make_numfmt (char *fmtstr, int *fldwid, int *nfrac, double minval, double maxval, int nint)
1261 int wid, frac, expon;
1263 double delta = (maxval - minval) / nint;
1264 double absmin = fabs(minval);
1265 double absmax = fabs(maxval);
1267 if (absmin > absmax)
1270 double logt = log10( absmax );
1272 if (fabs(logt) >= 6) { // use exponential format
1273 if (fabs(logt) > 99)
1278 if (*nfrac < 0) { // calculate frac
1279 delta /= pow (10., floor(logt)); // scale delta
1280 frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
1282 frac = 1; // to be safe, add decimal pt
1283 } else // use users' frac
1286 wid = 2 + frac + expon;
1287 if (minval < 0. || maxval < 0.)
1289 sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "g");
1290 } else { // use fixed format
1291 wid = static_cast<int>(trunc(logt)) + 1;
1294 if (minval < 0. || maxval < 0.)
1297 if (*nfrac < 0) { // calculate frac
1298 if (delta >= 0.999999)
1299 frac = 1; // add a decimal pt to be safe
1301 frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
1302 } else // use users' frac
1306 sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "f");