Click here to Skip to main content
11,436,803 members (68,948 online)
Click here to Skip to main content
Add your own
alternative version

A 2D Graph Component With Zoom Capability

, 21 Dec 2004 CPOL
A 2D graph component with zoom capability.
smartgraphdemo_src.zip
res
cur1.cur
cur2.cur
SmartGraphDemo.ico
SmartGraphDemo.manifest
SmartGraphDemoDoc.ico
Toolbar.bmp
smartgraph_dll_exe.zip
msvcr71.dll
Graph.dll
SmartGraphDemo.exe
smartgraph_src.zip
SmartGraph.bmp
_Graph.tlb
bitmap1.bmp
Graph.rgs
Graphps.def
smart_graph_dll_exe.zip
msvcr71.dll
SmartGraphDemo.exe
SmartGraph.dll
// SmartGraphDemoView.cpp : implementation of the CSmartGraphDemoView class
//

#include "stdafx.h"
#include "SmartGraphDemo.h"

#include "SmartGraphDemoDoc.h"
#include "SmartGraphDemoView.h"
#include ".\smartgraphdemoview.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CSmartGraphDemoView

IMPLEMENT_DYNCREATE(CSmartGraphDemoView, CFormView)

BEGIN_MESSAGE_MAP(CSmartGraphDemoView, CFormView)
	ON_COMMAND(ID_ZOOM_IN, OnZoomIn)
	ON_COMMAND(ID_ZOOM_OUT, OnZoomOut)
	ON_COMMAND(ID_ZOOM_RESET, OnZoomReset)
	ON_WM_SETCURSOR()
	ON_UPDATE_COMMAND_UI(ID_ZOOM_IN, OnUpdateZoomIn)
	ON_UPDATE_COMMAND_UI(ID_ZOOM_OUT, OnUpdateZoomOut)
	ON_WM_SIZE()
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	ON_COMMAND(ID_GRAPHTYPE_DOT, OnGraphtypeDot)
	ON_COMMAND(ID_GRAPHTYPE_LINE, OnGraphtypeLine)
	ON_COMMAND(ID_GRAPHTYPE_BAR, OnGraphtypeBar)
	ON_WM_NCPAINT()
END_MESSAGE_MAP()

// CSmartGraphDemoView construction/destruction

CSmartGraphDemoView::CSmartGraphDemoView()
: CFormView(CSmartGraphDemoView::IDD)
{
	m_bFirstCall = true;
	m_nSkipMove = 0;
	memset(&m_rcSelRect, 0, sizeof(m_rcSelRect));
	m_brSelection.CreateSolidBrush( RGB(255,255,0) );
	m_bSkip = false;
}

CSmartGraphDemoView::~CSmartGraphDemoView()
{
}

void CSmartGraphDemoView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_SMARTGRAPH1, m_Graph);
}

BOOL CSmartGraphDemoView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

void CSmartGraphDemoView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

	m_Graph.SetParentWnd(this->m_hWnd);
	m_Graph.SetPlotType(0);
	m_Graph.put_xLable("Time");
	m_Graph.put_yLable("Amplitude");
	m_Graph.put_Title("Graph Test");

	m_nCursor = CURSOR_NORMAL;
	m_hZoomIn = LoadCursor(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDC_CURSOR_ZOOM_IN));
	m_hZoomOut = LoadCursor(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDC_CURSOR_ZOOM_OUT));
	m_hNormal = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
}


// CSmartGraphDemoView diagnostics

#ifdef _DEBUG
void CSmartGraphDemoView::AssertValid() const
{
	CFormView::AssertValid();
}

void CSmartGraphDemoView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CSmartGraphDemoDoc* CSmartGraphDemoView::GetDocument() const // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSmartGraphDemoDoc)));
	return (CSmartGraphDemoDoc*)m_pDocument;
}
#endif //_DEBUG


// CSmartGraphDemoView message handlers

void CSmartGraphDemoView::OnZoomIn()
{
	if(m_nCursor == CURSOR_ZOOM_IN)
	{
		m_nCursor = CURSOR_NORMAL;
		SetCursor(m_hNormal);	
	}
	else
	{
		m_nCursor = CURSOR_ZOOM_IN;
		SetCursor(m_hZoomIn);
	}
}

