1 /*****************************************************************************
6 ** This is part of the CTSim program
7 ** Copyright (C) 1983-2000 Kevin Rosenberg
9 ** $Id: ezplot.cpp,v 1.26 2000/12/29 15:45:06 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());
140 m_pol.addSkipWord ("please");
\r
141 m_pol.addSkipWord ("use");
\r
142 m_pol.addSkipWord ("are");
\r
143 m_pol.addSkipWord ("and");
\r
144 m_pol.addSkipWord ("is");
\r
145 m_pol.addSkipWord ("the");
\r
146 m_pol.addSkipWord ("equals");
\r
147 m_pol.addSkipChar ('=');
\r
149 m_pol.usefile (POL::P_USE_STR,"");
\r
150 m_pol.set_inputline ("!eoc ,");
\r
152 m_pol.closefile ();
\r
158 EZPlot::initPlotSettings ()
160 m_iCurrentCurve = -1;
\r
183 o_color = DEF_CURVE_CLR;
186 o_linestyle = SGP::LS_SOLID;
193 o_legendbox = INSIDE;
206 clr_axis = C_BLACK; // set fixed colors
210 clr_number = C_GREEN;
215 EZPlot::setColor (unsigned int iCurve, int iColor)
\r
217 if (m_veciColor.size() <= iCurve) {
\r
218 m_veciColor.resize ((m_iCurrentCurve + 1) * 2);
\r
219 m_vecbColorSet.resize ((m_iCurrentCurve + 1) * 2);
\r
221 m_veciColor [iCurve] = iColor;
\r
222 m_vecbColorSet [iCurve] = true;
\r
226 EZPlot::setSymbol (unsigned int iCurve, int iSymbol)
\r
228 if (m_veciSymbol.size() <= iCurve) {
\r
229 m_veciSymbol.resize ((m_iCurrentCurve + 1) * 2);
\r
230 m_vecbSymbolSet.resize ((m_iCurrentCurve + 1) * 2);
\r
232 m_veciSymbol [iCurve] = iSymbol;
\r
233 m_vecbSymbolSet [iCurve] = true;
\r
237 EZPlot::setSymbolFreq (unsigned int iCurve, int iSymbolFreq)
\r
239 if (m_veciSymbolFreq.size() <= iCurve) {
\r
240 m_veciSymbolFreq.resize ((m_iCurrentCurve + 1) * 2);
\r
241 m_vecbSymbolFreqSet.resize ((m_iCurrentCurve + 1) * 2);
\r
243 m_veciSymbolFreq [iCurve] = iSymbolFreq;
\r
244 m_vecbSymbolFreqSet [iCurve] = true;
\r
248 EZPlot::setLinestyle (unsigned int iCurve, int iLinestyle)
\r
250 if (m_veciLinestyle.size() <= iCurve) {
\r
251 m_veciLinestyle.resize ((m_iCurrentCurve + 1) * 2);
\r
252 m_vecbLinestyleSet.resize ((m_iCurrentCurve + 1) * 2);
\r
254 m_veciLinestyle [iCurve] = iLinestyle;
\r
255 m_vecbLinestyleSet [iCurve] = true;
\r
259 EZPlot::setLegend (unsigned int iCurve, const std::string& strLegend)
\r
261 if (m_vecsLegend.size() <= iCurve) {
\r
262 m_vecsLegend.resize ((m_iCurrentCurve + 1) * 2);
\r
263 m_vecbLegendSet.resize ((m_iCurrentCurve + 1) * 2);
\r
265 m_vecsLegend [iCurve] = strLegend;
\r
266 m_vecbLegendSet [iCurve] = true;
\r
270 EZPlot::setLegend (unsigned int iCurve, const char* const pszLegend)
\r
272 if (m_vecsLegend.size() <= iCurve) {
\r
273 m_vecsLegend.resize ((m_iCurrentCurve + 1) * 2);
\r
274 m_vecbLegendSet.resize ((m_iCurrentCurve + 1) * 2);
\r
276 m_vecsLegend [iCurve] = pszLegend;
\r
277 m_vecbLegendSet [iCurve] = true;
\r
281 EZPlot::getColor (unsigned int iCurve) const
\r
283 if (m_veciColor.size() > iCurve && m_vecbColorSet[iCurve])
\r
284 return m_veciColor[iCurve];
\r
290 EZPlot::getSymbol (unsigned int iCurve) const
\r
292 if (m_veciSymbol.size() > iCurve && m_vecbSymbolSet[iCurve])
\r
293 return m_veciSymbol[iCurve];
\r
299 EZPlot::getSymbolFreq (unsigned int iCurve) const
\r
301 if (m_veciSymbolFreq.size() > iCurve && m_vecbSymbolFreqSet[iCurve])
\r
302 return m_veciSymbolFreq[iCurve];
\r
308 EZPlot::getLinestyle (unsigned int iCurve) const
\r
310 if (m_veciLinestyle.size() > iCurve && m_vecbLinestyleSet[iCurve])
\r
311 return m_veciLinestyle[iCurve];
\r
313 return o_linestyle;
\r
317 EZPlot::getLegend (unsigned int iCurve) const
\r
319 if (m_vecsLegend.size() > iCurve && m_vecbLegendSet[iCurve])
\r
320 return &m_vecsLegend[iCurve];
\r
327 * plot Plots all curves collected by addCurves ()
333 * This routine plots the curves that have stored by addCurves().
336 * drawAxes() & make_numfmt()
340 EZPlot::plot (SGP* pSGP)
342 if (m_vecCurves.size() <= 0)
347 m_pSGP->setWindow (0., 0., 1., 1.);
349 if (s_textsize == TRUE)
350 m_pSGP->setTextPointSize (v_textsize);
\r
351 charheight = m_pSGP->getCharHeight();
352 charwidth = m_pSGP->getCharWidth();
354 const EZPlotCurve& firstCurve = *m_vecCurves[0];
\r
355 double xmin = firstCurve.x[0]; // extent of curves in world coord
357 double ymin = firstCurve.y[0];
358 double ymax = ymin;
\r
360 for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
361 const EZPlotCurve* const pCurve = m_vecCurves [iCurve];
363 for (int ip = 0; ip < pCurve->m_iPointCount; ip++) {
364 if (pCurve->x[ip] > xmax)
365 xmax = pCurve->x[ip];
366 else if (pCurve->x[ip] < xmin)
367 xmin = pCurve->x[ip];
368 if (pCurve->y[ip] > ymax)
369 ymax = pCurve->y[ip];
370 else if (pCurve->y[ip] < ymin)
371 ymin = pCurve->y[ip];
375 // extend graph limits for user defined axis cross positions
376 if (s_xcross == TRUE) {
379 else if (v_xcross > xmax)
383 if (s_ycross == TRUE) {
386 else if (v_ycross > ymax)
390 // find nice endpoints for axes
391 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))
394 // check if user set x-axis extents
395 if (s_xmin == TRUE) {
397 x_nint = o_xmajortick - 1;
399 if (s_xmax == TRUE) {
401 x_nint = o_xmajortick - 1;
404 // check if user set y-axis extents
405 if (s_ymin == TRUE) {
407 y_nint = o_ymajortick - 1;
409 if (s_ymax == TRUE) {
411 y_nint = o_ymajortick - 1;
414 // calculate increment between major axis in world coordinates
415 xw_tickinc = (xgw_max - xgw_min) / x_nint;
416 yw_tickinc = (ygw_max - ygw_min) / y_nint;
418 // we have now calcuated xgw_min, xyw_max, ygw_min, & ygw_max
420 // set the number of decimal point to users' setting or default
421 // Two formats for numbers: Fixed: -nnn.f and Exponent: -n.fffE+eee
422 if (s_lxfrac == TRUE)
427 if (s_lyfrac == TRUE)
432 make_numfmt (x_numfmt, &x_fldwid, &x_frac, xgw_min, xgw_max, x_nint);
433 make_numfmt (y_numfmt, &y_fldwid, &y_frac, ygw_min, ygw_max, y_nint);
435 xtl_wid = x_fldwid * charwidth; // calc size of tick labels
436 ytl_wid = y_fldwid * charwidth;
437 tl_height = charheight;
439 // calculate the extent of the plot frame
442 xp_max = xp_min + o_xlength;
443 yp_max = yp_min + o_ylength;
445 xp_min = clamp (xp_min, 0., 1.);
446 xp_max = clamp (xp_max, 0., 1.);
447 yp_min = clamp (yp_min, 0., 1.);
448 yp_max = clamp (yp_max, 0., 1.);
450 xa_min = xp_min; // extent of axes
455 // adjust frame for title
456 title_row = ya_max;;
\r
457 if (c_title.length() > 0)
458 ya_max -= 2 * charheight;
\r
460 ya_max -= 0.7 * charheight; // allow room for yaxis ticklabel
\r
462 // calculate legend box boundaries
463 int max_leg = 0; // longest legend in characters
464 int num_leg = 0; // number of legend titles
\r
465 for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
466 const std::string* pstrLegend = getLegend (iCurve);
\r
467 if (pstrLegend && pstrLegend->length() > 0) {
\r
468 int nLegend = pstrLegend->length();
471 if (nLegend > max_leg)
\r
477 if (num_leg > 0 && o_legendbox != NOLEGEND) {
478 double leg_width = (max_leg + 2) * charwidth; // size of legend box
479 double leg_height = num_leg * 3 * charheight;
481 if (s_xlegend == TRUE)
485 if (o_legendbox == OUTSIDE)
486 xa_max -= (leg_width + 0.5 * charwidth);
488 xl_min = xl_max - leg_width;
490 if (s_ylegend == TRUE)
495 yl_min = yl_max - leg_height;
497 m_pSGP->setColor (clr_legend);
498 m_pSGP->drawRect (xl_min, yl_min, xl_max, yl_max);
500 int iLegend = 0; // current legend position
\r
501 for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
502 const std::string* pstrLegend = getLegend (iCurve);
503 if (! pstrLegend || pstrLegend->length() == 0)
506 double xmin = xl_min + 1.0 * charwidth;
507 double xmax = xl_max - 1.0 * charwidth;
508 double y = yl_max - (2.0 + iLegend * 3) * charheight;
510 m_pSGP->moveAbs (xmin, y + 0.5 * charheight);
\r
511 m_pSGP->drawText (pstrLegend->c_str());
512 m_pSGP->setColor (getColor (iCurve));
\r
513 int iLS = getLinestyle (iCurve);
514 if (iLS != SGP::LS_NOLINE) {
515 m_pSGP->setLineStyle (iLS);
516 m_pSGP->moveAbs (xmin, y);
517 m_pSGP->lineAbs (xmax, y);
519 int iSymbol = getSymbol (iCurve);
521 double xinc = (xmax - xmin) / (5 - 1);
522 m_pSGP->setLineStyle (SGP::LS_SOLID);
523 for (int j = 0; j < 5; j++) {
524 m_pSGP->moveAbs (xmin + j * xinc, y);
525 symbol(iSymbol, 0.5 * charwidth, 0.5 * charheight);
528 ++iLegend; // move to next legend position
530 } // end legend printing
532 // calculate the extent of the axes
534 /*-------------------------*/
535 /* adjust frame for labels */
536 /*-------------------------*/
539 if (c_xlabel.length() > 0)
540 ya_min += 1.5 * charheight;
541 xlbl_row = xp_min; // put x-label on bottom of plot frame
544 if (c_ylabel.length() > 0) {
\r
545 m_pSGP->setTextAngle (HALFPI);
\r
546 m_pSGP->setTextSize (1.5 * charheight);
\r
547 double xExtent, yExtent;
\r
548 m_pSGP->getTextExtent (c_ylabel.c_str(), &xExtent, &yExtent);
\r
549 m_pSGP->setTextSize (charheight);
\r
551 m_pSGP->setTextAngle (0.0);
\r
555 /*------------------------------*/
556 /* adjust frame for tick labels */
557 /*------------------------------*/
559 // Calc offset of tick labels from axes
560 if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
562 else if (o_xticks == BELOW)
563 xtl_ofs = -0.5 * charheight;
564 else if (o_xticks == ABOVE)
565 xtl_ofs = 0.5 * charheight;
567 if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
569 else if (o_yticks == LEFT)
570 ytl_ofs = -(2 + y_fldwid) * charwidth;
571 else if (o_yticks == RIGHT)
572 ytl_ofs = 1.5 * charwidth;
574 xa_max -= 0.7 * x_fldwid * charwidth; // make room for last x tick label
\r
581 // see if need to shrink axis extents and/or tick extents
582 if (xtl_ofs != 0.0 && s_ycross == FALSE) {
583 if (o_xticks == BELOW) {
584 ya_min += 1.5 * charheight;
586 } else if (o_xticks == ABOVE) {
588 yt_min = ya_min + 1.5 * charheight;
590 } else // noaxis, no t-labels, or user set cross
593 if (ytl_ofs != 0.0 && s_xcross == FALSE) {
594 if (o_yticks == LEFT) {
595 xa_min += (2 + y_fldwid) * charwidth;
597 } else if (o_yticks == RIGHT) {
599 xt_min = xa_min + ytl_ofs + y_fldwid * charwidth;
604 // decrease size of graph, if necessary, to accommadate space
605 // between axis boundary and boundary of ticks
606 double x_added_ticks = 0; // number of tick spaces added to axis
607 double y_added_ticks = 0;
608 if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
610 if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
613 if (o_grid == TRUE) {
614 if (x_added_ticks < 0)
616 if (y_added_ticks < 0)
620 if (x_added_ticks < 0) {
621 if (o_yticks == LEFT || s_ycross)
627 if (y_added_ticks < 0) {
628 if (o_xticks == BELOW || s_xcross)
634 xn_tickinc = (xt_max - xt_min) / (x_nint + x_added_ticks);
635 yn_tickinc = (yt_max - yt_min) / (y_nint + y_added_ticks);
637 xt_min += 0.5 * x_added_ticks * xn_tickinc;
638 xt_max -= 0.5 * x_added_ticks * xn_tickinc;
639 yt_min += 0.5 * y_added_ticks * yn_tickinc;
640 yt_max -= 0.5 * y_added_ticks * yn_tickinc;
647 //------------------------------------------------------------------------
649 m_xWorldScale = (xgn_max - xgn_min) / (xgw_max - xgw_min);
650 m_yWorldScale = (ygn_max - ygn_min) / (ygw_max - ygw_min);
654 m_pSGP->setLineStyle (SGP::LS_SOLID);
657 // size of symbol in NDC
658 double symwidth = charwidth;
659 double symheight = charheight;
661 double clipRect[4];
\r
662 clipRect[0] = xgn_min; clipRect[1] = ygn_min; clipRect[2] = xgn_max; clipRect[3] = ygn_max;
\r
664 for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
665 const EZPlotCurve* const pCurve = m_vecCurves [iCurve];
667 m_pSGP->setColor (getColor (iCurve));
\r
668 int iSym = getSymbol (iCurve);
\r
669 int iLS = getLinestyle (iCurve);
\r
671 if (iLS != SGP::LS_NOLINE) {
672 m_pSGP->setLineStyle (iLS);
673 double x1 = convertWorldToNDC_X (pCurve->x[0]);
674 double y1 = convertWorldToNDC_Y (pCurve->y[0]);
\r
675 for (int i = 1; i < pCurve->m_iPointCount; i++) {
676 double x2 = convertWorldToNDC_X (pCurve->x[i]);
677 double y2 = convertWorldToNDC_Y (pCurve->y[i]);
\r
678 double x2Clip = x2;
\r
679 double y2Clip = y2;
\r
680 if (clip_rect (x1, y1, x2Clip, y2Clip, clipRect)) {
\r
681 m_pSGP->moveAbs (x1, y1);
\r
682 m_pSGP->lineAbs (x2Clip, y2Clip);
\r
689 int iSymFreq = getSymbolFreq (iCurve);
690 m_pSGP->setLineStyle (SGP::LS_SOLID);
691 double x = convertWorldToNDC_X (pCurve->x[0]);
692 double y = convertWorldToNDC_Y (pCurve->y[0]);
693 m_pSGP->moveAbs (x, y);
694 symbol (iSym, symwidth, symheight);
695 for (int i = 1; i < pCurve->m_iPointCount; i++)
696 if (i % iSymFreq == 0 || i == pCurve->m_iPointCount - 1) {
697 x = convertWorldToNDC_X (pCurve->x[i]);
698 y = convertWorldToNDC_Y (pCurve->y[i]);
\r
699 if (y >= ygn_min && y <= ygn_max) {
700 m_pSGP->moveAbs (x, y);
701 symbol (iSym, symwidth, symheight);
\r
711 * drawAxes INTERNAL routine to draw axis & label them
721 double xticklen = 0, yticklen = 0; // length of ticks in NDC
722 double minorinc; // increment between minor axes
723 double xaxispos, yaxispos; // crossing of axes
725 bool axis_near; // TRUE if axis too close to print t-label
730 m_pSGP->setTextSize (charheight);
731 m_pSGP->setTextColor (1, -1);
733 if (o_xticks == ABOVE)
734 xticklen = 0.5 * charheight;
735 else if (o_xticks == BELOW)
736 xticklen = -0.5 * charheight;
738 if (o_yticks == RIGHT)
739 yticklen = charwidth;
740 else if (o_yticks == LEFT)
741 yticklen = -charwidth;
743 if (c_title.length() > 0) {
745 m_pSGP->setTextSize (charheight * 2.0);
746 m_pSGP->getTextExtent (c_title.c_str(), &wText, &hText);
747 m_pSGP->moveAbs (xa_min + (xa_max-xa_min)/2 - wText/2, title_row);
748 m_pSGP->setTextColor (clr_title, -1);
749 m_pSGP->drawText (c_title);
750 m_pSGP->setTextSize (charheight);
753 if (o_grid == TRUE || o_box == TRUE) {
754 m_pSGP->setColor (clr_axis);
755 m_pSGP->moveAbs (xa_min, ya_min);
756 m_pSGP->lineAbs (xa_max, ya_min);
757 m_pSGP->lineAbs (xa_max, ya_max);
758 m_pSGP->lineAbs (xa_min, ya_max);
759 m_pSGP->lineAbs (xa_min, ya_min);
762 // calculate position of axes
765 if (s_ycross == TRUE) { // convert users' world-coord
766 xaxispos = convertWorldToNDC_Y (v_ycross);// axis to its position in NDC
767 x = convertWorldToNDC_X (xgw_min);
772 if (s_xcross == TRUE) { // convert users' world-coord
773 yaxispos = convertWorldToNDC_X (v_xcross);// axis to its NDC position
774 y = convertWorldToNDC_Y (ygw_min);
782 if (o_xaxis == LINEAR) {
785 m_pSGP->setColor (clr_axis);
786 if (o_tag && !o_grid && !o_box && s_xcross) {
787 m_pSGP->moveAbs (xa_min, xaxispos - charheight);
788 m_pSGP->lineAbs (xa_min, xaxispos + charheight);
790 m_pSGP->moveAbs (xa_min, xaxispos);
791 m_pSGP->lineAbs (xa_max, xaxispos);
792 if (o_tag && !o_grid && !o_box) {
793 m_pSGP->moveAbs (xa_max, xaxispos - charheight);
794 m_pSGP->lineAbs (xa_max, xaxispos + charheight);
797 if (o_grid == TRUE) {
798 m_pSGP->setColor (clr_grid);
799 for (i = 0; i <= x_nint; i++) {
800 m_pSGP->moveAbs (xt_min + xn_tickinc * i, ya_max);
801 m_pSGP->lineAbs (xt_min + xn_tickinc * i, ya_min);
804 m_pSGP->setTextSize (charheight * 1.5);
805 m_pSGP->setTextColor (clr_label, -1);
\r
806 double wText, hText;
\r
807 m_pSGP->getTextExtent (c_xlabel.c_str(), &wText, &hText);
808 m_pSGP->moveAbs (xa_min + (xa_max-xa_min)/2 - wText / 2, xlbl_row + charheight * 1.5);
\r
809 m_pSGP->drawText (c_xlabel);
810 m_pSGP->setTextSize (charheight);
811 minorinc = xn_tickinc / (o_xminortick + 1);
813 for (i = 0; i <= x_nint; i++) {
814 x = xt_min + xn_tickinc * i;
815 m_pSGP->setColor (clr_axis);
816 m_pSGP->moveAbs (x, xaxispos);
817 m_pSGP->lineAbs (x, xaxispos + xticklen);
819 for (j = 1; j <= o_xminortick; j++) {
820 x2 = x + minorinc * j;
821 m_pSGP->moveAbs (x2, xaxispos);
822 m_pSGP->lineAbs (x2, xaxispos + TICKRATIO * xticklen);
825 if (xaxispos + xtl_ofs > ya_min && o_yaxis != NOAXIS) {
826 double xw = xgw_min + i * xw_tickinc;
827 double x = convertWorldToNDC_X (xw);
828 double d = x - yaxispos;
829 if (o_yticks == RIGHT && d >= 0 && d < 0.9 * xn_tickinc)
831 if (o_yticks == LEFT && d <= 0 && d > -0.9 * xn_tickinc)
835 if (o_xtlabel == TRUE && axis_near == FALSE) {
836 snprintf (str, sizeof(str), x_numfmt, xgw_min + xw_tickinc * i);
837 numstr = str_skip_head (str, " ");
838 m_pSGP->moveAbs (x-strlen(numstr)*charwidth/2, xaxispos + xtl_ofs);
839 m_pSGP->setTextColor (clr_number, -1);
840 m_pSGP->drawText (numstr);
850 if (o_yaxis == LINEAR) {
852 m_pSGP->setColor (clr_axis);
853 if (o_tag && !o_grid && !o_box && s_ycross) {
854 m_pSGP->moveAbs (yaxispos - charwidth, ya_min);
855 m_pSGP->lineAbs (yaxispos + charwidth, ya_min);
857 m_pSGP->moveAbs (yaxispos, ya_min);
858 m_pSGP->lineAbs (yaxispos, ya_max);
859 if (o_tag && !o_grid && !o_box) {
860 m_pSGP->moveAbs (yaxispos - charwidth, ya_max);
861 m_pSGP->lineAbs (yaxispos + charwidth, ya_max);
864 if (o_grid == TRUE) {
865 m_pSGP->setColor (clr_grid);
866 for (i = 0; i <= y_nint; i++) {
867 y = yt_min + yn_tickinc * i;
868 m_pSGP->moveAbs (xa_max, y);
869 m_pSGP->lineAbs (xa_min, y);
872 m_pSGP->setTextAngle (HALFPI);
873 m_pSGP->setTextSize (1.5 * charheight);
874 m_pSGP->setTextColor (clr_label, -1);
\r
875 double xExtent, yExtent;
\r
876 m_pSGP->getTextExtent (c_ylabel.c_str(), &xExtent, &yExtent);
\r
877 m_pSGP->moveAbs (ylbl_col, ya_min + (ya_max-ya_min)/2 - yExtent / 2);
\r
878 m_pSGP->drawText (c_ylabel);
\r
879 m_pSGP->setTextAngle (0.0);
880 m_pSGP->setTextSize (charheight);
881 minorinc = yn_tickinc / (o_yminortick + 1);
883 for (i = 0; i <= y_nint; i++) {
884 y = yt_min + yn_tickinc * i;
885 m_pSGP->setColor (clr_axis);
886 m_pSGP->moveAbs (yaxispos, y);
887 m_pSGP->lineAbs (yaxispos + yticklen, y);
889 for (j = 1; j <= o_yminortick; j++) {
890 y2 = y + minorinc * j;
891 m_pSGP->moveAbs (yaxispos, y2);
892 m_pSGP->lineAbs (yaxispos + TICKRATIO * yticklen, y2);
895 if (yaxispos + ytl_ofs > xa_min && o_xaxis != NOAXIS) {
896 double yw = ygw_min + i * yw_tickinc;
897 double y = convertWorldToNDC_Y (yw);
898 double d = y - xaxispos;
899 if (o_xticks == ABOVE && d >= 0 && d < 0.9 * yn_tickinc)
901 if (o_xticks == BELOW && d <= 0 && d > -0.9 * yn_tickinc)
904 if (o_ytlabel == TRUE && axis_near == FALSE) {
905 snprintf (str, sizeof(str), y_numfmt, ygw_min + yw_tickinc * i);
\r
906 m_pSGP->moveAbs (yaxispos + ytl_ofs, y + 0.5 * charheight);
\r
907 m_pSGP->setTextColor (clr_number, -1);
\r
908 m_pSGP->drawText (str);
916 EZPlot::symbol (int sym, double symwidth, double symheight)
921 if (sym == SB_CROSS) {
922 m_pSGP->moveRel (-0.5 * symwidth, -0.5 * symheight);
923 m_pSGP->lineRel (symwidth, symheight);
924 m_pSGP->moveRel (-symwidth, 0.0);
925 m_pSGP->lineRel (symwidth, -symheight);
926 m_pSGP->moveRel (-0.5 * symwidth, 0.5 * symheight);
927 } else if (sym == SB_PLUS) {
928 m_pSGP->moveRel (-0.5 * symwidth, 0.0);
929 m_pSGP->lineRel (symwidth, 0.0);
930 m_pSGP->moveRel (-0.5 * symwidth, -0.5 * symheight);
931 m_pSGP->lineRel (0.0, symheight);
932 m_pSGP->moveRel (0.0, -0.5 * symheight);
933 } else if (sym == SB_BOX) {
934 m_pSGP->moveRel (-0.5 * symwidth, -0.5 * symheight);
935 m_pSGP->lineRel (symwidth, 0.0);
936 m_pSGP->lineRel (0.0, symheight);
937 m_pSGP->lineRel (-symwidth, 0.0);
938 m_pSGP->lineRel (0.0, -symheight);
939 m_pSGP->moveRel (0.5 * symwidth, 0.5 * symheight);
940 } else if (sym == SB_CIRCLE) {
941 m_pSGP->drawCircle (symwidth);
942 } else if (sym == SB_ERRORBAR) {
943 m_pSGP->moveRel (-0.5 * symwidth, 0.5 * symheight);
944 m_pSGP->lineRel (symwidth, 0.0);
945 m_pSGP->moveRel (-0.5 * symwidth, 0.0);
946 m_pSGP->lineRel (0.0, -symheight);
947 m_pSGP->moveRel (-0.5 * symwidth, 0.0);
948 m_pSGP->lineRel (symwidth, 0.0);
949 m_pSGP->moveRel (-0.5 * symwidth, 0.5 * symheight);
956 * axis_scale calculates graph axis scaling
959 * retval = axis_scale (min, max, nint, minp, maxp, nintp,
960 * rec_total, rec_frac)
963 * double min Smallest value to plot
964 * double max Largest value to plot
965 * int nint Number of intervals desired
968 * int retval FALSE if illegal parameters, else TRUE
969 * double *minp Minimum graph value
970 * double *maxp Maximum graph value
971 * int *nintp Number of intervals for graph
972 * int *rec_total Recommended field width for printing out the number
973 * int *rec_frac Recommended number of digits for print fraction
977 EZPlot::axis_scale (double min, double max, int nint, double *minp, double *maxp, int *nintp)
979 if (min >= max || nint < 1) {
980 sys_error (ERR_WARNING, "Invalid params: min=%lf, max=%lf, num intervals=%d [axis_scale]", min, max, nint);
985 double a = fabs(min);
986 if (fabs(min) < fabs(max))
988 double scale = pow (10.0, floor(log10(a)));
990 double mina = min / scale;
991 double maxa = max / scale;
992 double d = (maxa - mina) / nint;
994 double e = floor (log10(d));
995 double f = d / pow (10.0, e);
999 else if (f < sqrt (10.0))
1001 else if (f < sqrt (50.0))
1003 double wdt = v * pow (10.0, e);
1004 double g = floor (mina / wdt);
1005 if (fabs(g + 1 - mina / wdt) < j)
1012 double h = floor (maxa / wdt) + 1.0;
1013 if (fabs(maxa / wdt + 1 - h) < j)
1019 *nintp = static_cast<int>(h - g);
1020 if (fabs(*maxp) >= 10.0 || fabs(*minp) >= 10.0) {
1021 scale = scale * 10.0;
1033 * make_numfmt Make a numeric format string
1036 * make_numfmt (fmtstr, fldwid, nfrac, min, max, nint)
1037 * char *fmtstr Returned format string for printf()
1038 * int *fldwid Returned field width
1039 * int *nfrac If < 0, then calculate best number of
1040 * fraction places & return that value
1041 * If >= 0, then use that number of places
1042 * double min Minimum value
1043 * double max Maximum value
1044 * int nint Number of intervals between min & max
1047 * This routine is written as an INTERNAL routine for EZPLOT
1050 static inline double
1061 EZPlot::make_numfmt (char *fmtstr, int *fldwid, int *nfrac, double minval, double maxval, int nint)
1063 int wid, frac, expon;
1065 double delta = (maxval - minval) / nint;
1066 double absmin = fabs(minval);
1067 double absmax = fabs(maxval);
\r
1068 if (absmin > absmax)
\r
1070 double logt = log10( absmax );
1072 if (fabs(logt) >= 6) { // use exponential format
1073 if (fabs(logt) > 99)
1078 if (*nfrac < 0) { // calculate frac
1079 delta /= pow (10., floor(logt)); // scale delta
1080 frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
1082 frac = 1; // to be safe, add decimal pt
1083 } else // use users' frac
1086 wid = 2 + frac + expon;
1087 if (minval < 0. || maxval < 0.)
1089 sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "g");
1090 } else { // use fixed format
1091 wid = static_cast<int>(trunc(logt)) + 1;
1094 if (minval < 0. || maxval < 0.)
1097 if (*nfrac < 0) { // calculate frac
1098 if (delta >= 0.999999)
1099 frac = 1; // add a decimal pt to be safe
1101 frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
1102 } else // use users' frac
1106 sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "f");