Ejemplo n.º 1
0
bool Graph::SetYScale()
{
	int nc;
	CCurve *pCurve;

	if(m_bAutoY)
	{
		bool bCurve = false;
		if (m_oaCurves.size())
		{
			//init only if we have a curve
			for (nc=0; nc < m_oaCurves.size(); nc++)
			{
				pCurve = (CCurve*)m_oaCurves[nc];
				if ((pCurve->IsVisible() ||pCurve->PointsVisible())  && pCurve->n>0)
					{
						bCurve = true;
						break;
					}
			}
		}
		if(bCurve)
		{
			Cymin =  9999999.0;
			Cymax = -9999999.0;
			for (nc=0; nc < m_oaCurves.size(); nc++)
			{
				pCurve = (CCurve*)m_oaCurves[nc];
				if ((pCurve->IsVisible() ||pCurve->PointsVisible()) && pCurve->n>0)
				{
					Cymin = qMin(Cymin, pCurve->GetyMin());
					Cymax = qMax(Cymax, pCurve->GetyMax());
				}
			}
			if(Cymax<=Cymin)
			{
				Cymax = (Cymin+1.0)*2.0;
			}

			if(m_Type == 1)
			{
				ymin = qMin(ymin, Cymin);
				ymax = qMax(ymax, Cymax);
			}
			else
			{
				ymin = Cymin;
				ymax = Cymax;
			}
			if(Cymin>=0.0) ymin = 0.0;
			if(Cymax<=0.0) ymax = 0.0;
		}
		else
		{
			// until things are made clear
			for (int nc=0; nc < m_oaCurves.size(); nc++)
			{
				pCurve = (CCurve*)m_oaCurves[nc];
				if ((pCurve->IsVisible() ||pCurve->PointsVisible())  && pCurve->n>0)
				{
					ymin = qMin(ymin, pCurve->y[0]);
					ymax = qMax(ymax, pCurve->y[0]);
				}
			}
		}
		yo=0.0;

		if (fabs((ymin-ymax)/ymin)<0.001)
		{
			if(fabs(ymin)<0.00001) ymax = 1.0;
			else
			{
				ymax = 2.0 * ymin;
				if(ymax < ymin)
				{
					double tmp = ymax;
					ymax = ymin;
					ymin = tmp;
				}
			}
		}

		if(m_h<=0.0) return false;

		if (!m_bYInverted)
		{
			m_scaley   = -(ymax-ymin)/m_h;
		}
		else
		{
			m_scaley   =  (ymax-ymin)/m_h;
		}

		//try to set an automatic scale for Y Axis
		SetAutoYUnit();
	}
	else
	{
		//scales are set manually
		if(m_h<=0) return false;

		if (!m_bYInverted)
		{
			m_scaley   = -(ymax-ymin)/m_h;
		}
		else
		{
			m_scaley   =  (ymax-ymin)/m_h;
		}

		if (yunit<1.0)
		{
			exp_y = (int)log10(yunit*1.00001)-1;
			exp_y = qMax(-4, exp_y);
		}
		else  exp_y = (int)log10(yunit*1.00001);

	}

	//graph center position
	int Yg = (m_rCltRect.top() + m_rCltRect.bottom())/2;
	// curves center position
	int Yc = (int)((ymin+ymax)/2.0/m_scaley);
	// center graph in drawing rectangle
	m_ptoffset.ry() = (Yg-Yc);

	return true;
}