void CSmartGraphDemoView::OnZoomOut()
{
	if(m_nCursor == CURSOR_ZOOM_OUT)
	{
		m_nCursor = CURSOR_NORMAL;
		SetCursor(m_hNormal);	
	}
	else
	{
		m_nCursor = CURSOR_ZOOM_OUT;
		SetCursor(m_hZoomIn);
	}
}

void CSmartGraphDemoView::OnZoomReset()
{
	m_nCursor = CURSOR_NORMAL;
	m_Graph.Reset();
	m_Graph.UpdateGraph();
	m_bFirstCall = true;
}

void CSmartGraphDemoView::ZoomIn()
{
	long bot,top,right,left;
	m_Graph.GetPlotRect(&left,&top,&right,&bot);
	m_rcSelRect.NormalizeRect();

	double start = (double)(m_rcSelRect.left-left)*100.0/(right-left);
	double end = (double)(m_rcSelRect.right-left)*100.0/(right-left);
	if(start < 0)
		start = 0;
	if(end > 100)
		end = 100;

	m_Graph.ZoomInByPercent(start, end);
	m_Graph.UpdateGraph();
	m_bFirstCall = true;
}

void CSmartGraphDemoView::OnUpdateZoomIn(CCmdUI *pCmdUI)
{
	if(m_nCursor == CURSOR_ZOOM_IN)
		pCmdUI->SetCheck(1);
	else
		pCmdUI->SetCheck(0);
}

void CSmartGraphDemoView::OnUpdateZoomOut(CCmdUI *pCmdUI)
{
	if(m_nCursor == CURSOR_ZOOM_OUT)
		pCmdUI->SetCheck(1);
	else
		pCmdUI->SetCheck(0);
}


void CSmartGraphDemoView::ZoomOut()
{
	m_Graph.ZoomOut();
	m_Graph.UpdateGraph();
	m_bFirstCall = true;
}


BOOL CSmartGraphDemoView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
	if ( nHitTest == HTCLIENT )
	{
		switch(m_nCursor)
		{

		case CURSOR_ZOOM_IN:
			SetCursor(m_hZoomIn);
			break;
		case CURSOR_ZOOM_OUT:
			SetCursor(m_hZoomOut);
			break;
		}
	}
	return true;
}
void CSmartGraphDemoView::OnSize(UINT nType, int cx, int cy)
{
	CFormView::OnSize(nType, cx, cy);
	
	if(++m_nSkipMove < 6)
		return;

	m_bFirstCall = true;
	RECT rc;
	GetClientRect(&rc);
	rc.top += 25;
	rc.bottom -= 80;
	rc.left += 15;
	rc.right -= 15;
	m_Graph.MoveWindow(&rc);
	Invalidate();
	m_Graph.UpdateGraph();
	int RES[] = {IDC_STATIC_START, IDC_START, IDC_STATIC_END, IDC_END};
	int top[] = {rc.bottom + 15, rc.bottom + 10, rc.bottom + 15, rc.bottom + 10};
	RECT rc2;		
	rc2.left = rc.left;
	rc2.right = rc2.left + 70;
	for(int i = 0; i < 4; i++)
	{
		rc2.top = top[i];
		rc2.bottom = rc2.top + 25;
		GetDlgItem(RES[i])->MoveWindow(&rc2);
		rc2.left = rc2.right + 15;
		rc2.right = rc2.left + 70;
	}
}
BEGIN_EVENTSINK_MAP(CSmartGraphDemoView, CFormView)
	ON_EVENT(CSmartGraphDemoView, IDC_SMARTGRAPH1, 1, LButtonDownSmartgraph1, VTS_I4 VTS_I4 VTS_I8)
	ON_EVENT(CSmartGraphDemoView, IDC_SMARTGRAPH1, 2, LButtonUpSmartgraph1, VTS_I4 VTS_I4 VTS_I8)
	ON_EVENT(CSmartGraphDemoView, IDC_SMARTGRAPH1, 3, MouseMoveSmartgraph1, VTS_I4 VTS_I4 VTS_I8)
END_EVENTSINK_MAP()

