1 /*****************************************************************************
6 ** This is part of the CTSim program
7 ** Copyright (C) 1983-2000 Kevin Rosenberg
9 ** $Id: ezplot.cpp,v 1.22 2000/12/25 21:54:26 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;
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 += 2.0 * charheight;
525 xlbl_row = xp_min; // put x-label on bottom of plot frame
528 if (c_ylabel.length() > 0)
529 xa_min += 2.0 * charheight;
532 /*------------------------------*/
533 /* adjust frame for tick labels */
534 /*------------------------------*/
536 // Calc offset of tick labels from axes
537 if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
539 else if (o_xticks == BELOW)
540 xtl_ofs = -1.5 * charheight;
541 else if (o_xticks == ABOVE)
542 xtl_ofs = 1.5 * charheight;
544 if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
546 else if (o_yticks == LEFT)
547 ytl_ofs = -(1 + y_fldwid) * charwidth;
548 else if (o_yticks == RIGHT)
549 ytl_ofs = 1.5 * charwidth;
556 // see if need to shrink axis extents and/or tick extents
557 if (xtl_ofs != 0.0 && s_ycross == FALSE) {
558 if (o_xticks == BELOW) {
559 ya_min += 2.5 * charheight;
561 } else if (o_xticks == ABOVE) {
563 yt_min = ya_min + 2.5 * charheight;
565 } else // noaxis, no t-labels, or user set cross
568 if (ytl_ofs != 0.0 && s_xcross == FALSE) {
569 if (o_yticks == LEFT) {
570 xa_min += (1 + y_fldwid) * charwidth;
572 } else if (o_yticks == RIGHT) {
574 xt_min = xa_min + ytl_ofs + y_fldwid * charwidth;
579 // decrease size of graph, if necessary, to accommadate space
580 // between axis boundary and boundary of ticks
581 double x_added_ticks = 0; // number of tick spaces added to axis
582 double y_added_ticks = 0;
583 if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
585 if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
588 if (o_grid == TRUE) {
589 if (x_added_ticks < 0)
591 if (y_added_ticks < 0)
595 if (x_added_ticks < 0) {
596 if (o_yticks == LEFT || s_ycross)
602 if (y_added_ticks < 0) {
603 if (o_xticks == BELOW || s_xcross)
609 xn_tickinc = (xt_max - xt_min) / (x_nint + x_added_ticks);
610 yn_tickinc = (yt_max - yt_min) / (y_nint + y_added_ticks);
612 xt_min += 0.5 * x_added_ticks * xn_tickinc;
613 xt_max -= 0.5 * x_added_ticks * xn_tickinc;
614 yt_min += 0.5 * y_added_ticks * yn_tickinc;
615 yt_max -= 0.5 * y_added_ticks * yn_tickinc;
622 //------------------------------------------------------------------------
624 m_xWorldScale = (xgn_max - xgn_min) / (xgw_max - xgw_min);
625 m_yWorldScale = (ygn_max - ygn_min) / (ygw_max - ygw_min);
629 rSGP.setLineStyle (SGP::LS_SOLID);
632 // size of symbol in NDC
633 double symwidth = charwidth;
634 double symheight = charheight;
636 double clipRect[4];
\r
637 clipRect[0] = xgn_min; clipRect[1] = ygn_min; clipRect[2] = xgn_max; clipRect[3] = ygn_max;
\r
639 for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) {
640 const EZPlotCurve* const pCurve = m_vecCurves [iCurve];
642 rSGP.setColor (getColor (iCurve));
\r
643 int iSym = getSymbol (iCurve);
\r
644 int iLS = getLinestyle (iCurve);
\r
646 if (iLS != SGP::LS_NOLINE) {
647 rSGP.setLineStyle (iLS);
648 double x1 = convertWorldToNDC_X (pCurve->x[0]);
649 double y1 = convertWorldToNDC_Y (pCurve->y[0]);
\r
650 for (int i = 1; i < pCurve->m_iPointCount; i++) {
651 double x2 = convertWorldToNDC_X (pCurve->x[i]);
652 double y2 = convertWorldToNDC_Y (pCurve->y[i]);
\r
653 double x2Clip = x2;
\r
654 double y2Clip = y2;
\r
655 if (clip_rect (x1, y1, x2Clip, y2Clip, clipRect)) {
\r
656 rSGP.moveAbs (x1, y1);
\r
657 rSGP.lineAbs (x2Clip, y2Clip);
\r
664 int iSymFreq = getSymbolFreq (iCurve);
665 rSGP.setLineStyle (SGP::LS_SOLID);
666 double x = convertWorldToNDC_X (pCurve->x[0]);
667 double y = convertWorldToNDC_Y (pCurve->y[0]);
669 symbol (iSym, symwidth, symheight);
670 for (int i = 1; i < pCurve->m_iPointCount; i++)
671 if (i % iSymFreq == 0 || i == pCurve->m_iPointCount - 1) {
672 x = convertWorldToNDC_X (pCurve->x[i]);
673 y = convertWorldToNDC_Y (pCurve->y[i]);
\r
674 if (y >= ygn_min && y <= ygn_max) {
676 symbol (iSym, symwidth, symheight);
\r
686 * drawAxes INTERNAL routine to draw axis & label them
696 double xticklen = 0, yticklen = 0; // length of ticks in NDC
697 double minorinc; // increment between minor axes
698 double xaxispos, yaxispos; // crossing of axes
700 bool axis_near; // TRUE if axis too close to print t-label
705 rSGP.setTextSize (charheight);
706 rSGP.setTextColor (1, -1);
708 if (o_xticks == ABOVE)
709 xticklen = charheight;
710 else if (o_xticks == BELOW)
711 xticklen = -charheight;
713 if (o_yticks == RIGHT)
714 yticklen = charwidth;
715 else if (o_yticks == LEFT)
716 yticklen = -charwidth;
718 if (c_title.length() > 0) {
720 rSGP.setTextSize (charheight * 2.0);
721 rSGP.getTextExtent (c_title.c_str(), &wText, &hText);
722 rSGP.moveAbs (xa_min + (xa_max-xa_min)/2 - wText/2, title_row);
723 rSGP.setTextColor (clr_title, -1);
724 rSGP.drawText (c_title);
725 rSGP.setTextSize (charheight);
728 if (o_grid == TRUE || o_box == TRUE) {
729 rSGP.setColor (clr_axis);
730 rSGP.moveAbs (xa_min, ya_min);
731 rSGP.lineAbs (xa_max, ya_min);
732 rSGP.lineAbs (xa_max, ya_max);
733 rSGP.lineAbs (xa_min, ya_max);
734 rSGP.lineAbs (xa_min, ya_min);
737 // calculate position of axes
740 if (s_ycross == TRUE) { // convert users' world-coord
741 xaxispos = convertWorldToNDC_Y (v_ycross);// axis to its position in NDC
742 x = convertWorldToNDC_X (xgw_min);
747 if (s_xcross == TRUE) { // convert users' world-coord
748 yaxispos = convertWorldToNDC_X (v_xcross);// axis to its NDC position
749 y = convertWorldToNDC_Y (ygw_min);
757 if (o_xaxis == LINEAR) {
760 rSGP.setColor (clr_axis);
761 if (o_tag && !o_grid && !o_box && s_xcross) {
762 rSGP.moveAbs (xa_min, xaxispos - charheight);
763 rSGP.lineAbs (xa_min, xaxispos + charheight);
765 rSGP.moveAbs (xa_min, xaxispos);
766 rSGP.lineAbs (xa_max, xaxispos);
767 if (o_tag && !o_grid && !o_box) {
768 rSGP.moveAbs (xa_max, xaxispos - charheight);
769 rSGP.lineAbs (xa_max, xaxispos + charheight);
772 if (o_grid == TRUE) {
773 rSGP.setColor (clr_grid);
774 for (i = 0; i <= x_nint; i++) {
775 rSGP.moveAbs (xt_min + xn_tickinc * i, ya_max);
776 rSGP.lineAbs (xt_min + xn_tickinc * i, ya_min);
779 rSGP.moveAbs (xa_min + (xa_max-xa_min)/2 - c_xlabel.length()*charwidth * 1.5, xlbl_row + charheight * 1.5);
780 rSGP.setTextSize (charheight * 1.5);
781 rSGP.setTextColor (clr_label, -1);
782 rSGP.drawText (c_xlabel);
783 rSGP.setTextSize (charheight);
784 minorinc = xn_tickinc / (o_xminortick + 1);
786 for (i = 0; i <= x_nint; i++) {
787 x = xt_min + xn_tickinc * i;
788 rSGP.setColor (clr_axis);
789 rSGP.moveAbs (x, xaxispos);
790 rSGP.lineAbs (x, xaxispos + xticklen);
792 for (j = 1; j <= o_xminortick; j++) {
793 x2 = x + minorinc * j;
794 rSGP.moveAbs (x2, xaxispos);
795 rSGP.lineAbs (x2, xaxispos + TICKRATIO * xticklen);
798 if (xaxispos + xtl_ofs > ya_min && o_yaxis != NOAXIS) {
799 double xw = xgw_min + i * xw_tickinc;
800 double x = convertWorldToNDC_X (xw);
801 double d = x - yaxispos;
802 if (o_yticks == RIGHT && d >= 0 && d < 0.9 * xn_tickinc)
804 if (o_yticks == LEFT && d <= 0 && d > -0.9 * xn_tickinc)
808 if (o_xtlabel == TRUE && axis_near == FALSE) {
809 snprintf (str, sizeof(str), x_numfmt, xgw_min + xw_tickinc * i);
810 numstr = str_skip_head (str, " ");
811 rSGP.moveAbs (x-strlen(numstr)*charwidth/2, xaxispos + xtl_ofs);
812 rSGP.setTextColor (clr_number, -1);
813 rSGP.drawText (numstr);
823 if (o_yaxis == LINEAR) {
825 rSGP.setColor (clr_axis);
826 if (o_tag && !o_grid && !o_box && s_ycross) {
827 rSGP.moveAbs (yaxispos - charwidth, ya_min);
828 rSGP.lineAbs (yaxispos + charwidth, ya_min);
830 rSGP.moveAbs (yaxispos, ya_min);
831 rSGP.lineAbs (yaxispos, ya_max);
832 if (o_tag && !o_grid && !o_box) {
833 rSGP.moveAbs (yaxispos - charwidth, ya_max);
834 rSGP.lineAbs (yaxispos + charwidth, ya_max);
837 if (o_grid == TRUE) {
838 rSGP.setColor (clr_grid);
839 for (i = 0; i <= y_nint; i++) {
840 y = yt_min + yn_tickinc * i;
841 rSGP.moveAbs (xa_max, y);
842 rSGP.lineAbs (xa_min, y);
846 rSGP.moveAbs(.5,.5);
\r
848 snprintf(s,sizeof(s),"%f, %f",charwidth, charheight);
\r
851 rSGP.setTextAngle (HALFPI);
852 rSGP.setTextSize (1.5 * charheight);
853 rSGP.setTextColor (clr_label, -1);
\r
854 double xExtent, yExtent;
\r
855 rSGP.getTextExtent (c_ylabel.c_str(), &xExtent, &yExtent);
\r
856 rSGP.moveAbs (ylbl_col, ya_min + (ya_max-ya_min)/2 - yExtent);
\r
857 rSGP.drawText (c_ylabel);
858 rSGP.setTextAngle (0.0);
859 rSGP.setTextSize (charheight);
860 minorinc = yn_tickinc / (o_yminortick + 1);
862 for (i = 0; i <= y_nint; i++) {
863 y = yt_min + yn_tickinc * i;
864 rSGP.setColor (clr_axis);
865 rSGP.moveAbs (yaxispos, y);
866 rSGP.lineAbs (yaxispos + yticklen, y);
868 for (j = 1; j <= o_yminortick; j++) {
869 y2 = y + minorinc * j;
870 rSGP.moveAbs (yaxispos, y2);
871 rSGP.lineAbs (yaxispos + TICKRATIO * yticklen, y2);
874 if (yaxispos + ytl_ofs > xa_min && o_xaxis != NOAXIS) {
875 double yw = ygw_min + i * yw_tickinc;
876 double y = convertWorldToNDC_Y (yw);
877 double d = y - xaxispos;
878 if (o_xticks == ABOVE && d >= 0 && d < 0.9 * yn_tickinc)
880 if (o_xticks == BELOW && d <= 0 && d > -0.9 * yn_tickinc)
883 if (o_ytlabel == TRUE && axis_near == FALSE) {
884 snprintf (str, sizeof(str), y_numfmt, ygw_min + yw_tickinc * i);
885 rSGP.moveAbs (yaxispos + ytl_ofs, y + 0.5 * charheight);
886 rSGP.setTextColor (clr_number, -1);
\r
895 EZPlot::symbol (int sym, double symwidth, double symheight)
900 if (sym == SB_CROSS) {
901 rSGP.moveRel (-0.5 * symwidth, -0.5 * symheight);
902 rSGP.lineRel (symwidth, symheight);
903 rSGP.moveRel (-symwidth, 0.0);
904 rSGP.lineRel (symwidth, -symheight);
905 rSGP.moveRel (-0.5 * symwidth, 0.5 * symheight);
906 } else if (sym == SB_PLUS) {
907 rSGP.moveRel (-0.5 * symwidth, 0.0);
908 rSGP.lineRel (symwidth, 0.0);
909 rSGP.moveRel (-0.5 * symwidth, -0.5 * symheight);
910 rSGP.lineRel (0.0, symheight);
911 rSGP.moveRel (0.0, -0.5 * symheight);
912 } else if (sym == SB_BOX) {
913 rSGP.moveRel (-0.5 * symwidth, -0.5 * symheight);
914 rSGP.lineRel (symwidth, 0.0);
915 rSGP.lineRel (0.0, symheight);
916 rSGP.lineRel (-symwidth, 0.0);
917 rSGP.lineRel (0.0, -symheight);
918 rSGP.moveRel (0.5 * symwidth, 0.5 * symheight);
919 } else if (sym == SB_CIRCLE) {
920 rSGP.drawCircle (symwidth);
921 } else if (sym == SB_ERRORBAR) {
922 rSGP.moveRel (-0.5 * symwidth, 0.5 * symheight);
923 rSGP.lineRel (symwidth, 0.0);
924 rSGP.moveRel (-0.5 * symwidth, 0.0);
925 rSGP.lineRel (0.0, -symheight);
926 rSGP.moveRel (-0.5 * symwidth, 0.0);
927 rSGP.lineRel (symwidth, 0.0);
928 rSGP.moveRel (-0.5 * symwidth, 0.5 * symheight);
935 * axis_scale calculates graph axis scaling
938 * retval = axis_scale (min, max, nint, minp, maxp, nintp,
939 * rec_total, rec_frac)
942 * double min Smallest value to plot
943 * double max Largest value to plot
944 * int nint Number of intervals desired
947 * int retval FALSE if illegal parameters, else TRUE
948 * double *minp Minimum graph value
949 * double *maxp Maximum graph value
950 * int *nintp Number of intervals for graph
951 * int *rec_total Recommended field width for printing out the number
952 * int *rec_frac Recommended number of digits for print fraction
956 EZPlot::axis_scale (double min, double max, int nint, double *minp, double *maxp, int *nintp)
958 if (min >= max || nint < 1) {
959 sys_error (ERR_WARNING, "Invalid params: min=%lf, max=%lf, num intervals=%d [axis_scale]", min, max, nint);
964 double a = fabs(min);
965 if (fabs(min) < fabs(max))
967 double scale = pow (10.0, floor(log10(a)));
969 double mina = min / scale;
970 double maxa = max / scale;
971 double d = (maxa - mina) / nint;
973 double e = floor (log10(d));
974 double f = d / pow (10.0, e);
978 else if (f < sqrt (10.0))
980 else if (f < sqrt (50.0))
982 double wdt = v * pow (10.0, e);
983 double g = floor (mina / wdt);
984 if (fabs(g + 1 - mina / wdt) < j)
991 double h = floor (maxa / wdt) + 1.0;
992 if (fabs(maxa / wdt + 1 - h) < j)
998 *nintp = static_cast<int>(h - g);
999 if (fabs(*maxp) >= 10.0 || fabs(*minp) >= 10.0) {
1000 scale = scale * 10.0;
1012 * make_numfmt Make a numeric format string
1015 * make_numfmt (fmtstr, fldwid, nfrac, min, max, nint)
1016 * char *fmtstr Returned format string for printf()
1017 * int *fldwid Returned field width
1018 * int *nfrac If < 0, then calculate best number of
1019 * fraction places & return that value
1020 * If >= 0, then use that number of places
1021 * double min Minimum value
1022 * double max Maximum value
1023 * int nint Number of intervals between min & max
1026 * This routine is written as an INTERNAL routine for EZPLOT
1029 static inline double
1040 EZPlot::make_numfmt (char *fmtstr, int *fldwid, int *nfrac, double minval, double maxval, int nint)
1042 int wid, frac, expon;
1044 double delta = (maxval - minval) / nint;
1045 double absmin = fabs(minval);
1046 double absmax = fabs(maxval);
\r
1047 if (absmin > absmax)
\r
1049 double logt = log10( absmax );
1051 if (fabs(logt) >= 6) { // use exponential format
1052 if (fabs(logt) > 99)
1057 if (*nfrac < 0) { // calculate frac
1058 delta /= pow (10., floor(logt)); // scale delta
1059 frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
1061 frac = 1; // to be safe, add decimal pt
1062 } else // use users' frac
1065 wid = 2 + frac + expon;
1066 if (minval < 0. || maxval < 0.)
1068 sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "g");
1069 } else { // use fixed format
1070 wid = static_cast<int>(trunc(logt)) + 1;
1073 if (minval < 0. || maxval < 0.)
1076 if (*nfrac < 0) { // calculate frac
1077 if (delta >= 0.999999)
1078 frac = 1; // add a decimal pt to be safe
1080 frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
1081 } else // use users' frac
1085 sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "f");