1 /*****************************************************************************
6 ** This is part of the CTSim program
7 ** Copyright (C) 1983-2000 Kevin Rosenberg
9 ** $Id: ezplot.cpp,v 1.13 2000/09/02 05:10:39 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 *y, int n)
79 for (int i = 0; i < n; i++)
82 addCurve (yDouble, n);
87 EZPlot::addCurve (const float x[], const double y[], int num)
91 for (int i = 0; i < num; i++)
94 addCurve (dx, y, num);
98 EZPlot::addCurve (const double x[], const float y[], int num)
102 for (int i = 0; i < num; i++)
105 addCurve (x, dy, num);
110 EZPlot::addCurve (const double x[], const double y[], int num)
115 EZPlotCurve* pCurve = new EZPlotCurve (x, y, num, o_color, o_linestyle, o_symbol, o_symfreq, c_legend);
116 m_vecCurves.push_back (pCurve);
122 for (EZPlotCurveIterator i = m_vecCurves.begin(); i != m_vecCurves.end(); i++)
127 EZPlot::clearCurves ()
129 for (EZPlotCurveIterator i = m_vecCurves.begin(); i != m_vecCurves.end(); i++)
131 m_vecCurves.erase (m_vecCurves.begin(), m_vecCurves.end());
136 EZPlot::EZPlot (SGP& sgp)
143 EZPlot::initPlotSettings ()
145 charwidth = DEF_CHARWIDTH;
146 charheight = DEF_CHARHEIGHT;
169 o_color = DEF_CURVE_CLR;
172 o_linestyle = SGP::LS_SOLID;
179 o_legendbox = INSIDE;
192 clr_axis = C_BLACK; /* set fixed colors */
196 clr_number = C_GREEN;
202 * plot Plots all curves collected by addCurves ()
208 * This routine plots the curves that have stored by addCurves().
211 * drawAxes() & make_numfmt()
217 double x_added_ticks; /* number of tick spaces added to axis */
218 double y_added_ticks;
219 double symwidth, symheight; /* size of symbol in NDC */
220 double leg_width, leg_height; /* size of legend box */
223 if (m_vecCurves.size() <= 0)
226 if (s_textsize == TRUE)
227 charheight = v_textsize;
229 charheight = DEF_CHARHEIGHT;
231 const EZPlotCurve& firstCurve = *m_vecCurves[0];
232 double xmin = firstCurve.x[0]; // extent of curves in world coord
234 double ymin = firstCurve.y[0];
237 for (EZPlotCurveConstIterator iterCurve = m_vecCurves.begin(); iterCurve != m_vecCurves.end(); iterCurve++) {
238 const EZPlotCurve& curve = **iterCurve;
240 for (ip = 0; ip < curve.m_iPointCount; ip++) {
241 if (curve.x[ip] > xmax)
243 else if (curve.x[ip] < xmin)
245 if (curve.y[ip] > ymax)
247 else if (curve.y[ip] < ymin)
252 // extend graph limits for user defined axis cross positions
253 if (s_xcross == TRUE) {
256 else if (v_xcross > xmax)
260 if (s_ycross == TRUE) {
263 else if (v_ycross > ymax)
267 /* find nice endpoints for axes */
268 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))
271 /* check if user set x-axis extents */
272 if (s_xmin == TRUE) {
274 x_nint = o_xmajortick - 1;
276 if (s_xmax == TRUE) {
278 x_nint = o_xmajortick - 1;
281 /* check if user set y-axis extents */
282 if (s_ymin == TRUE) {
284 y_nint = o_ymajortick - 1;
286 if (s_ymax == TRUE) {
288 y_nint = o_ymajortick - 1;
291 /* calculate increment between major axis in world coordinates */
292 xw_tickinc = (xgw_max - xgw_min) / x_nint;
293 yw_tickinc = (ygw_max - ygw_min) / y_nint;
295 /* we have now calcuated xgw_min, xyw_max, ygw_min, & ygw_max */
297 // set the number of decimal point to users' setting or default
298 // Two formats for numbers: Fixed: -nnn.f and Exponent: -n.fffE+eee
299 if (s_lxfrac == TRUE)
304 if (s_lyfrac == TRUE)
309 make_numfmt (x_numfmt, &x_fldwid, &x_frac, xgw_min, xgw_max, x_nint);
310 make_numfmt (y_numfmt, &y_fldwid, &y_frac, ygw_min, ygw_max, y_nint);
312 xtl_wid = x_fldwid * charwidth; /* calc size of tick labels */
313 ytl_wid = y_fldwid * charwidth;
314 tl_height = charheight;
316 // rSGP.getViewport (xp_min, yp_min, xp_max, yp_max);
317 /* calculate the extent of the plot frame */
320 xp_max = xp_min + o_xlength;
321 yp_max = yp_min + o_ylength;
323 xp_min = clamp (xp_min, 0., 1.);
324 xp_max = clamp (xp_max, 0., 1.);
325 yp_min = clamp (yp_min, 0., 1.);
326 yp_max = clamp (yp_max, 0., 1.);
328 xa_min = xp_min; /* extent of axes */
333 /* adjust frame for title */
334 if (c_title.length() > 0)
335 ya_max -= 2.5 * charheight;
336 title_row = ya_max + charheight;
338 /* calculate legend box boundaries */
339 int max_leg = 0; /* longest legend in characters */
340 int num_leg = 0; /* number of legend titles */
341 for (EZPlotCurveConstIterator iterCurve = m_vecCurves.begin(); iterCurve != m_vecCurves.end(); iterCurve++) {
342 const EZPlotCurve& curve = **iterCurve;
343 if ((n = curve.m_sLegend.length()) > 0) {
345 max_leg = max (max_leg, n);
349 if (num_leg > 0 && o_legendbox != NOLEGEND) {
350 leg_width = (max_leg + 2) * charwidth;
351 leg_height = num_leg * 3 * charheight;
353 if (s_xlegend == TRUE)
357 if (o_legendbox == OUTSIDE)
358 xa_max -= (leg_width + 0.5 * charwidth);
360 xl_min = xl_max - leg_width;
362 if (s_ylegend == TRUE)
367 yl_min = yl_max - leg_height;
369 rSGP.setWindow (xl_min, yl_min, xl_max, yl_max);
370 rSGP.setViewport (xl_min, yl_min, xl_max, yl_max);
371 rSGP.setColor (clr_legend);
372 rSGP.drawRect (xl_min, yl_min, xl_max, yl_max);
374 n = 0; /* current legend position */
375 for (EZPlotCurveIterator iterCurve = m_vecCurves.begin(); iterCurve != m_vecCurves.end(); iterCurve++) {
376 const EZPlotCurve& curve = **iterCurve;
378 if (curve.m_sLegend.length() == 0)
381 double xmin = xl_min + 1.0 * charwidth;
382 double xmax = xl_max - 1.0 * charwidth;
383 double y = yl_max - (2.0 + n * 3) * charheight;
385 rSGP.moveAbs (xmin, y + 0.5 * charheight);
386 rSGP.drawText (curve.m_sLegend);
387 rSGP.setColor (curve.m_iColor);
388 if (curve.m_iLineStyle != SGP::LS_NOLINE) {
389 rSGP.setLineStyle (curve.m_iLineStyle);
390 rSGP.moveAbs (xmin, y);
391 rSGP.lineAbs (xmax, y);
393 if (curve.m_iSymbol > 0) {
394 double xinc = (xmax - xmin) / (5 - 1);
395 rSGP.setLineStyle (SGP::LS_SOLID);
396 for (j = 0; j < 5; j++) {
397 rSGP.moveAbs (xmin + j * xinc, y);
398 symbol(curve.m_iSymbol, 0.5 * charwidth, 0.5 * charheight);
401 ++n; /* move to next legend position */
403 } /* end legend printing */
405 /* calculate the extent of the axes */
407 /*-------------------------*/
408 /* adjust frame for labels */
409 /*-------------------------*/
412 if (c_xlabel.length() > 0)
413 ya_min += 3.0 * charheight;
414 xlbl_row = xp_min; /* put x-label on bottom of plot frame */
417 if (c_ylabel.length() > 0)
418 xa_min += 3.0 * charwidth; /* reverse rSGP.setTextSize because writing text sideways */
419 ylbl_col = xp_min + 2 * charwidth;
421 /*------------------------------*/
422 /* adjust frame for tick labels */
423 /*------------------------------*/
425 /* calc offset of tick labels from axes */
426 if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
428 else if (o_xticks == BELOW)
429 xtl_ofs = -2.5 * charheight;
430 else if (o_xticks == ABOVE)
431 xtl_ofs = 1.5 * charheight;
433 if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
435 else if (o_yticks == LEFT)
436 ytl_ofs = -(1 + y_fldwid) * charwidth;
437 else if (o_yticks == RIGHT)
438 ytl_ofs = 1.5 * charwidth;
440 /* see if need to shrink axis extents and/or tick extents */
441 if (xtl_ofs != 0.0 && s_ycross == FALSE) {
442 if (o_xticks == BELOW) {
443 ya_min += 2.5 * charheight;
445 } else if (o_xticks == ABOVE) {
447 yt_min = ya_min + 2.5 * charheight;
449 } else /* noaxis, no t-labels, or user set cross */
452 if (ytl_ofs != 0.0 && s_xcross == FALSE) {
453 if (o_yticks == LEFT) {
454 xa_min += (1 + y_fldwid) * charwidth;
456 } else if (o_yticks == RIGHT) {
458 xt_min = xa_min + ytl_ofs + y_fldwid * charwidth;
466 /* decrease size of graph, if necessary, to accommadate space */
467 /* between axis boundary and boundary of ticks */
472 if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
474 if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
477 if (o_grid == TRUE) {
478 if (x_added_ticks < 0)
480 if (y_added_ticks < 0)
484 if (x_added_ticks < 0) {
485 if (o_yticks == LEFT || s_ycross)
491 if (y_added_ticks < 0) {
492 if (o_xticks == BELOW || s_xcross)
498 xn_tickinc = (xt_max - xt_min) / (x_nint + x_added_ticks);
499 yn_tickinc = (yt_max - yt_min) / (y_nint + y_added_ticks);
501 xt_min += 0.5 * x_added_ticks * xn_tickinc;
502 xt_max -= 0.5 * x_added_ticks * xn_tickinc;
503 yt_min += 0.5 * y_added_ticks * yn_tickinc;
504 yt_max -= 0.5 * y_added_ticks * yn_tickinc;
511 /*---------------------------------------------------------------------------*/
515 rSGP.setLineStyle (SGP::LS_SOLID);
518 /* Convert WC in graph boundary to axis boundary */
519 rSGP.setWindow (xgw_min, ygw_min, xgw_max, ygw_max); /* Graph boundary */
520 rSGP.setViewport (xgn_min, ygn_min, xgn_max, ygn_max);
521 double xminTemp = xa_min, xmaxTemp = xa_max;
522 double yminTemp = ya_min, ymaxTemp = ya_max;
523 rSGP.transformNDCtoMC (&xminTemp, &yminTemp); // calc WC to axis boundaries
524 rSGP.transformNDCtoMC (&xmaxTemp, &ymaxTemp);
525 rSGP.setWindow (xminTemp, yminTemp, xmaxTemp, ymaxTemp); // Set window to axis boundaries
526 rSGP.setViewport (xa_min, ya_min, xa_max, ya_max);
528 symwidth = charwidth * (xgw_max - xgw_min);
529 symheight = charheight * (ygw_max - ygw_min);
531 for (EZPlotCurveIterator iterCurve = m_vecCurves.begin(); iterCurve != m_vecCurves.end(); iterCurve++) {
532 const EZPlotCurve& curve = **iterCurve;
534 rSGP.setColor (curve.m_iColor);
535 if (curve.m_iLineStyle != SGP::LS_NOLINE) {
536 rSGP.setLineStyle (curve.m_iLineStyle);
537 rSGP.polylineAbs (curve.x, curve.y, curve.m_iPointCount);
539 if (curve.m_iSymbol > 0) {
540 rSGP.setLineStyle (SGP::LS_SOLID);
541 rSGP.moveAbs (curve.x[0], curve.y[0]);
542 symbol (curve.m_iSymbol, symwidth, symheight);
543 for (i = 1; i < curve.m_iPointCount; i++)
544 if (i % curve.m_iSymbolFreq == 0 || i == curve.m_iPointCount - 1) {
545 rSGP.moveAbs (curve.x[i], curve.y[i]);
546 symbol (curve.m_iSymbol, symwidth, symheight);
554 * drawAxes INTERNAL routine to draw axis & label them
562 EZPlot::drawAxes(void)
564 double xticklen = 0, yticklen = 0; /* length of ticks in NDC */
565 double minorinc; /* increment between minor axes */
566 double xaxispos, yaxispos; /* crossing of axes */
568 bool axis_near; /* TRUE if axis too close to print t-label */
573 rSGP.setTextSize (charheight);
574 rSGP.setTextColor (1, -1);
576 if (o_xticks == ABOVE)
577 xticklen = charheight;
578 else if (o_xticks == BELOW)
579 xticklen = -charheight;
581 if (o_yticks == RIGHT)
582 yticklen = charwidth;
583 else if (o_yticks == LEFT)
584 yticklen = -charwidth;
586 rSGP.setWindow (xp_min, yp_min, xp_max, yp_max);
587 rSGP.setViewport (xp_min, yp_min, xp_max, yp_max);
589 if (c_title.length() > 0) {
591 rSGP.getTextExtent (c_title.c_str(), &wText, &hText);
592 rSGP.moveAbs (xa_min + (xa_max-xa_min)/2 - wText/2, title_row);
593 rSGP.setTextSize (charheight * 2.0);
594 rSGP.setTextColor (clr_title, -1);
595 rSGP.drawText (c_title);
596 rSGP.setTextSize (charheight);
599 if (o_grid == TRUE || o_box == TRUE) {
600 rSGP.setColor (clr_axis);
601 rSGP.moveAbs (xa_min, ya_min);
602 rSGP.lineAbs (xa_max, ya_min);
603 rSGP.lineAbs (xa_max, ya_max);
604 rSGP.lineAbs (xa_min, ya_max);
605 rSGP.lineAbs (xa_min, ya_min);
608 /* calculate position of axes */
611 if (s_ycross == TRUE) { /* convert users' world-coord */
612 xaxispos = v_ycross; /* axis to its position in NDC */
613 rSGP.setWindow (xgw_min, ygw_min, xgw_max, ygw_max);
614 rSGP.setViewport (xgn_min, ygn_min, xgn_max, ygn_max);
616 rSGP.transformMCtoNDC (x, xaxispos, &x, &xaxispos);
621 if (s_xcross == TRUE) { /* convert users' world-coord */
622 yaxispos = v_xcross; /* axis to its NDC position */
623 rSGP.setWindow (xgw_min, ygw_min, xgw_max, ygw_max);
624 rSGP.setViewport (xgn_min, ygn_min, xgn_max, ygn_max);
626 rSGP.transformMCtoNDC (yaxispos, y, &yaxispos, &y);
634 if (o_xaxis == LINEAR) {
635 rSGP.setWindow (xp_min, yp_min, xp_max, yp_max);
636 rSGP.setViewport (xp_min, yp_min, xp_max, yp_max);
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) {
681 xw = xgw_min + i * xw_tickinc;
682 rSGP.setWindow (xgw_min, ygw_min, xgw_max, ygw_max);
683 rSGP.setViewport (xgn_min, ygn_min, xgn_max, ygn_max);
684 rSGP.transformMCtoNDC (xw, y, &x, &y);
685 rSGP.setWindow (xp_min, yp_min, xp_max, yp_max);
686 rSGP.setViewport (xp_min, yp_min, xp_max, yp_max);
688 if (o_yticks == RIGHT && d >= 0 && d < 0.9 * xn_tickinc)
690 if (o_yticks == LEFT && d <= 0 && d > -0.9 * xn_tickinc)
694 if (o_xtlabel == TRUE && axis_near == FALSE) {
695 snprintf (str, sizeof(str), x_numfmt, xgw_min + xw_tickinc * i);
696 numstr = str_skip_head (str, " ");
697 rSGP.moveAbs (x-strlen(numstr)*charwidth/2,xaxispos + xtl_ofs);
698 rSGP.setTextColor (clr_number, -1);
699 rSGP.drawText (numstr);
709 if (o_yaxis == LINEAR) {
710 rSGP.setWindow (xp_min, yp_min, xp_max, yp_max);
711 rSGP.setViewport (xp_min, yp_min, xp_max, yp_max);
713 rSGP.setColor (clr_axis);
714 if (o_tag && !o_grid && !o_box && s_ycross) {
715 rSGP.moveAbs (yaxispos - charwidth, ya_min);
716 rSGP.lineAbs (yaxispos + charwidth, ya_min);
718 rSGP.moveAbs (yaxispos, ya_min);
719 rSGP.lineAbs (yaxispos, ya_max);
720 if (o_tag && !o_grid && !o_box) {
721 rSGP.moveAbs (yaxispos - charwidth, ya_max);
722 rSGP.lineAbs (yaxispos + charwidth, ya_max);
725 if (o_grid == TRUE) {
726 rSGP.setColor (clr_grid);
727 for (i = 0; i <= y_nint; i++) {
728 y = yt_min + yn_tickinc * i;
729 rSGP.moveAbs (xa_max, y);
730 rSGP.lineAbs (xa_min, y);
733 rSGP.moveAbs (ylbl_col,ya_min + (ya_max-ya_min)/2 - c_ylabel.length()*charheight);
734 rSGP.setTextAngle (HALFPI);
735 rSGP.setTextSize (2 * charheight);
736 rSGP.setTextColor (clr_label, -1);
737 rSGP.drawText (c_ylabel);
738 rSGP.setTextAngle (0.0);
739 rSGP.setTextSize (charheight);
740 minorinc = yn_tickinc / (o_yminortick + 1);
742 for (i = 0; i <= y_nint; i++) {
743 y = yt_min + yn_tickinc * i;
744 rSGP.setColor (clr_axis);
745 rSGP.moveAbs (yaxispos, y);
746 rSGP.lineAbs (yaxispos + yticklen, y);
748 for (j = 1; j <= o_yminortick; j++) {
749 y2 = y + minorinc * j;
750 rSGP.moveAbs (yaxispos, y2);
751 rSGP.lineAbs (yaxispos + TICKRATIO * yticklen, y2);
754 if (yaxispos + ytl_ofs > xa_min && o_xaxis != NOAXIS) {
757 yw = ygw_min + i * yw_tickinc;
758 rSGP.setWindow (xgw_min, ygw_min, xgw_max, ygw_max);
759 rSGP.setViewport (xgn_min, ygn_min, xgn_max, ygn_max);
760 rSGP.transformMCtoNDC (x, yw, &x, &y);
761 rSGP.setWindow (xp_min, yp_min, xp_max, yp_max);
762 rSGP.setViewport (xp_min, yp_min, xp_max, yp_max);
764 if (o_xticks == ABOVE && d >= 0 && d < 0.9 * yn_tickinc)
766 if (o_xticks == BELOW && d <= 0 && d > -0.9 * yn_tickinc)
769 if (o_ytlabel == TRUE && axis_near == FALSE) {
770 snprintf (str, sizeof(str), y_numfmt, ygw_min + yw_tickinc * i);
771 rSGP.moveAbs (yaxispos + ytl_ofs, y - 0.5 * charheight);
772 rSGP.setTextColor (clr_number, -1);
781 EZPlot::symbol (int sym, double symwidth, double symheight)
786 if (sym == SB_CROSS) {
787 rSGP.moveRel (-0.5 * symwidth, -0.5 * symheight);
788 rSGP.lineRel (symwidth, symheight);
789 rSGP.moveRel (-symwidth, 0.0);
790 rSGP.lineRel (symwidth, -symheight);
791 rSGP.moveRel (-0.5 * symwidth, 0.5 * symheight);
792 } else if (sym == SB_PLUS) {
793 rSGP.moveRel (-0.5 * symwidth, 0.0);
794 rSGP.lineRel (symwidth, 0.0);
795 rSGP.moveRel (-0.5 * symwidth, -0.5 * symheight);
796 rSGP.lineRel (0.0, symheight);
797 rSGP.moveRel (0.0, -0.5 * symheight);
798 } else if (sym == SB_BOX) {
799 rSGP.moveRel (-0.5 * symwidth, -0.5 * symheight);
800 rSGP.lineRel (symwidth, 0.0);
801 rSGP.lineRel (0.0, symheight);
802 rSGP.lineRel (-symwidth, 0.0);
803 rSGP.lineRel (0.0, -symheight);
804 rSGP.moveRel (0.5 * symwidth, 0.5 * symheight);
805 } else if (sym == SB_CIRCLE) {
806 rSGP.drawCircle (symwidth);
807 } else if (sym == SB_ERRORBAR) {
808 rSGP.moveRel (-0.5 * symwidth, 0.5 * symheight);
809 rSGP.lineRel (symwidth, 0.0);
810 rSGP.moveRel (-0.5 * symwidth, 0.0);
811 rSGP.lineRel (0.0, -symheight);
812 rSGP.moveRel (-0.5 * symwidth, 0.0);
813 rSGP.lineRel (symwidth, 0.0);
814 rSGP.moveRel (-0.5 * symwidth, 0.5 * symheight);
821 * axis_scale calculates graph axis scaling
824 * retval = axis_scale (min, max, nint, minp, maxp, nintp,
825 * rec_total, rec_frac)
828 * double min Smallest value to plot
829 * double max Largest value to plot
830 * int nint Number of intervals desired
833 * int retval FALSE if illegal parameters, else TRUE
834 * double *minp Minimum graph value
835 * double *maxp Maximum graph value
836 * int *nintp Number of intervals for graph
837 * int *rec_total Recommended field width for printing out the number
838 * int *rec_frac Recommended number of digits for print fraction
842 EZPlot::axis_scale (double min, double max, int nint, double *minp, double *maxp, int *nintp)
844 if (min >= max || nint < 1) {
845 sys_error (ERR_WARNING, "Invalid params: min=%lf, max=%lf, num intervals=%d [axis_scale]", min, max, nint);
850 double a = fabs(min);
851 if (fabs(min) < fabs(max))
853 double scale = pow (10.0, floor(log10(a)));
855 double mina = min / scale;
856 double maxa = max / scale;
857 double d = (maxa - mina) / nint;
859 double e = floor (log10(d));
860 double f = d / pow (10.0, e);
864 else if (f < sqrt (10.0))
866 else if (f < sqrt (50.0))
868 double wdt = v * pow (10.0, e);
869 double g = floor (mina / wdt);
870 if (fabs(g + 1 - mina / wdt) < j)
873 double h = floor (maxa / wdt) + 1.0;
874 if (fabs(maxa / wdt + 1 - h) < j)
877 *nintp = static_cast<int>(h - g);
878 if (fabs(*maxp) >= 10.0 || fabs(*minp) >= 10.0) {
879 scale = scale * 10.0;
891 * make_numfmt Make a numeric format string
894 * make_numfmt (fmtstr, fldwid, nfrac, min, max, nint)
895 * char *fmtstr Returned format string for printf()
896 * int *fldwid Returned field width
897 * int *nfrac If < 0, then calculate best number of
898 * fraction places & return that value
899 * If >= 0, then use that number of places
900 * double min Minimum value
901 * double max Maximum value
902 * int nint Number of intervals between min & max
905 * This routine is written as an INTERNAL routine for EZPLOT
919 EZPlot::make_numfmt (char *fmtstr, int *fldwid, int *nfrac, double minval, double maxval, int nint)
921 int wid, frac, expon;
923 double delta = (maxval - minval) / nint;
924 double absmin = fabs(minval);
925 double absmax = fabs(maxval);
926 double logt = log10( max(absmin, absmax) );
928 if (fabs(logt) >= 6) { /* use exponential format */
930 expon = 5; /* E+102 */
932 expon = 4; /* E+00 */
934 if (*nfrac < 0) { /* calculate frac */
935 delta /= pow (10., floor(logt)); /* scale delta */
936 frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
938 frac = 1; /* to be safe, add decimal pt */
939 } else /* use users' frac */
942 wid = 2 + frac + expon;
943 if (minval < 0. || maxval < 0.)
945 sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "g");
946 } else { /* use fixed format */
947 wid = static_cast<int>(trunc(logt)) + 1;
950 if (minval < 0. || maxval < 0.)
953 if (*nfrac < 0) { /* calculate frac */
954 if (delta >= 0.999999)
955 frac = 1; /* add a decimal pt to be safe */
957 frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
958 } else /* use users' frac */
962 sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "f");