void CSmartGraphDemoView::LButtonDownSmartgraph1(long xPos, long yPos, LONGLONG wParam)
{
	long bot,top,right,left;
	m_Graph.GetPlotRect(&left,&top,&right,&bot);

	if(xPos > right)
		xPos = right;
	else if(xPos < left)
		xPos = left;

	m_rcSelRect.left = xPos;
}

void CSmartGraphDemoView::LButtonUpSmartgraph1(long xPos, long yPos, LONGLONG wParam)
{
	long bot,top,right,left;
	m_Graph.GetPlotRect(&left,&top,&right,&bot);

	if(xPos > right)
		xPos = right;
	else if(xPos < left)
		xPos = left;
	m_rcSelRect.right = xPos;

	switch(m_nCursor)
	{
	case CURSOR_ZOOM_IN:
		ZoomIn();
		break;
	case CURSOR_ZOOM_OUT:
		ZoomOut();
		break;
	}
}

void CSmartGraphDemoView::MouseMoveSmartgraph1(long xPos, long yPos, LONGLONG wParam)
{
	if(m_bSkip)
		return;
	if(!(wParam & MK_LBUTTON))
		return;
	static count = 0;
	if(++count < 2)
		return;

	long bot,top,right,left;
	count = 0;
	m_Graph.GetPlotRect(&left,&top,&right,&bot);
	if(xPos > right)
		xPos = right;
	else if(xPos < left)
		xPos = left;

	m_rcSelRect.right = xPos;
	m_rcSelRect.bottom = bot;
	m_rcSelRect.top = top;
	static CRect rcSelRect =  m_rcSelRect;

	CDC* pDC = GetDC();
	if ( m_bFirstCall )
	{
		pDC -> DrawDragRect( m_rcSelRect, m_rcSelRect.Size(), NULL, m_rcSelRect.Size(), &m_brSelection );
	}
	else
		pDC -> DrawDragRect( m_rcSelRect, m_rcSelRect.Size(), rcSelRect, rcSelRect.Size(), &m_brSelection );

	rcSelRect =  m_rcSelRect;

	double start = (double)(m_rcSelRect.left-left)*100.0/(right-left);
	double end = (double)(m_rcSelRect.right-left)*100.0/(right-left);
	if(start < 0)
		start = 0;
	if(end > 100)
		end = 100;
	__int64 x1,x2;
	m_Graph.GetDisplayedRange(&x1, &x2);
	int nSamples = x2-x1;
	SetDlgItemInt(IDC_START, start*nSamples/100);
	SetDlgItemInt(IDC_END, end*nSamples/100);
	m_bFirstCall = false;
}
void CSmartGraphDemoView::OnFileOpen()
{
	m_Graph.SetLegendText(" Smart Graph\n Hossein Khosravi\n 2004.08.29", 15, 5,50,50);
	m_Graph.put_Title("\"Windows XP Startup.wav\" Graph");
	char str[MAX_PATH]={0};
	GetWindowsDirectory(str, MAX_PATH);

	CFile file(str + CString("\\Media\\Windows XP Startup.wav"),CFile::modeRead|CFile::typeBinary);
	file.Seek(44,0);
	__int16 sample;
	int nLen = file.GetLength()/4;//16bit per sample
	double *xdata = new double[nLen];
	double *ydata = new double[nLen];
	for(int i = 0; i < nLen; i++)
	{
		file.Read(&sample,2);
		ydata[i] = sample;
		xdata[i] = i;
		file.Read(&sample,2);//ignore one channel
	}
	m_Graph.SetData(xdata,ydata,nLen, true);
	m_Graph.UpdateGraph();
}

void CSmartGraphDemoView::OnGraphtypeDot()
{
	m_Graph.SetPlotType(1);
}

void CSmartGraphDemoView::OnGraphtypeLine()
{
	m_Graph.SetPlotType(0);
}

void CSmartGraphDemoView::OnGraphtypeBar()
{
	m_Graph.SetPlotType(2);
}

void CSmartGraphDemoView::OnNcPaint()
{
	m_bFirstCall = true;
}

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)

Share

About the Author

Hossein Khosravi
Software Developer (Senior) FarsiOCR.ir
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 , Ashura , Islamic Quotes

Commercial Site: Farsi OCR
Follow on   Twitter   Google+

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150428.2 | Last Updated 22 Dec 2004
Article Copyright 2004 by Hossein Khosravi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid