1 /*****************************************************************************
6 ** This is part of the CTSim program
7 ** Copyright (C) 1983-2000 Kevin Rosenberg
9 ** $Id: ezplot.cpp,v 1.7 2000/07/28 08:28: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_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)
45 for (int i = 0; i < n; i++) {
50 int copyCount = n * sizeof(double);
51 memcpy (x, xData, copyCount);
52 memcpy (y, yData, copyCount);
56 m_iLineStyle = linestyle;
58 m_iSymbolFreq = symfreq;
61 EZPlotCurve::~EZPlotCurve ()
69 EZPlot::addCurve (const double *y, int n)
73 for (int i = 0; i < n; i++)
81 EZPlot::addCurve (const float x[], const double y[], int num)
85 for (int i = 0; i < num; i++)
88 addCurve (dx, y, num);
93 EZPlot::addCurve (const double x[], const double y[], int num)
98 int iNumCurves = m_vecCurves.size();
100 if (o_unknowncurves == FALSE && iNumCurves >= o_reqcurves)
103 if (o_unknowncurves == TRUE || iNumCurves < o_reqcurves) {
104 EZPlotCurve* pCurve = new EZPlotCurve (x, y, num, o_color, o_linestyle, o_symbol, o_symfreq, c_legend);
105 m_vecCurves.push_back (pCurve);
108 iNumCurves = m_vecCurves.size(); // recalculate
109 if (o_unknowncurves == FALSE && iNumCurves >= o_reqcurves) {
118 for (EZPlotCurveIterator i = m_vecCurves.begin(); i != m_vecCurves.end(); i++)
123 EZPlot::clearCurves ()
125 for (EZPlotCurveIterator i = m_vecCurves.begin(); i != m_vecCurves.end(); i++) delete *i;
126 m_vecCurves.erase (m_vecCurves.begin(), m_vecCurves.end());
130 EZPlot::EZPlot (SGP& sgp)
135 charwidth = DEF_CHARWIDTH;
136 charheight = DEF_CHARHEIGHT;
144 o_unknowncurves = FALSE;
162 o_color = DEF_CURVE_CLR;
165 o_linestyle = SGP::LS_SOLID;
172 o_legendbox = INSIDE;
185 clr_axis = C_WHITE; /* set fixed colors */
186 clr_title = (C_CYAN+8);
187 clr_label = (C_CYAN+8);
188 clr_legend = (C_RED+8);
189 clr_number = (C_GREEN+8);
190 clr_grid = (C_BLACK+8);
195 * plot INTERNAL: Plots all curves collected by ezplot()
201 * This routine plots the curves that have stored by ezplot().
207 * drawAxes() & make_numfmt()
213 double x_added_ticks; /* number of tick spaces added to axis */
214 double y_added_ticks;
215 double symwidth, symheight; /* size of symbol in NDC */
216 double leg_width, leg_height; /* size of legend box */
219 if (m_vecCurves.size() <= 0)
222 if (s_textsize == TRUE)
223 charheight = v_textsize;
225 charheight = DEF_CHARHEIGHT;
227 const EZPlotCurve& firstCurve = *m_vecCurves[0];
228 double xmin = firstCurve.x[0]; // extent of curves in world coord
230 double ymin = firstCurve.y[0];
233 for (EZPlotCurveConstIterator iterCurve = m_vecCurves.begin(); iterCurve != m_vecCurves.end(); iterCurve++) {
234 const EZPlotCurve& curve = **iterCurve;
236 for (ip = 0; ip < curve.m_iPointCount; ip++) {
237 if (curve.x[ip] > xmax)
239 else if (curve.x[ip] < xmin)
241 if (curve.y[ip] > ymax)
243 else if (curve.y[ip] < ymin)
248 // extend graph limits for user defined axis cross positions
249 if (s_xcross == TRUE) {
252 else if (v_xcross > xmax)
256 if (s_ycross == TRUE) {
259 else if (v_ycross > ymax)
263 /* find nice endpoints for axes */
264 axis_scale (xmin, xmax, o_xmajortick - 1, &xgw_min, &xgw_max, &x_nint);
265 axis_scale (ymin, ymax, o_ymajortick - 1, &ygw_min, &ygw_max, &y_nint);
267 /* check if user set x-axis extents */
268 if (s_xmin == TRUE) {
270 x_nint = o_xmajortick - 1;
272 if (s_xmax == TRUE) {
274 x_nint = o_xmajortick - 1;
277 /* check if user set y-axis extents */
278 if (s_ymin == TRUE) {
280 y_nint = o_ymajortick - 1;
282 if (s_ymax == TRUE) {
284 y_nint = o_ymajortick - 1;
287 /* calculate increment between major axis in world coordinates */
288 xw_tickinc = (xgw_max - xgw_min) / x_nint;
289 yw_tickinc = (ygw_max - ygw_min) / y_nint;
291 /* we have now calcuated xgw_min, xyw_max, ygw_min, & ygw_max */
293 // set the number of decimal point to users' setting or default
294 // Two formats for numbers: Fixed: -nnn.f and Exponent: -n.fffE+eee
295 if (s_lxfrac == TRUE)
300 if (s_lyfrac == TRUE)
305 make_numfmt (x_numfmt, &x_fldwid, &x_frac, xgw_min, xgw_max, x_nint);
306 make_numfmt (y_numfmt, &y_fldwid, &y_frac, ygw_min, ygw_max, y_nint);
308 xtl_wid = x_fldwid * charwidth; /* calc size of tick labels */
309 ytl_wid = y_fldwid * charwidth;
310 tl_height = charheight;
312 /* calculate the extent of the plot frame */
315 xp_max = xp_min + o_xlength;
316 yp_max = yp_min + o_ylength;
318 xp_min = clamp (xp_min, 0., 1.);
319 xp_max = clamp (xp_max, 0., 1.);
320 yp_min = clamp (yp_min, 0., 1.);
321 yp_max = clamp (yp_max, 0., 1.);
323 xa_min = xp_min; /* extent of axes */
328 /* adjust frame for title */
329 if (c_title.length() > 0)
330 ya_max -= 2.5 * charheight;
331 title_row = ya_max + 0.5 * charheight;
333 /* calculate legend box boundaries */
334 int max_leg = 0; /* longest legend in characters */
335 int num_leg = 0; /* number of legend titles */
336 for (EZPlotCurveConstIterator iterCurve = m_vecCurves.begin(); iterCurve != m_vecCurves.end(); iterCurve++) {
337 const EZPlotCurve& curve = **iterCurve;
338 if ((n = curve.m_sLegend.length()) > 0) {
340 max_leg = max (max_leg, n);
344 if (num_leg > 0 && o_legendbox != NOLEGEND) {
345 leg_width = (max_leg + 2) * charwidth;
346 leg_height = num_leg * 3 * charheight;
348 if (s_xlegend == TRUE)
352 if (o_legendbox == OUTSIDE)
353 xa_max -= (leg_width + 0.5 * charwidth);
355 xl_min = xl_max - leg_width;
357 if (s_ylegend == TRUE)
362 yl_min = yl_max - leg_height;
364 rSGP.setWindow (xl_min, yl_min, xl_max, yl_max);
365 rSGP.setViewport (xl_min, yl_min, xl_max, yl_max);
366 rSGP.setColor (clr_legend);
367 rSGP.drawRect (xl_min, yl_min, xl_max, yl_max);
369 n = 0; /* current legend position */
370 for (EZPlotCurveIterator iterCurve = m_vecCurves.begin(); iterCurve != m_vecCurves.end(); iterCurve++) {
371 const EZPlotCurve& curve = **iterCurve;
373 if (curve.m_sLegend.length() == 0)
376 double xmin = xl_min + 1.0 * charwidth;
377 double xmax = xl_max - 1.0 * charwidth;
378 double y = yl_max - (2.0 + n * 3) * charheight;
380 rSGP.moveAbs (xmin, y + 0.5 * charheight);
381 rSGP.drawText (curve.m_sLegend);
382 rSGP.setColor (curve.m_iColor);
383 if (curve.m_iLineStyle != SGP::LS_NOLINE) {
384 rSGP.setLineStyle (curve.m_iLineStyle);
385 rSGP.moveAbs (xmin, y);
386 rSGP.lineAbs (xmax, y);
388 if (curve.m_iSymbol > 0) {
389 double xinc = (xmax - xmin) / (5 - 1);
390 rSGP.setLineStyle (SGP::LS_SOLID);
391 for (j = 0; j < 5; j++) {
392 rSGP.moveAbs (xmin + j * xinc, y);
393 symbol(curve.m_iSymbol, 0.5 * charwidth, 0.5 * charheight);
396 ++n; /* move to next legend position */
398 } /* end legend printing */
400 /* calculate the extent of the axes */
402 /*-------------------------*/
403 /* adjust frame for labels */
404 /*-------------------------*/
407 if (c_xlabel.length() > 0)
408 ya_min += 3.0 * charheight;
409 xlbl_row = xp_min; /* put x-label on bottom of plot frame */
412 if (c_ylabel.length() > 0)
413 xa_min += 3.0 * charwidth; /* reverse rSGP.setTextSize because writing text sideways */
414 ylbl_col = xp_min + 2 * charwidth;
416 /*------------------------------*/
417 /* adjust frame for tick labels */
418 /*------------------------------*/
420 /* calc offset of tick labels from axes */
421 if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
423 else if (o_xticks == BELOW)
424 xtl_ofs = -2.5 * charheight;
425 else if (o_xticks == ABOVE)
426 xtl_ofs = 1.5 * charheight;
428 if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
430 else if (o_yticks == LEFT)
431 ytl_ofs = -(1 + y_fldwid) * charwidth;
432 else if (o_yticks == RIGHT)
433 ytl_ofs = 1.5 * charwidth;
435 /* see if need to shrink axis extents and/or tick extents */
436 if (xtl_ofs != 0.0 && s_ycross == FALSE) {
437 if (o_xticks == BELOW) {
438 ya_min += 2.5 * charheight;
440 } else if (o_xticks == ABOVE) {
442 yt_min = ya_min + 2.5 * charheight;
444 } else /* noaxis, no t-labels, or user set cross */
447 if (ytl_ofs != 0.0 && s_xcross == FALSE) {
448 if (o_yticks == LEFT) {
449 xa_min += (1 + y_fldwid) * charwidth;
451 } else if (o_yticks == RIGHT) {
453 xt_min = xa_min + ytl_ofs + y_fldwid * charwidth;
461 /* decrease size of graph, if necessary, to accommadate space */
462 /* between axis boundary and boundary of ticks */
467 if (o_xaxis == NOAXIS || o_xtlabel == FALSE)
469 if (o_yaxis == NOAXIS || o_ytlabel == FALSE)
472 if (o_grid == TRUE) {
473 if (x_added_ticks < 0)
475 if (y_added_ticks < 0)
479 if (x_added_ticks < 0) {
480 if (o_yticks == LEFT || s_ycross)
486 if (y_added_ticks < 0) {
487 if (o_xticks == BELOW || s_xcross)
493 xn_tickinc = (xt_max - xt_min) / (x_nint + x_added_ticks);
494 yn_tickinc = (yt_max - yt_min) / (y_nint + y_added_ticks);
496 xt_min += 0.5 * x_added_ticks * xn_tickinc;
497 xt_max -= 0.5 * x_added_ticks * xn_tickinc;
498 yt_min += 0.5 * y_added_ticks * yn_tickinc;
499 yt_max -= 0.5 * y_added_ticks * yn_tickinc;
506 /*---------------------------------------------------------------------------*/
510 rSGP.setLineStyle (SGP::LS_SOLID);
513 /* Convert WC in graph boundary to axis boundary */
514 rSGP.setWindow (xgw_min, ygw_min, xgw_max, ygw_max); /* Graph boundary */
515 rSGP.setViewport (xgn_min, ygn_min, xgn_max, ygn_max);
517 double xminTemp = xa_min, xmaxTemp = xa_max;
518 double yminTemp = ya_min, ymaxTemp = ya_max;
519 rSGP.transformNDCtoMC (&xminTemp, &yminTemp); // calc WC to axis boundaries
520 rSGP.transformNDCtoMC (&yminTemp, &ymaxTemp);
522 rSGP.setWindow (xminTemp, yminTemp, xmaxTemp, ymaxTemp); // Set window to axis boundaries
523 rSGP.setViewport (xa_min, ya_min, xa_max, ya_max);
525 symwidth = charwidth * (xgw_max - xgw_min);
526 symheight = charheight * (ygw_max - ygw_min);
528 for (EZPlotCurveIterator iterCurve = m_vecCurves.begin(); iterCurve != m_vecCurves.end(); iterCurve++) {
529 const EZPlotCurve& curve = **iterCurve;
531 rSGP.setColor (curve.m_iColor);
532 if (curve.m_iLineStyle != SGP::LS_NOLINE) {
533 rSGP.setLineStyle (curve.m_iLineStyle);
534 rSGP.polylineAbs (curve.x, curve.y, curve.m_iPointCount);
536 if (curve.m_iSymbol > 0) {
537 rSGP.setLineStyle (SGP::LS_SOLID);
538 rSGP.moveAbs (curve.x[0], curve.y[0]);
539 symbol (curve.m_iSymbol, symwidth, symheight);
540 for (i = 1; i < curve.m_iPointCount; i++)
541 if (i % curve.m_iSymbolFreq == 0 || i == curve.m_iPointCount - 1) {
542 rSGP.moveAbs (curve.x[i], curve.y[i]);
543 symbol (curve.m_iSymbol, symwidth, symheight);
551 * drawAxes INTERNAL routine to draw axis & label them
559 EZPlot::drawAxes(void)
561 double xticklen = 0, yticklen = 0; /* length of ticks in NDC */
562 double minorinc; /* increment between minor axes */
563 double xaxispos, yaxispos; /* crossing of axes */
565 bool axis_near; /* TRUE if axis too close to print t-label */
570 rSGP.setTextSize (charheight);
571 rSGP.setTextColor (1, -1);
573 if (o_xticks == ABOVE)
574 xticklen = charheight;
575 else if (o_xticks == BELOW)
576 xticklen = -charheight;
578 if (o_yticks == RIGHT)
579 yticklen = charwidth;
580 else if (o_yticks == LEFT)
581 yticklen = -charwidth;
583 rSGP.setWindow (xp_min, yp_min, xp_max, yp_max);
584 rSGP.setViewport (xp_min, yp_min, xp_max, yp_max);
586 if (c_title.length() > 0) {
587 rSGP.moveAbs (xa_min + (xa_max-xa_min)/2 - c_title.length()*charwidth, title_row);
588 rSGP.setTextSize (charheight * 2.0);
589 rSGP.setTextColor (clr_title, -1);
590 rSGP.drawText (c_title);
591 rSGP.setTextSize (charheight);
594 if (o_grid == TRUE || o_box == TRUE) {
595 rSGP.setColor (clr_axis);
596 rSGP.moveAbs (xa_min, ya_min);
597 rSGP.lineAbs (xa_max, ya_min);
598 rSGP.lineAbs (xa_max, ya_max);
599 rSGP.lineAbs (xa_min, ya_max);
600 rSGP.lineAbs (xa_min, ya_min);
603 /* calculate position of axes */
606 if (s_ycross == TRUE) { /* convert users' world-coord */
607 xaxispos = v_ycross; /* axis to its position in NDC */
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 (x, xaxispos, &x, &xaxispos);
616 if (s_xcross == TRUE) { /* convert users' world-coord */
617 yaxispos = v_xcross; /* axis to its NDC position */
618 rSGP.setWindow (xgw_min, ygw_min, xgw_max, ygw_max);
619 rSGP.setViewport (xgn_min, ygn_min, xgn_max, ygn_max);
621 rSGP.transformMCtoNDC (yaxispos, y, &yaxispos, &y);
629 if (o_xaxis == LINEAR) {
630 rSGP.setWindow (xp_min, yp_min, xp_max, yp_max);
631 rSGP.setViewport (xp_min, yp_min, xp_max, yp_max);
635 rSGP.setColor (clr_axis);
636 if (o_tag && !o_grid && !o_box && s_xcross) {
637 rSGP.moveAbs (xa_min, xaxispos - charheight);
638 rSGP.lineAbs (xa_min, xaxispos + charheight);
640 rSGP.moveAbs (xa_min, xaxispos);
641 rSGP.lineAbs (xa_max, xaxispos);
642 if (o_tag && !o_grid && !o_box) {
643 rSGP.moveAbs (xa_max, xaxispos - charheight);
644 rSGP.lineAbs (xa_max, xaxispos + charheight);
647 if (o_grid == TRUE) {
648 rSGP.setColor (clr_grid);
649 for (i = 0; i <= x_nint; i++) {
650 rSGP.moveAbs (xt_min + xn_tickinc * i, ya_max);
651 rSGP.lineAbs (xt_min + xn_tickinc * i, ya_min);
654 rSGP.moveAbs (xa_min + (xa_max-xa_min)/2 - c_xlabel.length()*charwidth, xlbl_row);
655 rSGP.setTextSize (charheight * 2.0);
656 rSGP.setTextColor (clr_label, -1);
657 rSGP.drawText (c_xlabel);
658 rSGP.setTextSize (charheight);
659 minorinc = xn_tickinc / (o_xminortick + 1);
661 for (i = 0; i <= x_nint; i++) {
662 x = xt_min + xn_tickinc * i;
663 rSGP.setColor (clr_axis);
664 rSGP.moveAbs (x, xaxispos);
665 rSGP.lineAbs (x, xaxispos + xticklen);
667 for (j = 1; j <= o_xminortick; j++) {
668 x2 = x + minorinc * j;
669 rSGP.moveAbs (x2, xaxispos);
670 rSGP.lineAbs (x2, xaxispos + TICKRATIO * xticklen);
673 if (xaxispos + xtl_ofs > ya_min && o_yaxis != NOAXIS) {
676 xw = xgw_min + i * xw_tickinc;
677 rSGP.setWindow (xgw_min, ygw_min, xgw_max, ygw_max);
678 rSGP.setViewport (xgn_min, ygn_min, xgn_max, ygn_max);
679 rSGP.transformMCtoNDC (xw, y, &x, &y);
680 rSGP.setWindow (xp_min, yp_min, xp_max, yp_max);
681 rSGP.setViewport (xp_min, yp_min, xp_max, yp_max);
683 if (o_yticks == RIGHT && d >= 0 && d < 0.9 * xn_tickinc)
685 if (o_yticks == LEFT && d <= 0 && d > -0.9 * xn_tickinc)
689 if (o_xtlabel == TRUE && axis_near == FALSE) {
690 snprintf (str, sizeof(str), x_numfmt, xgw_min + xw_tickinc * i);
691 numstr = str_skip_head (str, " ");
692 rSGP.moveAbs (x-strlen(numstr)*charwidth/2,xaxispos + xtl_ofs);
693 rSGP.setTextColor (clr_number, -1);
694 rSGP.drawText (numstr);
704 if (o_yaxis == LINEAR) {
705 rSGP.setWindow (xp_min, yp_min, xp_max, yp_max);
706 rSGP.setViewport (xp_min, yp_min, xp_max, yp_max);
708 rSGP.setColor (clr_axis);
709 if (o_tag && !o_grid && !o_box && s_ycross) {
710 rSGP.moveAbs (yaxispos - charwidth, ya_min);
711 rSGP.lineAbs (yaxispos + charwidth, ya_min);
713 rSGP.moveAbs (yaxispos, ya_min);
714 rSGP.lineAbs (yaxispos, ya_max);
715 if (o_tag && !o_grid && !o_box) {
716 rSGP.moveAbs (yaxispos - charwidth, ya_max);
717 rSGP.lineAbs (yaxispos + charwidth, ya_max);
720 if (o_grid == TRUE) {
721 rSGP.setColor (clr_grid);
722 for (i = 0; i <= y_nint; i++) {
723 y = yt_min + yn_tickinc * i;
724 rSGP.moveAbs (xa_max, y);
725 rSGP.lineAbs (xa_min, y);
728 rSGP.moveAbs (ylbl_col,ya_min + (ya_max-ya_min)/2 - c_ylabel.length()*charheight);
729 rSGP.setTextAngle (HALFPI);
730 rSGP.setTextSize (2 * charheight);
731 rSGP.setTextColor (clr_label, -1);
732 rSGP.drawText (c_ylabel);
733 rSGP.setTextAngle (0.0);
734 rSGP.setTextSize (charheight);
735 minorinc = yn_tickinc / (o_yminortick + 1);
737 for (i = 0; i <= y_nint; i++) {
738 y = yt_min + yn_tickinc * i;
739 rSGP.setColor (clr_axis);
740 rSGP.moveAbs (yaxispos, y);
741 rSGP.lineAbs (yaxispos + yticklen, y);
743 for (j = 1; j <= o_yminortick; j++) {
744 y2 = y + minorinc * j;
745 rSGP.moveAbs (yaxispos, y2);
746 rSGP.lineAbs (yaxispos + TICKRATIO * yticklen, y2);
749 if (yaxispos + ytl_ofs > xa_min && o_xaxis != NOAXIS) {
752 yw = ygw_min + i * yw_tickinc;
753 rSGP.setWindow (xgw_min, ygw_min, xgw_max, ygw_max);
754 rSGP.setViewport (xgn_min, ygn_min, xgn_max, ygn_max);
755 rSGP.transformMCtoNDC (x, yw, &x, &y);
756 rSGP.setWindow (xp_min, yp_min, xp_max, yp_max);
757 rSGP.setViewport (xp_min, yp_min, xp_max, yp_max);
759 if (o_xticks == ABOVE && d >= 0 && d < 0.9 * yn_tickinc)
761 if (o_xticks == BELOW && d <= 0 && d > -0.9 * yn_tickinc)
764 if (o_ytlabel == TRUE && axis_near == FALSE) {
765 snprintf (str, sizeof(str), y_numfmt, ygw_min + yw_tickinc * i);
766 rSGP.moveAbs (yaxispos + ytl_ofs, y - 0.5 * charheight);
767 rSGP.setTextColor (clr_number, -1);
776 EZPlot::symbol (int sym, double symwidth, double symheight)
781 if (sym == SB_CROSS) {
782 rSGP.moveRel (-0.5 * symwidth, -0.5 * symheight);
783 rSGP.lineRel (symwidth, symheight);
784 rSGP.moveRel (-symwidth, 0.0);
785 rSGP.lineRel (symwidth, -symheight);
786 rSGP.moveRel (-0.5 * symwidth, 0.5 * symheight);
787 } else if (sym == SB_PLUS) {
788 rSGP.moveRel (-0.5 * symwidth, 0.0);
789 rSGP.lineRel (symwidth, 0.0);
790 rSGP.moveRel (-0.5 * symwidth, -0.5 * symheight);
791 rSGP.lineRel (0.0, symheight);
792 rSGP.moveRel (0.0, -0.5 * symheight);
793 } else if (sym == SB_BOX) {
794 rSGP.moveRel (-0.5 * symwidth, -0.5 * symheight);
795 rSGP.lineRel (symwidth, 0.0);
796 rSGP.lineRel (0.0, symheight);
797 rSGP.lineRel (-symwidth, 0.0);
798 rSGP.lineRel (0.0, -symheight);
799 rSGP.moveRel (0.5 * symwidth, 0.5 * symheight);
800 } else if (sym == SB_CIRCLE) {
801 rSGP.drawCircle (symwidth);
802 } else if (sym == SB_ERRORBAR) {
803 rSGP.moveRel (-0.5 * symwidth, 0.5 * symheight);
804 rSGP.lineRel (symwidth, 0.0);
805 rSGP.moveRel (-0.5 * symwidth, 0.0);
806 rSGP.lineRel (0.0, -symheight);
807 rSGP.moveRel (-0.5 * symwidth, 0.0);
808 rSGP.lineRel (symwidth, 0.0);
809 rSGP.moveRel (-0.5 * symwidth, 0.5 * symheight);
816 * axis_scale calculates graph axis scaling
819 * retval = axis_scale (min, max, nint, minp, maxp, nintp,
820 * rec_total, rec_frac)
823 * double min Smallest value to plot
824 * double max Largest value to plot
825 * int nint Number of intervals desired
828 * int retval FALSE if illegal parameters, else TRUE
829 * double *minp Minimum graph value
830 * double *maxp Maximum graph value
831 * int *nintp Number of intervals for graph
832 * int *rec_total Recommended field width for printing out the number
833 * int *rec_frac Recommended number of digits for print fraction
837 EZPlot::axis_scale (double min, double max, int nint, double *minp, double *maxp, int *nintp)
839 if (min >= max || nint < 1) {
840 sys_error (ERR_WARNING, "Invalid params: min=%lf, min=%lf, num intervals=%d [axis_scale]", min, max, nint);
845 double a = fabs(min);
846 if (fabs(min) < fabs(max))
848 double scale = pow (10.0, floor(log10(a)));
850 double mina = min / scale;
851 double maxa = max / scale;
852 double d = (maxa - mina) / nint;
854 double e = floor (log10(d));
855 double f = d / pow (10.0, e);
859 else if (f < sqrt (10.0))
861 else if (f < sqrt (50.0))
863 double wdt = v * pow (10.0, e);
864 double g = floor (mina / wdt);
865 if (fabs(g + 1 - mina / wdt) < j)
868 double h = floor (maxa / wdt) + 1.0;
869 if (fabs(maxa / wdt + 1 - h) < j)
872 *nintp = static_cast<int>(h - g);
873 if (fabs(*maxp) >= 10.0 || fabs(*minp) >= 10.0) {
874 scale = scale * 10.0;
886 * make_numfmt Make a numeric format string
889 * make_numfmt (fmtstr, fldwid, nfrac, min, max, nint)
890 * char *fmtstr Returned format string for printf()
891 * int *fldwid Returned field width
892 * int *nfrac If < 0, then calculate best number of
893 * fraction places & return that value
894 * If >= 0, then use that number of places
895 * double min Minimum value
896 * double max Maximum value
897 * int nint Number of intervals between min & max
900 * This routine is written as an INTERNAL routine for EZPLOT
914 EZPlot::make_numfmt (char *fmtstr, int *fldwid, int *nfrac, double minval, double maxval, int nint)
916 int wid, frac, expon;
918 double delta = (maxval - minval) / nint;
919 double absmin = fabs(minval);
920 double absmax = fabs(maxval);
921 double logt = log10( max(absmin, absmax) );
923 if (fabs(logt) >= 6) { /* use exponential format */
925 expon = 5; /* E+102 */
927 expon = 4; /* E+00 */
929 if (*nfrac < 0) { /* calculate frac */
930 delta /= pow (10., floor(logt)); /* scale delta */
931 frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
933 frac = 1; /* to be safe, add decimal pt */
934 } else /* use users' frac */
937 wid = 2 + frac + expon;
938 if (minval < 0. || maxval < 0.)
940 sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "g");
941 } else { /* use fixed format */
942 wid = static_cast<int>(trunc(logt)) + 1;
945 if (minval < 0. || maxval < 0.)
948 if (*nfrac < 0) { /* calculate frac */
949 if (delta >= 0.999999)
950 frac = 1; /* add a decimal pt to be safe */
952 frac = static_cast<int>(fabs(trunc(log10(delta)))) + 1;
953 } else /* use users' frac */
957 sprintf (fmtstr, "%s%d%s%d%s", "%", wid, ".", frac, "f");