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;
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)
471 if (m_vecCurves.size() <= 0)
476 m_pSGP->setWindow (0., 0., 1., 1.);
478 if (s_textsize == TRUE)
479 m_pSGP->setTextPointSize (v_textsize);
481 charheight = m_pSGP->getCharHeight();
482 charwidth = m_pSGP->getCharWidth();
483 double symheight = charheight * 0.3; // size of symbol in NDC
484 double symwidth = symheight;
486 if (m_vecCurves.size() < 1)
487 return; // can't plot if there are no curves to plot
489 // find a curve with at least one point to get base point
490 double xmin, xmax, ymin, ymax; // extent of curves in world coord
493 for (unsigned int iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
494 const EZPlotCurve* const pCurve = m_vecCurves [iCurve];
495 if (pCurve->m_iPointCount > 0) {
505 return; // curve(s) are empty, so no plotting
507 for (unsigned int iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
508 const EZPlotCurve* const pCurve = m_vecCurves [iCurve];
510 for (int ip = 0; ip < pCurve->m_iPointCount; ip++) {
511 if (pCurve->x[ip] > xmax)
512 xmax = pCurve->x[ip];
513 else if (pCurve->x[ip] < xmin)
514 xmin = pCurve->x[ip];
515 if (pCurve->y[ip] > ymax)
516 ymax = pCurve->y[ip];
517 else if (pCurve->y[ip] < ymin)
518 ymin = pCurve->y[ip];
522 // extend graph limits for user defined axis cross positions
523 if (s_xcross == TRUE) {
526 else if (v_xcross > xmax)
530 if (s_ycross == TRUE) {
533 else if (v_ycross > ymax)
537 // find nice endpoints for axes
538 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))
542 // check if user set x-axis extents
543 if (s_xmin == TRUE) {
545 x_nint = o_xmajortick - 1;
547 if (s_xmax == TRUE) {
549 x_nint = o_xmajortick - 1;
552 // check if user set y-axis extents
553 if (s_ymin == TRUE) {
555 y_nint = o_ymajortick - 1;
557 if (s_ymax == TRUE) {
559 y_nint = o_ymajortick - 1;
562 // calculate increment between major axis in world coordinates
563 xw_tickinc = (xgw_max - xgw_min) / x_nint;
564 yw_tickinc = (ygw_max - ygw_min) / y_nint;
566 // we have now calcuated xgw_min, xyw_max, ygw_min, & ygw_max
568 // set the number of decimal point to users' setting or default
569 // Two formats for numbers: Fixed: -nnn.f and Exponent: -n.fffE+eee
570 if (s_lxfrac == TRUE)
575 if (s_lyfrac == TRUE)
580 make_numfmt (x_numfmt, &x_fldwid, &x_frac, xgw_min, xgw_max, x_nint);
581 make_numfmt (y_numfmt, &y_fldwid, &y_frac, ygw_min, ygw_max, y_nint);
583 xtl_wid = x_fldwid * charwidth; // calc size of tick labels
584 ytl_wid = y_fldwid * charwidth;
585 tl_height = charheight;
587 // calculate the extent of the plot frame
590 xp_max = xp_min + o_xlength;
591 yp_max = yp_min + o_ylength;
593 xp_min = clamp (xp_min, 0., 1.);
594 xp_max = clamp (xp_max, 0., 1.);
595 yp_min = clamp (yp_min, 0., 1.);
596 yp_max = clamp (yp_max, 0., 1.);
598 xa_min = xp_min; // extent of axes
603 // adjust frame for title
606 if (c_title.length() > 0)
607 ya_max -= 2 * charheight;
611 ya_max -= 0.7 * charheight; // allow room for yaxis ticklabel
614 // calculate legend box boundaries
615 int max_leg = 0; // longest legend in characters
616 int num_leg = 0; // number of legend titles
618 for (unsigned int iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
619 const std::string* pstrLegend = getLegend (iCurve);
621 if (pstrLegend && pstrLegend->length() > 0) {
623 int nLegend = pstrLegend->length();
626 if (nLegend > max_leg)
634 if (num_leg > 0 && o_legendbox != NOLEGEND) {
635 double leg_width = (max_leg + 2) * charwidth; // size of legend box
636 double leg_height = num_leg * 3 * charheight;
638 if (s_xlegend == TRUE)
642 if (o_legendbox == OUTSIDE)
643 xa_max -= (leg_width + 0.5 * charwidth);
645 xl_min = xl_max - leg_width;
647 if (s_ylegend == TRUE)
652 yl_min = yl_max - leg_height;
654 m_pSGP->setColor (clr_legend);
655 m_pSGP->drawRect (xl_min, yl_min, xl_max, yl_max);
657 int iLegend = 0; // current legend position
659 for (unsigned int iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
660 const std::string* pstrLegend = getLegend (iCurve);
661 if (! pstrLegend || pstrLegend->length() == 0)
664 double xmin = xl_min + 1.0 * charwidth;
665 double xmax = xl_max - 1.0 * charwidth;
666 double y = yl_max - (2.0 + iLegend * 3) * charheight;
668 m_pSGP->moveAbs (xmin, y + 0.5 * charheight);
670 m_pSGP->drawText (pstrLegend->c_str());
671 m_pSGP->setColor (getColor (iCurve));
673 int iLS = getLinestyle (iCurve);
674 if (iLS != SGP::LS_NOLINE) {
675 m_pSGP->setLineStyle (iLS);
676 m_pSGP->moveAbs (xmin, y);
677 m_pSGP->lineAbs (xmax, y);
680 int iSymbol = getSymbol (iCurve);
682 double xinc = (xmax - xmin) / (5 - 1);
683 m_pSGP->setLineStyle (SGP::LS_SOLID);
684 for (int j = 0; j < 5; j++) {
685 m_pSGP->moveAbs (xmin + j * xinc, y);
686 symbol (iSymbol, symwidth, symheight);
689 ++iLegend; // move to next legend position
691 } // end legend printing
693 // calculate the extent of the axes
695 /*-------------------------*/
696 /* adjust frame for labels */
697 /*-------------------------*/
700 if (c_xlabel.length() > 0)
701 ya_min += 1.5 * charheight;
702 xlbl_row = xp_min; // put x-label on bottom of plot frame
705 if (c_ylabel.length() > 0) {
707 m_pSGP->setTextAngle (HALFPI);
709 m_pSGP->setTextSize (1.5 * charheight);
711 double xExtent, yExtent;
713 m_pSGP->getTextExtent (c_ylabel.c_str(), &xExtent, &yExtent);
715 m_pSGP->setTextSize (charheight);
719 m_pSGP->setTextAngle (0.0);
724 /*------------------------------*/
725 /* adjust frame for tick labels */
726 /*------------------------------*/
728 // Calc offset of tick labels from axes
729 if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
731 else if (o_xticks == BELOW)
732 xtl_ofs = -0.5 * charheight;
733 else if (o_xticks == ABOVE)
734 xtl_ofs = 0.5 * charheight;
736 if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
738 else if (o_yticks == LEFT) {
739 double xExtentMin, xExtentMax, yExtent;
741 snprintf (s, sizeof(s), y_numfmt, ymin);
742 m_pSGP->getTextExtent (s, &xExtentMin, &yExtent);
743 snprintf (s, sizeof(s), y_numfmt, ymax);
744 m_pSGP->getTextExtent (s, &xExtentMax, &yExtent);
745 if (xExtentMin > xExtentMax)
746 xExtentMax = xExtentMin;
747 ytl_ofs = -xExtentMax;
748 } else if (o_yticks == RIGHT)
749 ytl_ofs = 1.5 * charwidth;
751 xa_max -= 0.7 * x_fldwid * charwidth; // make room for last x tick label
758 // see if need to shrink axis extents and/or tick extents
759 if (xtl_ofs != 0.0 && s_ycross == FALSE) {
760 if (o_xticks == BELOW) {
761 ya_min += 1.5 * charheight;
763 } else if (o_xticks == ABOVE) {
765 yt_min = ya_min + 1.5 * charheight;
767 } else // noaxis, no t-labels, or user set cross
770 if (ytl_ofs != 0.0 && s_xcross == FALSE) {
771 if (o_yticks == LEFT) {
772 xa_min += 2*charwidth - ytl_ofs; // (2 + y_fldwid) * charwidth;
774 } else if (o_yticks == RIGHT) {
776 xt_min = xa_min + ytl_ofs; // + y_fldwid * charwidth;
781 // decrease size of graph, if necessary, to accommadate space
782 // between axis boundary and boundary of ticks
783 double x_added_ticks = 0; // number of tick spaces added to axis
784 double y_added_ticks = 0;
785 if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
787 if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
790 if (o_grid == TRUE) {
791 if (x_added_ticks < 0)
793 if (y_added_ticks < 0)
797 if (x_added_ticks < 0) {
798 if (o_yticks == LEFT || s_ycross)
804 if (y_added_ticks < 0) {
805 if (o_xticks == BELOW || s_xcross)
811 xn_tickinc = (xt_max - xt_min) / (x_nint + x_added_ticks);
812 yn_tickinc = (yt_max - yt_min) / (y_nint + y_added_ticks);
814 xt_min += 0.5 * x_added_ticks * xn_tickinc;
815 xt_max -= 0.5 * x_added_ticks * xn_tickinc;
816 yt_min += 0.5 * y_added_ticks * yn_tickinc;
817 yt_max -= 0.5 * y_added_ticks * yn_tickinc;
824 //------------------------------------------------------------------------
826 m_xWorldScale = (xgn_max - xgn_min) / (xgw_max - xgw_min);
827 m_yWorldScale = (ygn_max - ygn_min) / (ygw_max - ygw_min);
831 m_pSGP->setLineStyle (SGP::LS_SOLID);
837 clipRect[0] = xgn_min; clipRect[1] = ygn_min; clipRect[2] = xgn_max; clipRect[3] = ygn_max;
841 for (unsigned int iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
842 const EZPlotCurve* const pCurve = m_vecCurves [iCurve];
844 m_pSGP->setColor (getColor (iCurve));
845 int iSym = getSymbol (iCurve);
846 int iLS = getLinestyle (iCurve);
848 if (iLS != SGP::LS_NOLINE) {
849 m_pSGP->setLineStyle (iLS);
850 double x1 = convertWorldToNDC_X (pCurve->x[0]);
851 double y1 = convertWorldToNDC_Y (pCurve->y[0]);
853 for (int i = 1; i < pCurve->m_iPointCount; i++) {
854 double x2 = convertWorldToNDC_X (pCurve->x[i]);
855 double y2 = convertWorldToNDC_Y (pCurve->y[i]);
859 if (clip_rect (x1, y1, x2Clip, y2Clip, clipRect)) {
860 m_pSGP->moveAbs (x1, y1);
861 m_pSGP->lineAbs (x2Clip, y2Clip);
868 int iSymFreq = getSymbolFreq (iCurve);
869 m_pSGP->setLineStyle (SGP::LS_SOLID);
870 double x = convertWorldToNDC_X (pCurve->x[0]);
871 double y = convertWorldToNDC_Y (pCurve->y[0]);
872 m_pSGP->moveAbs (x, y);
873 symbol (iSym, symwidth, symheight);
874 for (int i = 1; i < pCurve->m_iPointCount; i++)
875 if (i % iSymFreq == 0 || i == pCurve->m_iPointCount - 1) {
876 x = convertWorldToNDC_X (pCurve->x[i]);
877 y = convertWorldToNDC_Y (pCurve->y[i]);
879 if (y >= ygn_min && y <= ygn_max) {
880 m_pSGP->moveAbs (x, y);
881 symbol (iSym, symwidth, symheight);
890 * drawAxes INTERNAL routine to draw axis & label them
900 double xticklen = 0, yticklen = 0; // length of ticks in NDC
901 double minorinc; // increment between minor axes
902 double xaxispos, yaxispos; // crossing of axes
904 bool axis_near; // TRUE if axis too close to print t-label
909 m_pSGP->setTextSize (charheight);
910 m_pSGP->setTextColor (1, -1);
912 if (o_xticks == ABOVE)
913 xticklen = 0.5 * charheight;
914 else if (o_xticks == BELOW)
915 xticklen = -0.5 * charheight;
917 if (o_yticks == RIGHT)
918 yticklen = charwidth;
919 else if (o_yticks == LEFT)
920 yticklen = -charwidth;
922 if (c_title.length() > 0) {
924 m_pSGP->setTextSize (charheight * 2.0);
925 m_pSGP->getTextExtent (c_title.c_str(), &wText, &hText);
926 m_pSGP->moveAbs (xa_min + (xa_max-xa_min)/2 - wText/2, title_row);
927 m_pSGP->setTextColor (clr_title, -1);
928 m_pSGP->drawText (c_title);
929 m_pSGP->setTextSize (charheight);
932 if (o_grid == TRUE || o_box == TRUE) {
933 m_pSGP->setColor (clr_axis);
934 m_pSGP->moveAbs (xa_min, ya_min);
935 m_pSGP->lineAbs (xa_max, ya_min);
936 m_pSGP->lineAbs (xa_max, ya_max);
937 m_pSGP->lineAbs (xa_min, ya_max);
938 m_pSGP->lineAbs (xa_min, ya_min);
941 // calculate position of axes
944 if (s_ycross == TRUE) { // convert users' world-coord
945 xaxispos = convertWorldToNDC_Y (v_ycross);// axis to its position in NDC
946 x = convertWorldToNDC_X (xgw_min);
951 if (s_xcross == TRUE) { // convert users' world-coord
952 yaxispos = convertWorldToNDC_X (v_xcross);// axis to its NDC position
953 y = convertWorldToNDC_Y (ygw_min);
961 if (o_xaxis == LINEAR) {
964 m_pSGP->setColor (clr_axis);
965 if (o_tag && !o_grid && !o_box && s_xcross) {
966 m_pSGP->moveAbs (xa_min, xaxispos - charheight);
967 m_pSGP->lineAbs (xa_min, xaxispos + charheight);
969 m_pSGP->moveAbs (xa_min, xaxispos);
970 m_pSGP->lineAbs (xa_max, xaxispos);
971 if (o_tag && !o_grid && !o_box) {
972 m_pSGP->moveAbs (xa_max, xaxispos - charheight);
973 m_pSGP->lineAbs (xa_max, xaxispos + charheight);
976 if (o_grid == TRUE) {
977 m_pSGP->setColor (clr_grid);
978 for (i = 0; i <= x_nint; i++) {
979 m_pSGP->moveAbs (xt_min + xn_tickinc * i, ya_max);
980 m_pSGP->lineAbs (xt_min + xn_tickinc * i, ya_min);
983 m_pSGP->setTextSize (charheight * 1.5);
984 m_pSGP->setTextColor (clr_label, -1);
988 m_pSGP->getTextExtent (c_xlabel.c_str(), &wText, &hText);
989 m_pSGP->moveAbs (xa_min + (xa_max-xa_min)/2 - wText / 2, xlbl_row + charheight * 1.5);
991 m_pSGP->drawText (c_xlabel);
992 m_pSGP->setTextSize (charheight);
993 minorinc = xn_tickinc / (o_xminortick + 1);
995 for (i = 0; i <= x_nint; i++) {
996 x = xt_min + xn_tickinc * i;
997 m_pSGP->setColor (clr_axis);
998 m_pSGP->moveAbs (x, xaxispos);
999 m_pSGP->lineAbs (x, xaxispos + xticklen);
1001 for (j = 1; j <= o_xminortick; j++) {
1002 x2 = x + minorinc * j;
1003 m_pSGP->moveAbs (x2, xaxispos);
1004 m_pSGP->lineAbs (x2, xaxispos + TICKRATIO * xticklen);
1008 if (xaxispos + xtl_ofs > ya_min && o_yaxis != NOAXIS) {
1009 double xw = xgw_min + i * xw_tickinc;
1010 double x = convertWorldToNDC_X (xw);
1011 double d = x - yaxispos;
1012 if (o_yticks == RIGHT && d >= 0 && d < 0.9 * xn_tickinc)
1014 if (o_yticks == LEFT && d <= 0 && d > -0.9 * xn_tickinc)
1018 if (o_xtlabel == TRUE && axis_near == FALSE) {
1019 snprintf (str, sizeof(str), x_numfmt, xgw_min + xw_tickinc * i);
1020 numstr = str_skip_head (str, " ");
1021 double xExtent, yExtent;
1022 m_pSGP->getTextExtent (numstr, &xExtent, &yExtent);
1023 m_pSGP->moveAbs (x - xExtent/2, xaxispos + xtl_ofs);
1024 m_pSGP->setTextColor (clr_number, -1);
1025 m_pSGP->drawText (numstr);
1035 if (o_yaxis == LINEAR) {
1037 m_pSGP->setColor (clr_axis);
1038 if (o_tag && !o_grid && !o_box && s_ycross) {
1039 m_pSGP->moveAbs (yaxispos - charwidth, ya_min);
1040 m_pSGP->lineAbs (yaxispos + charwidth, ya_min);
1042 m_pSGP->moveAbs (yaxispos, ya_min);
1043 m_pSGP->lineAbs (yaxispos, ya_max);
1044 if (o_tag && !o_grid && !o_box) {
1045 m_pSGP->moveAbs (yaxispos - charwidth, ya_max);
1046 m_pSGP->lineAbs (yaxispos + charwidth, ya_max);
1049 if (o_grid == TRUE) {
1050 m_pSGP->setColor (clr_grid);
1051 for (i = 0; i <= y_nint; i++) {
1052 y = yt_min + yn_tickinc * i;
1053 m_pSGP->moveAbs (xa_max, y);
1054 m_pSGP->lineAbs (xa_min, y);
1058 m_pSGP->setTextAngle (HALFPI);
1059 m_pSGP->setTextSize (1.5 * charheight);
1060 m_pSGP->setTextColor (clr_label, -1);
1062 double xExtent, yExtent;
1064 m_pSGP->getTextExtent (c_ylabel.c_str(), &xExtent, &yExtent);
1065 m_pSGP->moveAbs (ylbl_col, ya_min + (ya_max-ya_min)/2 - yExtent / 2);
1066 m_pSGP->drawText (c_ylabel);
1068 m_pSGP->setTextAngle (0.0);
1069 m_pSGP->setTextSize (charheight);
1070 minorinc = yn_tickinc / (o_yminortick + 1);
1072 for (i = 0; i <= y_nint; i++) {
1073 y = yt_min + yn_tickinc * i;
1074 m_pSGP->setColor (clr_axis);
1075 m_pSGP->moveAbs (yaxispos, y);
1076 m_pSGP->lineAbs (yaxispos + yticklen, y);
1078 for (j = 1; j <= o_yminortick; j++) {
1079 y2 = y + minorinc * j;
1080 m_pSGP->moveAbs (yaxispos, y2);
1081 m_pSGP->lineAbs (yaxispos + TICKRATIO * yticklen, y2);
1085 if (yaxispos + ytl_ofs > xa_min && o_xaxis != NOAXIS) {
1086 double yw = ygw_min + i * yw_tickinc;
1087 double y = convertWorldToNDC_Y (yw);
1088 double d = y - xaxispos;
1089 if (o_xticks == ABOVE && d >= 0 && d < 0.9 * yn_tickinc)
1091 if (o_xticks == BELOW && d <= 0 && d > -0.9 * yn_tickinc)
1094 if (o_ytlabel == TRUE && axis_near == FALSE) {
1095 snprintf (str, sizeof(str), y_numfmt, ygw_min + yw_tickinc * i);
1096 double xExtent, yExtent;
1097 m_pSGP->getTextExtent (str, &xExtent, &yExtent);
1098 if (o_yticks == LEFT)
1099 m_pSGP->moveAbs (yaxispos - 1.5 * charwidth - xExtent, y + 0.5 * yExtent);
1101 m_pSGP->moveAbs (yaxispos + 1.5 * charwidth, y + 0.5 * yExtent);
1102 m_pSGP->setTextColor (clr_number, -1);
1103 m_pSGP->drawText (str);
1111 EZPlot::symbol (int sym, double symwidth, double symheight)
1116 if (sym == SB_CROSS) {
1117 m_pSGP->markerRel (0, 0);
1118 // m_pSGP->moveRel (-0.5 * symwidth, -0.5 * symheight);
1119 // m_pSGP->lineRel (symwidth, symheight);
1120 // m_pSGP->moveRel (-symwidth, 0.0);
1121 // m_pSGP->lineRel (symwidth, -symheight);
1122 // m_pSGP->moveRel (-0.5 * symwidth, 0.5 * symheight);
1123 } else if (sym == SB_PLUS) {
1124 m_pSGP->moveRel (-0.5 * symwidth, 0.0);
1125 m_pSGP->lineRel (symwidth, 0.0);
1126 m_pSGP->moveRel (-0.5 * symwidth, -0.5 * symheight);
1127 m_pSGP->lineRel (0.0, symheight);
1128 m_pSGP->moveRel (0.0, -0.5 * symheight);
1129 } else if (sym == SB_BOX) {
1130 m_pSGP->moveRel (-0.5 * symwidth, -0.5 * symheight);
1131 m_pSGP->lineRel (symwidth, 0.0);
1132 m_pSGP->lineRel (0.0, symheight);
1133 m_pSGP->lineRel (-symwidth, 0.0);
1134 m_pSGP->lineRel (0.0, -symheight);
1135 m_pSGP->moveRel (0.5 * symwidth, 0.5 * symheight);
1136 } else if (sym == SB_CIRCLE) {
1137 m_pSGP->drawCircle (symwidth);
1138 } else if (sym == SB_ERRORBAR) {
1139 m_pSGP->moveRel (-0.5 * symwidth, 0.5 * symheight);
1140 m_pSGP->lineRel (symwidth, 0.0);
1141 m_pSGP->moveRel (-0.5 * symwidth, 0.0);
1142 m_pSGP->lineRel (0.0, -symheight);
1143 m_pSGP->moveRel (-0.5 * symwidth, 0.0);
1144 m_pSGP->lineRel (symwidth, 0.0);
1145 m_pSGP->moveRel (-0.5 * symwidth, 0.5 * symheight);
1146 } else if (sym == SB_POINT) {
1147 m_pSGP->pointRel (0, 0);
1154 * axis_scale calculates graph axis scaling
1157 * retval = axis_scale (min, max, nint, minp, maxp, nintp,
1158 * rec_total, rec_frac)
1161 * double min Smallest value to plot
1162 * double max Largest value to plot
1163 * int nint Number of intervals desired
1166 * int retval FALSE if illegal parameters, else TRUE
1167 * double *minp Minimum graph value
1168 * double *maxp Maximum graph value
1169 * int *nintp Number of intervals for graph
1170 * int *rec_total Recommended field width for printing out the number
1171 * int *rec_frac Recommended number of digits for print fraction
1175 EZPlot::axis_scale (double min, double max, int nint, double *minp, double *maxp, int *nintp)
1178 sys_error (ERR_WARNING, "No intervals to plot: num intervals=%d [axis_scale]", nint);
1182 double scaled = fabs(max) / 10;
1185 *minp = min - scaled;
1186 *maxp = max + scaled;
1192 double a = fabs(min);
1193 if (fabs(min) < fabs(max))
1195 double scale = pow (10.0, floor(log10(a)));
1197 double mina = min / scale;
1198 double maxa = max / scale;
1199 double d = (maxa - mina) / nint;
1201 double e = floor (log10(d));
1202 double f = d / pow (10.0, e);
1206 else if (f < sqrt (10.0))
1208 else if (f < sqrt (50.0))
1210 double wdt = v * pow (10.0, e);
1211 double g = floor (mina / wdt);
1212 if (fabs(g + 1 - mina / wdt) < j)
1220 double h = floor (maxa / wdt) + 1.0;
1221 if (fabs(maxa / wdt + 1 - h) < j)
1227 *nintp = static_cast<int>(h - g);
1228 if (fabs(*maxp) >= 10.0 || fabs(*minp) >= 10.0) {
1229 scale = scale * 10.0;
1241 * make_numfmt Make a numeric format string
1244 * make_numfmt (fmtstr, fldwid, nfrac, min, max, nint)
1245 * char *fmtstr Returned format string for printf()
1246 * int *fldwid Returned field width
1247 * int *nfrac If < 0, then calculate best number of
1248 * fraction places & return that value
1249 * If >= 0, then use that number of places
1250 * double min Minimum value
1251 * double max Maximum value
1252 * int nint Number of intervals between min & max
1255 * This routine is written as an INTERNAL routine for EZPLOT
1258 static inline double
1269 EZPlot::make_numfmt (char *fmtstr, int *fldwid, int *nfrac, double minval, double maxval, int nint)
1271 int wid, frac, expon;
1273 double delta = (maxval - minval) / nint;
1274 double absmin = fabs(minval);
1275 double absmax = fabs(maxval);
1277 if (absmin > absmax)
1280 double logt = log10( absmax );
1282 if (fabs(logt) >= 6) { // use exponential format
1283 if (fabs(logt) > 99)
1288 if (*nfrac < 0) { // calculate frac
1289 delta /= pow (10., floor(logt)); // scale delta
1290 frac = static_cast<int>(fabs(my_trunc(log10(delta)))) + 1;
1292 frac = 1; // to be safe, add decimal pt
1293 } else // use users' frac
1296 wid = 2 + frac + expon;
1297 if (minval < 0. || maxval < 0.)
1299 sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "g");
1300 } else { // use fixed format
1301 wid = static_cast<int>(my_trunc(logt)) + 1;
1304 if (minval < 0. || maxval < 0.)
1307 if (*nfrac < 0) { // calculate frac
1308 if (delta >= 0.999999)
1309 frac = 1; // add a decimal pt to be safe
1311 frac = static_cast<int>(fabs(my_trunc(log10(delta)))) + 1;
1312 } else // use users' frac
1316 sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "f");