1 /*****************************************************************************
6 ** This is part of the CTSim program
7 ** Copyright (C) 1983-2000 Kevin Rosenberg
9 ** $Id: ezplot.cpp,v 1.9 2000/07/29 19:50:08 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_RED;
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 ()
133 charwidth = DEF_CHARWIDTH;
134 charheight = DEF_CHARHEIGHT;
157 o_color = DEF_CURVE_CLR;
160 o_linestyle = SGP::LS_SOLID;
167 o_legendbox = INSIDE;
180 clr_axis = C_BLACK; /* set fixed colors */
184 clr_number = C_GREEN;
190 * plot Plots all curves collected by addCurves ()
196 * This routine plots the curves that have stored by addCurves().
199 * drawAxes() & make_numfmt()
205 double x_added_ticks; /* number of tick spaces added to axis */
206 double y_added_ticks;
207 double symwidth, symheight; /* size of symbol in NDC */
208 double leg_width, leg_height; /* size of legend box */
211 if (m_vecCurves.size() <= 0)
214 if (s_textsize == TRUE)
215 charheight = v_textsize;
217 charheight = DEF_CHARHEIGHT;
219 const EZPlotCurve& firstCurve = *m_vecCurves[0];
220 double xmin = firstCurve.x[0]; // extent of curves in world coord
222 double ymin = firstCurve.y[0];
225 for (EZPlotCurveConstIterator iterCurve = m_vecCurves.begin(); iterCurve != m_vecCurves.end(); iterCurve++) {
226 const EZPlotCurve& curve = **iterCurve;
228 for (ip = 0; ip < curve.m_iPointCount; ip++) {
229 if (curve.x[ip] > xmax)
231 else if (curve.x[ip] < xmin)
233 if (curve.y[ip] > ymax)
235 else if (curve.y[ip] < ymin)
240 // extend graph limits for user defined axis cross positions
241 if (s_xcross == TRUE) {
244 else if (v_xcross > xmax)
248 if (s_ycross == TRUE) {
251 else if (v_ycross > ymax)
255 /* find nice endpoints for axes */
256 axis_scale (xmin, xmax, o_xmajortick - 1, &xgw_min, &xgw_max, &x_nint);
257 axis_scale (ymin, ymax, o_ymajortick - 1, &ygw_min, &ygw_max, &y_nint);
259 /* check if user set x-axis extents */
260 if (s_xmin == TRUE) {
262 x_nint = o_xmajortick - 1;
264 if (s_xmax == TRUE) {
266 x_nint = o_xmajortick - 1;
269 /* check if user set y-axis extents */
270 if (s_ymin == TRUE) {
272 y_nint = o_ymajortick - 1;
274 if (s_ymax == TRUE) {
276 y_nint = o_ymajortick - 1;
279 /* calculate increment between major axis in world coordinates */
280 xw_tickinc = (xgw_max - xgw_min) / x_nint;
281 yw_tickinc = (ygw_max - ygw_min) / y_nint;
283 /* we have now calcuated xgw_min, xyw_max, ygw_min, & ygw_max */
285 // set the number of decimal point to users' setting or default
286 // Two formats for numbers: Fixed: -nnn.f and Exponent: -n.fffE+eee
287 if (s_lxfrac == TRUE)
292 if (s_lyfrac == TRUE)
297 make_numfmt (x_numfmt, &x_fldwid, &x_frac, xgw_min, xgw_max, x_nint);
298 make_numfmt (y_numfmt, &y_fldwid, &y_frac, ygw_min, ygw_max, y_nint);
300 xtl_wid = x_fldwid * charwidth; /* calc size of tick labels */
301 ytl_wid = y_fldwid * charwidth;
302 tl_height = charheight;
304 /* calculate the extent of the plot frame */
307 xp_max = xp_min + o_xlength;
308 yp_max = yp_min + o_ylength;
310 xp_min = clamp (xp_min, 0., 1.);
311 xp_max = clamp (xp_max, 0., 1.);
312 yp_min = clamp (yp_min, 0., 1.);
313 yp_max = clamp (yp_max, 0., 1.);
315 xa_min = xp_min; /* extent of axes */
320 /* adjust frame for title */
321 if (c_title.length() > 0)
322 ya_max -= 2.5 * charheight;
323 title_row = ya_max + 0.5 * charheight;
325 /* calculate legend box boundaries */
326 int max_leg = 0; /* longest legend in characters */
327 int num_leg = 0; /* number of legend titles */
328 for (EZPlotCurveConstIterator iterCurve = m_vecCurves.begin(); iterCurve != m_vecCurves.end(); iterCurve++) {
329 const EZPlotCurve& curve = **iterCurve;
330 if ((n = curve.m_sLegend.length()) > 0) {
332 max_leg = max (max_leg, n);
336 if (num_leg > 0 && o_legendbox != NOLEGEND) {
337 leg_width = (max_leg + 2) * charwidth;
338 leg_height = num_leg * 3 * charheight;
340 if (s_xlegend == TRUE)
344 if (o_legendbox == OUTSIDE)
345 xa_max -= (leg_width + 0.5 * charwidth);
347 xl_min = xl_max - leg_width;
349 if (s_ylegend == TRUE)
354 yl_min = yl_max - leg_height;
356 rSGP.setWindow (xl_min, yl_min, xl_max, yl_max);
357 rSGP.setViewport (xl_min, yl_min, xl_max, yl_max);
358 rSGP.setColor (clr_legend);
359 rSGP.drawRect (xl_min, yl_min, xl_max, yl_max);
361 n = 0; /* current legend position */
362 for (EZPlotCurveIterator iterCurve = m_vecCurves.begin(); iterCurve != m_vecCurves.end(); iterCurve++) {
363 const EZPlotCurve& curve = **iterCurve;
365 if (curve.m_sLegend.length() == 0)
368 double xmin = xl_min + 1.0 * charwidth;
369 double xmax = xl_max - 1.0 * charwidth;
370 double y = yl_max - (2.0 + n * 3) * charheight;
372 rSGP.moveAbs (xmin, y + 0.5 * charheight);
373 rSGP.drawText (curve.m_sLegend);
374 rSGP.setColor (curve.m_iColor);
375 if (curve.m_iLineStyle != SGP::LS_NOLINE) {
376 rSGP.setLineStyle (curve.m_iLineStyle);
377 rSGP.moveAbs (xmin, y);
378 rSGP.lineAbs (xmax, y);
380 if (curve.m_iSymbol > 0) {
381 double xinc = (xmax - xmin) / (5 - 1);
382 rSGP.setLineStyle (SGP::LS_SOLID);
383 for (j = 0; j < 5; j++) {
384 rSGP.moveAbs (xmin + j * xinc, y);
385 symbol(curve.m_iSymbol, 0.5 * charwidth, 0.5 * charheight);
388 ++n; /* move to next legend position */
390 } /* end legend printing */
392 /* calculate the extent of the axes */
394 /*-------------------------*/
395 /* adjust frame for labels */
396 /*-------------------------*/
399 if (c_xlabel.length() > 0)
400 ya_min += 3.0 * charheight;
401 xlbl_row = xp_min; /* put x-label on bottom of plot frame */
404 if (c_ylabel.length() > 0)
405 xa_min += 3.0 * charwidth; /* reverse rSGP.setTextSize because writing text sideways */
406 ylbl_col = xp_min + 2 * charwidth;
408 /*------------------------------*/
409 /* adjust frame for tick labels */
410 /*------------------------------*/
412 /* calc offset of tick labels from axes */
413 if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
415 else if (o_xticks == BELOW)
416 xtl_ofs = -2.5 * charheight;
417 else if (o_xticks == ABOVE)
418 xtl_ofs = 1.5 * charheight;
420 if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
422 else if (o_yticks == LEFT)
423 ytl_ofs = -(1 + y_fldwid) * charwidth;
424 else if (o_yticks == RIGHT)
425 ytl_ofs = 1.5 * charwidth;
427 /* see if need to shrink axis extents and/or tick extents */
428 if (xtl_ofs != 0.0 && s_ycross == FALSE) {
429 if (o_xticks == BELOW) {
430 ya_min += 2.5 * charheight;
432 } else if (o_xticks == ABOVE) {
434 yt_min = ya_min + 2.5 * charheight;
436 } else /* noaxis, no t-labels, or user set cross */
439 if (ytl_ofs != 0.0 && s_xcross == FALSE) {
440 if (o_yticks == LEFT) {
441 xa_min += (1 + y_fldwid) * charwidth;
443 } else if (o_yticks == RIGHT) {
445 xt_min = xa_min + ytl_ofs + y_fldwid * charwidth;
453 /* decrease size of graph, if necessary, to accommadate space */
454 /* between axis boundary and boundary of ticks */
459 if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
461 if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
464 if (o_grid == TRUE) {
465 if (x_added_ticks < 0)
467 if (y_added_ticks < 0)
471 if (x_added_ticks < 0) {
472 if (o_yticks == LEFT || s_ycross)
478 if (y_added_ticks < 0) {
479 if (o_xticks == BELOW || s_xcross)
485 xn_tickinc = (xt_max - xt_min) / (x_nint + x_added_ticks);
486 yn_tickinc = (yt_max - yt_min) / (y_nint + y_added_ticks);
488 xt_min += 0.5 * x_added_ticks * xn_tickinc;
489 xt_max -= 0.5 * x_added_ticks * xn_tickinc;
490 yt_min += 0.5 * y_added_ticks * yn_tickinc;
491 yt_max -= 0.5 * y_added_ticks * yn_tickinc;
498 /*---------------------------------------------------------------------------*/
502 rSGP.setLineStyle (SGP::LS_SOLID);
505 /* Convert WC in graph boundary to axis boundary */
506 rSGP.setWindow (xgw_min, ygw_min, xgw_max, ygw_max); /* Graph boundary */
507 rSGP.setViewport (xgn_min, ygn_min, xgn_max, ygn_max);
508 double xminTemp = xa_min, xmaxTemp = xa_max;
509 double yminTemp = ya_min, ymaxTemp = ya_max;
510 rSGP.transformNDCtoMC (&xminTemp, &yminTemp); // calc WC to axis boundaries
511 rSGP.transformNDCtoMC (&xmaxTemp, &ymaxTemp);
512 rSGP.setWindow (xminTemp, yminTemp, xmaxTemp, ymaxTemp); // Set window to axis boundaries
513 rSGP.setViewport (xa_min, ya_min, xa_max, ya_max);
515 symwidth = charwidth * (xgw_max - xgw_min);
516 symheight = charheight * (ygw_max - ygw_min);
518 for (EZPlotCurveIterator iterCurve = m_vecCurves.begin(); iterCurve != m_vecCurves.end(); iterCurve++) {
519 const EZPlotCurve& curve = **iterCurve;
521 rSGP.setColor (curve.m_iColor);
522 if (curve.m_iLineStyle != SGP::LS_NOLINE) {
523 rSGP.setLineStyle (curve.m_iLineStyle);
524 rSGP.polylineAbs (curve.x, curve.y, curve.m_iPointCount);
526 if (curve.m_iSymbol > 0) {
527 rSGP.setLineStyle (SGP::LS_SOLID);
528 rSGP.moveAbs (curve.x[0], curve.y[0]);
529 symbol (curve.m_iSymbol, symwidth, symheight);
530 for (i = 1; i < curve.m_iPointCount; i++)
531 if (i % curve.m_iSymbolFreq == 0 || i == curve.m_iPointCount - 1) {
532 rSGP.moveAbs (curve.x[i], curve.y[i]);
533 symbol (curve.m_iSymbol, symwidth, symheight);
541 * drawAxes INTERNAL routine to draw axis & label them
549 EZPlot::drawAxes(void)
551 double xticklen = 0, yticklen = 0; /* length of ticks in NDC */
552 double minorinc; /* increment between minor axes */
553 double xaxispos, yaxispos; /* crossing of axes */
555 bool axis_near; /* TRUE if axis too close to print t-label */
560 rSGP.setTextSize (charheight);
561 rSGP.setTextColor (1, -1);
563 if (o_xticks == ABOVE)
564 xticklen = charheight;
565 else if (o_xticks == BELOW)
566 xticklen = -charheight;
568 if (o_yticks == RIGHT)
569 yticklen = charwidth;
570 else if (o_yticks == LEFT)
571 yticklen = -charwidth;
573 rSGP.setWindow (xp_min, yp_min, xp_max, yp_max);
574 rSGP.setViewport (xp_min, yp_min, xp_max, yp_max);
576 if (c_title.length() > 0) {
577 rSGP.moveAbs (xa_min + (xa_max-xa_min)/2 - c_title.length()*charwidth, title_row);
578 rSGP.setTextSize (charheight * 2.0);
579 rSGP.setTextColor (clr_title, -1);
580 rSGP.drawText (c_title);
581 rSGP.setTextSize (charheight);
584 if (o_grid == TRUE || o_box == TRUE) {
585 rSGP.setColor (clr_axis);
586 rSGP.moveAbs (xa_min, ya_min);
587 rSGP.lineAbs (xa_max, ya_min);
588 rSGP.lineAbs (xa_max, ya_max);
589 rSGP.lineAbs (xa_min, ya_max);
590 rSGP.lineAbs (xa_min, ya_min);
593 /* calculate position of axes */
596 if (s_ycross == TRUE) { /* convert users' world-coord */
597 xaxispos = v_ycross; /* axis to its position in NDC */
598 rSGP.setWindow (xgw_min, ygw_min, xgw_max, ygw_max);
599 rSGP.setViewport (xgn_min, ygn_min, xgn_max, ygn_max);
601 rSGP.transformMCtoNDC (x, xaxispos, &x, &xaxispos);
606 if (s_xcross == TRUE) { /* convert users' world-coord */
607 yaxispos = v_xcross; /* axis to its NDC position */
608 rSGP.setWindow (xgw_min, ygw_min, xgw_max, ygw_max);
609 rSGP.setViewport (xgn_min, ygn_min, xgn_max, ygn_max);
611 rSGP.transformMCtoNDC (yaxispos, y, &yaxispos, &y);
619 if (o_xaxis == LINEAR) {
620 rSGP.setWindow (xp_min, yp_min, xp_max, yp_max);
621 rSGP.setViewport (xp_min, yp_min, xp_max, yp_max);
625 rSGP.setColor (clr_axis);
626 if (o_tag && !o_grid && !o_box && s_xcross) {
627 rSGP.moveAbs (xa_min, xaxispos - charheight);
628 rSGP.lineAbs (xa_min, xaxispos + charheight);
630 rSGP.moveAbs (xa_min, xaxispos);
631 rSGP.lineAbs (xa_max, xaxispos);
632 if (o_tag && !o_grid && !o_box) {
633 rSGP.moveAbs (xa_max, xaxispos - charheight);
634 rSGP.lineAbs (xa_max, xaxispos + charheight);
637 if (o_grid == TRUE) {
638 rSGP.setColor (clr_grid);
639 for (i = 0; i <= x_nint; i++) {
640 rSGP.moveAbs (xt_min + xn_tickinc * i, ya_max);
641 rSGP.lineAbs (xt_min + xn_tickinc * i, ya_min);
644 rSGP.moveAbs (xa_min + (xa_max-xa_min)/2 - c_xlabel.length()*charwidth, xlbl_row);
645 rSGP.setTextSize (charheight * 2.0);
646 rSGP.setTextColor (clr_label, -1);
647 rSGP.drawText (c_xlabel);
648 rSGP.setTextSize (charheight);
649 minorinc = xn_tickinc / (o_xminortick + 1);
651 for (i = 0; i <= x_nint; i++) {
652 x = xt_min + xn_tickinc * i;
653 rSGP.setColor (clr_axis);
654 rSGP.moveAbs (x, xaxispos);
655 rSGP.lineAbs (x, xaxispos + xticklen);
657 for (j = 1; j <= o_xminortick; j++) {
658 x2 = x + minorinc * j;
659 rSGP.moveAbs (x2, xaxispos);
660 rSGP.lineAbs (x2, xaxispos + TICKRATIO * xticklen);
663 if (xaxispos + xtl_ofs > ya_min && o_yaxis != NOAXIS) {
666 xw = xgw_min + i * xw_tickinc;
667 rSGP.setWindow (xgw_min, ygw_min, xgw_max, ygw_max);
668 rSGP.setViewport (xgn_min, ygn_min, xgn_max, ygn_max);
669 rSGP.transformMCtoNDC (xw, y, &x, &y);
670 rSGP.setWindow (xp_min, yp_min, xp_max, yp_max);
671 rSGP.setViewport (xp_min, yp_min, xp_max, yp_max);
673 if (o_yticks == RIGHT && d >= 0 && d < 0.9 * xn_tickinc)
675 if (o_yticks == LEFT && d <= 0 && d > -0.9 * xn_tickinc)
679 if (o_xtlabel == TRUE && axis_near == FALSE) {
680 snprintf (str, sizeof(str), x_numfmt, xgw_min + xw_tickinc * i);
681 numstr = str_skip_head (str, " ");
682 rSGP.moveAbs (x-strlen(numstr)*charwidth/2,xaxispos + xtl_ofs);
683 rSGP.setTextColor (clr_number, -1);
684 rSGP.drawText (numstr);
694 if (o_yaxis == LINEAR) {
695 rSGP.setWindow (xp_min, yp_min, xp_max, yp_max);
696 rSGP.setViewport (xp_min, yp_min, xp_max, yp_max);
698 rSGP.setColor (clr_axis);
699 if (o_tag && !o_grid && !o_box && s_ycross) {
700 rSGP.moveAbs (yaxispos - charwidth, ya_min);
701 rSGP.lineAbs (yaxispos + charwidth, ya_min);
703 rSGP.moveAbs (yaxispos, ya_min);
704 rSGP.lineAbs (yaxispos, ya_max);
705 if (o_tag && !o_grid && !o_box) {
706 rSGP.moveAbs (yaxispos - charwidth, ya_max);
707 rSGP.lineAbs (yaxispos + charwidth, ya_max);
710 if (o_grid == TRUE) {
711 rSGP.setColor (clr_grid);
712 for (i = 0; i <= y_nint; i++) {
713 y = yt_min + yn_tickinc * i;
714 rSGP.moveAbs (xa_max, y);
715 rSGP.lineAbs (xa_min, y);
718 rSGP.moveAbs (ylbl_col,ya_min + (ya_max-ya_min)/2 - c_ylabel.length()*charheight);
719 rSGP.setTextAngle (HALFPI);
720 rSGP.setTextSize (2 * charheight);
721 rSGP.setTextColor (clr_label, -1);
722 rSGP.drawText (c_ylabel);
723 rSGP.setTextAngle (0.0);
724 rSGP.setTextSize (charheight);
725 minorinc = yn_tickinc / (o_yminortick + 1);
727 for (i = 0; i <= y_nint; i++) {
728 y = yt_min + yn_tickinc * i;
729 rSGP.setColor (clr_axis);
730 rSGP.moveAbs (yaxispos, y);
731 rSGP.lineAbs (yaxispos + yticklen, y);
733 for (j = 1; j <= o_yminortick; j++) {
734 y2 = y + minorinc * j;
735 rSGP.moveAbs (yaxispos, y2);
736 rSGP.lineAbs (yaxispos + TICKRATIO * yticklen, y2);
739 if (yaxispos + ytl_ofs > xa_min && o_xaxis != NOAXIS) {
742 yw = ygw_min + i * yw_tickinc;
743 rSGP.setWindow (xgw_min, ygw_min, xgw_max, ygw_max);
744 rSGP.setViewport (xgn_min, ygn_min, xgn_max, ygn_max);
745 rSGP.transformMCtoNDC (x, yw, &x, &y);
746 rSGP.setWindow (xp_min, yp_min, xp_max, yp_max);
747 rSGP.setViewport (xp_min, yp_min, xp_max, yp_max);
749 if (o_xticks == ABOVE && d >= 0 && d < 0.9 * yn_tickinc)
751 if (o_xticks == BELOW && d <= 0 && d > -0.9 * yn_tickinc)
754 if (o_ytlabel == TRUE && axis_near == FALSE) {
755 snprintf (str, sizeof(str), y_numfmt, ygw_min + yw_tickinc * i);
756 rSGP.moveAbs (yaxispos + ytl_ofs, y - 0.5 * charheight);
757 rSGP.setTextColor (clr_number, -1);
766 EZPlot::symbol (int sym, double symwidth, double symheight)
771 if (sym == SB_CROSS) {
772 rSGP.moveRel (-0.5 * symwidth, -0.5 * symheight);
773 rSGP.lineRel (symwidth, symheight);
774 rSGP.moveRel (-symwidth, 0.0);
775 rSGP.lineRel (symwidth, -symheight);
776 rSGP.moveRel (-0.5 * symwidth, 0.5 * symheight);
777 } else if (sym == SB_PLUS) {
778 rSGP.moveRel (-0.5 * symwidth, 0.0);
779 rSGP.lineRel (symwidth, 0.0);
780 rSGP.moveRel (-0.5 * symwidth, -0.5 * symheight);
781 rSGP.lineRel (0.0, symheight);
782 rSGP.moveRel (0.0, -0.5 * symheight);
783 } else if (sym == SB_BOX) {
784 rSGP.moveRel (-0.5 * symwidth, -0.5 * symheight);
785 rSGP.lineRel (symwidth, 0.0);
786 rSGP.lineRel (0.0, symheight);
787 rSGP.lineRel (-symwidth, 0.0);
788 rSGP.lineRel (0.0, -symheight);
789 rSGP.moveRel (0.5 * symwidth, 0.5 * symheight);
790 } else if (sym == SB_CIRCLE) {
791 rSGP.drawCircle (symwidth);
792 } else if (sym == SB_ERRORBAR) {
793 rSGP.moveRel (-0.5 * symwidth, 0.5 * symheight);
794 rSGP.lineRel (symwidth, 0.0);
795 rSGP.moveRel (-0.5 * symwidth, 0.0);
796 rSGP.lineRel (0.0, -symheight);
797 rSGP.moveRel (-0.5 * symwidth, 0.0);
798 rSGP.lineRel (symwidth, 0.0);
799 rSGP.moveRel (-0.5 * symwidth, 0.5 * symheight);
806 * axis_scale calculates graph axis scaling
809 * retval = axis_scale (min, max, nint, minp, maxp, nintp,
810 * rec_total, rec_frac)
813 * double min Smallest value to plot
814 * double max Largest value to plot
815 * int nint Number of intervals desired
818 * int retval FALSE if illegal parameters, else TRUE
819 * double *minp Minimum graph value
820 * double *maxp Maximum graph value
821 * int *nintp Number of intervals for graph
822 * int *rec_total Recommended field width for printing out the number
823 * int *rec_frac Recommended number of digits for print fraction
827 EZPlot::axis_scale (double min, double max, int nint, double *minp, double *maxp, int *nintp)
829 if (min >= max || nint < 1) {
830 sys_error (ERR_WARNING, "Invalid params: min=%lf, min=%lf, num intervals=%d [axis_scale]", min, max, nint);
835 double a = fabs(min);
836 if (fabs(min) < fabs(max))
838 double scale = pow (10.0, floor(log10(a)));
840 double mina = min / scale;
841 double maxa = max / scale;
842 double d = (maxa - mina) / nint;
844 double e = floor (log10(d));
845 double f = d / pow (10.0, e);
849 else if (f < sqrt (10.0))
851 else if (f < sqrt (50.0))
853 double wdt = v * pow (10.0, e);
854 double g = floor (mina / wdt);
855 if (fabs(g + 1 - mina / wdt) < j)
858 double h = floor (maxa / wdt) + 1.0;
859 if (fabs(maxa / wdt + 1 - h) < j)
862 *nintp = static_cast<int>(h - g);
863 if (fabs(*maxp) >= 10.0 || fabs(*minp) >= 10.0) {
864 scale = scale * 10.0;
876 * make_numfmt Make a numeric format string
879 * make_numfmt (fmtstr, fldwid, nfrac, min, max, nint)
880 * char *fmtstr Returned format string for printf()
881 * int *fldwid Returned field width
882 * int *nfrac If < 0, then calculate best number of
883 * fraction places & return that value
884 * If >= 0, then use that number of places
885 * double min Minimum value
886 * double max Maximum value
887 * int nint Number of intervals between min & max
890 * This routine is written as an INTERNAL routine for EZPLOT
904 EZPlot::make_numfmt (char *fmtstr, int *fldwid, int *nfrac, double minval, double maxval, int nint)
906 int wid, frac, expon;
908 double delta = (maxval - minval) / nint;
909 double absmin = fabs(minval);
910 double absmax = fabs(maxval);
911 double logt = log10( max(absmin, absmax) );
913 if (fabs(logt) >= 6) { /* use exponential format */
915 expon = 5; /* E+102 */
917 expon = 4; /* E+00 */
919 if (*nfrac < 0) { /* calculate frac */
920 delta /= pow (10., floor(logt)); /* scale delta */
921 frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
923 frac = 1; /* to be safe, add decimal pt */
924 } else /* use users' frac */
927 wid = 2 + frac + expon;
928 if (minval < 0. || maxval < 0.)
930 sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "g");
931 } else { /* use fixed format */
932 wid = static_cast<int>(trunc(logt)) + 1;
935 if (minval < 0. || maxval < 0.)
938 if (*nfrac < 0) { /* calculate frac */
939 if (delta >= 0.999999)
940 frac = 1; /* add a decimal pt to be safe */
942 frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
943 } else /* use users' frac */
947 sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "f");