Click here to Skip to main content
15,861,366 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.1M   99.5K   787  
A flexible charting control to display 2D data
/*
 *
 *	ChartGradient.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.
 *
 *
 */

#include "stdafx.h"
#include "ChartGradient.h"

CChartGradient::CChartGradient()
{
}

CChartGradient::~CChartGradient()
{
}

void CChartGradient::DrawGradient(CDC* pDC, const CRect& GradientRect, COLORREF Color1, 
								  COLORREF Color2, EGradientType GradientType)
{
#if _MFC_VER > 0x0600
	if ( (GradientType == gtHorizontal) || (GradientType == gtVertical) )
	{
		TRIVERTEX vertex[2] ;
		vertex[0].x     = GradientRect.left;
		vertex[0].y     = GradientRect.top;
		vertex[0].Red   = ((COLOR16)GetRValue(Color1))<<8;
		vertex[0].Green = ((COLOR16)GetGValue(Color1))<<8;
		vertex[0].Blue  = ((COLOR16)GetBValue(Color1))<<8;
		vertex[0].Alpha = 0x0000;
		vertex[1].x     = GradientRect.right;
		vertex[1].y     = GradientRect.bottom; 
		vertex[1].Red   = ((COLOR16)GetRValue(Color2))<<8;
		vertex[1].Green = ((COLOR16)GetGValue(Color2))<<8;
		vertex[1].Blue  = ((COLOR16)GetBValue(Color2))<<8;
		vertex[1].Alpha = 0x0000;
		GRADIENT_RECT gRect;
		gRect.UpperLeft  = 0;
		gRect.LowerRight = 1;
		if (GradientType == gtHorizontal)
			pDC->GradientFill(vertex,2,&gRect,1,GRADIENT_FILL_RECT_H);
		else
			pDC->GradientFill(vertex,2,&gRect,1,GRADIENT_FILL_RECT_V);
	}
	else
	{
		for (int i=0;i<2; i++)
		{
			TRIVERTEX vertex[2] ;
			if (GradientType == gtHorizontalDouble)
			{
				vertex[0].x     = GradientRect.left + (GradientRect.Width()/2) * i;
				vertex[0].y     = GradientRect.top;
			}
			else
			{
				vertex[0].x     = GradientRect.left;
				vertex[0].y     = GradientRect.top + (GradientRect.Height()/2) * i; 
			}
			if (i==0)
			{
				vertex[0].Red   = ((COLOR16)GetRValue(Color1))<<8;
				vertex[0].Green = ((COLOR16)GetGValue(Color1))<<8;
				vertex[0].Blue  = ((COLOR16)GetBValue(Color1))<<8;
			}
			else
			{
				vertex[0].Red   = ((COLOR16)GetRValue(Color2))<<8;
				vertex[0].Green = ((COLOR16)GetGValue(Color2))<<8;
				vertex[0].Blue  = ((COLOR16)GetBValue(Color2))<<8;
			}
			vertex[0].Alpha = 0x0000;
			if (GradientType == gtHorizontalDouble)
			{
				vertex[1].x     = GradientRect.left + (GradientRect.Width()/2) * (i+1);
				vertex[1].y     = GradientRect.bottom; 
			}
			else
			{
				vertex[1].x     = GradientRect.right;
				vertex[1].y     = GradientRect.top + (GradientRect.Height()/2) * (i+1); 
			}
			if (i==0)
			{
				vertex[1].Red   = ((COLOR16)GetRValue(Color2))<<8;
				vertex[1].Green = ((COLOR16)GetGValue(Color2))<<8;
				vertex[1].Blue  = ((COLOR16)GetBValue(Color2))<<8;
			}
			else
			{
				vertex[1].Red   = ((COLOR16)GetRValue(Color1))<<8;
				vertex[1].Green = ((COLOR16)GetGValue(Color1))<<8;
				vertex[1].Blue  = ((COLOR16)GetBValue(Color1))<<8;
			}			
			vertex[1].Alpha = 0x0000;

			GRADIENT_RECT gRect;
			gRect.UpperLeft  = 0;
			gRect.LowerRight = 1;
			if (GradientType == gtHorizontalDouble)
				pDC->GradientFill(vertex,2,&gRect,1,GRADIENT_FILL_RECT_H);
			else
				pDC->GradientFill(vertex,2,&gRect,1,GRADIENT_FILL_RECT_V);
		}
	}
#else
	CBrush NewBrush(Color1);
	pDC->FillRect(GradientRect,&NewBrush);
	DeleteObject(NewBrush);
#endif

}

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