Click here to Skip to main content
15,892,161 members
Articles / Desktop Programming / ATL

A 2D Graph Component With Zoom Capability

Rate me:
Please Sign up or sign in to vote.
4.92/5 (37 votes)
21 Dec 2004CPOL2 min read 289.6K   21.5K   166  
A 2D graph component with zoom capability.
#pragma once
#include <Atlimage.h>

#define SAFE_DELETE(p) {if(p) delete [] p; p = 0;}
//---------------------------------------------------------------
template <typename DataType>
class CGraphDataSet
{
public:
	CGraphDataSet()
	{
		m_yDataSet.pData = NULL;
		m_xDataSet.pData = NULL;
		m_clr = RGB(0,255,0);
	};
	//divNo: graph divides to divNo slices
	//SubPlot: where new data will be ploted. an integer between 0 and divNo.
	void SetData(DataType* xData, DataType* yData, int nLen)
	{
		//ATLASSERT(xData);
		ATLASSERT(yData);
		DataType yMin = yData[0];
		DataType yMax = yData[0];
		DataType xMin = 0;
		DataType xMax = 0;
		if(xData == NULL)
		{
			xMin = 0; xMax = nLen;
			xData = new DataType[nLen];
			for(int i = 0; i < nLen; i++)
				xData[i] = i;
		}
		else
		{
			xMin = xMax = xData[0];
			for(int i = 1; i < nLen; i++)	
			{
				if(xData[i] > xMax)
					xMax = xData[i];
				else if(xData[i] < xMin)
					xMin = xData[i];
			}
		}
		for(int i = 1; i < nLen; i++)	
		{
			if(yData[i] > yMax)
				yMax = yData[i];
			else if(yData[i] < yMin)
				yMin = yData[i];
		}
		SetData(xData,yData,nLen,xMin,xMax,yMin,yMax);
	}

	void SetData(DataType* xData, DataType* yData, int nLen, 
		DataType xMin, DataType xMax, DataType yMin, DataType yMax)
	{
		if(m_xDataSet.pData)
			SAFE_DELETE(m_xDataSet.pData);
		if(m_yDataSet.pData)
			SAFE_DELETE(m_yDataSet.pData);

		m_xDataSet.pData = new DataType[nLen];
		m_yDataSet.pData = new DataType[nLen];
		
		memcpy(m_yDataSet.pData, yData, nLen*sizeof(DataType));
		m_yDataSet.size = nLen;
		m_yDataSet.Max = yMax;
		m_yDataSet.Min = yMin;

		memcpy(m_xDataSet.pData, xData, nLen*sizeof(DataType));
		m_xDataSet.size = nLen;
		m_xDataSet.Max = xMax;
		m_xDataSet.Min = xMin;
	};

	struct DataSet{
		DataType *pData;
		int size;
		double Max;
		double Min;
	};
	void Draw(HDC hdc, RECT &rc, int nType = 0, int divNo = 1, int SubPlot = 0)
	{
		ATLASSERT(hdc);
		ATLASSERT(divNo > 0);
		ATLASSERT(SubPlot > -1 && SubPlot < divNo);

		int H = rc.bottom- rc.top;
		int W = rc.right- rc.left;

		double dx = (double)W/((m_xDataSet.Max-m_xDataSet.Min) * divNo);

		int X0 = rc.left;
		int Y0 = rc.top;
		int x1 = 0, x2 = 0, y1 = 0, y2 = 0;
		if(nType == 0)//Normal graph
		{
			for(int i = 0; i < m_yDataSet.size-1; i++)
			{
				x1 = X0 + (W/(m_xDataSet.Max - m_xDataSet.Min))*(m_xDataSet.pData[i] - m_xDataSet.Min);
				x2 = X0 + (W/(m_xDataSet.Max - m_xDataSet.Min))*(m_xDataSet.pData[i+1] - m_xDataSet.Min);
				y1 = Y0 + H - (H/(m_yDataSet.Max - m_yDataSet.Min))*(m_yDataSet.pData[i] - m_yDataSet.Min);
				y2 = Y0 + H - (H/(m_yDataSet.Max - m_yDataSet.Min))*(m_yDataSet.pData[i+1] - m_yDataSet.Min);
				MoveToEx(hdc,x1,y1,0);
				LineTo(hdc,x2,y2);
			}
		}
		else if(nType == 1)//dot graph
		{
			for(int i = 0; i < m_yDataSet.size; i++)
			{
				x1 = X0 + (W/(m_xDataSet.Max - m_xDataSet.Min))*(m_xDataSet.pData[i] - m_xDataSet.Min);
				y1 = Y0 + H - (H/(m_yDataSet.Max - m_yDataSet.Min))*(m_yDataSet.pData[i] - m_yDataSet.Min);
				SetPixel(hdc,x1,y1,m_clr);
			}
		}
		else//if nType == 2 BarGraph 
		{
			int Y02 = Y0 + H - (H/(m_yDataSet.Max - m_yDataSet.Min))*(0 - m_yDataSet.Min);
			for(int i = 0; i < m_yDataSet.size; i++)
			{
				x1 = X0 + (W/(m_xDataSet.Max - m_xDataSet.Min))*(m_xDataSet.pData[i] - m_xDataSet.Min);
				y1 = Y0 + H - (H/(m_yDataSet.Max - m_yDataSet.Min))*(m_yDataSet.pData[i] - m_yDataSet.Min);
				MoveToEx(hdc, x1, Y02, 0);
				LineTo(hdc, x1, y1);
			}
		}
		return;
	}

	DataSet m_yDataSet,m_xDataSet;
	COLORREF m_clr;
};

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
Software Developer (Senior) https://shahaab-co.com
Iran (Islamic Republic of) Iran (Islamic Republic of)
Currently I'm working at Dept. of Electrical Engineering in University of Shahrood.
Pattern Recognition (specially OCR), Neural Networks, Image Processing and Machine Vision are my interests. However I'm a PROGRAMMER as well.
BSc: Sharif University of technology @ 2002
MSc. and PhD: Tarbiat Modarres University @ 2006 & 2010 respectively

Personal Blog: Andisheh Online

Religious Blogs: Shia Muslims , Islamic Quotes

Company Site: Shahaab-co
My old Site: Farsi OCR

Comments and Discussions