Click here to Skip to main content
15,884,472 members
Articles / Desktop Programming / MFC

High-speed Charting Control

Rate me:
Please Sign up or sign in to vote.
4.95/5 (327 votes)
13 Jul 2010CPOL35 min read 4.2M   100.5K   787  
A flexible charting control to display 2D data
/*
 *
 *	ChartLineSerie.cpp
 *
 *	Written by C�dric Moonen (cedric_moonen@hotmail.com)
 *
 *
 *
 *	This code may be used for any non-commercial and commercial purposes in a compiled form.
 *	The code may be redistributed as long as it remains unmodified and providing that the 
 *	author name and this disclaimer remain intact. The sources can be modified WITH the author 
 *	consent only.
 *	
 *	This code is provided without any garanties. I cannot be held responsible for the damage or
 *	the loss of time it causes. Use it at your own risks
 *
 *	An e-mail to notify me that you are using this code is appreciated also.
 *
 *	History:
 *		- 25/03/2008: Line series with a width > 1 can now have a style other than solid
 *					  (thanks to Bruno Lavier).
 *
 *
 */

#include "stdafx.h"
#include "ChartLineSerie.h"
#include "ChartCtrl.h"

#include "Math.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CChartLineSerie::CChartLineSerie(CChartCtrl* pParent) : CChartSerie(pParent,stLineSerie)
{
	m_iLineWidth = 1;
	m_iPenStyle = PS_SOLID;
}

CChartLineSerie::~CChartLineSerie()
{

}

void CChartLineSerie::SetPenStyle(int NewStyle)  
{ 
	m_iPenStyle = NewStyle; 
	m_pParent->RefreshCtrl();
}

void CChartLineSerie::SetWidth(int NewValue)  
{ 
	m_iLineWidth = NewValue; 
	m_pParent->RefreshCtrl();
}

void CChartLineSerie::DrawAll(CDC *pDC)
{
	if (!m_bIsVisible)
		return;

	int iFirst=0, iLast=0;
	GetVisiblePoints(iFirst,iLast);

	if (iFirst>0)
		iFirst--;
	if (iLast<(int)GetPointsCount()-1)
		iLast++;
	if (pDC->GetSafeHdc())
	{
		LOGBRUSH lb;
        lb.lbStyle = BS_SOLID;
        lb.lbColor = m_ObjectColor;
		CPen NewPen(PS_GEOMETRIC | m_iPenStyle,m_iLineWidth,&lb);
		lb.lbColor = m_ShadowColor;
		CPen ShadowPen(PS_GEOMETRIC | m_iPenStyle,m_iLineWidth,&lb);
		CPen* pOldPen;

		pDC->SetBkMode(TRANSPARENT);
		//To have lines limited in the drawing rectangle :
		pDC->IntersectClipRect(m_ObjectRect);
		pOldPen = pDC->SelectObject(&NewPen);

		for (m_iLastDrawnPoint=iFirst;m_iLastDrawnPoint<iLast;m_iLastDrawnPoint++)
		{
			//We don't draw a line between the origin and the first point -> we must have
			// a least 2 points before begining drawing
			
			CPoint ScreenPoint;

			if (m_bShadow)
			{
				pDC->SelectObject(&ShadowPen);
				ValueToScreen(m_vPoints[m_iLastDrawnPoint].X,m_vPoints[m_iLastDrawnPoint].Y,ScreenPoint);
				pDC->MoveTo(ScreenPoint.x+m_iShadowDepth,ScreenPoint.y+m_iShadowDepth);
				ValueToScreen(m_vPoints[m_iLastDrawnPoint+1].X,m_vPoints[m_iLastDrawnPoint+1].Y,ScreenPoint);
				pDC->LineTo(ScreenPoint.x+m_iShadowDepth,ScreenPoint.y+m_iShadowDepth);
				
				pDC->SelectObject(&NewPen);
			}

			ValueToScreen(m_vPoints[m_iLastDrawnPoint].X,m_vPoints[m_iLastDrawnPoint].Y,ScreenPoint);
			pDC->MoveTo(ScreenPoint.x,ScreenPoint.y);
			ValueToScreen(m_vPoints[m_iLastDrawnPoint+1].X,m_vPoints[m_iLastDrawnPoint+1].Y,ScreenPoint);
			pDC->LineTo(ScreenPoint.x,ScreenPoint.y);
		}

		pDC->SelectClipRgn(NULL);
		pDC->SelectObject(pOldPen);
		DeleteObject(NewPen);
	}
}

void CChartLineSerie::Draw(CDC* pDC)
{
	if (!m_bIsVisible)
		return;

	// If shadow is enabled, the all series must be redrawn.
	if (m_bShadow)
	{
		DrawAll(pDC);
		return;
	}

	if (pDC->GetSafeHdc())
	{
		LOGBRUSH lb;
        lb.lbStyle = BS_SOLID;
        lb.lbColor = m_ObjectColor;
		CPen NewPen(PS_GEOMETRIC | m_iPenStyle,m_iLineWidth,&lb);
		CPen* pOldPen;

		pDC->SetBkMode(TRANSPARENT);
		//To have lines limited in the drawing rectangle :
		pDC->IntersectClipRect(m_ObjectRect);
		pOldPen = pDC->SelectObject(&NewPen);

		//Draw all points that haven't been drawn yet
		for (m_iLastDrawnPoint;m_iLastDrawnPoint<(int)GetPointsCount()-1;m_iLastDrawnPoint++)
		{
			//We don't draw a line between the origin and the first point -> we must have
			// a least 2 points before begining drawing
		//	if (m_vPoints<1)
		//		break;

			CPoint ScreenPoint;
			ValueToScreen(m_vPoints[m_iLastDrawnPoint].X,m_vPoints[m_iLastDrawnPoint].Y,ScreenPoint);
			pDC->MoveTo(ScreenPoint.x,ScreenPoint.y);

			ValueToScreen(m_vPoints[m_iLastDrawnPoint+1].X,m_vPoints[m_iLastDrawnPoint+1].Y,ScreenPoint);
			pDC->LineTo(ScreenPoint.x,ScreenPoint.y);
		}

		pDC->SelectClipRgn(NULL);
		pDC->SelectObject(pOldPen);
		DeleteObject(NewPen);
	}
}

void CChartLineSerie::DrawLegend(CDC *pDC, const CRect& rectBitmap) const
{
	if (m_strSerieName== _T(""))
		return;

	//Draw line:
	LOGBRUSH lb;
	lb.lbStyle = BS_SOLID;
	lb.lbColor = m_ObjectColor;
	CPen NewPen(PS_GEOMETRIC | m_iPenStyle,m_iLineWidth,&lb);
	CPen* pOldPen = pDC->SelectObject(&NewPen);
	pDC->MoveTo(rectBitmap.left,rectBitmap.CenterPoint().y);
	pDC->LineTo(rectBitmap.right,rectBitmap.CenterPoint().y);
	pDC->SelectObject(pOldPen);
	DeleteObject(NewPen);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
Belgium Belgium
I am a 29 years old guy and I live with my girlfriend in Hoegaarden, little city from Belgium well known for its white beer Smile | :) .
I studied as an industrial engineer in electronics but I oriented myself more towards software development when I started to work.
Currently I am working in a research centre in mechatronica. I mainly develop in C++ but I also do a bit of Java.
When I have so spare time, I like to read (mainly fantasy) and play electric guitar.

Comments and Discussions