1 /*****************************************************************************
6 ** This is part of the CTSim program
7 ** Copyright (c) 1983-2001 Kevin Rosenberg
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)
469 if (m_vecCurves.size() <= 0)
474 m_pSGP->setWindow (0., 0., 1., 1.);
476 if (s_textsize == TRUE)
477 m_pSGP->setTextPointSize (v_textsize);
479 charheight = m_pSGP->getCharHeight();
480 charwidth = m_pSGP->getCharWidth();
481 double symheight = charheight * 0.3; // size of symbol in NDC
482 double symwidth = symheight;
484 const EZPlotCurve& firstCurve = *m_vecCurves[0];
486 double xmin = firstCurve.x[0]; // extent of curves in world coord
488 double ymin = firstCurve.y[0];
492 for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
493 const EZPlotCurve* const pCurve = m_vecCurves [iCurve];
495 for (int ip = 0; ip < pCurve->m_iPointCount; ip++) {
496 if (pCurve->x[ip] > xmax)
497 xmax = pCurve->x[ip];
498 else if (pCurve->x[ip] < xmin)
499 xmin = pCurve->x[ip];
500 if (pCurve->y[ip] > ymax)
501 ymax = pCurve->y[ip];
502 else if (pCurve->y[ip] < ymin)
503 ymin = pCurve->y[ip];
507 // extend graph limits for user defined axis cross positions
508 if (s_xcross == TRUE) {
511 else if (v_xcross > xmax)
515 if (s_ycross == TRUE) {
518 else if (v_ycross > ymax)
522 // find nice endpoints for axes
523 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))
526 // check if user set x-axis extents
527 if (s_xmin == TRUE) {
529 x_nint = o_xmajortick - 1;
531 if (s_xmax == TRUE) {
533 x_nint = o_xmajortick - 1;
536 // check if user set y-axis extents
537 if (s_ymin == TRUE) {
539 y_nint = o_ymajortick - 1;
541 if (s_ymax == TRUE) {
543 y_nint = o_ymajortick - 1;
546 // calculate increment between major axis in world coordinates
547 xw_tickinc = (xgw_max - xgw_min) / x_nint;
548 yw_tickinc = (ygw_max - ygw_min) / y_nint;
550 // we have now calcuated xgw_min, xyw_max, ygw_min, & ygw_max
552 // set the number of decimal point to users' setting or default
553 // Two formats for numbers: Fixed: -nnn.f and Exponent: -n.fffE+eee
554 if (s_lxfrac == TRUE)
559 if (s_lyfrac == TRUE)
564 make_numfmt (x_numfmt, &x_fldwid, &x_frac, xgw_min, xgw_max, x_nint);
565 make_numfmt (y_numfmt, &y_fldwid, &y_frac, ygw_min, ygw_max, y_nint);
567 xtl_wid = x_fldwid * charwidth; // calc size of tick labels
568 ytl_wid = y_fldwid * charwidth;
569 tl_height = charheight;
571 // calculate the extent of the plot frame
574 xp_max = xp_min + o_xlength;
575 yp_max = yp_min + o_ylength;
577 xp_min = clamp (xp_min, 0., 1.);
578 xp_max = clamp (xp_max, 0., 1.);
579 yp_min = clamp (yp_min, 0., 1.);
580 yp_max = clamp (yp_max, 0., 1.);
582 xa_min = xp_min; // extent of axes
587 // adjust frame for title
590 if (c_title.length() > 0)
591 ya_max -= 2 * charheight;
595 ya_max -= 0.7 * charheight; // allow room for yaxis ticklabel
598 // calculate legend box boundaries
599 int max_leg = 0; // longest legend in characters
600 int num_leg = 0; // number of legend titles
602 for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
603 const std::string* pstrLegend = getLegend (iCurve);
605 if (pstrLegend && pstrLegend->length() > 0) {
607 int nLegend = pstrLegend->length();
610 if (nLegend > max_leg)
618 if (num_leg > 0 && o_legendbox != NOLEGEND) {
619 double leg_width = (max_leg + 2) * charwidth; // size of legend box
620 double leg_height = num_leg * 3 * charheight;
622 if (s_xlegend == TRUE)
626 if (o_legendbox == OUTSIDE)
627 xa_max -= (leg_width + 0.5 * charwidth);
629 xl_min = xl_max - leg_width;
631 if (s_ylegend == TRUE)
636 yl_min = yl_max - leg_height;
638 m_pSGP->setColor (clr_legend);
639 m_pSGP->drawRect (xl_min, yl_min, xl_max, yl_max);
641 int iLegend = 0; // current legend position
643 for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
644 const std::string* pstrLegend = getLegend (iCurve);
645 if (! pstrLegend || pstrLegend->length() == 0)
648 double xmin = xl_min + 1.0 * charwidth;
649 double xmax = xl_max - 1.0 * charwidth;
650 double y = yl_max - (2.0 + iLegend * 3) * charheight;
652 m_pSGP->moveAbs (xmin, y + 0.5 * charheight);
654 m_pSGP->drawText (pstrLegend->c_str());
655 m_pSGP->setColor (getColor (iCurve));
657 int iLS = getLinestyle (iCurve);
658 if (iLS != SGP::LS_NOLINE) {
659 m_pSGP->setLineStyle (iLS);
660 m_pSGP->moveAbs (xmin, y);
661 m_pSGP->lineAbs (xmax, y);
664 int iSymbol = getSymbol (iCurve);
666 double xinc = (xmax - xmin) / (5 - 1);
667 m_pSGP->setLineStyle (SGP::LS_SOLID);
668 for (int j = 0; j < 5; j++) {
669 m_pSGP->moveAbs (xmin + j * xinc, y);
670 symbol (iSymbol, symwidth, symheight);
673 ++iLegend; // move to next legend position
675 } // end legend printing
677 // calculate the extent of the axes
679 /*-------------------------*/
680 /* adjust frame for labels */
681 /*-------------------------*/
684 if (c_xlabel.length() > 0)
685 ya_min += 1.5 * charheight;
686 xlbl_row = xp_min; // put x-label on bottom of plot frame
689 if (c_ylabel.length() > 0) {
691 m_pSGP->setTextAngle (HALFPI);
693 m_pSGP->setTextSize (1.5 * charheight);
695 double xExtent, yExtent;
697 m_pSGP->getTextExtent (c_ylabel.c_str(), &xExtent, &yExtent);
699 m_pSGP->setTextSize (charheight);
703 m_pSGP->setTextAngle (0.0);
708 /*------------------------------*/
709 /* adjust frame for tick labels */
710 /*------------------------------*/
712 // Calc offset of tick labels from axes
713 if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
715 else if (o_xticks == BELOW)
716 xtl_ofs = -0.5 * charheight;
717 else if (o_xticks == ABOVE)
718 xtl_ofs = 0.5 * charheight;
720 if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
722 else if (o_yticks == LEFT) {
723 double xExtentMin, xExtentMax, yExtent;
725 snprintf (s, sizeof(s), y_numfmt, ymin);
726 m_pSGP->getTextExtent (s, &xExtentMin, &yExtent);
727 snprintf (s, sizeof(s), y_numfmt, ymax);
728 m_pSGP->getTextExtent (s, &xExtentMax, &yExtent);
729 if (xExtentMin > xExtentMax)
730 xExtentMax = xExtentMin;
731 ytl_ofs = -xExtentMax;
732 } else if (o_yticks == RIGHT)
733 ytl_ofs = 1.5 * charwidth;
735 xa_max -= 0.7 * x_fldwid * charwidth; // make room for last x tick label
742 // see if need to shrink axis extents and/or tick extents
743 if (xtl_ofs != 0.0 && s_ycross == FALSE) {
744 if (o_xticks == BELOW) {
745 ya_min += 1.5 * charheight;
747 } else if (o_xticks == ABOVE) {
749 yt_min = ya_min + 1.5 * charheight;
751 } else // noaxis, no t-labels, or user set cross
754 if (ytl_ofs != 0.0 && s_xcross == FALSE) {
755 if (o_yticks == LEFT) {
756 xa_min += 2*charwidth - ytl_ofs; // (2 + y_fldwid) * charwidth;
758 } else if (o_yticks == RIGHT) {
760 xt_min = xa_min + ytl_ofs; // + y_fldwid * charwidth;
765 // decrease size of graph, if necessary, to accommadate space
766 // between axis boundary and boundary of ticks
767 double x_added_ticks = 0; // number of tick spaces added to axis
768 double y_added_ticks = 0;
769 if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
771 if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
774 if (o_grid == TRUE) {
775 if (x_added_ticks < 0)
777 if (y_added_ticks < 0)
781 if (x_added_ticks < 0) {
782 if (o_yticks == LEFT || s_ycross)
788 if (y_added_ticks < 0) {
789 if (o_xticks == BELOW || s_xcross)
795 xn_tickinc = (xt_max - xt_min) / (x_nint + x_added_ticks);
796 yn_tickinc = (yt_max - yt_min) / (y_nint + y_added_ticks);
798 xt_min += 0.5 * x_added_ticks * xn_tickinc;
799 xt_max -= 0.5 * x_added_ticks * xn_tickinc;
800 yt_min += 0.5 * y_added_ticks * yn_tickinc;
801 yt_max -= 0.5 * y_added_ticks * yn_tickinc;
808 //------------------------------------------------------------------------
810 m_xWorldScale = (xgn_max - xgn_min) / (xgw_max - xgw_min);
811 m_yWorldScale = (ygn_max - ygn_min) / (ygw_max - ygw_min);
815 m_pSGP->setLineStyle (SGP::LS_SOLID);
821 clipRect[0] = xgn_min; clipRect[1] = ygn_min; clipRect[2] = xgn_max; clipRect[3] = ygn_max;
825 for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
826 const EZPlotCurve* const pCurve = m_vecCurves [iCurve];
829 m_pSGP->setColor (getColor (iCurve));
831 int iSym = getSymbol (iCurve);
833 int iLS = getLinestyle (iCurve);
837 if (iLS != SGP::LS_NOLINE) {
838 m_pSGP->setLineStyle (iLS);
839 double x1 = convertWorldToNDC_X (pCurve->x[0]);
840 double y1 = convertWorldToNDC_Y (pCurve->y[0]);
842 for (int i = 1; i < pCurve->m_iPointCount; i++) {
843 double x2 = convertWorldToNDC_X (pCurve->x[i]);
844 double y2 = convertWorldToNDC_Y (pCurve->y[i]);
848 if (clip_rect (x1, y1, x2Clip, y2Clip, clipRect)) {
849 m_pSGP->moveAbs (x1, y1);
850 m_pSGP->lineAbs (x2Clip, y2Clip);
857 int iSymFreq = getSymbolFreq (iCurve);
858 m_pSGP->setLineStyle (SGP::LS_SOLID);
859 double x = convertWorldToNDC_X (pCurve->x[0]);
860 double y = convertWorldToNDC_Y (pCurve->y[0]);
861 m_pSGP->moveAbs (x, y);
862 symbol (iSym, symwidth, symheight);
863 for (int i = 1; i < pCurve->m_iPointCount; i++)
864 if (i % iSymFreq == 0 || i == pCurve->m_iPointCount - 1) {
865 x = convertWorldToNDC_X (pCurve->x[i]);
866 y = convertWorldToNDC_Y (pCurve->y[i]);
868 if (y >= ygn_min && y <= ygn_max) {
869 m_pSGP->moveAbs (x, y);
870 symbol (iSym, symwidth, symheight);
879 * drawAxes INTERNAL routine to draw axis & label them
889 double xticklen = 0, yticklen = 0; // length of ticks in NDC
890 double minorinc; // increment between minor axes
891 double xaxispos, yaxispos; // crossing of axes
893 bool axis_near; // TRUE if axis too close to print t-label
898 m_pSGP->setTextSize (charheight);
899 m_pSGP->setTextColor (1, -1);
901 if (o_xticks == ABOVE)
902 xticklen = 0.5 * charheight;
903 else if (o_xticks == BELOW)
904 xticklen = -0.5 * charheight;
906 if (o_yticks == RIGHT)
907 yticklen = charwidth;
908 else if (o_yticks == LEFT)
909 yticklen = -charwidth;
911 if (c_title.length() > 0) {
913 m_pSGP->setTextSize (charheight * 2.0);
914 m_pSGP->getTextExtent (c_title.c_str(), &wText, &hText);
915 m_pSGP->moveAbs (xa_min + (xa_max-xa_min)/2 - wText/2, title_row);
916 m_pSGP->setTextColor (clr_title, -1);
917 m_pSGP->drawText (c_title);
918 m_pSGP->setTextSize (charheight);
921 if (o_grid == TRUE || o_box == TRUE) {
922 m_pSGP->setColor (clr_axis);
923 m_pSGP->moveAbs (xa_min, ya_min);
924 m_pSGP->lineAbs (xa_max, ya_min);
925 m_pSGP->lineAbs (xa_max, ya_max);
926 m_pSGP->lineAbs (xa_min, ya_max);
927 m_pSGP->lineAbs (xa_min, ya_min);
930 // calculate position of axes
933 if (s_ycross == TRUE) { // convert users' world-coord
934 xaxispos = convertWorldToNDC_Y (v_ycross);// axis to its position in NDC
935 x = convertWorldToNDC_X (xgw_min);
940 if (s_xcross == TRUE) { // convert users' world-coord
941 yaxispos = convertWorldToNDC_X (v_xcross);// axis to its NDC position
942 y = convertWorldToNDC_Y (ygw_min);
950 if (o_xaxis == LINEAR) {
953 m_pSGP->setColor (clr_axis);
954 if (o_tag && !o_grid && !o_box && s_xcross) {
955 m_pSGP->moveAbs (xa_min, xaxispos - charheight);
956 m_pSGP->lineAbs (xa_min, xaxispos + charheight);
958 m_pSGP->moveAbs (xa_min, xaxispos);
959 m_pSGP->lineAbs (xa_max, xaxispos);
960 if (o_tag && !o_grid && !o_box) {
961 m_pSGP->moveAbs (xa_max, xaxispos - charheight);
962 m_pSGP->lineAbs (xa_max, xaxispos + charheight);
965 if (o_grid == TRUE) {
966 m_pSGP->setColor (clr_grid);
967 for (i = 0; i <= x_nint; i++) {
968 m_pSGP->moveAbs (xt_min + xn_tickinc * i, ya_max);
969 m_pSGP->lineAbs (xt_min + xn_tickinc * i, ya_min);
972 m_pSGP->setTextSize (charheight * 1.5);
973 m_pSGP->setTextColor (clr_label, -1);
977 m_pSGP->getTextExtent (c_xlabel.c_str(), &wText, &hText);
978 m_pSGP->moveAbs (xa_min + (xa_max-xa_min)/2 - wText / 2, xlbl_row + charheight * 1.5);
980 m_pSGP->drawText (c_xlabel);
981 m_pSGP->setTextSize (charheight);
982 minorinc = xn_tickinc / (o_xminortick + 1);
984 for (i = 0; i <= x_nint; i++) {
985 x = xt_min + xn_tickinc * i;
986 m_pSGP->setColor (clr_axis);
987 m_pSGP->moveAbs (x, xaxispos);
988 m_pSGP->lineAbs (x, xaxispos + xticklen);
990 for (j = 1; j <= o_xminortick; j++) {
991 x2 = x + minorinc * j;
992 m_pSGP->moveAbs (x2, xaxispos);
993 m_pSGP->lineAbs (x2, xaxispos + TICKRATIO * xticklen);
996 if (xaxispos + xtl_ofs > ya_min && o_yaxis != NOAXIS) {
997 double xw = xgw_min + i * xw_tickinc;
998 double x = convertWorldToNDC_X (xw);
999 double d = x - yaxispos;
1000 if (o_yticks == RIGHT && d >= 0 && d < 0.9 * xn_tickinc)
1002 if (o_yticks == LEFT && d <= 0 && d > -0.9 * xn_tickinc)
1006 if (o_xtlabel == TRUE && axis_near == FALSE) {
1007 snprintf (str, sizeof(str), x_numfmt, xgw_min + xw_tickinc * i);
1008 numstr = str_skip_head (str, " ");
1009 double xExtent, yExtent;
1010 m_pSGP->getTextExtent (numstr, &xExtent, &yExtent);
1011 m_pSGP->moveAbs (x - xExtent/2, xaxispos + xtl_ofs);
1012 m_pSGP->setTextColor (clr_number, -1);
1013 m_pSGP->drawText (numstr);
1023 if (o_yaxis == LINEAR) {
1025 m_pSGP->setColor (clr_axis);
1026 if (o_tag && !o_grid && !o_box && s_ycross) {
1027 m_pSGP->moveAbs (yaxispos - charwidth, ya_min);
1028 m_pSGP->lineAbs (yaxispos + charwidth, ya_min);
1030 m_pSGP->moveAbs (yaxispos, ya_min);
1031 m_pSGP->lineAbs (yaxispos, ya_max);
1032 if (o_tag && !o_grid && !o_box) {
1033 m_pSGP->moveAbs (yaxispos - charwidth, ya_max);
1034 m_pSGP->lineAbs (yaxispos + charwidth, ya_max);
1037 if (o_grid == TRUE) {
1038 m_pSGP->setColor (clr_grid);
1039 for (i = 0; i <= y_nint; i++) {
1040 y = yt_min + yn_tickinc * i;
1041 m_pSGP->moveAbs (xa_max, y);
1042 m_pSGP->lineAbs (xa_min, y);
1046 m_pSGP->setTextAngle (HALFPI);
1047 m_pSGP->setTextSize (1.5 * charheight);
1048 m_pSGP->setTextColor (clr_label, -1);
1050 double xExtent, yExtent;
1052 m_pSGP->getTextExtent (c_ylabel.c_str(), &xExtent, &yExtent);
1053 m_pSGP->moveAbs (ylbl_col, ya_min + (ya_max-ya_min)/2 - yExtent / 2);
1054 m_pSGP->drawText (c_ylabel);
1056 m_pSGP->setTextAngle (0.0);
1057 m_pSGP->setTextSize (charheight);
1058 minorinc = yn_tickinc / (o_yminortick + 1);
1060 for (i = 0; i <= y_nint; i++) {
1061 y = yt_min + yn_tickinc * i;
1062 m_pSGP->setColor (clr_axis);
1063 m_pSGP->moveAbs (yaxispos, y);
1064 m_pSGP->lineAbs (yaxispos + yticklen, y);
1066 for (j = 1; j <= o_yminortick; j++) {
1067 y2 = y + minorinc * j;
1068 m_pSGP->moveAbs (yaxispos, y2);
1069 m_pSGP->lineAbs (yaxispos + TICKRATIO * yticklen, y2);
1072 if (yaxispos + ytl_ofs > xa_min && o_xaxis != NOAXIS) {
1073 double yw = ygw_min + i * yw_tickinc;
1074 double y = convertWorldToNDC_Y (yw);
1075 double d = y - xaxispos;
1076 if (o_xticks == ABOVE && d >= 0 && d < 0.9 * yn_tickinc)
1078 if (o_xticks == BELOW && d <= 0 && d > -0.9 * yn_tickinc)
1081 if (o_ytlabel == TRUE && axis_near == FALSE) {
1082 snprintf (str, sizeof(str), y_numfmt, ygw_min + yw_tickinc * i);
1083 double xExtent, yExtent;
1084 m_pSGP->getTextExtent (str, &xExtent, &yExtent);
1085 if (o_yticks == LEFT)
1086 m_pSGP->moveAbs (yaxispos - 1.5 * charwidth - xExtent, y + 0.5 * yExtent);
1088 m_pSGP->moveAbs (yaxispos + 1.5 * charwidth, y + 0.5 * yExtent);
1089 m_pSGP->setTextColor (clr_number, -1);
1090 m_pSGP->drawText (str);
1098 EZPlot::symbol (int sym, double symwidth, double symheight)
1103 if (sym == SB_CROSS) {
1104 m_pSGP->markerRel (0, 0);
1105 // m_pSGP->moveRel (-0.5 * symwidth, -0.5 * symheight);
1106 // m_pSGP->lineRel (symwidth, symheight);
1107 // m_pSGP->moveRel (-symwidth, 0.0);
1108 // m_pSGP->lineRel (symwidth, -symheight);
1109 // m_pSGP->moveRel (-0.5 * symwidth, 0.5 * symheight);
1110 } else if (sym == SB_PLUS) {
1111 m_pSGP->moveRel (-0.5 * symwidth, 0.0);
1112 m_pSGP->lineRel (symwidth, 0.0);
1113 m_pSGP->moveRel (-0.5 * symwidth, -0.5 * symheight);
1114 m_pSGP->lineRel (0.0, symheight);
1115 m_pSGP->moveRel (0.0, -0.5 * symheight);
1116 } else if (sym == SB_BOX) {
1117 m_pSGP->moveRel (-0.5 * symwidth, -0.5 * symheight);
1118 m_pSGP->lineRel (symwidth, 0.0);
1119 m_pSGP->lineRel (0.0, symheight);
1120 m_pSGP->lineRel (-symwidth, 0.0);
1121 m_pSGP->lineRel (0.0, -symheight);
1122 m_pSGP->moveRel (0.5 * symwidth, 0.5 * symheight);
1123 } else if (sym == SB_CIRCLE) {
1124 m_pSGP->drawCircle (symwidth);
1125 } else if (sym == SB_ERRORBAR) {
1126 m_pSGP->moveRel (-0.5 * symwidth, 0.5 * symheight);
1127 m_pSGP->lineRel (symwidth, 0.0);
1128 m_pSGP->moveRel (-0.5 * symwidth, 0.0);
1129 m_pSGP->lineRel (0.0, -symheight);
1130 m_pSGP->moveRel (-0.5 * symwidth, 0.0);
1131 m_pSGP->lineRel (symwidth, 0.0);
1132 m_pSGP->moveRel (-0.5 * symwidth, 0.5 * symheight);
1133 } else if (sym == SB_POINT) {
1134 m_pSGP->pointRel (0, 0);
1141 * axis_scale calculates graph axis scaling
1144 * retval = axis_scale (min, max, nint, minp, maxp, nintp,
1145 * rec_total, rec_frac)
1148 * double min Smallest value to plot
1149 * double max Largest value to plot
1150 * int nint Number of intervals desired
1153 * int retval FALSE if illegal parameters, else TRUE
1154 * double *minp Minimum graph value
1155 * double *maxp Maximum graph value
1156 * int *nintp Number of intervals for graph
1157 * int *rec_total Recommended field width for printing out the number
1158 * int *rec_frac Recommended number of digits for print fraction
1162 EZPlot::axis_scale (double min, double max, int nint, double *minp, double *maxp, int *nintp)
1164 if (min >= max || nint < 1) {
1165 sys_error (ERR_WARNING, "Invalid params: min=%lf, max=%lf, num intervals=%d [axis_scale]", min, max, nint);
1170 double a = fabs(min);
1171 if (fabs(min) < fabs(max))
1173 double scale = pow (10.0, floor(log10(a)));
1175 double mina = min / scale;
1176 double maxa = max / scale;
1177 double d = (maxa - mina) / nint;
1179 double e = floor (log10(d));
1180 double f = d / pow (10.0, e);
1184 else if (f < sqrt (10.0))
1186 else if (f < sqrt (50.0))
1188 double wdt = v * pow (10.0, e);
1189 double g = floor (mina / wdt);
1190 if (fabs(g + 1 - mina / wdt) < j)
1198 double h = floor (maxa / wdt) + 1.0;
1199 if (fabs(maxa / wdt + 1 - h) < j)
1205 *nintp = static_cast<int>(h - g);
1206 if (fabs(*maxp) >= 10.0 || fabs(*minp) >= 10.0) {
1207 scale = scale * 10.0;
1219 * make_numfmt Make a numeric format string
1222 * make_numfmt (fmtstr, fldwid, nfrac, min, max, nint)
1223 * char *fmtstr Returned format string for printf()
1224 * int *fldwid Returned field width
1225 * int *nfrac If < 0, then calculate best number of
1226 * fraction places & return that value
1227 * If >= 0, then use that number of places
1228 * double min Minimum value
1229 * double max Maximum value
1230 * int nint Number of intervals between min & max
1233 * This routine is written as an INTERNAL routine for EZPLOT
1236 static inline double
1247 EZPlot::make_numfmt (char *fmtstr, int *fldwid, int *nfrac, double minval, double maxval, int nint)
1249 int wid, frac, expon;
1251 double delta = (maxval - minval) / nint;
1252 double absmin = fabs(minval);
1253 double absmax = fabs(maxval);
1255 if (absmin > absmax)
1258 double logt = log10( absmax );
1260 if (fabs(logt) >= 6) { // use exponential format
1261 if (fabs(logt) > 99)
1266 if (*nfrac < 0) { // calculate frac
1267 delta /= pow (10., floor(logt)); // scale delta
1268 frac = static_cast<int>(fabs(my_trunc(log10(delta)))) + 1;
1270 frac = 1; // to be safe, add decimal pt
1271 } else // use users' frac
1274 wid = 2 + frac + expon;
1275 if (minval < 0. || maxval < 0.)
1277 sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "g");
1278 } else { // use fixed format
1279 wid = static_cast<int>(my_trunc(logt)) + 1;
1282 if (minval < 0. || maxval < 0.)
1285 if (*nfrac < 0) { // calculate frac
1286 if (delta >= 0.999999)
1287 frac = 1; // add a decimal pt to be safe
1289 frac = static_cast<int>(fabs(my_trunc(log10(delta)))) + 1;
1290 } else // use users' frac
1294 sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "f");