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

2D Graph ActiveX Control

Rate me:
Please Sign up or sign in to vote.
4.89/5 (159 votes)
5 Aug 2003MIT8 min read 1.5M   70.2K   339  
An ActiveX control for 2D data visualisation
// DemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Demo.h"
#include "DemoDlg.h"

#include <math.h>

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

/////////////////////////////////////////////////////////////////////////////
// CDemoDlg dialog

CDemoDlg::CDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDemoDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDemoDlg)
	DDX_Control(pDX, IDC_SHOW_CURSOR, m_cbShowCursor);
	DDX_Control(pDX, IDC_CHECK_SHOW_ELEMENT4, m_cbShowElement4);
	DDX_Control(pDX, IDC_CHECK_SHOW_ELEMENT3, m_cbShowElement3);
	DDX_Control(pDX, IDC_CHECK_SHOW_ELEMENT2, m_cbShowElement2);
	DDX_Control(pDX, IDC_CHECK_SHOW_ELEMENT1, m_cbShowElement1);
	DDX_Control(pDX, IDC_NTGRAPHCTRL1, m_Graph);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDemoDlg, CDialog)
	//{{AFX_MSG_MAP(CDemoDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CHECK_SHOW_ELEMENT1, OnCheckShowElement1)
	ON_BN_CLICKED(IDC_CHECK_SHOW_ELEMENT2, OnCheckShowElement2)
	ON_BN_CLICKED(IDC_CHECK_SHOW_ELEMENT3, OnCheckShowElement3)
	ON_BN_CLICKED(IDC_CHECK_SHOW_ELEMENT4, OnCheckShowElement4)
	ON_BN_CLICKED(IDC_BUTTON_PLOT, OnButtonPlot)
	ON_BN_CLICKED(IDC_BUTTON_CLEAR, OnButtonClear)
	ON_BN_CLICKED(IDC_BUTTON_COPY, OnButtonCopy)
	ON_BN_CLICKED(IDC_SHOW_CURSOR, OnShowCursor)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BEGIN_EVENTSINK_MAP(CDemoDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CDemoDlg)
	ON_EVENT(CDemoDlg, IDC_NTGRAPHCTRL1, 1 /* CursorEvent */, OnCursorEventNtgraphctrl1, VTS_R8 VTS_R8)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDemoDlg message handlers

BOOL CDemoDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	m_cbShowElement1.SetCheck(1);
	m_cbShowElement2.SetCheck(1);
	m_cbShowElement3.SetCheck(1);
	m_cbShowElement4.SetCheck(1);
	
	// Customize Graph 
	m_Graph.SetBackColor (RGB(0,0,0));
	m_Graph.SetAxisColor (RGB(0,255,0));
	m_Graph.SetLabelColor (RGB(128,255,255));
    
	m_Graph.SetGridColor(RGB(192,192,192));
	m_Graph.SetShowGrid (TRUE);

	m_Graph.SetCursorColor (RGB(255,0,0));
	m_Graph.SetCursorMode (TRUE);

	m_Graph.SetGraphTitle("2D Graph");
	m_Graph.SetXLabel ("Wavelength (nm)");
	m_Graph.SetYLabel("Intensity (counts/sec.)");
	m_Graph.SetRange(0.,10,-1,1.);

	// Set the number of elements to be plotted. 
	m_Graph.SetElementCount (4);

	// Allocate space for Elements with 2000 points.
	if ( !m_Graph.CreateElement(2000) ) {
		AfxMessageBox("Error allocating graph element") ;
        return FALSE;
	}

	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CDemoDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CDemoDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


void CDemoDlg::OnButtonPlot() 
{

//
// Customize Graph Elements 
//
//
   // Select Graph Element to change the plot color 
   m_Graph.SetElement(0); 
   // Change the plot color of selected Graph Element
   m_Graph.SetElementColor (RGB(255,0,0));

   m_Graph.SetElement(1);
   m_Graph.SetElementColor (RGB(0,255,0));
   m_Graph.SetElement(2);
   m_Graph.SetElementColor (RGB(0,0,255));
   m_Graph.SetElement(3);
   m_Graph.SetElementColor(RGB(255,127,0));
   
   m_Graph.SetRange(0,2000,-0.1,2);

   
//
// Load Data int the Graph Elements 
//



   for ( int i = 0 ; i < 2000 ; i++)
   {
	   double sigma = 150;
	   double pi = 3.14;
	   double x0 = 1000.;
	   double x = (double)i;
	   double y = 600*(exp(-pow(((x-x0)/sigma),2)/2) / (sqrt(2*pi)*sigma));
        
	    m_Graph.SetXYValue(x,y,i,0);         //Element 1
		m_Graph.SetXYValue(x+100,y,i,1);     //Element 2
 		m_Graph.SetXYValue(x-100,y*0.1,i,2); //Element 3
		m_Graph.SetXYValue(x+200,y*1.2,i,3); //Element 4
   }


//
// Redraw the Control
//
   m_Graph.Invalidate ();

}

void CDemoDlg::OnButtonClear() 
{
	// Clear the Graph
	m_Graph.ClearGraph ();
}

void CDemoDlg::OnCheckShowElement1() 
{
	ShowElement(0, m_cbShowElement1.GetCheck());
}

void CDemoDlg::OnCheckShowElement2() 
{
	ShowElement(1, m_cbShowElement2.GetCheck());	
}

void CDemoDlg::OnCheckShowElement3() 
{
	ShowElement(2, m_cbShowElement3.GetCheck());	
}

void CDemoDlg::OnCheckShowElement4() 
{
	ShowElement(3, m_cbShowElement4.GetCheck());	
}

void CDemoDlg::ShowElement(const int nElement, const BOOL bShow)
{
	m_Graph.SetElement(nElement);
	m_Graph.SetShowElement(bShow);
}

void CDemoDlg::OnButtonCopy() 
{
    m_Graph.Copy2Clipboard();	
}



void CDemoDlg::OnCursorEventNtgraphctrl1(double x, double y) 
{
	TRACE( "X%3.3f, Y%3.3f\n", x, y ); // gibt die Koordinaten korrekt aus.
	
}


void CDemoDlg::OnShowCursor() 
{
	 m_Graph.SetCursorMode(m_cbShowCursor.GetCheck());
}

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 MIT License


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions