1 /*****************************************************************************
6 ** This is part of the CTSim program
7 ** Copyright (C) 1983-2000 Kevin Rosenberg
9 ** $Id: ezplot.cpp,v 1.16 2000/12/06 01:46:43 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 double DEF_CHARHEIGHT = (1./43.); //size of characters in NDC
36 static const double DEF_CHARWIDTH = (1./80.); // size of characters in NDC
37 static const int DEF_CURVE_CLR = C_RED;
40 EZPlotCurve::EZPlotCurve (const double* xData, const double* yData, int n, int color, int linestyle, int symbol, int symfreq, const string& legend)
41 : x(NULL), y(NULL), m_sLegend (legend)
46 int copyCount = n * sizeof(double);
47 memcpy (x, xData, copyCount);
48 memcpy (y, yData, copyCount);
52 m_iLineStyle = linestyle;
54 m_iSymbolFreq = symfreq;
57 EZPlotCurve::~EZPlotCurve ()
65 EZPlot::addCurve (const double *y, int n)
67 double* x = new double [n];
69 for (int i = 0; i < n; i++)
78 EZPlot::addCurve (const float *y, int n)
80 double* yDouble = new double [n];
82 for (int i = 0; i < n; i++)
85 addCurve (yDouble, n);
91 EZPlot::addCurve (const float x[], const double y[], int num)
93 double* dx = new double [num];
95 for (int i = 0; i < num; i++)
98 addCurve (dx, y, num);
103 EZPlot::addCurve (const double x[], const float y[], int num)
105 double* dy = new double [num];
107 for (int i = 0; i < num; i++)
110 addCurve (x, dy, num);
117 EZPlot::addCurve (const double x[], const double y[], int num)
122 EZPlotCurve* pCurve = new EZPlotCurve (x, y, num, o_color, o_linestyle, o_symbol, o_symfreq, c_legend);
123 m_vecCurves.push_back (pCurve);
129 for (EZPlotCurveIterator i = m_vecCurves.begin(); i != m_vecCurves.end(); i++)
134 EZPlot::clearCurves ()
136 for (EZPlotCurveIterator i = m_vecCurves.begin(); i != m_vecCurves.end(); i++)
138 m_vecCurves.erase (m_vecCurves.begin(), m_vecCurves.end());
143 EZPlot::EZPlot (SGP& sgp)
150 EZPlot::initPlotSettings ()
152 charheight = DEF_CHARHEIGHT;
153 charwidth = DEF_CHARWIDTH;
176 o_color = DEF_CURVE_CLR;
179 o_linestyle = SGP::LS_SOLID;
186 o_legendbox = INSIDE;
199 clr_axis = C_BLACK; // set fixed colors
203 clr_number = C_GREEN;
209 * plot Plots all curves collected by addCurves ()
215 * This routine plots the curves that have stored by addCurves().
218 * drawAxes() & make_numfmt()
224 if (m_vecCurves.size() <= 0)
227 rSGP.setWindow (0., 0., 1., 1.);
229 if (s_textsize == TRUE) {
230 charheight = v_textsize;
231 charwidth = rSGP.getCharWidth();
233 charheight = rSGP.getCharHeight();
234 charwidth = rSGP.getCharWidth();
237 const EZPlotCurve& firstCurve = *m_vecCurves[0];
238 double xmin = firstCurve.x[0]; // extent of curves in world coord
240 double ymin = firstCurve.y[0];
243 for (EZPlotCurveConstIterator iterCurve = m_vecCurves.begin(); iterCurve != m_vecCurves.end(); iterCurve++) {
244 const EZPlotCurve& curve = **iterCurve;
246 for (int ip = 0; ip < curve.m_iPointCount; ip++) {
247 if (curve.x[ip] > xmax)
249 else if (curve.x[ip] < xmin)
251 if (curve.y[ip] > ymax)
253 else if (curve.y[ip] < ymin)
258 // extend graph limits for user defined axis cross positions
259 if (s_xcross == TRUE) {
262 else if (v_xcross > xmax)
266 if (s_ycross == TRUE) {
269 else if (v_ycross > ymax)
273 // find nice endpoints for axes
274 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))
277 // check if user set x-axis extents
278 if (s_xmin == TRUE) {
280 x_nint = o_xmajortick - 1;
282 if (s_xmax == TRUE) {
284 x_nint = o_xmajortick - 1;
287 // check if user set y-axis extents
288 if (s_ymin == TRUE) {
290 y_nint = o_ymajortick - 1;
292 if (s_ymax == TRUE) {
294 y_nint = o_ymajortick - 1;
297 // calculate increment between major axis in world coordinates
298 xw_tickinc = (xgw_max - xgw_min) / x_nint;
299 yw_tickinc = (ygw_max - ygw_min) / y_nint;
301 // we have now calcuated xgw_min, xyw_max, ygw_min, & ygw_max
303 // set the number of decimal point to users' setting or default
304 // Two formats for numbers: Fixed: -nnn.f and Exponent: -n.fffE+eee
305 if (s_lxfrac == TRUE)
310 if (s_lyfrac == TRUE)
315 make_numfmt (x_numfmt, &x_fldwid, &x_frac, xgw_min, xgw_max, x_nint);
316 make_numfmt (y_numfmt, &y_fldwid, &y_frac, ygw_min, ygw_max, y_nint);
318 xtl_wid = x_fldwid * charwidth; // calc size of tick labels
319 ytl_wid = y_fldwid * charwidth;
320 tl_height = charheight;
322 // calculate the extent of the plot frame
325 xp_max = xp_min + o_xlength;
326 yp_max = yp_min + o_ylength;
328 xp_min = clamp (xp_min, 0., 1.);
329 xp_max = clamp (xp_max, 0., 1.);
330 yp_min = clamp (yp_min, 0., 1.);
331 yp_max = clamp (yp_max, 0., 1.);
333 xa_min = xp_min; // extent of axes
338 // adjust frame for title
339 if (c_title.length() > 0)
340 ya_max -= 2 * charheight;
341 title_row = ya_max + 2 * charheight;
343 // calculate legend box boundaries
344 int max_leg = 0; // longest legend in characters
345 int num_leg = 0; // number of legend titles
346 for (EZPlotCurveConstIterator iterCurve2 = m_vecCurves.begin(); iterCurve2 != m_vecCurves.end(); iterCurve2++) {
347 const EZPlotCurve& curve = **iterCurve2;
348 int nLegend = curve.m_sLegend.length();
351 if (nLegend > max_leg)
\r
356 if (num_leg > 0 && o_legendbox != NOLEGEND) {
357 double leg_width = (max_leg + 2) * charwidth; // size of legend box
358 double leg_height = num_leg * 3 * charheight;
360 if (s_xlegend == TRUE)
364 if (o_legendbox == OUTSIDE)
365 xa_max -= (leg_width + 0.5 * charwidth);
367 xl_min = xl_max - leg_width;
369 if (s_ylegend == TRUE)
374 yl_min = yl_max - leg_height;
376 rSGP.setColor (clr_legend);
377 rSGP.drawRect (xl_min, yl_min, xl_max, yl_max);
379 int iLegend = 0; // current legend position
380 for (EZPlotCurveIterator iterCurve = m_vecCurves.begin(); iterCurve != m_vecCurves.end(); iterCurve++) {
381 const EZPlotCurve& curve = **iterCurve;
383 if (curve.m_sLegend.length() == 0)
386 double xmin = xl_min + 1.0 * charwidth;
387 double xmax = xl_max - 1.0 * charwidth;
388 double y = yl_max - (2.0 + iLegend * 3) * charheight;
390 rSGP.moveAbs (xmin, y + 0.5 * charheight);
391 rSGP.drawText (curve.m_sLegend);
392 rSGP.setColor (curve.m_iColor);
393 if (curve.m_iLineStyle != SGP::LS_NOLINE) {
394 rSGP.setLineStyle (curve.m_iLineStyle);
395 rSGP.moveAbs (xmin, y);
396 rSGP.lineAbs (xmax, y);
398 if (curve.m_iSymbol > 0) {
399 double xinc = (xmax - xmin) / (5 - 1);
400 rSGP.setLineStyle (SGP::LS_SOLID);
401 for (int j = 0; j < 5; j++) {
402 rSGP.moveAbs (xmin + j * xinc, y);
403 symbol(curve.m_iSymbol, 0.5 * charwidth, 0.5 * charheight);
406 ++iLegend; // move to next legend position
408 } // end legend printing
410 // calculate the extent of the axes
412 /*-------------------------*/
413 /* adjust frame for labels */
414 /*-------------------------*/
417 if (c_xlabel.length() > 0)
418 ya_min += 3.0 * charheight;
419 xlbl_row = xp_min; // put x-label on bottom of plot frame
422 if (c_ylabel.length() > 0)
423 xa_min += 3.0 * charwidth; // reverse rSGP.setTextSize because writing text sideways
424 ylbl_col = xp_min + 2 * charwidth;
426 /*------------------------------*/
427 /* adjust frame for tick labels */
428 /*------------------------------*/
430 // Calc offset of tick labels from axes
431 if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
433 else if (o_xticks == BELOW)
434 xtl_ofs = -1.5 * charheight; // kr
435 else if (o_xticks == ABOVE)
436 xtl_ofs = 1.5 * charheight;
438 if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
440 else if (o_yticks == LEFT)
441 ytl_ofs = -(1 + y_fldwid) * charwidth;
442 else if (o_yticks == RIGHT)
443 ytl_ofs = 1.5 * charwidth;
450 // see if need to shrink axis extents and/or tick extents
451 if (xtl_ofs != 0.0 && s_ycross == FALSE) {
452 if (o_xticks == BELOW) {
453 ya_min += 2.5 * charheight;
455 } else if (o_xticks == ABOVE) {
457 yt_min = ya_min + 2.5 * charheight;
459 } else // noaxis, no t-labels, or user set cross
462 if (ytl_ofs != 0.0 && s_xcross == FALSE) {
463 if (o_yticks == LEFT) {
464 xa_min += (1 + y_fldwid) * charwidth;
466 } else if (o_yticks == RIGHT) {
468 xt_min = xa_min + ytl_ofs + y_fldwid * charwidth;
473 // decrease size of graph, if necessary, to accommadate space
474 // between axis boundary and boundary of ticks
475 double x_added_ticks = -1; // number of tick spaces added to axis
476 double y_added_ticks = -1;
477 if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
479 if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
482 if (o_grid == TRUE) {
483 if (x_added_ticks < 0)
485 if (y_added_ticks < 0)
489 if (x_added_ticks < 0) {
490 if (o_yticks == LEFT || s_ycross)
496 if (y_added_ticks < 0) {
497 if (o_xticks == BELOW || s_xcross)
503 xn_tickinc = (xt_max - xt_min) / (x_nint + x_added_ticks);
504 yn_tickinc = (yt_max - yt_min) / (y_nint + y_added_ticks);
506 xt_min += 0.5 * x_added_ticks * xn_tickinc;
507 xt_max -= 0.5 * x_added_ticks * xn_tickinc;
508 yt_min += 0.5 * y_added_ticks * yn_tickinc;
509 yt_max -= 0.5 * y_added_ticks * yn_tickinc;
516 //------------------------------------------------------------------------
518 m_xWorldScale = (xgn_max - xgn_min) / (xgw_max - xgw_min);
519 m_yWorldScale = (ygn_max - ygn_min) / (ygw_max - ygw_min);
523 rSGP.setLineStyle (SGP::LS_SOLID);
526 // size of symbol in NDC
527 double symwidth = charwidth;
528 double symheight = charheight;
530 for (EZPlotCurveIterator iterCurve3 = m_vecCurves.begin(); iterCurve3 != m_vecCurves.end(); iterCurve3++) {
531 const EZPlotCurve& curve = **iterCurve3;
533 rSGP.setColor (curve.m_iColor);
535 if (curve.m_iLineStyle != SGP::LS_NOLINE) {
536 rSGP.setLineStyle (curve.m_iLineStyle);
537 double x = convertWorldToNDC_X (curve.x[0]);
538 double y = convertWorldToNDC_Y (curve.y[0]);
540 for (int i = 1; i < curve.m_iPointCount; i++) {
541 x = convertWorldToNDC_X (curve.x[i]);
542 y = convertWorldToNDC_Y (curve.y[i]);
546 if (curve.m_iSymbol > 0) {
547 rSGP.setLineStyle (SGP::LS_SOLID);
548 double x = convertWorldToNDC_X (curve.x[0]);
549 double y = convertWorldToNDC_Y (curve.y[0]);
551 symbol (curve.m_iSymbol, symwidth, symheight);
552 for (int i = 1; i < curve.m_iPointCount; i++)
553 if (i % curve.m_iSymbolFreq == 0 || i == curve.m_iPointCount - 1) {
554 x = convertWorldToNDC_X (curve.x[i]);
555 y = convertWorldToNDC_Y (curve.y[i]);
557 symbol (curve.m_iSymbol, symwidth, symheight);
566 * drawAxes INTERNAL routine to draw axis & label them
576 double xticklen = 0, yticklen = 0; // length of ticks in NDC
577 double minorinc; // increment between minor axes
578 double xaxispos, yaxispos; // crossing of axes
580 bool axis_near; // TRUE if axis too close to print t-label
585 rSGP.setTextSize (charheight);
586 rSGP.setTextColor (1, -1);
588 if (o_xticks == ABOVE)
589 xticklen = charheight;
590 else if (o_xticks == BELOW)
591 xticklen = -charheight;
593 if (o_yticks == RIGHT)
594 yticklen = charwidth;
595 else if (o_yticks == LEFT)
596 yticklen = -charwidth;
598 if (c_title.length() > 0) {
600 rSGP.setTextSize (charheight * 2.0);
601 rSGP.getTextExtent (c_title.c_str(), &wText, &hText);
602 rSGP.moveAbs (xa_min + (xa_max-xa_min)/2 - wText/2, title_row);
603 rSGP.setTextColor (clr_title, -1);
604 rSGP.drawText (c_title);
605 rSGP.setTextSize (charheight);
608 if (o_grid == TRUE || o_box == TRUE) {
609 rSGP.setColor (clr_axis);
610 rSGP.moveAbs (xa_min, ya_min);
611 rSGP.lineAbs (xa_max, ya_min);
612 rSGP.lineAbs (xa_max, ya_max);
613 rSGP.lineAbs (xa_min, ya_max);
614 rSGP.lineAbs (xa_min, ya_min);
617 // calculate position of axes
620 if (s_ycross == TRUE) { // convert users' world-coord
621 xaxispos = convertWorldToNDC_Y (v_ycross);// axis to its position in NDC
622 x = convertWorldToNDC_X (xgw_min);
627 if (s_xcross == TRUE) { // convert users' world-coord
628 yaxispos = convertWorldToNDC_X (v_xcross);// axis to its NDC position
629 y = convertWorldToNDC_Y (ygw_min);
637 if (o_xaxis == LINEAR) {
640 rSGP.setColor (clr_axis);
641 if (o_tag && !o_grid && !o_box && s_xcross) {
642 rSGP.moveAbs (xa_min, xaxispos - charheight);
643 rSGP.lineAbs (xa_min, xaxispos + charheight);
645 rSGP.moveAbs (xa_min, xaxispos);
646 rSGP.lineAbs (xa_max, xaxispos);
647 if (o_tag && !o_grid && !o_box) {
648 rSGP.moveAbs (xa_max, xaxispos - charheight);
649 rSGP.lineAbs (xa_max, xaxispos + charheight);
652 if (o_grid == TRUE) {
653 rSGP.setColor (clr_grid);
654 for (i = 0; i <= x_nint; i++) {
655 rSGP.moveAbs (xt_min + xn_tickinc * i, ya_max);
656 rSGP.lineAbs (xt_min + xn_tickinc * i, ya_min);
659 rSGP.moveAbs (xa_min + (xa_max-xa_min)/2 - c_xlabel.length()*charwidth, xlbl_row);
660 rSGP.setTextSize (charheight * 2.0);
661 rSGP.setTextColor (clr_label, -1);
662 rSGP.drawText (c_xlabel);
663 rSGP.setTextSize (charheight);
664 minorinc = xn_tickinc / (o_xminortick + 1);
666 for (i = 0; i <= x_nint; i++) {
667 x = xt_min + xn_tickinc * i;
668 rSGP.setColor (clr_axis);
669 rSGP.moveAbs (x, xaxispos);
670 rSGP.lineAbs (x, xaxispos + xticklen);
672 for (j = 1; j <= o_xminortick; j++) {
673 x2 = x + minorinc * j;
674 rSGP.moveAbs (x2, xaxispos);
675 rSGP.lineAbs (x2, xaxispos + TICKRATIO * xticklen);
678 if (xaxispos + xtl_ofs > ya_min && o_yaxis != NOAXIS) {
679 double xw = xgw_min + i * xw_tickinc;
680 double x = convertWorldToNDC_X (xw);
681 double d = x - yaxispos;
682 if (o_yticks == RIGHT && d >= 0 && d < 0.9 * xn_tickinc)
684 if (o_yticks == LEFT && d <= 0 && d > -0.9 * xn_tickinc)
688 if (o_xtlabel == TRUE && axis_near == FALSE) {
689 snprintf (str, sizeof(str), x_numfmt, xgw_min + xw_tickinc * i);
690 numstr = str_skip_head (str, " ");
691 rSGP.moveAbs (x-strlen(numstr)*charwidth/2, xaxispos + xtl_ofs);
692 rSGP.setTextColor (clr_number, -1);
693 rSGP.drawText (numstr);
703 if (o_yaxis == LINEAR) {
705 rSGP.setColor (clr_axis);
706 if (o_tag && !o_grid && !o_box && s_ycross) {
707 rSGP.moveAbs (yaxispos - charwidth, ya_min);
708 rSGP.lineAbs (yaxispos + charwidth, ya_min);
710 rSGP.moveAbs (yaxispos, ya_min);
711 rSGP.lineAbs (yaxispos, ya_max);
712 if (o_tag && !o_grid && !o_box) {
713 rSGP.moveAbs (yaxispos - charwidth, ya_max);
714 rSGP.lineAbs (yaxispos + charwidth, ya_max);
717 if (o_grid == TRUE) {
718 rSGP.setColor (clr_grid);
719 for (i = 0; i <= y_nint; i++) {
720 y = yt_min + yn_tickinc * i;
721 rSGP.moveAbs (xa_max, y);
722 rSGP.lineAbs (xa_min, y);
725 rSGP.moveAbs (ylbl_col, ya_min + (ya_max-ya_min)/2 - c_ylabel.length()*charheight);
726 rSGP.setTextAngle (HALFPI);
727 rSGP.setTextSize (2 * charheight);
728 rSGP.setTextColor (clr_label, -1);
729 rSGP.drawText (c_ylabel);
730 rSGP.setTextAngle (0.0);
731 rSGP.setTextSize (charheight);
732 minorinc = yn_tickinc / (o_yminortick + 1);
734 for (i = 0; i <= y_nint; i++) {
735 y = yt_min + yn_tickinc * i;
736 rSGP.setColor (clr_axis);
737 rSGP.moveAbs (yaxispos, y);
738 rSGP.lineAbs (yaxispos + yticklen, y);
740 for (j = 1; j <= o_yminortick; j++) {
741 y2 = y + minorinc * j;
742 rSGP.moveAbs (yaxispos, y2);
743 rSGP.lineAbs (yaxispos + TICKRATIO * yticklen, y2);
746 if (yaxispos + ytl_ofs > xa_min && o_xaxis != NOAXIS) {
747 double yw = ygw_min + i * yw_tickinc;
748 double y = convertWorldToNDC_Y (yw);
749 double d = y - xaxispos;
750 if (o_xticks == ABOVE && d >= 0 && d < 0.9 * yn_tickinc)
752 if (o_xticks == BELOW && d <= 0 && d > -0.9 * yn_tickinc)
755 if (o_ytlabel == TRUE && axis_near == FALSE) {
756 snprintf (str, sizeof(str), y_numfmt, ygw_min + yw_tickinc * i);
757 rSGP.moveAbs (yaxispos + ytl_ofs, y + 0.5 * charheight);
758 rSGP.setTextColor (clr_number, -1);
767 EZPlot::symbol (int sym, double symwidth, double symheight)
772 if (sym == SB_CROSS) {
773 rSGP.moveRel (-0.5 * symwidth, -0.5 * symheight);
774 rSGP.lineRel (symwidth, symheight);
775 rSGP.moveRel (-symwidth, 0.0);
776 rSGP.lineRel (symwidth, -symheight);
777 rSGP.moveRel (-0.5 * symwidth, 0.5 * symheight);
778 } else if (sym == SB_PLUS) {
779 rSGP.moveRel (-0.5 * symwidth, 0.0);
780 rSGP.lineRel (symwidth, 0.0);
781 rSGP.moveRel (-0.5 * symwidth, -0.5 * symheight);
782 rSGP.lineRel (0.0, symheight);
783 rSGP.moveRel (0.0, -0.5 * symheight);
784 } else if (sym == SB_BOX) {
785 rSGP.moveRel (-0.5 * symwidth, -0.5 * symheight);
786 rSGP.lineRel (symwidth, 0.0);
787 rSGP.lineRel (0.0, symheight);
788 rSGP.lineRel (-symwidth, 0.0);
789 rSGP.lineRel (0.0, -symheight);
790 rSGP.moveRel (0.5 * symwidth, 0.5 * symheight);
791 } else if (sym == SB_CIRCLE) {
792 rSGP.drawCircle (symwidth);
793 } else if (sym == SB_ERRORBAR) {
794 rSGP.moveRel (-0.5 * symwidth, 0.5 * symheight);
795 rSGP.lineRel (symwidth, 0.0);
796 rSGP.moveRel (-0.5 * symwidth, 0.0);
797 rSGP.lineRel (0.0, -symheight);
798 rSGP.moveRel (-0.5 * symwidth, 0.0);
799 rSGP.lineRel (symwidth, 0.0);
800 rSGP.moveRel (-0.5 * symwidth, 0.5 * symheight);
807 * axis_scale calculates graph axis scaling
810 * retval = axis_scale (min, max, nint, minp, maxp, nintp,
811 * rec_total, rec_frac)
814 * double min Smallest value to plot
815 * double max Largest value to plot
816 * int nint Number of intervals desired
819 * int retval FALSE if illegal parameters, else TRUE
820 * double *minp Minimum graph value
821 * double *maxp Maximum graph value
822 * int *nintp Number of intervals for graph
823 * int *rec_total Recommended field width for printing out the number
824 * int *rec_frac Recommended number of digits for print fraction
828 EZPlot::axis_scale (double min, double max, int nint, double *minp, double *maxp, int *nintp)
830 if (min >= max || nint < 1) {
831 sys_error (ERR_WARNING, "Invalid params: min=%lf, max=%lf, num intervals=%d [axis_scale]", min, max, nint);
836 double a = fabs(min);
837 if (fabs(min) < fabs(max))
839 double scale = pow (10.0, floor(log10(a)));
841 double mina = min / scale;
842 double maxa = max / scale;
843 double d = (maxa - mina) / nint;
845 double e = floor (log10(d));
846 double f = d / pow (10.0, e);
850 else if (f < sqrt (10.0))
852 else if (f < sqrt (50.0))
854 double wdt = v * pow (10.0, e);
855 double g = floor (mina / wdt);
856 if (fabs(g + 1 - mina / wdt) < j)
862 double h = floor (maxa / wdt) + 1.0;
863 if (fabs(maxa / wdt + 1 - h) < j)
869 *nintp = static_cast<int>(h - g);
870 if (fabs(*maxp) >= 10.0 || fabs(*minp) >= 10.0) {
871 scale = scale * 10.0;
883 * make_numfmt Make a numeric format string
886 * make_numfmt (fmtstr, fldwid, nfrac, min, max, nint)
887 * char *fmtstr Returned format string for printf()
888 * int *fldwid Returned field width
889 * int *nfrac If < 0, then calculate best number of
890 * fraction places & return that value
891 * If >= 0, then use that number of places
892 * double min Minimum value
893 * double max Maximum value
894 * int nint Number of intervals between min & max
897 * This routine is written as an INTERNAL routine for EZPLOT
911 EZPlot::make_numfmt (char *fmtstr, int *fldwid, int *nfrac, double minval, double maxval, int nint)
913 int wid, frac, expon;
915 double delta = (maxval - minval) / nint;
916 double absmin = fabs(minval);
917 double absmax = fabs(maxval);
\r
918 if (absmin > absmax)
\r
920 double logt = log10( absmax );
922 if (fabs(logt) >= 6) { // use exponential format
928 if (*nfrac < 0) { // calculate frac
929 delta /= pow (10., floor(logt)); // scale delta
930 frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
932 frac = 1; // to be safe, add decimal pt
933 } else // use users' frac
936 wid = 2 + frac + expon;
937 if (minval < 0. || maxval < 0.)
939 sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "g");
940 } else { // use fixed format
941 wid = static_cast<int>(trunc(logt)) + 1;
944 if (minval < 0. || maxval < 0.)
947 if (*nfrac < 0) { // calculate frac
948 if (delta >= 0.999999)
949 frac = 1; // add a decimal pt to be safe
951 frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
952 } else // use users' frac
956 sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "f");