1 /*****************************************************************************
6 ** This is part of the CTSim program
7 ** Copyright (C) 1983-2000 Kevin Rosenberg
9 ** $Id: ezplot.cpp,v 1.23 2000/12/26 21:13:15 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());
137 EZPlot::EZPlot (SGP& sgp)
144 EZPlot::initPlotSettings ()
146 charheight = rSGP.getCharHeight();
147 charwidth = rSGP.getCharWidth();
149 m_iCurrentCurve = -1;
\r
171 o_color = DEF_CURVE_CLR;
174 o_linestyle = SGP::LS_SOLID;
181 o_legendbox = INSIDE;
194 clr_axis = C_BLACK; // set fixed colors
198 clr_number = C_GREEN;
203 EZPlot::setColor (unsigned int iCurve, int iColor)
\r
205 if (m_veciColor.size() <= iCurve) {
\r
206 m_veciColor.resize ((m_iCurrentCurve + 1) * 2);
\r
207 m_vecbColorSet.resize ((m_iCurrentCurve + 1) * 2);
\r
209 m_veciColor [iCurve] = iColor;
\r
210 m_vecbColorSet [iCurve] = true;
\r
214 EZPlot::setSymbol (unsigned int iCurve, int iSymbol)
\r
216 if (m_veciSymbol.size() <= iCurve) {
\r
217 m_veciSymbol.resize ((m_iCurrentCurve + 1) * 2);
\r
218 m_vecbSymbolSet.resize ((m_iCurrentCurve + 1) * 2);
\r
220 m_veciSymbol [iCurve] = iSymbol;
\r
221 m_vecbSymbolSet [iCurve] = true;
\r
225 EZPlot::setSymbolFreq (unsigned int iCurve, int iSymbolFreq)
\r
227 if (m_veciSymbolFreq.size() <= iCurve) {
\r
228 m_veciSymbolFreq.resize ((m_iCurrentCurve + 1) * 2);
\r
229 m_vecbSymbolFreqSet.resize ((m_iCurrentCurve + 1) * 2);
\r
231 m_veciSymbolFreq [iCurve] = iSymbolFreq;
\r
232 m_vecbSymbolFreqSet [iCurve] = true;
\r
236 EZPlot::setLinestyle (unsigned int iCurve, int iLinestyle)
\r
238 if (m_veciLinestyle.size() <= iCurve) {
\r
239 m_veciLinestyle.resize ((m_iCurrentCurve + 1) * 2);
\r
240 m_vecbLinestyleSet.resize ((m_iCurrentCurve + 1) * 2);
\r
242 m_veciLinestyle [iCurve] = iLinestyle;
\r
243 m_vecbLinestyleSet [iCurve] = true;
\r
247 EZPlot::setLegend (unsigned int iCurve, const std::string& strLegend)
\r
249 if (m_vecsLegend.size() <= iCurve) {
\r
250 m_vecsLegend.resize ((m_iCurrentCurve + 1) * 2);
\r
251 m_vecbLegendSet.resize ((m_iCurrentCurve + 1) * 2);
\r
253 m_vecsLegend [iCurve] = strLegend;
\r
254 m_vecbLegendSet [iCurve] = true;
\r
258 EZPlot::setLegend (unsigned int iCurve, const char* const pszLegend)
\r
260 if (m_vecsLegend.size() <= iCurve) {
\r
261 m_vecsLegend.resize ((m_iCurrentCurve + 1) * 2);
\r
262 m_vecbLegendSet.resize ((m_iCurrentCurve + 1) * 2);
\r
264 m_vecsLegend [iCurve] = pszLegend;
\r
265 m_vecbLegendSet [iCurve] = true;
\r
269 EZPlot::getColor (unsigned int iCurve) const
\r
271 if (m_veciColor.size() > iCurve && m_vecbColorSet[iCurve])
\r
272 return m_veciColor[iCurve];
\r
278 EZPlot::getSymbol (unsigned int iCurve) const
\r
280 if (m_veciSymbol.size() > iCurve && m_vecbSymbolSet[iCurve])
\r
281 return m_veciSymbol[iCurve];
\r
287 EZPlot::getSymbolFreq (unsigned int iCurve) const
\r
289 if (m_veciSymbolFreq.size() > iCurve && m_vecbSymbolFreqSet[iCurve])
\r
290 return m_veciSymbolFreq[iCurve];
\r
296 EZPlot::getLinestyle (unsigned int iCurve) const
\r
298 if (m_veciLinestyle.size() > iCurve && m_vecbLinestyleSet[iCurve])
\r
299 return m_veciLinestyle[iCurve];
\r
301 return o_linestyle;
\r
305 EZPlot::getLegend (unsigned int iCurve) const
\r
307 if (m_vecsLegend.size() > iCurve && m_vecbLegendSet[iCurve])
\r
308 return &m_vecsLegend[iCurve];
\r
315 * plot Plots all curves collected by addCurves ()
321 * This routine plots the curves that have stored by addCurves().
324 * drawAxes() & make_numfmt()
330 if (m_vecCurves.size() <= 0)
333 rSGP.setWindow (0., 0., 1., 1.);
335 if (s_textsize == TRUE)
336 rSGP.setTextPointSize (v_textsize);
\r
337 charheight = rSGP.getCharHeight();
338 charwidth = rSGP.getCharWidth();
340 const EZPlotCurve& firstCurve = *m_vecCurves[0];
\r
341 double xmin = firstCurve.x[0]; // extent of curves in world coord
343 double ymin = firstCurve.y[0];
344 double ymax = ymin;
\r
346 for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
347 const EZPlotCurve* const pCurve = m_vecCurves [iCurve];
349 for (int ip = 0; ip < pCurve->m_iPointCount; ip++) {
350 if (pCurve->x[ip] > xmax)
351 xmax = pCurve->x[ip];
352 else if (pCurve->x[ip] < xmin)
353 xmin = pCurve->x[ip];
354 if (pCurve->y[ip] > ymax)
355 ymax = pCurve->y[ip];
356 else if (pCurve->y[ip] < ymin)
357 ymin = pCurve->y[ip];
361 // extend graph limits for user defined axis cross positions
362 if (s_xcross == TRUE) {
365 else if (v_xcross > xmax)
369 if (s_ycross == TRUE) {
372 else if (v_ycross > ymax)
376 // find nice endpoints for axes
377 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))
380 // check if user set x-axis extents
381 if (s_xmin == TRUE) {
383 x_nint = o_xmajortick - 1;
385 if (s_xmax == TRUE) {
387 x_nint = o_xmajortick - 1;
390 // check if user set y-axis extents
391 if (s_ymin == TRUE) {
393 y_nint = o_ymajortick - 1;
395 if (s_ymax == TRUE) {
397 y_nint = o_ymajortick - 1;
400 // calculate increment between major axis in world coordinates
401 xw_tickinc = (xgw_max - xgw_min) / x_nint;
402 yw_tickinc = (ygw_max - ygw_min) / y_nint;
404 // we have now calcuated xgw_min, xyw_max, ygw_min, & ygw_max
406 // set the number of decimal point to users' setting or default
407 // Two formats for numbers: Fixed: -nnn.f and Exponent: -n.fffE+eee
408 if (s_lxfrac == TRUE)
413 if (s_lyfrac == TRUE)
418 make_numfmt (x_numfmt, &x_fldwid, &x_frac, xgw_min, xgw_max, x_nint);
419 make_numfmt (y_numfmt, &y_fldwid, &y_frac, ygw_min, ygw_max, y_nint);
421 xtl_wid = x_fldwid * charwidth; // calc size of tick labels
422 ytl_wid = y_fldwid * charwidth;
423 tl_height = charheight;
425 // calculate the extent of the plot frame
428 xp_max = xp_min + o_xlength;
429 yp_max = yp_min + o_ylength;
431 xp_min = clamp (xp_min, 0., 1.);
432 xp_max = clamp (xp_max, 0., 1.);
433 yp_min = clamp (yp_min, 0., 1.);
434 yp_max = clamp (yp_max, 0., 1.);
436 xa_min = xp_min; // extent of axes
441 // adjust frame for title
442 title_row = ya_max;;
\r
443 if (c_title.length() > 0)
444 ya_max -= 2 * charheight;
\r
446 // calculate legend box boundaries
447 int max_leg = 0; // longest legend in characters
448 int num_leg = 0; // number of legend titles
\r
449 for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
450 const std::string* pstrLegend = getLegend (iCurve);
\r
451 if (pstrLegend && pstrLegend->length() > 0) {
\r
452 int nLegend = pstrLegend->length();
455 if (nLegend > max_leg)
\r
461 if (num_leg > 0 && o_legendbox != NOLEGEND) {
462 double leg_width = (max_leg + 2) * charwidth; // size of legend box
463 double leg_height = num_leg * 3 * charheight;
465 if (s_xlegend == TRUE)
469 if (o_legendbox == OUTSIDE)
470 xa_max -= (leg_width + 0.5 * charwidth);
472 xl_min = xl_max - leg_width;
474 if (s_ylegend == TRUE)
479 yl_min = yl_max - leg_height;
481 rSGP.setColor (clr_legend);
482 rSGP.drawRect (xl_min, yl_min, xl_max, yl_max);
484 int iLegend = 0; // current legend position
\r
485 for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
486 const std::string* pstrLegend = getLegend (iCurve);
487 if (! pstrLegend || pstrLegend->length() == 0)
490 double xmin = xl_min + 1.0 * charwidth;
491 double xmax = xl_max - 1.0 * charwidth;
492 double y = yl_max - (2.0 + iLegend * 3) * charheight;
494 rSGP.moveAbs (xmin, y + 0.5 * charheight);
\r
495 rSGP.drawText (pstrLegend->c_str());
496 rSGP.setColor (getColor (iCurve));
\r
497 int iLS = getLinestyle (iCurve);
498 if (iLS != SGP::LS_NOLINE) {
499 rSGP.setLineStyle (iLS);
500 rSGP.moveAbs (xmin, y);
501 rSGP.lineAbs (xmax, y);
503 int iSymbol = getSymbol (iCurve);
505 double xinc = (xmax - xmin) / (5 - 1);
506 rSGP.setLineStyle (SGP::LS_SOLID);
507 for (int j = 0; j < 5; j++) {
508 rSGP.moveAbs (xmin + j * xinc, y);
509 symbol(iSymbol, 0.5 * charwidth, 0.5 * charheight);
512 ++iLegend; // move to next legend position
514 } // end legend printing
516 // calculate the extent of the axes
518 /*-------------------------*/
519 /* adjust frame for labels */
520 /*-------------------------*/
523 if (c_xlabel.length() > 0)
524 ya_min += 1.5 * charheight;
525 xlbl_row = xp_min; // put x-label on bottom of plot frame
528 if (c_ylabel.length() > 0) {
\r
529 rSGP.setTextAngle (HALFPI);
\r
530 rSGP.setTextSize (1.5 * charheight);
\r
531 double xExtent, yExtent;
\r
532 rSGP.getTextExtent (c_ylabel.c_str(), &xExtent, &yExtent);
\r
533 rSGP.setTextSize (charheight);
\r
535 rSGP.setTextAngle (0.0);
\r
539 /*------------------------------*/
540 /* adjust frame for tick labels */
541 /*------------------------------*/
543 // Calc offset of tick labels from axes
544 if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
546 else if (o_xticks == BELOW)
547 xtl_ofs = -1.5 * charheight;
548 else if (o_xticks == ABOVE)
549 xtl_ofs = 1.5 * charheight;
551 if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
553 else if (o_yticks == LEFT)
554 ytl_ofs = -(1 + y_fldwid) * charwidth;
555 else if (o_yticks == RIGHT)
556 ytl_ofs = 1.5 * charwidth;
563 // see if need to shrink axis extents and/or tick extents
564 if (xtl_ofs != 0.0 && s_ycross == FALSE) {
565 if (o_xticks == BELOW) {
566 ya_min += 2.5 * charheight;
568 } else if (o_xticks == ABOVE) {
570 yt_min = ya_min + 2.5 * charheight;
572 } else // noaxis, no t-labels, or user set cross
575 if (ytl_ofs != 0.0 && s_xcross == FALSE) {
576 if (o_yticks == LEFT) {
577 xa_min += (1 + y_fldwid) * charwidth;
579 } else if (o_yticks == RIGHT) {
581 xt_min = xa_min + ytl_ofs + y_fldwid * charwidth;
586 // decrease size of graph, if necessary, to accommadate space
587 // between axis boundary and boundary of ticks
588 double x_added_ticks = 0; // number of tick spaces added to axis
589 double y_added_ticks = 0;
590 if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
592 if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
595 if (o_grid == TRUE) {
596 if (x_added_ticks < 0)
598 if (y_added_ticks < 0)
602 if (x_added_ticks < 0) {
603 if (o_yticks == LEFT || s_ycross)
609 if (y_added_ticks < 0) {
610 if (o_xticks == BELOW || s_xcross)
616 xn_tickinc = (xt_max - xt_min) / (x_nint + x_added_ticks);
617 yn_tickinc = (yt_max - yt_min) / (y_nint + y_added_ticks);
619 xt_min += 0.5 * x_added_ticks * xn_tickinc;
620 xt_max -= 0.5 * x_added_ticks * xn_tickinc;
621 yt_min += 0.5 * y_added_ticks * yn_tickinc;
622 yt_max -= 0.5 * y_added_ticks * yn_tickinc;
629 //------------------------------------------------------------------------
631 m_xWorldScale = (xgn_max - xgn_min) / (xgw_max - xgw_min);
632 m_yWorldScale = (ygn_max - ygn_min) / (ygw_max - ygw_min);
636 rSGP.setLineStyle (SGP::LS_SOLID);
639 // size of symbol in NDC
640 double symwidth = charwidth;
641 double symheight = charheight;
643 double clipRect[4];
\r
644 clipRect[0] = xgn_min; clipRect[1] = ygn_min; clipRect[2] = xgn_max; clipRect[3] = ygn_max;
\r
646 for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
647 const EZPlotCurve* const pCurve = m_vecCurves [iCurve];
649 rSGP.setColor (getColor (iCurve));
\r
650 int iSym = getSymbol (iCurve);
\r
651 int iLS = getLinestyle (iCurve);
\r
653 if (iLS != SGP::LS_NOLINE) {
654 rSGP.setLineStyle (iLS);
655 double x1 = convertWorldToNDC_X (pCurve->x[0]);
656 double y1 = convertWorldToNDC_Y (pCurve->y[0]);
\r
657 for (int i = 1; i < pCurve->m_iPointCount; i++) {
658 double x2 = convertWorldToNDC_X (pCurve->x[i]);
659 double y2 = convertWorldToNDC_Y (pCurve->y[i]);
\r
660 double x2Clip = x2;
\r
661 double y2Clip = y2;
\r
662 if (clip_rect (x1, y1, x2Clip, y2Clip, clipRect)) {
\r
663 rSGP.moveAbs (x1, y1);
\r
664 rSGP.lineAbs (x2Clip, y2Clip);
\r
671 int iSymFreq = getSymbolFreq (iCurve);
672 rSGP.setLineStyle (SGP::LS_SOLID);
673 double x = convertWorldToNDC_X (pCurve->x[0]);
674 double y = convertWorldToNDC_Y (pCurve->y[0]);
676 symbol (iSym, symwidth, symheight);
677 for (int i = 1; i < pCurve->m_iPointCount; i++)
678 if (i % iSymFreq == 0 || i == pCurve->m_iPointCount - 1) {
679 x = convertWorldToNDC_X (pCurve->x[i]);
680 y = convertWorldToNDC_Y (pCurve->y[i]);
\r
681 if (y >= ygn_min && y <= ygn_max) {
683 symbol (iSym, symwidth, symheight);
\r
693 * drawAxes INTERNAL routine to draw axis & label them
703 double xticklen = 0, yticklen = 0; // length of ticks in NDC
704 double minorinc; // increment between minor axes
705 double xaxispos, yaxispos; // crossing of axes
707 bool axis_near; // TRUE if axis too close to print t-label
712 rSGP.setTextSize (charheight);
713 rSGP.setTextColor (1, -1);
715 if (o_xticks == ABOVE)
716 xticklen = charheight;
717 else if (o_xticks == BELOW)
718 xticklen = -charheight;
720 if (o_yticks == RIGHT)
721 yticklen = charwidth;
722 else if (o_yticks == LEFT)
723 yticklen = -charwidth;
725 if (c_title.length() > 0) {
727 rSGP.setTextSize (charheight * 2.0);
728 rSGP.getTextExtent (c_title.c_str(), &wText, &hText);
729 rSGP.moveAbs (xa_min + (xa_max-xa_min)/2 - wText/2, title_row);
730 rSGP.setTextColor (clr_title, -1);
731 rSGP.drawText (c_title);
732 rSGP.setTextSize (charheight);
735 if (o_grid == TRUE || o_box == TRUE) {
736 rSGP.setColor (clr_axis);
737 rSGP.moveAbs (xa_min, ya_min);
738 rSGP.lineAbs (xa_max, ya_min);
739 rSGP.lineAbs (xa_max, ya_max);
740 rSGP.lineAbs (xa_min, ya_max);
741 rSGP.lineAbs (xa_min, ya_min);
744 // calculate position of axes
747 if (s_ycross == TRUE) { // convert users' world-coord
748 xaxispos = convertWorldToNDC_Y (v_ycross);// axis to its position in NDC
749 x = convertWorldToNDC_X (xgw_min);
754 if (s_xcross == TRUE) { // convert users' world-coord
755 yaxispos = convertWorldToNDC_X (v_xcross);// axis to its NDC position
756 y = convertWorldToNDC_Y (ygw_min);
764 if (o_xaxis == LINEAR) {
767 rSGP.setColor (clr_axis);
768 if (o_tag && !o_grid && !o_box && s_xcross) {
769 rSGP.moveAbs (xa_min, xaxispos - charheight);
770 rSGP.lineAbs (xa_min, xaxispos + charheight);
772 rSGP.moveAbs (xa_min, xaxispos);
773 rSGP.lineAbs (xa_max, xaxispos);
774 if (o_tag && !o_grid && !o_box) {
775 rSGP.moveAbs (xa_max, xaxispos - charheight);
776 rSGP.lineAbs (xa_max, xaxispos + charheight);
779 if (o_grid == TRUE) {
780 rSGP.setColor (clr_grid);
781 for (i = 0; i <= x_nint; i++) {
782 rSGP.moveAbs (xt_min + xn_tickinc * i, ya_max);
783 rSGP.lineAbs (xt_min + xn_tickinc * i, ya_min);
786 rSGP.setTextSize (charheight * 1.5);
787 rSGP.setTextColor (clr_label, -1);
\r
788 double wText, hText;
\r
789 rSGP.getTextExtent (c_xlabel.c_str(), &wText, &hText);
790 rSGP.moveAbs (xa_min + (xa_max-xa_min)/2 - wText / 2, xlbl_row + charheight * 1.5);
\r
791 rSGP.drawText (c_xlabel);
792 rSGP.setTextSize (charheight);
793 minorinc = xn_tickinc / (o_xminortick + 1);
795 for (i = 0; i <= x_nint; i++) {
796 x = xt_min + xn_tickinc * i;
797 rSGP.setColor (clr_axis);
798 rSGP.moveAbs (x, xaxispos);
799 rSGP.lineAbs (x, xaxispos + xticklen);
801 for (j = 1; j <= o_xminortick; j++) {
802 x2 = x + minorinc * j;
803 rSGP.moveAbs (x2, xaxispos);
804 rSGP.lineAbs (x2, xaxispos + TICKRATIO * xticklen);
807 if (xaxispos + xtl_ofs > ya_min && o_yaxis != NOAXIS) {
808 double xw = xgw_min + i * xw_tickinc;
809 double x = convertWorldToNDC_X (xw);
810 double d = x - yaxispos;
811 if (o_yticks == RIGHT && d >= 0 && d < 0.9 * xn_tickinc)
813 if (o_yticks == LEFT && d <= 0 && d > -0.9 * xn_tickinc)
817 if (o_xtlabel == TRUE && axis_near == FALSE) {
818 snprintf (str, sizeof(str), x_numfmt, xgw_min + xw_tickinc * i);
819 numstr = str_skip_head (str, " ");
820 rSGP.moveAbs (x-strlen(numstr)*charwidth/2, xaxispos + xtl_ofs);
821 rSGP.setTextColor (clr_number, -1);
822 rSGP.drawText (numstr);
832 if (o_yaxis == LINEAR) {
834 rSGP.setColor (clr_axis);
835 if (o_tag && !o_grid && !o_box && s_ycross) {
836 rSGP.moveAbs (yaxispos - charwidth, ya_min);
837 rSGP.lineAbs (yaxispos + charwidth, ya_min);
839 rSGP.moveAbs (yaxispos, ya_min);
840 rSGP.lineAbs (yaxispos, ya_max);
841 if (o_tag && !o_grid && !o_box) {
842 rSGP.moveAbs (yaxispos - charwidth, ya_max);
843 rSGP.lineAbs (yaxispos + charwidth, ya_max);
846 if (o_grid == TRUE) {
847 rSGP.setColor (clr_grid);
848 for (i = 0; i <= y_nint; i++) {
849 y = yt_min + yn_tickinc * i;
850 rSGP.moveAbs (xa_max, y);
851 rSGP.lineAbs (xa_min, y);
854 rSGP.setTextAngle (HALFPI);
855 rSGP.setTextSize (1.5 * charheight);
856 rSGP.setTextColor (clr_label, -1);
\r
857 double xExtent, yExtent;
\r
858 rSGP.getTextExtent (c_ylabel.c_str(), &xExtent, &yExtent);
\r
859 rSGP.moveAbs (ylbl_col, ya_min + (ya_max-ya_min)/2 - yExtent / 2);
\r
860 rSGP.drawText (c_ylabel);
\r
861 rSGP.setTextAngle (0.0);
862 rSGP.setTextSize (charheight);
863 minorinc = yn_tickinc / (o_yminortick + 1);
865 for (i = 0; i <= y_nint; i++) {
866 y = yt_min + yn_tickinc * i;
867 rSGP.setColor (clr_axis);
868 rSGP.moveAbs (yaxispos, y);
869 rSGP.lineAbs (yaxispos + yticklen, y);
871 for (j = 1; j <= o_yminortick; j++) {
872 y2 = y + minorinc * j;
873 rSGP.moveAbs (yaxispos, y2);
874 rSGP.lineAbs (yaxispos + TICKRATIO * yticklen, y2);
877 if (yaxispos + ytl_ofs > xa_min && o_xaxis != NOAXIS) {
878 double yw = ygw_min + i * yw_tickinc;
879 double y = convertWorldToNDC_Y (yw);
880 double d = y - xaxispos;
881 if (o_xticks == ABOVE && d >= 0 && d < 0.9 * yn_tickinc)
883 if (o_xticks == BELOW && d <= 0 && d > -0.9 * yn_tickinc)
886 if (o_ytlabel == TRUE && axis_near == FALSE) {
887 snprintf (str, sizeof(str), y_numfmt, ygw_min + yw_tickinc * i);
888 rSGP.moveAbs (yaxispos + ytl_ofs, y + 0.5 * charheight);
889 rSGP.setTextColor (clr_number, -1);
\r
898 EZPlot::symbol (int sym, double symwidth, double symheight)
903 if (sym == SB_CROSS) {
904 rSGP.moveRel (-0.5 * symwidth, -0.5 * symheight);
905 rSGP.lineRel (symwidth, symheight);
906 rSGP.moveRel (-symwidth, 0.0);
907 rSGP.lineRel (symwidth, -symheight);
908 rSGP.moveRel (-0.5 * symwidth, 0.5 * symheight);
909 } else if (sym == SB_PLUS) {
910 rSGP.moveRel (-0.5 * symwidth, 0.0);
911 rSGP.lineRel (symwidth, 0.0);
912 rSGP.moveRel (-0.5 * symwidth, -0.5 * symheight);
913 rSGP.lineRel (0.0, symheight);
914 rSGP.moveRel (0.0, -0.5 * symheight);
915 } else if (sym == SB_BOX) {
916 rSGP.moveRel (-0.5 * symwidth, -0.5 * symheight);
917 rSGP.lineRel (symwidth, 0.0);
918 rSGP.lineRel (0.0, symheight);
919 rSGP.lineRel (-symwidth, 0.0);
920 rSGP.lineRel (0.0, -symheight);
921 rSGP.moveRel (0.5 * symwidth, 0.5 * symheight);
922 } else if (sym == SB_CIRCLE) {
923 rSGP.drawCircle (symwidth);
924 } else if (sym == SB_ERRORBAR) {
925 rSGP.moveRel (-0.5 * symwidth, 0.5 * symheight);
926 rSGP.lineRel (symwidth, 0.0);
927 rSGP.moveRel (-0.5 * symwidth, 0.0);
928 rSGP.lineRel (0.0, -symheight);
929 rSGP.moveRel (-0.5 * symwidth, 0.0);
930 rSGP.lineRel (symwidth, 0.0);
931 rSGP.moveRel (-0.5 * symwidth, 0.5 * symheight);
938 * axis_scale calculates graph axis scaling
941 * retval = axis_scale (min, max, nint, minp, maxp, nintp,
942 * rec_total, rec_frac)
945 * double min Smallest value to plot
946 * double max Largest value to plot
947 * int nint Number of intervals desired
950 * int retval FALSE if illegal parameters, else TRUE
951 * double *minp Minimum graph value
952 * double *maxp Maximum graph value
953 * int *nintp Number of intervals for graph
954 * int *rec_total Recommended field width for printing out the number
955 * int *rec_frac Recommended number of digits for print fraction
959 EZPlot::axis_scale (double min, double max, int nint, double *minp, double *maxp, int *nintp)
961 if (min >= max || nint < 1) {
962 sys_error (ERR_WARNING, "Invalid params: min=%lf, max=%lf, num intervals=%d [axis_scale]", min, max, nint);
967 double a = fabs(min);
968 if (fabs(min) < fabs(max))
970 double scale = pow (10.0, floor(log10(a)));
972 double mina = min / scale;
973 double maxa = max / scale;
974 double d = (maxa - mina) / nint;
976 double e = floor (log10(d));
977 double f = d / pow (10.0, e);
981 else if (f < sqrt (10.0))
983 else if (f < sqrt (50.0))
985 double wdt = v * pow (10.0, e);
986 double g = floor (mina / wdt);
987 if (fabs(g + 1 - mina / wdt) < j)
994 double h = floor (maxa / wdt) + 1.0;
995 if (fabs(maxa / wdt + 1 - h) < j)
1001 *nintp = static_cast<int>(h - g);
1002 if (fabs(*maxp) >= 10.0 || fabs(*minp) >= 10.0) {
1003 scale = scale * 10.0;
1015 * make_numfmt Make a numeric format string
1018 * make_numfmt (fmtstr, fldwid, nfrac, min, max, nint)
1019 * char *fmtstr Returned format string for printf()
1020 * int *fldwid Returned field width
1021 * int *nfrac If < 0, then calculate best number of
1022 * fraction places & return that value
1023 * If >= 0, then use that number of places
1024 * double min Minimum value
1025 * double max Maximum value
1026 * int nint Number of intervals between min & max
1029 * This routine is written as an INTERNAL routine for EZPLOT
1032 static inline double
1043 EZPlot::make_numfmt (char *fmtstr, int *fldwid, int *nfrac, double minval, double maxval, int nint)
1045 int wid, frac, expon;
1047 double delta = (maxval - minval) / nint;
1048 double absmin = fabs(minval);
1049 double absmax = fabs(maxval);
\r
1050 if (absmin > absmax)
\r
1052 double logt = log10( absmax );
1054 if (fabs(logt) >= 6) { // use exponential format
1055 if (fabs(logt) > 99)
1060 if (*nfrac < 0) { // calculate frac
1061 delta /= pow (10., floor(logt)); // scale delta
1062 frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
1064 frac = 1; // to be safe, add decimal pt
1065 } else // use users' frac
1068 wid = 2 + frac + expon;
1069 if (minval < 0. || maxval < 0.)
1071 sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "g");
1072 } else { // use fixed format
1073 wid = static_cast<int>(trunc(logt)) + 1;
1076 if (minval < 0. || maxval < 0.)
1079 if (*nfrac < 0) { // calculate frac
1080 if (delta >= 0.999999)
1081 frac = 1; // add a decimal pt to be safe
1083 frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
1084 } else // use users' frac
1088 sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "f");