X-Git-Url: http://git.kpe.io/?p=ctsim.git;a=blobdiff_plain;f=libctgraphics%2Fezplot.cpp;h=b59d92dde5a8f5723aae5f338253761a7400c017;hp=e4537f01cd47d3e3861ef725e777811c68a74720;hb=9f29c8b32c972db1178d6f8551d5cd57ceb67083;hpb=5c6b29ab4885308cc3381af6e0a68f4804956d2e diff --git a/libctgraphics/ezplot.cpp b/libctgraphics/ezplot.cpp index e4537f0..b59d92d 100644 --- a/libctgraphics/ezplot.cpp +++ b/libctgraphics/ezplot.cpp @@ -4,9 +4,9 @@ ** EZPLOT ** ** This is part of the CTSim program -** Copyright (C) 1983-2000 Kevin Rosenberg +** Copyright (c) 1983-2001 Kevin Rosenberg ** -** $Id: ezplot.cpp,v 1.26 2000/12/29 15:45:06 kevin Exp $ +** $Id: ezplot.cpp,v 1.30 2001/01/28 19:10:18 kevin Exp $ ** ** This program is free software; you can redistribute it and/or modify ** it under the terms of the GNU General Public License (version 2) as @@ -136,29 +136,46 @@ EZPlot::clearCurves () EZPlot::EZPlot () { - initKeywords(); - m_pol.addSkipWord ("please"); - m_pol.addSkipWord ("use"); - m_pol.addSkipWord ("are"); - m_pol.addSkipWord ("and"); - m_pol.addSkipWord ("is"); - m_pol.addSkipWord ("the"); - m_pol.addSkipWord ("equals"); - m_pol.addSkipChar ('='); - - m_pol.usefile (POL::P_USE_STR,""); - m_pol.set_inputline ("!eoc ,"); - m_pol.reader (); - m_pol.closefile (); - + initKeywords(); + + m_pol.addSkipWord ("please"); + + m_pol.addSkipWord ("use"); + + m_pol.addSkipWord ("are"); + + m_pol.addSkipWord ("and"); + + m_pol.addSkipWord ("is"); + + m_pol.addSkipWord ("the"); + + m_pol.addSkipWord ("equals"); + + m_pol.addSkipChar ('='); + + + + m_pol.usefile (POL::P_USE_STR,""); + + m_pol.set_inputline ("!eoc ,"); + + m_pol.reader (); + + m_pol.closefile (); + + + initPlotSettings(); } void EZPlot::initPlotSettings () { - m_iCurrentCurve = -1; - m_pSGP = NULL; + m_iCurrentCurve = -1; + + m_pSGP = NULL; + c_xlabel = ""; c_ylabel = ""; @@ -203,7 +220,7 @@ EZPlot::initPlotSettings () s_ylegend = FALSE; s_textsize = FALSE; - clr_axis = C_BLACK; // set fixed colors + clr_axis = C_LTGRAY; // set fixed colors clr_title = C_RED; clr_label = C_BLUE; clr_legend = C_CYAN; @@ -211,118 +228,230 @@ EZPlot::initPlotSettings () clr_grid = C_LTGRAY; } -void -EZPlot::setColor (unsigned int iCurve, int iColor) -{ - if (m_veciColor.size() <= iCurve) { - m_veciColor.resize ((m_iCurrentCurve + 1) * 2); - m_vecbColorSet.resize ((m_iCurrentCurve + 1) * 2); - } - m_veciColor [iCurve] = iColor; - m_vecbColorSet [iCurve] = true; -} - -void -EZPlot::setSymbol (unsigned int iCurve, int iSymbol) -{ - if (m_veciSymbol.size() <= iCurve) { - m_veciSymbol.resize ((m_iCurrentCurve + 1) * 2); - m_vecbSymbolSet.resize ((m_iCurrentCurve + 1) * 2); - } - m_veciSymbol [iCurve] = iSymbol; - m_vecbSymbolSet [iCurve] = true; -} - -void -EZPlot::setSymbolFreq (unsigned int iCurve, int iSymbolFreq) -{ - if (m_veciSymbolFreq.size() <= iCurve) { - m_veciSymbolFreq.resize ((m_iCurrentCurve + 1) * 2); - m_vecbSymbolFreqSet.resize ((m_iCurrentCurve + 1) * 2); - } - m_veciSymbolFreq [iCurve] = iSymbolFreq; - m_vecbSymbolFreqSet [iCurve] = true; -} - -void -EZPlot::setLinestyle (unsigned int iCurve, int iLinestyle) -{ - if (m_veciLinestyle.size() <= iCurve) { - m_veciLinestyle.resize ((m_iCurrentCurve + 1) * 2); - m_vecbLinestyleSet.resize ((m_iCurrentCurve + 1) * 2); - } - m_veciLinestyle [iCurve] = iLinestyle; - m_vecbLinestyleSet [iCurve] = true; -} - -void -EZPlot::setLegend (unsigned int iCurve, const std::string& strLegend) -{ - if (m_vecsLegend.size() <= iCurve) { - m_vecsLegend.resize ((m_iCurrentCurve + 1) * 2); - m_vecbLegendSet.resize ((m_iCurrentCurve + 1) * 2); - } - m_vecsLegend [iCurve] = strLegend; - m_vecbLegendSet [iCurve] = true; -} - -void -EZPlot::setLegend (unsigned int iCurve, const char* const pszLegend) -{ - if (m_vecsLegend.size() <= iCurve) { - m_vecsLegend.resize ((m_iCurrentCurve + 1) * 2); - m_vecbLegendSet.resize ((m_iCurrentCurve + 1) * 2); - } - m_vecsLegend [iCurve] = pszLegend; - m_vecbLegendSet [iCurve] = true; -} - -int -EZPlot::getColor (unsigned int iCurve) const -{ - if (m_veciColor.size() > iCurve && m_vecbColorSet[iCurve]) - return m_veciColor[iCurve]; - else - return o_color; -} - -int -EZPlot::getSymbol (unsigned int iCurve) const -{ - if (m_veciSymbol.size() > iCurve && m_vecbSymbolSet[iCurve]) - return m_veciSymbol[iCurve]; - else - return o_symbol; -} - -int -EZPlot::getSymbolFreq (unsigned int iCurve) const -{ - if (m_veciSymbolFreq.size() > iCurve && m_vecbSymbolFreqSet[iCurve]) - return m_veciSymbolFreq[iCurve]; - else - return o_symfreq; -} - -int -EZPlot::getLinestyle (unsigned int iCurve) const -{ - if (m_veciLinestyle.size() > iCurve && m_vecbLinestyleSet[iCurve]) - return m_veciLinestyle[iCurve]; - else - return o_linestyle; -} - -const std::string* -EZPlot::getLegend (unsigned int iCurve) const -{ - if (m_vecsLegend.size() > iCurve && m_vecbLegendSet[iCurve]) - return &m_vecsLegend[iCurve]; - else - return NULL; -} - - +void + +EZPlot::setColor (unsigned int iCurve, int iColor) + +{ + + if (m_veciColor.size() <= iCurve) { + + m_veciColor.resize ((m_iCurrentCurve + 1) * 2); + + m_vecbColorSet.resize ((m_iCurrentCurve + 1) * 2); + + } + + m_veciColor [iCurve] = iColor; + + m_vecbColorSet [iCurve] = true; + +} + + + +void + +EZPlot::setSymbol (unsigned int iCurve, int iSymbol) + +{ + + if (m_veciSymbol.size() <= iCurve) { + + m_veciSymbol.resize ((m_iCurrentCurve + 1) * 2); + + m_vecbSymbolSet.resize ((m_iCurrentCurve + 1) * 2); + + } + + m_veciSymbol [iCurve] = iSymbol; + + m_vecbSymbolSet [iCurve] = true; + +} + + + +void + +EZPlot::setSymbolFreq (unsigned int iCurve, int iSymbolFreq) + +{ + + if (m_veciSymbolFreq.size() <= iCurve) { + + m_veciSymbolFreq.resize ((m_iCurrentCurve + 1) * 2); + + m_vecbSymbolFreqSet.resize ((m_iCurrentCurve + 1) * 2); + + } + + m_veciSymbolFreq [iCurve] = iSymbolFreq; + + m_vecbSymbolFreqSet [iCurve] = true; + +} + + + +void + +EZPlot::setLinestyle (unsigned int iCurve, int iLinestyle) + +{ + + if (m_veciLinestyle.size() <= iCurve) { + + m_veciLinestyle.resize ((m_iCurrentCurve + 1) * 2); + + m_vecbLinestyleSet.resize ((m_iCurrentCurve + 1) * 2); + + } + + m_veciLinestyle [iCurve] = iLinestyle; + + m_vecbLinestyleSet [iCurve] = true; + +} + + + +void + +EZPlot::setLegend (unsigned int iCurve, const std::string& strLegend) + +{ + + if (m_vecsLegend.size() <= iCurve) { + + m_vecsLegend.resize ((m_iCurrentCurve + 1) * 2); + + m_vecbLegendSet.resize ((m_iCurrentCurve + 1) * 2); + + } + + m_vecsLegend [iCurve] = strLegend; + + m_vecbLegendSet [iCurve] = true; + +} + + + +void + +EZPlot::setLegend (unsigned int iCurve, const char* const pszLegend) + +{ + + if (m_vecsLegend.size() <= iCurve) { + + m_vecsLegend.resize ((m_iCurrentCurve + 1) * 2); + + m_vecbLegendSet.resize ((m_iCurrentCurve + 1) * 2); + + } + + m_vecsLegend [iCurve] = pszLegend; + + m_vecbLegendSet [iCurve] = true; + +} + + + +int + +EZPlot::getColor (unsigned int iCurve) const + +{ + + if (m_veciColor.size() > iCurve && m_vecbColorSet[iCurve]) + + return m_veciColor[iCurve]; + + else + + return o_color; + +} + + + +int + +EZPlot::getSymbol (unsigned int iCurve) const + +{ + + if (m_veciSymbol.size() > iCurve && m_vecbSymbolSet[iCurve]) + + return m_veciSymbol[iCurve]; + + else + + return o_symbol; + +} + + + +int + +EZPlot::getSymbolFreq (unsigned int iCurve) const + +{ + + if (m_veciSymbolFreq.size() > iCurve && m_vecbSymbolFreqSet[iCurve]) + + return m_veciSymbolFreq[iCurve]; + + else + + return o_symfreq; + +} + + + +int + +EZPlot::getLinestyle (unsigned int iCurve) const + +{ + + if (m_veciLinestyle.size() > iCurve && m_vecbLinestyleSet[iCurve]) + + return m_veciLinestyle[iCurve]; + + else + + return o_linestyle; + +} + + + +const std::string* + +EZPlot::getLegend (unsigned int iCurve) const + +{ + + if (m_vecsLegend.size() > iCurve && m_vecbLegendSet[iCurve]) + + return &m_vecsLegend[iCurve]; + + else + + return NULL; + +} + + + + + /* NAME * plot Plots all curves collected by addCurves () * @@ -338,24 +467,30 @@ EZPlot::getLegend (unsigned int iCurve) const void EZPlot::plot (SGP* pSGP) -{ +{ + if (m_vecCurves.size() <= 0) return; - - m_pSGP = pSGP; + + + m_pSGP = pSGP; + m_pSGP->setWindow (0., 0., 1., 1.); if (s_textsize == TRUE) - m_pSGP->setTextPointSize (v_textsize); + m_pSGP->setTextPointSize (v_textsize); + charheight = m_pSGP->getCharHeight(); charwidth = m_pSGP->getCharWidth(); - const EZPlotCurve& firstCurve = *m_vecCurves[0]; + const EZPlotCurve& firstCurve = *m_vecCurves[0]; + double xmin = firstCurve.x[0]; // extent of curves in world coord double xmax = xmin; double ymin = firstCurve.y[0]; - double ymax = ymin; + double ymax = ymin; + unsigned int iCurve; for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) { const EZPlotCurve* const pCurve = m_vecCurves [iCurve]; @@ -453,23 +588,32 @@ EZPlot::plot (SGP* pSGP) ya_max = yp_max; // adjust frame for title - title_row = ya_max;; + title_row = ya_max;; + if (c_title.length() > 0) - ya_max -= 2 * charheight; - else - ya_max -= 0.7 * charheight; // allow room for yaxis ticklabel + ya_max -= 2 * charheight; + + else + + ya_max -= 0.7 * charheight; // allow room for yaxis ticklabel + // calculate legend box boundaries int max_leg = 0; // longest legend in characters - int num_leg = 0; // number of legend titles + int num_leg = 0; // number of legend titles + for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) { - const std::string* pstrLegend = getLegend (iCurve); - if (pstrLegend && pstrLegend->length() > 0) { + const std::string* pstrLegend = getLegend (iCurve); + + if (pstrLegend && pstrLegend->length() > 0) { + int nLegend = pstrLegend->length(); if (nLegend > 0) { ++num_leg; - if (nLegend > max_leg) - nLegend = max_leg; + if (nLegend > max_leg) + + nLegend = max_leg; + } } } @@ -497,7 +641,8 @@ EZPlot::plot (SGP* pSGP) m_pSGP->setColor (clr_legend); m_pSGP->drawRect (xl_min, yl_min, xl_max, yl_max); - int iLegend = 0; // current legend position + int iLegend = 0; // current legend position + for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) { const std::string* pstrLegend = getLegend (iCurve); if (! pstrLegend || pstrLegend->length() == 0) @@ -507,15 +652,18 @@ EZPlot::plot (SGP* pSGP) double xmax = xl_max - 1.0 * charwidth; double y = yl_max - (2.0 + iLegend * 3) * charheight; - m_pSGP->moveAbs (xmin, y + 0.5 * charheight); + m_pSGP->moveAbs (xmin, y + 0.5 * charheight); + m_pSGP->drawText (pstrLegend->c_str()); - m_pSGP->setColor (getColor (iCurve)); + m_pSGP->setColor (getColor (iCurve)); + int iLS = getLinestyle (iCurve); if (iLS != SGP::LS_NOLINE) { m_pSGP->setLineStyle (iLS); m_pSGP->moveAbs (xmin, y); m_pSGP->lineAbs (xmax, y); - } + } + int iSymbol = getSymbol (iCurve); if (iSymbol > 0) { double xinc = (xmax - xmin) / (5 - 1); @@ -541,14 +689,22 @@ EZPlot::plot (SGP* pSGP) xlbl_row = xp_min; // put x-label on bottom of plot frame // Y-Label - if (c_ylabel.length() > 0) { - m_pSGP->setTextAngle (HALFPI); - m_pSGP->setTextSize (1.5 * charheight); - double xExtent, yExtent; - m_pSGP->getTextExtent (c_ylabel.c_str(), &xExtent, &yExtent); - m_pSGP->setTextSize (charheight); - xa_min += xExtent; - m_pSGP->setTextAngle (0.0); + if (c_ylabel.length() > 0) { + + m_pSGP->setTextAngle (HALFPI); + + m_pSGP->setTextSize (1.5 * charheight); + + double xExtent, yExtent; + + m_pSGP->getTextExtent (c_ylabel.c_str(), &xExtent, &yExtent); + + m_pSGP->setTextSize (charheight); + + xa_min += xExtent; + + m_pSGP->setTextAngle (0.0); + } ylbl_col = xp_min; @@ -566,13 +722,21 @@ EZPlot::plot (SGP* pSGP) if (o_yaxis == NOAXIS || o_ytlabel == FALSE) ytl_ofs = 0.0; - else if (o_yticks == LEFT) - ytl_ofs = -(2 + y_fldwid) * charwidth; - else if (o_yticks == RIGHT) + else if (o_yticks == LEFT) { + double xExtentMin, xExtentMax, yExtent; + char s[1024]; + snprintf (s, sizeof(s), y_numfmt, ymin); + m_pSGP->getTextExtent (s, &xExtentMin, &yExtent); + snprintf (s, sizeof(s), y_numfmt, ymax); + m_pSGP->getTextExtent (s, &xExtentMax, &yExtent); + if (xExtentMin > xExtentMax) + xExtentMax = xExtentMin; + ytl_ofs = -xExtentMax; + } else if (o_yticks == RIGHT) ytl_ofs = 1.5 * charwidth; - xa_max -= 0.7 * x_fldwid * charwidth; // make room for last x tick label - + xa_max -= 0.7 * x_fldwid * charwidth; // make room for last x tick label + xt_min = xa_min; yt_min = ya_min; xt_max = xa_max; @@ -592,11 +756,11 @@ EZPlot::plot (SGP* pSGP) if (ytl_ofs != 0.0 && s_xcross == FALSE) { if (o_yticks == LEFT) { - xa_min += (2 + y_fldwid) * charwidth; + xa_min += 2*charwidth - ytl_ofs; // (2 + y_fldwid) * charwidth; xt_min = xa_min; } else if (o_yticks == RIGHT) { xa_min += 0.0; - xt_min = xa_min + ytl_ofs + y_fldwid * charwidth; + xt_min = xa_min + ytl_ofs; // + y_fldwid * charwidth; } } else xt_min = xa_min; @@ -657,35 +821,55 @@ EZPlot::plot (SGP* pSGP) // size of symbol in NDC double symwidth = charwidth; double symheight = charheight; - - double clipRect[4]; - clipRect[0] = xgn_min; clipRect[1] = ygn_min; clipRect[2] = xgn_max; clipRect[3] = ygn_max; - + + + double clipRect[4]; + + clipRect[0] = xgn_min; clipRect[1] = ygn_min; clipRect[2] = xgn_max; clipRect[3] = ygn_max; + + + for (iCurve = 0; iCurve < m_vecCurves.size(); iCurve++) { const EZPlotCurve* const pCurve = m_vecCurves [iCurve]; - - m_pSGP->setColor (getColor (iCurve)); - int iSym = getSymbol (iCurve); - int iLS = getLinestyle (iCurve); - + + + m_pSGP->setColor (getColor (iCurve)); + + int iSym = getSymbol (iCurve); + + int iLS = getLinestyle (iCurve); + + + if (iLS != SGP::LS_NOLINE) { m_pSGP->setLineStyle (iLS); double x1 = convertWorldToNDC_X (pCurve->x[0]); - double y1 = convertWorldToNDC_Y (pCurve->y[0]); + double y1 = convertWorldToNDC_Y (pCurve->y[0]); + for (int i = 1; i < pCurve->m_iPointCount; i++) { double x2 = convertWorldToNDC_X (pCurve->x[i]); - double y2 = convertWorldToNDC_Y (pCurve->y[i]); - double x2Clip = x2; - double y2Clip = y2; - if (clip_rect (x1, y1, x2Clip, y2Clip, clipRect)) { - m_pSGP->moveAbs (x1, y1); - m_pSGP->lineAbs (x2Clip, y2Clip); - } - x1 = x2; - y1 = y2; + double y2 = convertWorldToNDC_Y (pCurve->y[i]); + + double x2Clip = x2; + + double y2Clip = y2; + + if (clip_rect (x1, y1, x2Clip, y2Clip, clipRect)) { + + m_pSGP->moveAbs (x1, y1); + + m_pSGP->lineAbs (x2Clip, y2Clip); + + } + + x1 = x2; + + y1 = y2; + } } - if (iSym > 0) { + if (iSym > 0) { + int iSymFreq = getSymbolFreq (iCurve); m_pSGP->setLineStyle (SGP::LS_SOLID); double x = convertWorldToNDC_X (pCurve->x[0]); @@ -695,15 +879,16 @@ EZPlot::plot (SGP* pSGP) for (int i = 1; i < pCurve->m_iPointCount; i++) if (i % iSymFreq == 0 || i == pCurve->m_iPointCount - 1) { x = convertWorldToNDC_X (pCurve->x[i]); - y = convertWorldToNDC_Y (pCurve->y[i]); + y = convertWorldToNDC_Y (pCurve->y[i]); + if (y >= ygn_min && y <= ygn_max) { m_pSGP->moveAbs (x, y); - symbol (iSym, symwidth, symheight); + symbol (iSym, symwidth, symheight); + } } } - } - + } } @@ -802,10 +987,13 @@ EZPlot::drawAxes() } } m_pSGP->setTextSize (charheight * 1.5); - m_pSGP->setTextColor (clr_label, -1); - double wText, hText; + m_pSGP->setTextColor (clr_label, -1); + + double wText, hText; + m_pSGP->getTextExtent (c_xlabel.c_str(), &wText, &hText); - m_pSGP->moveAbs (xa_min + (xa_max-xa_min)/2 - wText / 2, xlbl_row + charheight * 1.5); + m_pSGP->moveAbs (xa_min + (xa_max-xa_min)/2 - wText / 2, xlbl_row + charheight * 1.5); + m_pSGP->drawText (c_xlabel); m_pSGP->setTextSize (charheight); minorinc = xn_tickinc / (o_xminortick + 1); @@ -835,7 +1023,9 @@ EZPlot::drawAxes() if (o_xtlabel == TRUE && axis_near == FALSE) { snprintf (str, sizeof(str), x_numfmt, xgw_min + xw_tickinc * i); numstr = str_skip_head (str, " "); - m_pSGP->moveAbs (x-strlen(numstr)*charwidth/2, xaxispos + xtl_ofs); + double xExtent, yExtent; + m_pSGP->getTextExtent (numstr, &xExtent, &yExtent); + m_pSGP->moveAbs (x - xExtent/2, xaxispos + xtl_ofs); m_pSGP->setTextColor (clr_number, -1); m_pSGP->drawText (numstr); } @@ -868,14 +1058,18 @@ EZPlot::drawAxes() m_pSGP->moveAbs (xa_max, y); m_pSGP->lineAbs (xa_min, y); } - } + } + m_pSGP->setTextAngle (HALFPI); m_pSGP->setTextSize (1.5 * charheight); - m_pSGP->setTextColor (clr_label, -1); - double xExtent, yExtent; - m_pSGP->getTextExtent (c_ylabel.c_str(), &xExtent, &yExtent); - m_pSGP->moveAbs (ylbl_col, ya_min + (ya_max-ya_min)/2 - yExtent / 2); - m_pSGP->drawText (c_ylabel); + m_pSGP->setTextColor (clr_label, -1); + + double xExtent, yExtent; + + m_pSGP->getTextExtent (c_ylabel.c_str(), &xExtent, &yExtent); + m_pSGP->moveAbs (ylbl_col, ya_min + (ya_max-ya_min)/2 - yExtent / 2); + m_pSGP->drawText (c_ylabel); + m_pSGP->setTextAngle (0.0); m_pSGP->setTextSize (charheight); minorinc = yn_tickinc / (o_yminortick + 1); @@ -902,9 +1096,14 @@ EZPlot::drawAxes() axis_near = TRUE; } if (o_ytlabel == TRUE && axis_near == FALSE) { - snprintf (str, sizeof(str), y_numfmt, ygw_min + yw_tickinc * i); - m_pSGP->moveAbs (yaxispos + ytl_ofs, y + 0.5 * charheight); - m_pSGP->setTextColor (clr_number, -1); + snprintf (str, sizeof(str), y_numfmt, ygw_min + yw_tickinc * i); + double xExtent, yExtent; + m_pSGP->getTextExtent (str, &xExtent, &yExtent); + if (o_yticks == LEFT) + m_pSGP->moveAbs (yaxispos - 1.5 * charwidth - xExtent, y + 0.5 * yExtent); + else + m_pSGP->moveAbs (yaxispos + 1.5 * charwidth, y + 0.5 * yExtent); + m_pSGP->setTextColor (clr_number, -1); m_pSGP->drawText (str); } } @@ -1004,7 +1203,8 @@ loop: double g = floor (mina / wdt); if (fabs(g + 1 - mina / wdt) < j) g = g + 1; -#undef TEST1 +#undef TEST1 + #ifdef TEST1 g++; #endif @@ -1064,8 +1264,10 @@ EZPlot::make_numfmt (char *fmtstr, int *fldwid, int *nfrac, double minval, doubl double delta = (maxval - minval) / nint; double absmin = fabs(minval); - double absmax = fabs(maxval); - if (absmin > absmax) + double absmax = fabs(maxval); + + if (absmin > absmax) + absmax = absmin; double logt = log10( absmax );