//*******************************************************************************
void CBCGPChartInterLineColoringEffect::OnDraw(CBCGPGraphicsManager* pGM)
{
	ASSERT_VALID(this);

	if (m_pSeries1 == NULL || m_arPointsSeries1.GetSize() < 2 || !IsVisible())
	{
		return;
	}

	BOOL bWasTransparency = CBCGPGraphicsManagerGDI::IsTransparencyEnabled();
	CBCGPGraphicsManagerGDI::EnableTransparency();

	BCGPChartFormatSeries::ChartCurveType curveType = m_pSeries1->GetCurveType();

	if (curveType == BCGPChartFormatSeries::CCT_STEP || curveType == BCGPChartFormatSeries::CCT_REVERSED_STEP ||
		m_pSeries2 != NULL && (m_pSeries2->GetCurveType() == BCGPChartFormatSeries::CCT_STEP || 
								m_pSeries2->GetCurveType() == BCGPChartFormatSeries::CCT_REVERSED_STEP))
	{
		return;
	}

	CBCGPChartAxis* pXAxis = m_pSeries1->GetRelatedAxis(CBCGPChartSeries::AI_X);
	CBCGPRect rectBounds = pXAxis->GetBoundingRect();

	CBCGPChartAxis* pYAxis = m_pSeries1->GetRelatedAxis(CBCGPChartSeries::AI_Y);

	if (pYAxis->m_bReverseOrder)
	{
		pYAxis->IsVertical() ? rectBounds.SwapTopBottom() : rectBounds.SwapLeftRight();
	}

	if (pXAxis->m_bReverseOrder)
	{
		pXAxis->IsVertical() ?  rectBounds.SwapTopBottom() : rectBounds.SwapLeftRight();
	}

	BCGPSeriesColorsPtr colors;
	m_pSeries1->GetColors(colors, -1);

	CBCGPBrush& brFill = m_brTopBrush.IsEmpty() ? *colors.m_pBrElementFillColor : m_brTopBrush;
	
	CBCGPGeometry* pDrawGeometry = CreateGeometry(m_arPointsSeries1, rectBounds, curveType, FALSE);
	CBCGPGeometry* pClipGeometry = m_pSeries2 != NULL ? 
		CreateGeometry(m_arPointsSeries2, rectBounds, m_pSeries2->GetCurveType(), TRUE) : 
		CreateClipGeometry(m_dblOrigin);

	ASSERT_VALID(pDrawGeometry);
	ASSERT_VALID(pClipGeometry);

	DrawEffect(pGM, pDrawGeometry, pClipGeometry, rectBounds, brFill);

	delete pClipGeometry;
	delete pDrawGeometry;

	if (m_pSeries2 != NULL && m_arPointsSeries2.GetSize() > 2 && !m_bTopOnly)
	{
		BCGPSeriesColorsPtr colors;
		m_pSeries2->GetColors(colors, -1);

		pDrawGeometry = CreateGeometry(m_arPointsSeries2, rectBounds, m_pSeries2->GetCurveType(), FALSE);
		pClipGeometry = CreateGeometry(m_arPointsSeries1, rectBounds, curveType, TRUE);

		CBCGPBrush& brFill = m_brBottomBrush.IsEmpty() ? *colors.m_pBrElementFillColor : m_brBottomBrush;
		DrawEffect(pGM, pDrawGeometry, pClipGeometry, rectBounds, brFill);

		delete pClipGeometry;
		delete pDrawGeometry;
	}

	CBCGPGraphicsManagerGDI::EnableTransparency(bWasTransparency);
}
//*******************************************************************************
void CBCGPChartObject::OnCalcScreenPoints(CBCGPGraphicsManager* pGM)
{
	ASSERT_VALID(this);

	m_rectScreen.SetRectEmpty();
	m_rectScreenBounds.SetRectEmpty();
	m_ptAnchor.x = m_ptAnchor.y = 0.;

	if (m_pParentChart == NULL)
	{
		return;
	}

	CBCGPSize szScaleRatio = m_pParentChart->GetScaleRatio();

	CBCGPChartAxis* pXAxis = m_pXAxis;
	CBCGPChartAxis* pYAxis = m_pYAxis;

	if (m_coordinateMode == CBCGPChartObject::CM_CHART_VALUES || 
		m_coordinateMode == CBCGPChartObject::CM_CHART_VALUE_DIST_ANGLE ||
		m_coordinateMode == CBCGPChartObject::CM_AXIS_INSIDE_MARK ||
		m_coordinateMode == CBCGPChartObject::CM_AXIS_OUTSIDE_MARK)
	{
		if (pXAxis == NULL)
		{
			m_pXAxis = pXAxis = m_pParentChart->GetChartAxis(BCGP_CHART_X_PRIMARY_AXIS);
		}

		if (pYAxis == NULL)
		{
			m_pYAxis = pYAxis = m_pParentChart->GetChartAxis(BCGP_CHART_Y_PRIMARY_AXIS);
		}
	}

	CBCGPSize szObjectSize = m_szObjectSize.IsNull() ?  OnCalcObjectSize(pGM) : m_szObjectSize;

	m_rectScreenBounds = OnCalcBoundingRect();

	CBCGPRect rectBounds = m_rectScreenBounds;

	CBCGPPoint ptLeftTop(CBCGPChartObject::_EmptyPoint);
	CBCGPPoint ptRightBottom(CBCGPChartObject::_EmptyPoint);

	if (m_coordinateMode == CBCGPChartObject::CM_PERCENTS)
	{
		if (m_rectCoordinates.left != CBCGPChartObject::_EmptyCoordinate)
		{
			ptLeftTop.x = rectBounds.left + rectBounds.Width() * m_rectCoordinates.left / 100.;
		}

		if (m_rectCoordinates.top != CBCGPChartObject::_EmptyCoordinate)
		{
			ptLeftTop.y = rectBounds.top + rectBounds.Height() * m_rectCoordinates.top / 100;
		}

		if (m_rectCoordinates.right != CBCGPChartObject::_EmptyCoordinate)
		{
			ptRightBottom.x = rectBounds.right - rectBounds.Width() * m_rectCoordinates.right / 100.;
		}

		if (m_rectCoordinates.bottom != CBCGPChartObject::_EmptyCoordinate)
		{
			ptRightBottom.y = rectBounds.bottom - rectBounds.Height() * m_rectCoordinates.bottom / 100.;
		}
	}
	else if (m_coordinateMode == CBCGPChartObject::CM_PIXELS)
	{
		if (m_rectCoordinates.left != CBCGPChartObject::_EmptyCoordinate)
		{
			ptLeftTop.x = rectBounds.left + m_rectCoordinates.left * szScaleRatio.cx;
		}

		if (m_rectCoordinates.top != CBCGPChartObject::_EmptyCoordinate)
		{
			ptLeftTop.y = rectBounds.top + m_rectCoordinates.top * szScaleRatio.cy;
		}

		if (m_rectCoordinates.right != CBCGPChartObject::_EmptyCoordinate)
		{
			ptRightBottom.x = rectBounds.right - m_rectCoordinates.right * szScaleRatio.cx;
		}

		if (m_rectCoordinates.bottom != CBCGPChartObject::_EmptyCoordinate)
		{
			ptRightBottom.y = rectBounds.bottom - m_rectCoordinates.bottom * szScaleRatio.cy;
		}
	}
	else if (m_coordinateMode == CBCGPChartObject::CM_PIXELS_FIXED_SIZE)
	{
		if (m_rectCoordinates.left != CBCGPChartObject::_EmptyCoordinate)
		{
			ptLeftTop.x = rectBounds.left + m_rectCoordinates.left * szScaleRatio.cx;
		}
		else
		{
			ptLeftTop.x = rectBounds.left;
		}

		if (m_rectCoordinates.top != CBCGPChartObject::_EmptyCoordinate)
		{
			ptLeftTop.y = rectBounds.top + m_rectCoordinates.top * szScaleRatio.cy;
		}
		else
		{
			ptLeftTop.y = rectBounds.top;
		}

		if (m_rectCoordinates.right != CBCGPChartObject::_EmptyCoordinate)
		{
			ptRightBottom.x = ptLeftTop.x + m_rectCoordinates.right * szScaleRatio.cx;
		}
		else
		{
			ptRightBottom.x = ptLeftTop.x + rectBounds.Width();
		}

		if (m_rectCoordinates.bottom != CBCGPChartObject::_EmptyCoordinate)
		{
			ptRightBottom.y = ptLeftTop.y + m_rectCoordinates.bottom * szScaleRatio.cy;
		}
		else
		{
			ptRightBottom.y = ptLeftTop.y + rectBounds.Height();
		}
	}
	else if (m_coordinateMode == CBCGPChartObject::CM_CHART_VALUES)
	{
		ASSERT_VALID(pXAxis);
		ASSERT_VALID(pYAxis);

		CBCGPRect rectCoordinates = m_rectCoordinates;

		if (pXAxis != NULL && pXAxis->IsVertical())
		{
			rectCoordinates = CBCGPRect(m_rectCoordinates.bottom, m_rectCoordinates.right,
										m_rectCoordinates.top, m_rectCoordinates.left);
			pXAxis = m_pYAxis;
			pYAxis = m_pXAxis;
		}

 		if (pXAxis->m_bReverseOrder && !pXAxis->IsVertical())
 		{
			if ((rectCoordinates.left != CBCGPChartObject::_EmptyCoordinate && 
				 rectCoordinates.right == CBCGPChartObject::_EmptyCoordinate) || 
				 (rectCoordinates.left == CBCGPChartObject::_EmptyCoordinate && 
				 rectCoordinates.right != CBCGPChartObject::_EmptyCoordinate))
			{
				rectCoordinates.SwapLeftRight();
			}
 		}
 
 		if (pYAxis->m_bReverseOrder && pYAxis->IsVertical())
 		{
			if ((rectCoordinates.top != CBCGPChartObject::_EmptyCoordinate && 
				 rectCoordinates.bottom == CBCGPChartObject::_EmptyCoordinate) || 
				 (rectCoordinates.top == CBCGPChartObject::_EmptyCoordinate && 
				 rectCoordinates.bottom != CBCGPChartObject::_EmptyCoordinate))
			{
				rectCoordinates.SwapTopBottom();
			}
 		}

		if (rectCoordinates.left != CBCGPChartObject::_EmptyCoordinate)
		{
			ptLeftTop.x = pXAxis->PointFromValue(rectCoordinates.left, FALSE);
		}
		else if (szObjectSize.cx == 0)
		{
			ptLeftTop.x = pXAxis->PointFromValue(pXAxis->GetMinDisplayedValue(TRUE), TRUE);
		}

		if (rectCoordinates.top != CBCGPChartObject::_EmptyCoordinate)
		{
			ptLeftTop.y = pYAxis->PointFromValue(rectCoordinates.top, FALSE);
		}
		else if (szObjectSize.cy == 0)
		{
			ptLeftTop.y = pYAxis->PointFromValue(pYAxis->GetMaxDisplayedValue(TRUE), TRUE);
		}

		if (rectCoordinates.right != CBCGPChartObject::_EmptyCoordinate)
		{
			ptRightBottom.x = pXAxis->PointFromValue(rectCoordinates.right, FALSE);
		}
		else if (szObjectSize.cx == 0)
		{
			ptRightBottom.x = pXAxis->PointFromValue(pXAxis->GetMaxDisplayedValue(TRUE), TRUE);
		}

		if (rectCoordinates.bottom != CBCGPChartObject::_EmptyCoordinate)
		{
			ptRightBottom.y = pYAxis->PointFromValue(rectCoordinates.bottom, FALSE);
		}
		else if (szObjectSize.cy == 0)
		{
			ptRightBottom.y = pYAxis->PointFromValue(pYAxis->GetMinDisplayedValue(TRUE), TRUE);
		}
	}
	else if (m_coordinateMode == CM_CHART_VALUE_DIST_ANGLE)
	{
		ASSERT_VALID(pXAxis);
		ASSERT_VALID(pYAxis);

		if (!IsRectOffsetsValid())
		{
			return;
		}

		CBCGPRect rectCoordinates = m_rectCoordinates;

		if (pXAxis != NULL && pXAxis->IsVertical())
		{
			rectCoordinates = CBCGPRect(m_rectCoordinates.top, m_rectCoordinates.left,
										m_rectCoordinates.right, m_rectCoordinates.bottom);
			pXAxis = m_pYAxis;
			pYAxis = m_pXAxis;
		}

		m_ptAnchor.x = pXAxis->PointFromValue(rectCoordinates.left, FALSE);
		m_ptAnchor.y = pYAxis->PointFromValue(rectCoordinates.top, FALSE); 

		double dblDistanceX = rectCoordinates.right * szScaleRatio.cx;
		double dblDistanceY = rectCoordinates.right * szScaleRatio.cy;

		CBCGPPoint ptCenter(m_ptAnchor.x + dblDistanceX * sin(bcg_deg2rad(rectCoordinates.bottom)),
							m_ptAnchor.y - dblDistanceY * cos(bcg_deg2rad(rectCoordinates.bottom)));

		ptLeftTop.SetPoint(ptCenter.x - szObjectSize.cx / 2, ptCenter.y - szObjectSize.cy / 2);
		ptRightBottom.SetPoint(ptCenter.x + szObjectSize.cx / 2, ptCenter.y + szObjectSize.cy / 2);
	}
	else if (m_coordinateMode == CM_AXIS_INSIDE_MARK || m_coordinateMode == CM_AXIS_OUTSIDE_MARK)
	{
		ASSERT_VALID(pXAxis);
		ASSERT_VALID(pYAxis);

		if (m_rectCoordinates.top == _EmptyCoordinate && m_rectCoordinates.left == _EmptyCoordinate)
		{
			return;
		}

		double dblHorzOffset = m_rectCoordinates.right == _EmptyCoordinate ? 0 : m_rectCoordinates.right * szScaleRatio.cx;
		double dblVertOffset = m_rectCoordinates.bottom == _EmptyCoordinate ? 0 : m_rectCoordinates.bottom * szScaleRatio.cy;

		CBCGPChartAxis* pUsedAxis = m_rectCoordinates.left != _EmptyCoordinate ? pXAxis : pYAxis;
		double dblUsedValue = m_rectCoordinates.left != _EmptyCoordinate ? m_rectCoordinates.left : m_rectCoordinates.top;

		double dblScrVal = pUsedAxis->PointFromValue(dblUsedValue, FALSE);
		CBCGPRect rectAxis = pUsedAxis->GetAxisRect(FALSE, FALSE, TRUE);

		CBCGPPoint ptCenter;

		if (m_coordinateMode == CM_AXIS_INSIDE_MARK && 
			(pUsedAxis->m_axisDefaultPosition == CBCGPChartAxis::ADP_BOTTOM || pUsedAxis->m_axisDefaultPosition == CBCGPChartAxis::ADP_LEFT) ||
			m_coordinateMode == CM_AXIS_OUTSIDE_MARK && 
			(pUsedAxis->m_axisDefaultPosition == CBCGPChartAxis::ADP_TOP || pUsedAxis->m_axisDefaultPosition == CBCGPChartAxis::ADP_RIGHT))
		{
			if (pUsedAxis->IsVertical())
			{
				ptCenter.x = rectAxis.right + dblVertOffset + szObjectSize.cx / 2;
				ptCenter.y = dblScrVal - dblHorzOffset;
			}
			else
			{
				ptCenter.x = dblScrVal + dblHorzOffset;
				ptCenter.y = rectAxis.top - dblVertOffset - szObjectSize.cy /2;
			}
		}
		else
		{
			if (pUsedAxis->IsVertical())
			{
				ptCenter.x = rectAxis.left - dblVertOffset - szObjectSize.cx / 2;
				ptCenter.y = dblScrVal - dblHorzOffset;
			}
			else
			{
				ptCenter.x = dblScrVal + dblHorzOffset;
				ptCenter.y = rectAxis.bottom + dblVertOffset + szObjectSize.cy /2;
			}
		}

		if (pUsedAxis->IsVertical() && (ptCenter.y < rectAxis.top || ptCenter.y > rectAxis.bottom) || 
			!pUsedAxis->IsVertical() && (ptCenter.x < rectAxis.left || ptCenter.x > rectAxis.right))
		{
			return;
		}

		ptLeftTop.SetPoint(ptCenter.x - szObjectSize.cx / 2, ptCenter.y - szObjectSize.cy / 2);
		ptRightBottom.SetPoint(ptCenter.x + szObjectSize.cx / 2, ptCenter.y + szObjectSize.cy / 2);
	}
	else
	{
		return;
	}

	if (m_format.m_textFormat.IsWordWrap())
	{
		if (ptLeftTop.x == CBCGPChartObject::_EmptyCoordinate && 
			ptRightBottom.x == CBCGPChartObject::_EmptyCoordinate && 
			szObjectSize.cx == 0)
		{
			ASSERT(FALSE);
			TRACE0(" CBCGPChartObject::OnCalcScreenPoints: Left and right offsets must be specified in order to properly use wrapped text.\n");
			return;
		}

		szObjectSize = pGM->GetTextSize(m_strText, m_format.m_textFormat, ptRightBottom.x - ptLeftTop.x);
	}
	

	if (ptLeftTop.x == CBCGPChartObject::_EmptyCoordinate)
	{
		ptLeftTop.x = ptRightBottom.x - szObjectSize.cx;
	}

	if (ptLeftTop.y == CBCGPChartObject::_EmptyCoordinate)
	{
		ptLeftTop.y = ptRightBottom.y - szObjectSize.cy;
	}

	if (ptRightBottom.x == CBCGPChartObject::_EmptyCoordinate)
	{
		ptRightBottom.x = ptLeftTop.x + szObjectSize.cx;
	}

	if (ptRightBottom.y == CBCGPChartObject::_EmptyCoordinate)
	{
		ptRightBottom.y = ptLeftTop.y + szObjectSize.cy;
	}

	m_rectScreen.SetRect(ptLeftTop, ptRightBottom);
}