1 /*****************************************************************************
6 ** This is part of the CTSim program
7 ** Copyright (C) 1983-2000 Kevin Rosenberg
9 ** $Id: ezplot.cpp,v 1.29 2001/01/12 16:41:56 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());
141 m_pol.addSkipWord ("please");
143 m_pol.addSkipWord ("use");
145 m_pol.addSkipWord ("are");
147 m_pol.addSkipWord ("and");
149 m_pol.addSkipWord ("is");
151 m_pol.addSkipWord ("the");
153 m_pol.addSkipWord ("equals");
155 m_pol.addSkipChar ('=');
159 m_pol.usefile (POL::P_USE_STR,"");
161 m_pol.set_inputline ("!eoc ,");
173 EZPlot::initPlotSettings ()
175 m_iCurrentCurve = -1;
200 o_color = DEF_CURVE_CLR;
203 o_linestyle = SGP::LS_SOLID;
210 o_legendbox = INSIDE;
223 clr_axis = C_LTGRAY; // set fixed colors
227 clr_number = C_GREEN;
233 EZPlot::setColor (unsigned int iCurve, int iColor)
237 if (m_veciColor.size() <= iCurve) {
239 m_veciColor.resize ((m_iCurrentCurve + 1) * 2);
241 m_vecbColorSet.resize ((m_iCurrentCurve + 1) * 2);
245 m_veciColor [iCurve] = iColor;
247 m_vecbColorSet [iCurve] = true;
255 EZPlot::setSymbol (unsigned int iCurve, int iSymbol)
259 if (m_veciSymbol.size() <= iCurve) {
261 m_veciSymbol.resize ((m_iCurrentCurve + 1) * 2);
263 m_vecbSymbolSet.resize ((m_iCurrentCurve + 1) * 2);
267 m_veciSymbol [iCurve] = iSymbol;
269 m_vecbSymbolSet [iCurve] = true;
277 EZPlot::setSymbolFreq (unsigned int iCurve, int iSymbolFreq)
281 if (m_veciSymbolFreq.size() <= iCurve) {
283 m_veciSymbolFreq.resize ((m_iCurrentCurve + 1) * 2);
285 m_vecbSymbolFreqSet.resize ((m_iCurrentCurve + 1) * 2);
289 m_veciSymbolFreq [iCurve] = iSymbolFreq;
291 m_vecbSymbolFreqSet [iCurve] = true;
299 EZPlot::setLinestyle (unsigned int iCurve, int iLinestyle)
303 if (m_veciLinestyle.size() <= iCurve) {
305 m_veciLinestyle.resize ((m_iCurrentCurve + 1) * 2);
307 m_vecbLinestyleSet.resize ((m_iCurrentCurve + 1) * 2);
311 m_veciLinestyle [iCurve] = iLinestyle;
313 m_vecbLinestyleSet [iCurve] = true;
321 EZPlot::setLegend (unsigned int iCurve, const std::string& strLegend)
325 if (m_vecsLegend.size() <= iCurve) {
327 m_vecsLegend.resize ((m_iCurrentCurve + 1) * 2);
329 m_vecbLegendSet.resize ((m_iCurrentCurve + 1) * 2);
333 m_vecsLegend [iCurve] = strLegend;
335 m_vecbLegendSet [iCurve] = true;
343 EZPlot::setLegend (unsigned int iCurve, const char* const pszLegend)
347 if (m_vecsLegend.size() <= iCurve) {
349 m_vecsLegend.resize ((m_iCurrentCurve + 1) * 2);
351 m_vecbLegendSet.resize ((m_iCurrentCurve + 1) * 2);
355 m_vecsLegend [iCurve] = pszLegend;
357 m_vecbLegendSet [iCurve] = true;
365 EZPlot::getColor (unsigned int iCurve) const
369 if (m_veciColor.size() > iCurve && m_vecbColorSet[iCurve])
371 return m_veciColor[iCurve];
383 EZPlot::getSymbol (unsigned int iCurve) const
387 if (m_veciSymbol.size() > iCurve && m_vecbSymbolSet[iCurve])
389 return m_veciSymbol[iCurve];
401 EZPlot::getSymbolFreq (unsigned int iCurve) const
405 if (m_veciSymbolFreq.size() > iCurve && m_vecbSymbolFreqSet[iCurve])
407 return m_veciSymbolFreq[iCurve];
419 EZPlot::getLinestyle (unsigned int iCurve) const
423 if (m_veciLinestyle.size() > iCurve && m_vecbLinestyleSet[iCurve])
425 return m_veciLinestyle[iCurve];
437 EZPlot::getLegend (unsigned int iCurve) const
441 if (m_vecsLegend.size() > iCurve && m_vecbLegendSet[iCurve])
443 return &m_vecsLegend[iCurve];
456 * plot Plots all curves collected by addCurves ()
462 * This routine plots the curves that have stored by addCurves().
465 * drawAxes() & make_numfmt()
469 EZPlot::plot (SGP* pSGP)
472 if (m_vecCurves.size() <= 0)
479 m_pSGP->setWindow (0., 0., 1., 1.);
481 if (s_textsize == TRUE)
482 m_pSGP->setTextPointSize (v_textsize);
484 charheight = m_pSGP->getCharHeight();
485 charwidth = m_pSGP->getCharWidth();
487 const EZPlotCurve& firstCurve = *m_vecCurves[0];
489 double xmin = firstCurve.x[0]; // extent of curves in world coord
491 double ymin = firstCurve.y[0];
495 for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
496 const EZPlotCurve* const pCurve = m_vecCurves [iCurve];
498 for (int ip = 0; ip < pCurve->m_iPointCount; ip++) {
499 if (pCurve->x[ip] > xmax)
500 xmax = pCurve->x[ip];
501 else if (pCurve->x[ip] < xmin)
502 xmin = pCurve->x[ip];
503 if (pCurve->y[ip] > ymax)
504 ymax = pCurve->y[ip];
505 else if (pCurve->y[ip] < ymin)
506 ymin = pCurve->y[ip];
510 // extend graph limits for user defined axis cross positions
511 if (s_xcross == TRUE) {
514 else if (v_xcross > xmax)
518 if (s_ycross == TRUE) {
521 else if (v_ycross > ymax)
525 // find nice endpoints for axes
526 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))
529 // check if user set x-axis extents
530 if (s_xmin == TRUE) {
532 x_nint = o_xmajortick - 1;
534 if (s_xmax == TRUE) {
536 x_nint = o_xmajortick - 1;
539 // check if user set y-axis extents
540 if (s_ymin == TRUE) {
542 y_nint = o_ymajortick - 1;
544 if (s_ymax == TRUE) {
546 y_nint = o_ymajortick - 1;
549 // calculate increment between major axis in world coordinates
550 xw_tickinc = (xgw_max - xgw_min) / x_nint;
551 yw_tickinc = (ygw_max - ygw_min) / y_nint;
553 // we have now calcuated xgw_min, xyw_max, ygw_min, & ygw_max
555 // set the number of decimal point to users' setting or default
556 // Two formats for numbers: Fixed: -nnn.f and Exponent: -n.fffE+eee
557 if (s_lxfrac == TRUE)
562 if (s_lyfrac == TRUE)
567 make_numfmt (x_numfmt, &x_fldwid, &x_frac, xgw_min, xgw_max, x_nint);
568 make_numfmt (y_numfmt, &y_fldwid, &y_frac, ygw_min, ygw_max, y_nint);
570 xtl_wid = x_fldwid * charwidth; // calc size of tick labels
571 ytl_wid = y_fldwid * charwidth;
572 tl_height = charheight;
574 // calculate the extent of the plot frame
577 xp_max = xp_min + o_xlength;
578 yp_max = yp_min + o_ylength;
580 xp_min = clamp (xp_min, 0., 1.);
581 xp_max = clamp (xp_max, 0., 1.);
582 yp_min = clamp (yp_min, 0., 1.);
583 yp_max = clamp (yp_max, 0., 1.);
585 xa_min = xp_min; // extent of axes
590 // adjust frame for title
593 if (c_title.length() > 0)
594 ya_max -= 2 * charheight;
598 ya_max -= 0.7 * charheight; // allow room for yaxis ticklabel
601 // calculate legend box boundaries
602 int max_leg = 0; // longest legend in characters
603 int num_leg = 0; // number of legend titles
605 for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
606 const std::string* pstrLegend = getLegend (iCurve);
608 if (pstrLegend && pstrLegend->length() > 0) {
610 int nLegend = pstrLegend->length();
613 if (nLegend > max_leg)
621 if (num_leg > 0 && o_legendbox != NOLEGEND) {
622 double leg_width = (max_leg + 2) * charwidth; // size of legend box
623 double leg_height = num_leg * 3 * charheight;
625 if (s_xlegend == TRUE)
629 if (o_legendbox == OUTSIDE)
630 xa_max -= (leg_width + 0.5 * charwidth);
632 xl_min = xl_max - leg_width;
634 if (s_ylegend == TRUE)
639 yl_min = yl_max - leg_height;
641 m_pSGP->setColor (clr_legend);
642 m_pSGP->drawRect (xl_min, yl_min, xl_max, yl_max);
644 int iLegend = 0; // current legend position
646 for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
647 const std::string* pstrLegend = getLegend (iCurve);
648 if (! pstrLegend || pstrLegend->length() == 0)
651 double xmin = xl_min + 1.0 * charwidth;
652 double xmax = xl_max - 1.0 * charwidth;
653 double y = yl_max - (2.0 + iLegend * 3) * charheight;
655 m_pSGP->moveAbs (xmin, y + 0.5 * charheight);
657 m_pSGP->drawText (pstrLegend->c_str());
658 m_pSGP->setColor (getColor (iCurve));
660 int iLS = getLinestyle (iCurve);
661 if (iLS != SGP::LS_NOLINE) {
662 m_pSGP->setLineStyle (iLS);
663 m_pSGP->moveAbs (xmin, y);
664 m_pSGP->lineAbs (xmax, y);
667 int iSymbol = getSymbol (iCurve);
669 double xinc = (xmax - xmin) / (5 - 1);
670 m_pSGP->setLineStyle (SGP::LS_SOLID);
671 for (int j = 0; j < 5; j++) {
672 m_pSGP->moveAbs (xmin + j * xinc, y);
673 symbol(iSymbol, 0.5 * charwidth, 0.5 * charheight);
676 ++iLegend; // move to next legend position
678 } // end legend printing
680 // calculate the extent of the axes
682 /*-------------------------*/
683 /* adjust frame for labels */
684 /*-------------------------*/
687 if (c_xlabel.length() > 0)
688 ya_min += 1.5 * charheight;
689 xlbl_row = xp_min; // put x-label on bottom of plot frame
692 if (c_ylabel.length() > 0) {
694 m_pSGP->setTextAngle (HALFPI);
696 m_pSGP->setTextSize (1.5 * charheight);
698 double xExtent, yExtent;
700 m_pSGP->getTextExtent (c_ylabel.c_str(), &xExtent, &yExtent);
702 m_pSGP->setTextSize (charheight);
706 m_pSGP->setTextAngle (0.0);
711 /*------------------------------*/
712 /* adjust frame for tick labels */
713 /*------------------------------*/
715 // Calc offset of tick labels from axes
716 if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
718 else if (o_xticks == BELOW)
719 xtl_ofs = -0.5 * charheight;
720 else if (o_xticks == ABOVE)
721 xtl_ofs = 0.5 * charheight;
723 if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
725 else if (o_yticks == LEFT) {
726 double xExtentMin, xExtentMax, yExtent;
728 snprintf (s, sizeof(s), y_numfmt, ymin);
729 m_pSGP->getTextExtent (s, &xExtentMin, &yExtent);
730 snprintf (s, sizeof(s), y_numfmt, ymax);
731 m_pSGP->getTextExtent (s, &xExtentMax, &yExtent);
732 if (xExtentMin > xExtentMax)
733 xExtentMax = xExtentMin;
734 ytl_ofs = -xExtentMax;
735 } else if (o_yticks == RIGHT)
736 ytl_ofs = 1.5 * charwidth;
738 xa_max -= 0.7 * x_fldwid * charwidth; // make room for last x tick label
745 // see if need to shrink axis extents and/or tick extents
746 if (xtl_ofs != 0.0 && s_ycross == FALSE) {
747 if (o_xticks == BELOW) {
748 ya_min += 1.5 * charheight;
750 } else if (o_xticks == ABOVE) {
752 yt_min = ya_min + 1.5 * charheight;
754 } else // noaxis, no t-labels, or user set cross
757 if (ytl_ofs != 0.0 && s_xcross == FALSE) {
758 if (o_yticks == LEFT) {
759 xa_min += 2*charwidth - ytl_ofs; // (2 + y_fldwid) * charwidth;
761 } else if (o_yticks == RIGHT) {
763 xt_min = xa_min + ytl_ofs; // + y_fldwid * charwidth;
768 // decrease size of graph, if necessary, to accommadate space
769 // between axis boundary and boundary of ticks
770 double x_added_ticks = 0; // number of tick spaces added to axis
771 double y_added_ticks = 0;
772 if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
774 if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
777 if (o_grid == TRUE) {
778 if (x_added_ticks < 0)
780 if (y_added_ticks < 0)
784 if (x_added_ticks < 0) {
785 if (o_yticks == LEFT || s_ycross)
791 if (y_added_ticks < 0) {
792 if (o_xticks == BELOW || s_xcross)
798 xn_tickinc = (xt_max - xt_min) / (x_nint + x_added_ticks);
799 yn_tickinc = (yt_max - yt_min) / (y_nint + y_added_ticks);
801 xt_min += 0.5 * x_added_ticks * xn_tickinc;
802 xt_max -= 0.5 * x_added_ticks * xn_tickinc;
803 yt_min += 0.5 * y_added_ticks * yn_tickinc;
804 yt_max -= 0.5 * y_added_ticks * yn_tickinc;
811 //------------------------------------------------------------------------
813 m_xWorldScale = (xgn_max - xgn_min) / (xgw_max - xgw_min);
814 m_yWorldScale = (ygn_max - ygn_min) / (ygw_max - ygw_min);
818 m_pSGP->setLineStyle (SGP::LS_SOLID);
821 // size of symbol in NDC
822 double symwidth = charwidth;
823 double symheight = charheight;
828 clipRect[0] = xgn_min; clipRect[1] = ygn_min; clipRect[2] = xgn_max; clipRect[3] = ygn_max;
832 for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
833 const EZPlotCurve* const pCurve = m_vecCurves [iCurve];
836 m_pSGP->setColor (getColor (iCurve));
838 int iSym = getSymbol (iCurve);
840 int iLS = getLinestyle (iCurve);
844 if (iLS != SGP::LS_NOLINE) {
845 m_pSGP->setLineStyle (iLS);
846 double x1 = convertWorldToNDC_X (pCurve->x[0]);
847 double y1 = convertWorldToNDC_Y (pCurve->y[0]);
849 for (int i = 1; i < pCurve->m_iPointCount; i++) {
850 double x2 = convertWorldToNDC_X (pCurve->x[i]);
851 double y2 = convertWorldToNDC_Y (pCurve->y[i]);
857 if (clip_rect (x1, y1, x2Clip, y2Clip, clipRect)) {
859 m_pSGP->moveAbs (x1, y1);
861 m_pSGP->lineAbs (x2Clip, y2Clip);
873 int iSymFreq = getSymbolFreq (iCurve);
874 m_pSGP->setLineStyle (SGP::LS_SOLID);
875 double x = convertWorldToNDC_X (pCurve->x[0]);
876 double y = convertWorldToNDC_Y (pCurve->y[0]);
877 m_pSGP->moveAbs (x, y);
878 symbol (iSym, symwidth, symheight);
879 for (int i = 1; i < pCurve->m_iPointCount; i++)
880 if (i % iSymFreq == 0 || i == pCurve->m_iPointCount - 1) {
881 x = convertWorldToNDC_X (pCurve->x[i]);
882 y = convertWorldToNDC_Y (pCurve->y[i]);
884 if (y >= ygn_min && y <= ygn_max) {
885 m_pSGP->moveAbs (x, y);
886 symbol (iSym, symwidth, symheight);
896 * drawAxes INTERNAL routine to draw axis & label them
906 double xticklen = 0, yticklen = 0; // length of ticks in NDC
907 double minorinc; // increment between minor axes
908 double xaxispos, yaxispos; // crossing of axes
910 bool axis_near; // TRUE if axis too close to print t-label
915 m_pSGP->setTextSize (charheight);
916 m_pSGP->setTextColor (1, -1);
918 if (o_xticks == ABOVE)
919 xticklen = 0.5 * charheight;
920 else if (o_xticks == BELOW)
921 xticklen = -0.5 * charheight;
923 if (o_yticks == RIGHT)
924 yticklen = charwidth;
925 else if (o_yticks == LEFT)
926 yticklen = -charwidth;
928 if (c_title.length() > 0) {
930 m_pSGP->setTextSize (charheight * 2.0);
931 m_pSGP->getTextExtent (c_title.c_str(), &wText, &hText);
932 m_pSGP->moveAbs (xa_min + (xa_max-xa_min)/2 - wText/2, title_row);
933 m_pSGP->setTextColor (clr_title, -1);
934 m_pSGP->drawText (c_title);
935 m_pSGP->setTextSize (charheight);
938 if (o_grid == TRUE || o_box == TRUE) {
939 m_pSGP->setColor (clr_axis);
940 m_pSGP->moveAbs (xa_min, ya_min);
941 m_pSGP->lineAbs (xa_max, ya_min);
942 m_pSGP->lineAbs (xa_max, ya_max);
943 m_pSGP->lineAbs (xa_min, ya_max);
944 m_pSGP->lineAbs (xa_min, ya_min);
947 // calculate position of axes
950 if (s_ycross == TRUE) { // convert users' world-coord
951 xaxispos = convertWorldToNDC_Y (v_ycross);// axis to its position in NDC
952 x = convertWorldToNDC_X (xgw_min);
957 if (s_xcross == TRUE) { // convert users' world-coord
958 yaxispos = convertWorldToNDC_X (v_xcross);// axis to its NDC position
959 y = convertWorldToNDC_Y (ygw_min);
967 if (o_xaxis == LINEAR) {
970 m_pSGP->setColor (clr_axis);
971 if (o_tag && !o_grid && !o_box && s_xcross) {
972 m_pSGP->moveAbs (xa_min, xaxispos - charheight);
973 m_pSGP->lineAbs (xa_min, xaxispos + charheight);
975 m_pSGP->moveAbs (xa_min, xaxispos);
976 m_pSGP->lineAbs (xa_max, xaxispos);
977 if (o_tag && !o_grid && !o_box) {
978 m_pSGP->moveAbs (xa_max, xaxispos - charheight);
979 m_pSGP->lineAbs (xa_max, xaxispos + charheight);
982 if (o_grid == TRUE) {
983 m_pSGP->setColor (clr_grid);
984 for (i = 0; i <= x_nint; i++) {
985 m_pSGP->moveAbs (xt_min + xn_tickinc * i, ya_max);
986 m_pSGP->lineAbs (xt_min + xn_tickinc * i, ya_min);
989 m_pSGP->setTextSize (charheight * 1.5);
990 m_pSGP->setTextColor (clr_label, -1);
994 m_pSGP->getTextExtent (c_xlabel.c_str(), &wText, &hText);
995 m_pSGP->moveAbs (xa_min + (xa_max-xa_min)/2 - wText / 2, xlbl_row + charheight * 1.5);
997 m_pSGP->drawText (c_xlabel);
998 m_pSGP->setTextSize (charheight);
999 minorinc = xn_tickinc / (o_xminortick + 1);
1001 for (i = 0; i <= x_nint; i++) {
1002 x = xt_min + xn_tickinc * i;
1003 m_pSGP->setColor (clr_axis);
1004 m_pSGP->moveAbs (x, xaxispos);
1005 m_pSGP->lineAbs (x, xaxispos + xticklen);
1007 for (j = 1; j <= o_xminortick; j++) {
1008 x2 = x + minorinc * j;
1009 m_pSGP->moveAbs (x2, xaxispos);
1010 m_pSGP->lineAbs (x2, xaxispos + TICKRATIO * xticklen);
1013 if (xaxispos + xtl_ofs > ya_min && o_yaxis != NOAXIS) {
1014 double xw = xgw_min + i * xw_tickinc;
1015 double x = convertWorldToNDC_X (xw);
1016 double d = x - yaxispos;
1017 if (o_yticks == RIGHT && d >= 0 && d < 0.9 * xn_tickinc)
1019 if (o_yticks == LEFT && d <= 0 && d > -0.9 * xn_tickinc)
1023 if (o_xtlabel == TRUE && axis_near == FALSE) {
1024 snprintf (str, sizeof(str), x_numfmt, xgw_min + xw_tickinc * i);
1025 numstr = str_skip_head (str, " ");
1026 double xExtent, yExtent;
1027 m_pSGP->getTextExtent (numstr, &xExtent, &yExtent);
1028 m_pSGP->moveAbs (x - xExtent/2, xaxispos + xtl_ofs);
1029 m_pSGP->setTextColor (clr_number, -1);
1030 m_pSGP->drawText (numstr);
1040 if (o_yaxis == LINEAR) {
1042 m_pSGP->setColor (clr_axis);
1043 if (o_tag && !o_grid && !o_box && s_ycross) {
1044 m_pSGP->moveAbs (yaxispos - charwidth, ya_min);
1045 m_pSGP->lineAbs (yaxispos + charwidth, ya_min);
1047 m_pSGP->moveAbs (yaxispos, ya_min);
1048 m_pSGP->lineAbs (yaxispos, ya_max);
1049 if (o_tag && !o_grid && !o_box) {
1050 m_pSGP->moveAbs (yaxispos - charwidth, ya_max);
1051 m_pSGP->lineAbs (yaxispos + charwidth, ya_max);
1054 if (o_grid == TRUE) {
1055 m_pSGP->setColor (clr_grid);
1056 for (i = 0; i <= y_nint; i++) {
1057 y = yt_min + yn_tickinc * i;
1058 m_pSGP->moveAbs (xa_max, y);
1059 m_pSGP->lineAbs (xa_min, y);
1063 m_pSGP->setTextAngle (HALFPI);
1064 m_pSGP->setTextSize (1.5 * charheight);
1065 m_pSGP->setTextColor (clr_label, -1);
1067 double xExtent, yExtent;
1069 m_pSGP->getTextExtent (c_ylabel.c_str(), &xExtent, &yExtent);
1070 m_pSGP->moveAbs (ylbl_col, ya_min + (ya_max-ya_min)/2 - yExtent / 2);
1071 m_pSGP->drawText (c_ylabel);
1073 m_pSGP->setTextAngle (0.0);
1074 m_pSGP->setTextSize (charheight);
1075 minorinc = yn_tickinc / (o_yminortick + 1);
1077 for (i = 0; i <= y_nint; i++) {
1078 y = yt_min + yn_tickinc * i;
1079 m_pSGP->setColor (clr_axis);
1080 m_pSGP->moveAbs (yaxispos, y);
1081 m_pSGP->lineAbs (yaxispos + yticklen, y);
1083 for (j = 1; j <= o_yminortick; j++) {
1084 y2 = y + minorinc * j;
1085 m_pSGP->moveAbs (yaxispos, y2);
1086 m_pSGP->lineAbs (yaxispos + TICKRATIO * yticklen, y2);
1089 if (yaxispos + ytl_ofs > xa_min && o_xaxis != NOAXIS) {
1090 double yw = ygw_min + i * yw_tickinc;
1091 double y = convertWorldToNDC_Y (yw);
1092 double d = y - xaxispos;
1093 if (o_xticks == ABOVE && d >= 0 && d < 0.9 * yn_tickinc)
1095 if (o_xticks == BELOW && d <= 0 && d > -0.9 * yn_tickinc)
1098 if (o_ytlabel == TRUE && axis_near == FALSE) {
1099 snprintf (str, sizeof(str), y_numfmt, ygw_min + yw_tickinc * i);
1100 double xExtent, yExtent;
1101 m_pSGP->getTextExtent (str, &xExtent, &yExtent);
1102 if (o_yticks == LEFT)
1103 m_pSGP->moveAbs (yaxispos - 1.5 * charwidth - xExtent, y + 0.5 * yExtent);
1105 m_pSGP->moveAbs (yaxispos + 1.5 * charwidth, y + 0.5 * yExtent);
1106 m_pSGP->setTextColor (clr_number, -1);
1107 m_pSGP->drawText (str);
1115 EZPlot::symbol (int sym, double symwidth, double symheight)
1120 if (sym == SB_CROSS) {
1121 m_pSGP->moveRel (-0.5 * symwidth, -0.5 * symheight);
1122 m_pSGP->lineRel (symwidth, symheight);
1123 m_pSGP->moveRel (-symwidth, 0.0);
1124 m_pSGP->lineRel (symwidth, -symheight);
1125 m_pSGP->moveRel (-0.5 * symwidth, 0.5 * symheight);
1126 } else if (sym == SB_PLUS) {
1127 m_pSGP->moveRel (-0.5 * symwidth, 0.0);
1128 m_pSGP->lineRel (symwidth, 0.0);
1129 m_pSGP->moveRel (-0.5 * symwidth, -0.5 * symheight);
1130 m_pSGP->lineRel (0.0, symheight);
1131 m_pSGP->moveRel (0.0, -0.5 * symheight);
1132 } else if (sym == SB_BOX) {
1133 m_pSGP->moveRel (-0.5 * symwidth, -0.5 * symheight);
1134 m_pSGP->lineRel (symwidth, 0.0);
1135 m_pSGP->lineRel (0.0, symheight);
1136 m_pSGP->lineRel (-symwidth, 0.0);
1137 m_pSGP->lineRel (0.0, -symheight);
1138 m_pSGP->moveRel (0.5 * symwidth, 0.5 * symheight);
1139 } else if (sym == SB_CIRCLE) {
1140 m_pSGP->drawCircle (symwidth);
1141 } else if (sym == SB_ERRORBAR) {
1142 m_pSGP->moveRel (-0.5 * symwidth, 0.5 * symheight);
1143 m_pSGP->lineRel (symwidth, 0.0);
1144 m_pSGP->moveRel (-0.5 * symwidth, 0.0);
1145 m_pSGP->lineRel (0.0, -symheight);
1146 m_pSGP->moveRel (-0.5 * symwidth, 0.0);
1147 m_pSGP->lineRel (symwidth, 0.0);
1148 m_pSGP->moveRel (-0.5 * symwidth, 0.5 * symheight);
1155 * axis_scale calculates graph axis scaling
1158 * retval = axis_scale (min, max, nint, minp, maxp, nintp,
1159 * rec_total, rec_frac)
1162 * double min Smallest value to plot
1163 * double max Largest value to plot
1164 * int nint Number of intervals desired
1167 * int retval FALSE if illegal parameters, else TRUE
1168 * double *minp Minimum graph value
1169 * double *maxp Maximum graph value
1170 * int *nintp Number of intervals for graph
1171 * int *rec_total Recommended field width for printing out the number
1172 * int *rec_frac Recommended number of digits for print fraction
1176 EZPlot::axis_scale (double min, double max, int nint, double *minp, double *maxp, int *nintp)
1178 if (min >= max || nint < 1) {
1179 sys_error (ERR_WARNING, "Invalid params: min=%lf, max=%lf, num intervals=%d [axis_scale]", min, max, nint);
1184 double a = fabs(min);
1185 if (fabs(min) < fabs(max))
1187 double scale = pow (10.0, floor(log10(a)));
1189 double mina = min / scale;
1190 double maxa = max / scale;
1191 double d = (maxa - mina) / nint;
1193 double e = floor (log10(d));
1194 double f = d / pow (10.0, e);
1198 else if (f < sqrt (10.0))
1200 else if (f < sqrt (50.0))
1202 double wdt = v * pow (10.0, e);
1203 double g = floor (mina / wdt);
1204 if (fabs(g + 1 - mina / wdt) < j)
1212 double h = floor (maxa / wdt) + 1.0;
1213 if (fabs(maxa / wdt + 1 - h) < j)
1219 *nintp = static_cast<int>(h - g);
1220 if (fabs(*maxp) >= 10.0 || fabs(*minp) >= 10.0) {
1221 scale = scale * 10.0;
1233 * make_numfmt Make a numeric format string
1236 * make_numfmt (fmtstr, fldwid, nfrac, min, max, nint)
1237 * char *fmtstr Returned format string for printf()
1238 * int *fldwid Returned field width
1239 * int *nfrac If < 0, then calculate best number of
1240 * fraction places & return that value
1241 * If >= 0, then use that number of places
1242 * double min Minimum value
1243 * double max Maximum value
1244 * int nint Number of intervals between min & max
1247 * This routine is written as an INTERNAL routine for EZPLOT
1250 static inline double
1261 EZPlot::make_numfmt (char *fmtstr, int *fldwid, int *nfrac, double minval, double maxval, int nint)
1263 int wid, frac, expon;
1265 double delta = (maxval - minval) / nint;
1266 double absmin = fabs(minval);
1267 double absmax = fabs(maxval);
1269 if (absmin > absmax)
1272 double logt = log10( absmax );
1274 if (fabs(logt) >= 6) { // use exponential format
1275 if (fabs(logt) > 99)
1280 if (*nfrac < 0) { // calculate frac
1281 delta /= pow (10., floor(logt)); // scale delta
1282 frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
1284 frac = 1; // to be safe, add decimal pt
1285 } else // use users' frac
1288 wid = 2 + frac + expon;
1289 if (minval < 0. || maxval < 0.)
1291 sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "g");
1292 } else { // use fixed format
1293 wid = static_cast<int>(trunc(logt)) + 1;
1296 if (minval < 0. || maxval < 0.)
1299 if (*nfrac < 0) { // calculate frac
1300 if (delta >= 0.999999)
1301 frac = 1; // add a decimal pt to be safe
1303 frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
1304 } else // use users' frac
1308 sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "f");