1 /*****************************************************************************
6 ** This is part of the CTSim program
7 ** Copyright (C) 1983-2000 Kevin Rosenberg
9 ** $Id: ezplot.cpp,v 1.8 2000/07/28 10:51:31 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 ******************************************************************************/
30 static const double TICKRATIO = 0.4; // ratio of minor to major tick lengths
31 static const int MAXNUMFMT = 15; // maximum length of a numeric format
32 static const double DEF_CHARHEIGHT = (1./43.); //size of characters in NDC
33 static const double DEF_CHARWIDTH = (1./80.); // size of characters in NDC
34 static const int DEF_CURVE_CLR = C_YELLOW;
38 EZPlotCurve::EZPlotCurve (const double* xData, const double* yData, int n, int color, int linestyle, int symbol, int symfreq, const string& legend)
39 : x(NULL), y(NULL), m_sLegend (legend)
44 int copyCount = n * sizeof(double);
45 memcpy (x, xData, copyCount);
46 memcpy (y, yData, copyCount);
50 m_iLineStyle = linestyle;
52 m_iSymbolFreq = symfreq;
55 EZPlotCurve::~EZPlotCurve ()
63 EZPlot::addCurve (const double *y, int n)
67 for (int i = 0; i < n; i++)
75 EZPlot::addCurve (const float x[], const double y[], int num)
79 for (int i = 0; i < num; i++)
82 addCurve (dx, y, num);
86 EZPlot::addCurve (const double x[], const float y[], int num)
90 for (int i = 0; i < num; i++)
93 addCurve (x, dy, num);
98 EZPlot::addCurve (const double x[], const double y[], int num)
103 EZPlotCurve* pCurve = new EZPlotCurve (x, y, num, o_color, o_linestyle, o_symbol, o_symfreq, c_legend);
104 m_vecCurves.push_back (pCurve);
110 for (EZPlotCurveIterator i = m_vecCurves.begin(); i != m_vecCurves.end(); i++)
115 EZPlot::clearCurves ()
117 for (EZPlotCurveIterator i = m_vecCurves.begin(); i != m_vecCurves.end(); i++)
119 m_vecCurves.erase (m_vecCurves.begin(), m_vecCurves.end());
124 EZPlot::EZPlot (SGP& sgp)
131 EZPlot::initPlotSettings ()
135 charwidth = DEF_CHARWIDTH;
136 charheight = DEF_CHARHEIGHT;
159 o_color = DEF_CURVE_CLR;
162 o_linestyle = SGP::LS_SOLID;
169 o_legendbox = INSIDE;
182 clr_axis = C_WHITE; /* set fixed colors */
183 clr_title = (C_CYAN+8);
184 clr_label = (C_CYAN+8);
185 clr_legend = (C_RED+8);
186 clr_number = (C_GREEN+8);
187 clr_grid = (C_BLACK+8);
192 * plot Plots all curves collected by addCurves ()
198 * This routine plots the curves that have stored by addCurves().
201 * drawAxes() & make_numfmt()
207 double x_added_ticks; /* number of tick spaces added to axis */
208 double y_added_ticks;
209 double symwidth, symheight; /* size of symbol in NDC */
210 double leg_width, leg_height; /* size of legend box */
213 if (m_vecCurves.size() <= 0)
216 if (s_textsize == TRUE)
217 charheight = v_textsize;
219 charheight = DEF_CHARHEIGHT;
221 const EZPlotCurve& firstCurve = *m_vecCurves[0];
222 double xmin = firstCurve.x[0]; // extent of curves in world coord
224 double ymin = firstCurve.y[0];
227 for (EZPlotCurveConstIterator iterCurve = m_vecCurves.begin(); iterCurve != m_vecCurves.end(); iterCurve++) {
228 const EZPlotCurve& curve = **iterCurve;
230 for (ip = 0; ip < curve.m_iPointCount; ip++) {
231 if (curve.x[ip] > xmax)
233 else if (curve.x[ip] < xmin)
235 if (curve.y[ip] > ymax)
237 else if (curve.y[ip] < ymin)
242 // extend graph limits for user defined axis cross positions
243 if (s_xcross == TRUE) {
246 else if (v_xcross > xmax)
250 if (s_ycross == TRUE) {
253 else if (v_ycross > ymax)
257 /* find nice endpoints for axes */
258 axis_scale (xmin, xmax, o_xmajortick - 1, &xgw_min, &xgw_max, &x_nint);
259 axis_scale (ymin, ymax, o_ymajortick - 1, &ygw_min, &ygw_max, &y_nint);
261 /* check if user set x-axis extents */
262 if (s_xmin == TRUE) {
264 x_nint = o_xmajortick - 1;
266 if (s_xmax == TRUE) {
268 x_nint = o_xmajortick - 1;
271 /* check if user set y-axis extents */
272 if (s_ymin == TRUE) {
274 y_nint = o_ymajortick - 1;
276 if (s_ymax == TRUE) {
278 y_nint = o_ymajortick - 1;
281 /* calculate increment between major axis in world coordinates */
282 xw_tickinc = (xgw_max - xgw_min) / x_nint;
283 yw_tickinc = (ygw_max - ygw_min) / y_nint;
285 /* we have now calcuated xgw_min, xyw_max, ygw_min, & ygw_max */
287 // set the number of decimal point to users' setting or default
288 // Two formats for numbers: Fixed: -nnn.f and Exponent: -n.fffE+eee
289 if (s_lxfrac == TRUE)
294 if (s_lyfrac == TRUE)
299 make_numfmt (x_numfmt, &x_fldwid, &x_frac, xgw_min, xgw_max, x_nint);
300 make_numfmt (y_numfmt, &y_fldwid, &y_frac, ygw_min, ygw_max, y_nint);
302 xtl_wid = x_fldwid * charwidth; /* calc size of tick labels */
303 ytl_wid = y_fldwid * charwidth;
304 tl_height = charheight;
306 /* calculate the extent of the plot frame */
309 xp_max = xp_min + o_xlength;
310 yp_max = yp_min + o_ylength;
312 xp_min = clamp (xp_min, 0., 1.);
313 xp_max = clamp (xp_max, 0., 1.);
314 yp_min = clamp (yp_min, 0., 1.);
315 yp_max = clamp (yp_max, 0., 1.);
317 xa_min = xp_min; /* extent of axes */
322 /* adjust frame for title */
323 if (c_title.length() > 0)
324 ya_max -= 2.5 * charheight;
325 title_row = ya_max + 0.5 * charheight;
327 /* calculate legend box boundaries */
328 int max_leg = 0; /* longest legend in characters */
329 int num_leg = 0; /* number of legend titles */
330 for (EZPlotCurveConstIterator iterCurve = m_vecCurves.begin(); iterCurve != m_vecCurves.end(); iterCurve++) {
331 const EZPlotCurve& curve = **iterCurve;
332 if ((n = curve.m_sLegend.length()) > 0) {
334 max_leg = max (max_leg, n);
338 if (num_leg > 0 && o_legendbox != NOLEGEND) {
339 leg_width = (max_leg + 2) * charwidth;
340 leg_height = num_leg * 3 * charheight;
342 if (s_xlegend == TRUE)
346 if (o_legendbox == OUTSIDE)
347 xa_max -= (leg_width + 0.5 * charwidth);
349 xl_min = xl_max - leg_width;
351 if (s_ylegend == TRUE)
356 yl_min = yl_max - leg_height;
358 rSGP.setWindow (xl_min, yl_min, xl_max, yl_max);
359 rSGP.setViewport (xl_min, yl_min, xl_max, yl_max);
360 rSGP.setColor (clr_legend);
361 rSGP.drawRect (xl_min, yl_min, xl_max, yl_max);
363 n = 0; /* current legend position */
364 for (EZPlotCurveIterator iterCurve = m_vecCurves.begin(); iterCurve != m_vecCurves.end(); iterCurve++) {
365 const EZPlotCurve& curve = **iterCurve;
367 if (curve.m_sLegend.length() == 0)
370 double xmin = xl_min + 1.0 * charwidth;
371 double xmax = xl_max - 1.0 * charwidth;
372 double y = yl_max - (2.0 + n * 3) * charheight;
374 rSGP.moveAbs (xmin, y + 0.5 * charheight);
375 rSGP.drawText (curve.m_sLegend);
376 rSGP.setColor (curve.m_iColor);
377 if (curve.m_iLineStyle != SGP::LS_NOLINE) {
378 rSGP.setLineStyle (curve.m_iLineStyle);
379 rSGP.moveAbs (xmin, y);
380 rSGP.lineAbs (xmax, y);
382 if (curve.m_iSymbol > 0) {
383 double xinc = (xmax - xmin) / (5 - 1);
384 rSGP.setLineStyle (SGP::LS_SOLID);
385 for (j = 0; j < 5; j++) {
386 rSGP.moveAbs (xmin + j * xinc, y);
387 symbol(curve.m_iSymbol, 0.5 * charwidth, 0.5 * charheight);
390 ++n; /* move to next legend position */
392 } /* end legend printing */
394 /* calculate the extent of the axes */
396 /*-------------------------*/
397 /* adjust frame for labels */
398 /*-------------------------*/
401 if (c_xlabel.length() > 0)
402 ya_min += 3.0 * charheight;
403 xlbl_row = xp_min; /* put x-label on bottom of plot frame */
406 if (c_ylabel.length() > 0)
407 xa_min += 3.0 * charwidth; /* reverse rSGP.setTextSize because writing text sideways */
408 ylbl_col = xp_min + 2 * charwidth;
410 /*------------------------------*/
411 /* adjust frame for tick labels */
412 /*------------------------------*/
414 /* calc offset of tick labels from axes */
415 if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
417 else if (o_xticks == BELOW)
418 xtl_ofs = -2.5 * charheight;
419 else if (o_xticks == ABOVE)
420 xtl_ofs = 1.5 * charheight;
422 if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
424 else if (o_yticks == LEFT)
425 ytl_ofs = -(1 + y_fldwid) * charwidth;
426 else if (o_yticks == RIGHT)
427 ytl_ofs = 1.5 * charwidth;
429 /* see if need to shrink axis extents and/or tick extents */
430 if (xtl_ofs != 0.0 && s_ycross == FALSE) {
431 if (o_xticks == BELOW) {
432 ya_min += 2.5 * charheight;
434 } else if (o_xticks == ABOVE) {
436 yt_min = ya_min + 2.5 * charheight;
438 } else /* noaxis, no t-labels, or user set cross */
441 if (ytl_ofs != 0.0 && s_xcross == FALSE) {
442 if (o_yticks == LEFT) {
443 xa_min += (1 + y_fldwid) * charwidth;
445 } else if (o_yticks == RIGHT) {
447 xt_min = xa_min + ytl_ofs + y_fldwid * charwidth;
455 /* decrease size of graph, if necessary, to accommadate space */
456 /* between axis boundary and boundary of ticks */
461 if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
463 if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
466 if (o_grid == TRUE) {
467 if (x_added_ticks < 0)
469 if (y_added_ticks < 0)
473 if (x_added_ticks < 0) {
474 if (o_yticks == LEFT || s_ycross)
480 if (y_added_ticks < 0) {
481 if (o_xticks == BELOW || s_xcross)
487 xn_tickinc = (xt_max - xt_min) / (x_nint + x_added_ticks);
488 yn_tickinc = (yt_max - yt_min) / (y_nint + y_added_ticks);
490 xt_min += 0.5 * x_added_ticks * xn_tickinc;
491 xt_max -= 0.5 * x_added_ticks * xn_tickinc;
492 yt_min += 0.5 * y_added_ticks * yn_tickinc;
493 yt_max -= 0.5 * y_added_ticks * yn_tickinc;
500 /*---------------------------------------------------------------------------*/
504 rSGP.setLineStyle (SGP::LS_SOLID);
507 /* Convert WC in graph boundary to axis boundary */
508 rSGP.setWindow (xgw_min, ygw_min, xgw_max, ygw_max); /* Graph boundary */
509 rSGP.setViewport (xgn_min, ygn_min, xgn_max, ygn_max);
510 double xminTemp = xa_min, xmaxTemp = xa_max;
511 double yminTemp = ya_min, ymaxTemp = ya_max;
512 rSGP.transformNDCtoMC (&xminTemp, &yminTemp); // calc WC to axis boundaries
513 rSGP.transformNDCtoMC (&xmaxTemp, &ymaxTemp);
514 rSGP.setWindow (xminTemp, yminTemp, xmaxTemp, ymaxTemp); // Set window to axis boundaries
515 rSGP.setViewport (xa_min, ya_min, xa_max, ya_max);
517 symwidth = charwidth * (xgw_max - xgw_min);
518 symheight = charheight * (ygw_max - ygw_min);
520 for (EZPlotCurveIterator iterCurve = m_vecCurves.begin(); iterCurve != m_vecCurves.end(); iterCurve++) {
521 const EZPlotCurve& curve = **iterCurve;
523 rSGP.setColor (curve.m_iColor);
524 if (curve.m_iLineStyle != SGP::LS_NOLINE) {
525 rSGP.setLineStyle (curve.m_iLineStyle);
526 rSGP.polylineAbs (curve.x, curve.y, curve.m_iPointCount);
528 if (curve.m_iSymbol > 0) {
529 rSGP.setLineStyle (SGP::LS_SOLID);
530 rSGP.moveAbs (curve.x[0], curve.y[0]);
531 symbol (curve.m_iSymbol, symwidth, symheight);
532 for (i = 1; i < curve.m_iPointCount; i++)
533 if (i % curve.m_iSymbolFreq == 0 || i == curve.m_iPointCount - 1) {
534 rSGP.moveAbs (curve.x[i], curve.y[i]);
535 symbol (curve.m_iSymbol, symwidth, symheight);
543 * drawAxes INTERNAL routine to draw axis & label them
551 EZPlot::drawAxes(void)
553 double xticklen = 0, yticklen = 0; /* length of ticks in NDC */
554 double minorinc; /* increment between minor axes */
555 double xaxispos, yaxispos; /* crossing of axes */
557 bool axis_near; /* TRUE if axis too close to print t-label */
562 rSGP.setTextSize (charheight);
563 rSGP.setTextColor (1, -1);
565 if (o_xticks == ABOVE)
566 xticklen = charheight;
567 else if (o_xticks == BELOW)
568 xticklen = -charheight;
570 if (o_yticks == RIGHT)
571 yticklen = charwidth;
572 else if (o_yticks == LEFT)
573 yticklen = -charwidth;
575 rSGP.setWindow (xp_min, yp_min, xp_max, yp_max);
576 rSGP.setViewport (xp_min, yp_min, xp_max, yp_max);
578 if (c_title.length() > 0) {
579 rSGP.moveAbs (xa_min + (xa_max-xa_min)/2 - c_title.length()*charwidth, title_row);
580 rSGP.setTextSize (charheight * 2.0);
581 rSGP.setTextColor (clr_title, -1);
582 rSGP.drawText (c_title);
583 rSGP.setTextSize (charheight);
586 if (o_grid == TRUE || o_box == TRUE) {
587 rSGP.setColor (clr_axis);
588 rSGP.moveAbs (xa_min, ya_min);
589 rSGP.lineAbs (xa_max, ya_min);
590 rSGP.lineAbs (xa_max, ya_max);
591 rSGP.lineAbs (xa_min, ya_max);
592 rSGP.lineAbs (xa_min, ya_min);
595 /* calculate position of axes */
598 if (s_ycross == TRUE) { /* convert users' world-coord */
599 xaxispos = v_ycross; /* axis to its position in NDC */
600 rSGP.setWindow (xgw_min, ygw_min, xgw_max, ygw_max);
601 rSGP.setViewport (xgn_min, ygn_min, xgn_max, ygn_max);
603 rSGP.transformMCtoNDC (x, xaxispos, &x, &xaxispos);
608 if (s_xcross == TRUE) { /* convert users' world-coord */
609 yaxispos = v_xcross; /* axis to its NDC position */
610 rSGP.setWindow (xgw_min, ygw_min, xgw_max, ygw_max);
611 rSGP.setViewport (xgn_min, ygn_min, xgn_max, ygn_max);
613 rSGP.transformMCtoNDC (yaxispos, y, &yaxispos, &y);
621 if (o_xaxis == LINEAR) {
622 rSGP.setWindow (xp_min, yp_min, xp_max, yp_max);
623 rSGP.setViewport (xp_min, yp_min, xp_max, yp_max);
627 rSGP.setColor (clr_axis);
628 if (o_tag && !o_grid && !o_box && s_xcross) {
629 rSGP.moveAbs (xa_min, xaxispos - charheight);
630 rSGP.lineAbs (xa_min, xaxispos + charheight);
632 rSGP.moveAbs (xa_min, xaxispos);
633 rSGP.lineAbs (xa_max, xaxispos);
634 if (o_tag && !o_grid && !o_box) {
635 rSGP.moveAbs (xa_max, xaxispos - charheight);
636 rSGP.lineAbs (xa_max, xaxispos + charheight);
639 if (o_grid == TRUE) {
640 rSGP.setColor (clr_grid);
641 for (i = 0; i <= x_nint; i++) {
642 rSGP.moveAbs (xt_min + xn_tickinc * i, ya_max);
643 rSGP.lineAbs (xt_min + xn_tickinc * i, ya_min);
646 rSGP.moveAbs (xa_min + (xa_max-xa_min)/2 - c_xlabel.length()*charwidth, xlbl_row);
647 rSGP.setTextSize (charheight * 2.0);
648 rSGP.setTextColor (clr_label, -1);
649 rSGP.drawText (c_xlabel);
650 rSGP.setTextSize (charheight);
651 minorinc = xn_tickinc / (o_xminortick + 1);
653 for (i = 0; i <= x_nint; i++) {
654 x = xt_min + xn_tickinc * i;
655 rSGP.setColor (clr_axis);
656 rSGP.moveAbs (x, xaxispos);
657 rSGP.lineAbs (x, xaxispos + xticklen);
659 for (j = 1; j <= o_xminortick; j++) {
660 x2 = x + minorinc * j;
661 rSGP.moveAbs (x2, xaxispos);
662 rSGP.lineAbs (x2, xaxispos + TICKRATIO * xticklen);
665 if (xaxispos + xtl_ofs > ya_min && o_yaxis != NOAXIS) {
668 xw = xgw_min + i * xw_tickinc;
669 rSGP.setWindow (xgw_min, ygw_min, xgw_max, ygw_max);
670 rSGP.setViewport (xgn_min, ygn_min, xgn_max, ygn_max);
671 rSGP.transformMCtoNDC (xw, y, &x, &y);
672 rSGP.setWindow (xp_min, yp_min, xp_max, yp_max);
673 rSGP.setViewport (xp_min, yp_min, xp_max, yp_max);
675 if (o_yticks == RIGHT && d >= 0 && d < 0.9 * xn_tickinc)
677 if (o_yticks == LEFT && d <= 0 && d > -0.9 * xn_tickinc)
681 if (o_xtlabel == TRUE && axis_near == FALSE) {
682 snprintf (str, sizeof(str), x_numfmt, xgw_min + xw_tickinc * i);
683 numstr = str_skip_head (str, " ");
684 rSGP.moveAbs (x-strlen(numstr)*charwidth/2,xaxispos + xtl_ofs);
685 rSGP.setTextColor (clr_number, -1);
686 rSGP.drawText (numstr);
696 if (o_yaxis == LINEAR) {
697 rSGP.setWindow (xp_min, yp_min, xp_max, yp_max);
698 rSGP.setViewport (xp_min, yp_min, xp_max, yp_max);
700 rSGP.setColor (clr_axis);
701 if (o_tag && !o_grid && !o_box && s_ycross) {
702 rSGP.moveAbs (yaxispos - charwidth, ya_min);
703 rSGP.lineAbs (yaxispos + charwidth, ya_min);
705 rSGP.moveAbs (yaxispos, ya_min);
706 rSGP.lineAbs (yaxispos, ya_max);
707 if (o_tag && !o_grid && !o_box) {
708 rSGP.moveAbs (yaxispos - charwidth, ya_max);
709 rSGP.lineAbs (yaxispos + charwidth, ya_max);
712 if (o_grid == TRUE) {
713 rSGP.setColor (clr_grid);
714 for (i = 0; i <= y_nint; i++) {
715 y = yt_min + yn_tickinc * i;
716 rSGP.moveAbs (xa_max, y);
717 rSGP.lineAbs (xa_min, y);
720 rSGP.moveAbs (ylbl_col,ya_min + (ya_max-ya_min)/2 - c_ylabel.length()*charheight);
721 rSGP.setTextAngle (HALFPI);
722 rSGP.setTextSize (2 * charheight);
723 rSGP.setTextColor (clr_label, -1);
724 rSGP.drawText (c_ylabel);
725 rSGP.setTextAngle (0.0);
726 rSGP.setTextSize (charheight);
727 minorinc = yn_tickinc / (o_yminortick + 1);
729 for (i = 0; i <= y_nint; i++) {
730 y = yt_min + yn_tickinc * i;
731 rSGP.setColor (clr_axis);
732 rSGP.moveAbs (yaxispos, y);
733 rSGP.lineAbs (yaxispos + yticklen, y);
735 for (j = 1; j <= o_yminortick; j++) {
736 y2 = y + minorinc * j;
737 rSGP.moveAbs (yaxispos, y2);
738 rSGP.lineAbs (yaxispos + TICKRATIO * yticklen, y2);
741 if (yaxispos + ytl_ofs > xa_min && o_xaxis != NOAXIS) {
744 yw = ygw_min + i * yw_tickinc;
745 rSGP.setWindow (xgw_min, ygw_min, xgw_max, ygw_max);
746 rSGP.setViewport (xgn_min, ygn_min, xgn_max, ygn_max);
747 rSGP.transformMCtoNDC (x, yw, &x, &y);
748 rSGP.setWindow (xp_min, yp_min, xp_max, yp_max);
749 rSGP.setViewport (xp_min, yp_min, xp_max, yp_max);
751 if (o_xticks == ABOVE && d >= 0 && d < 0.9 * yn_tickinc)
753 if (o_xticks == BELOW && d <= 0 && d > -0.9 * yn_tickinc)
756 if (o_ytlabel == TRUE && axis_near == FALSE) {
757 snprintf (str, sizeof(str), y_numfmt, ygw_min + yw_tickinc * i);
758 rSGP.moveAbs (yaxispos + ytl_ofs, y - 0.5 * charheight);
759 rSGP.setTextColor (clr_number, -1);
768 EZPlot::symbol (int sym, double symwidth, double symheight)
773 if (sym == SB_CROSS) {
774 rSGP.moveRel (-0.5 * symwidth, -0.5 * symheight);
775 rSGP.lineRel (symwidth, symheight);
776 rSGP.moveRel (-symwidth, 0.0);
777 rSGP.lineRel (symwidth, -symheight);
778 rSGP.moveRel (-0.5 * symwidth, 0.5 * symheight);
779 } else if (sym == SB_PLUS) {
780 rSGP.moveRel (-0.5 * symwidth, 0.0);
781 rSGP.lineRel (symwidth, 0.0);
782 rSGP.moveRel (-0.5 * symwidth, -0.5 * symheight);
783 rSGP.lineRel (0.0, symheight);
784 rSGP.moveRel (0.0, -0.5 * symheight);
785 } else if (sym == SB_BOX) {
786 rSGP.moveRel (-0.5 * symwidth, -0.5 * symheight);
787 rSGP.lineRel (symwidth, 0.0);
788 rSGP.lineRel (0.0, symheight);
789 rSGP.lineRel (-symwidth, 0.0);
790 rSGP.lineRel (0.0, -symheight);
791 rSGP.moveRel (0.5 * symwidth, 0.5 * symheight);
792 } else if (sym == SB_CIRCLE) {
793 rSGP.drawCircle (symwidth);
794 } else if (sym == SB_ERRORBAR) {
795 rSGP.moveRel (-0.5 * symwidth, 0.5 * symheight);
796 rSGP.lineRel (symwidth, 0.0);
797 rSGP.moveRel (-0.5 * symwidth, 0.0);
798 rSGP.lineRel (0.0, -symheight);
799 rSGP.moveRel (-0.5 * symwidth, 0.0);
800 rSGP.lineRel (symwidth, 0.0);
801 rSGP.moveRel (-0.5 * symwidth, 0.5 * symheight);
808 * axis_scale calculates graph axis scaling
811 * retval = axis_scale (min, max, nint, minp, maxp, nintp,
812 * rec_total, rec_frac)
815 * double min Smallest value to plot
816 * double max Largest value to plot
817 * int nint Number of intervals desired
820 * int retval FALSE if illegal parameters, else TRUE
821 * double *minp Minimum graph value
822 * double *maxp Maximum graph value
823 * int *nintp Number of intervals for graph
824 * int *rec_total Recommended field width for printing out the number
825 * int *rec_frac Recommended number of digits for print fraction
829 EZPlot::axis_scale (double min, double max, int nint, double *minp, double *maxp, int *nintp)
831 if (min >= max || nint < 1) {
832 sys_error (ERR_WARNING, "Invalid params: min=%lf, min=%lf, num intervals=%d [axis_scale]", min, max, nint);
837 double a = fabs(min);
838 if (fabs(min) < fabs(max))
840 double scale = pow (10.0, floor(log10(a)));
842 double mina = min / scale;
843 double maxa = max / scale;
844 double d = (maxa - mina) / nint;
846 double e = floor (log10(d));
847 double f = d / pow (10.0, e);
851 else if (f < sqrt (10.0))
853 else if (f < sqrt (50.0))
855 double wdt = v * pow (10.0, e);
856 double g = floor (mina / wdt);
857 if (fabs(g + 1 - mina / wdt) < j)
860 double h = floor (maxa / wdt) + 1.0;
861 if (fabs(maxa / wdt + 1 - h) < j)
864 *nintp = static_cast<int>(h - g);
865 if (fabs(*maxp) >= 10.0 || fabs(*minp) >= 10.0) {
866 scale = scale * 10.0;
878 * make_numfmt Make a numeric format string
881 * make_numfmt (fmtstr, fldwid, nfrac, min, max, nint)
882 * char *fmtstr Returned format string for printf()
883 * int *fldwid Returned field width
884 * int *nfrac If < 0, then calculate best number of
885 * fraction places & return that value
886 * If >= 0, then use that number of places
887 * double min Minimum value
888 * double max Maximum value
889 * int nint Number of intervals between min & max
892 * This routine is written as an INTERNAL routine for EZPLOT
906 EZPlot::make_numfmt (char *fmtstr, int *fldwid, int *nfrac, double minval, double maxval, int nint)
908 int wid, frac, expon;
910 double delta = (maxval - minval) / nint;
911 double absmin = fabs(minval);
912 double absmax = fabs(maxval);
913 double logt = log10( max(absmin, absmax) );
915 if (fabs(logt) >= 6) { /* use exponential format */
917 expon = 5; /* E+102 */
919 expon = 4; /* E+00 */
921 if (*nfrac < 0) { /* calculate frac */
922 delta /= pow (10., floor(logt)); /* scale delta */
923 frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
925 frac = 1; /* to be safe, add decimal pt */
926 } else /* use users' frac */
929 wid = 2 + frac + expon;
930 if (minval < 0. || maxval < 0.)
932 sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "g");
933 } else { /* use fixed format */
934 wid = static_cast<int>(trunc(logt)) + 1;
937 if (minval < 0. || maxval < 0.)
940 if (*nfrac < 0) { /* calculate frac */
941 if (delta >= 0.999999)
942 frac = 1; /* add a decimal pt to be safe */
944 frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
945 } else /* use users' frac */
949 sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "f");