Click here to Skip to main content
15,867,453 members
Articles / Desktop Programming / ATL
Article

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 286.8K   21.5K   166   69
A 2D graph component with zoom capability.

Sample Image - SmartGraph2.png

Update (2004-December-21):

  1. In SmartGraph, there was a problem in labeling X and Y axes, now it has been solved.
  2. Selection problem in demo project solved.
  3. Name of output file changed to SmartGraph rather than Graph.

Introduction

In my recent projects, I needed to show some data in a 2D graph. This graph must have capability of zoom in, zoom out, and colorization. At first, I wrote some code directly in my VC project and the result was satisfactory, but I had to do it for any new project and it was time consuming. So, I thought of doing it as an ATL component so that it does all the things I need. This SmartGraph is what I want.

SmartGraph is written in VC++ .NET 2003 using ATL and has several good methods, events and properties that makes it something easy to use.

This SmartGraph is an ActiveX, so it can be used in any visual language that supports ActiveX, such as Visual C++, Visual Basic, C#, Delphi, ... It can resample its data more quickly by using SetData(.,.,.,true). It also can be viewed as Regular graph, Dot graph, or Bar graph by using SetPlotType(.).

Bar Graph

How to use SmartGraph?

To use SmartGraph, you must do these steps:

  1. Register SmartGraph using this command at command prompt: "regsvr32 <PATH>\Graph.dll", or only compile "Graph.sln".

    Note: if you don’t have VC++ .NET, you can’t register Graph.dll unless you copy "msvcr71.dll" to your Windows directory. (This file is attached to this article.)

  2. In your project, press right-click on your ToolBox and then "Add/Remove Item", then select "SmartGraph" from "COM Components" tab, now it must appear in your toolbox.
  3. Now, only click on it and drag it to your dialog, now create a variable and use it:
    m_Graph.SetParentWnd(this->m_hWnd);
    /*SetParentWnd : Sets Parent Window because this component 
                     is WindowLess, it needs a handle for 
                     some painting operation.*/
    m_Graph.SetPlotType(0);// Regular Graph
    m_Graph.put_xLable("Time");
    m_Graph.put_yLable("Amplitude");
    m_Graph.put_Title("Graph Test");

    and when your data is ready, use:

    m_Graph.SetData(xdata,ydata,nLen,true);
    m_Graph.UpdateGraph();

SmartGraph Properties:

  • Back Color: use it to change background color of the graph (put_BackColor and get_BackColor).
  • BorderColor
  • ForeColor
  • Title: title of graph.
  • Columns and Rows: number of columns and rows to be drawn if grid is on (ShowGrid(true)).
  • xLable and yLable: Labels appearing in X and Y axes.
  • MarginTop, MarginLeft, MarginRight, MarginBottom: Margins for labels, title and …

SmartGraph Methods:

  • SetData and SetData2: use SetData if you don’t want to set maximum and minimum of X and Y axis. Use SetData2 if you want to specify max and min of data.
  • ShowGrid
  • UpdateGraph
  • SetPlotType: 0 - Regular, 1 - Dot Graph, 2 - Bar Graph.
  • ZoomIn, ZoomInByPercent: Zoom in (x axes) from sample/percent "FromSample"/"FromPercent" to sample/percent "ToSample"/"ToPercent".
  • ZoomOut: returns graph to previous view.
  • Reset: returns graph to original view.
  • GetDisplayedRange: returns range of samples that already are shown.
  • SetLegendText

SmartGraph Events:

  • LButtonDown
  • LButtonUp
  • MouseMove

Sample application provided is a good demonstration of this SmartGraph.

Use, enjoy and guide me for future improvements.

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

 
GeneralRe: 'double %' Pin
SnowstarTeam18-May-07 10:05
SnowstarTeam18-May-07 10:05 
GeneralRe: 'double %' Pin
jcnhal15-Oct-07 19:57
jcnhal15-Oct-07 19:57 
Questionwhy do you not give the project file(.dsw) int demo? Pin
yejianhui1686-Apr-07 4:29
yejianhui1686-Apr-07 4:29 
AnswerRe: why do you not give the project file(.dsw) int demo? Pin
Hossein Khosravi6-Apr-07 5:25
Hossein Khosravi6-Apr-07 5:25 
GeneralRe: why do you not give the project file(.dsw) int demo? Pin
yejianhui1688-Apr-07 2:12
yejianhui1688-Apr-07 2:12 
QuestionHow can i update the graph? Pin
mah_gh18-Dec-06 1:24
mah_gh18-Dec-06 1:24 
AnswerRe: How can i update the graph? Pin
Hossein Khosravi18-Dec-06 21:23
Hossein Khosravi18-Dec-06 21:23 
GeneralRe: How can i update the graph? Pin
mah_gh21-Dec-06 0:42
mah_gh21-Dec-06 0:42 
QuestionHow can i update the graph? Pin
mah_gh18-Dec-06 1:23
mah_gh18-Dec-06 1:23 
QuestionDoesn`t WORK VC++ NET 2005..can u pls hlp Pin
EngAbdALLA13-Dec-06 7:37
EngAbdALLA13-Dec-06 7:37 
AnswerRe: Doesn`t WORK VC++ NET 2005..can u pls hlp Pin
Hossein Khosravi15-Dec-06 19:02
Hossein Khosravi15-Dec-06 19:02 
QuestionHow to include SmartGraph.dll in my VC++ 6 project? Pin
Liu xiaoyan1-Dec-06 7:17
Liu xiaoyan1-Dec-06 7:17 
AnswerRe: How to include SmartGraph.dll in my VC++ 6 project? Pin
Hossein Khosravi2-Dec-06 1:59
Hossein Khosravi2-Dec-06 1:59 
GeneralRe: How to include SmartGraph.dll in my VC++ 6 project? Pin
Liu xiaoyan4-Dec-06 7:37
Liu xiaoyan4-Dec-06 7:37 
GeneralRe: How to include SmartGraph.dll in my VC++ 6 project? Pin
Hossein Khosravi4-Dec-06 18:25
Hossein Khosravi4-Dec-06 18:25 
GeneralRe: How to include SmartGraph.dll in my VC++ 6 project? Pin
Liu xiaoyan5-Dec-06 11:48
Liu xiaoyan5-Dec-06 11:48 
QuestionHow to install and use it in Visual C++? Pin
Zeus 198126-Oct-06 13:15
Zeus 198126-Oct-06 13:15 
AnswerRe: How to install and use it in Visual C++? Pin
Hossein Khosravi27-Oct-06 21:12
Hossein Khosravi27-Oct-06 21:12 
GeneralRe: How to install and use it in Visual C++? Pin
Zeus 198130-Oct-06 0:59
Zeus 198130-Oct-06 0:59 
GeneralRe: How to install and use it in Visual C++? Pin
Hossein Khosravi30-Oct-06 17:43
Hossein Khosravi30-Oct-06 17:43 
GeneralThanks! Pin
warrenblackwell1-Sep-05 12:11
warrenblackwell1-Sep-05 12:11 
AnswerRe: Thanks! Pin
Hossein Khosravi2-Sep-05 20:28
Hossein Khosravi2-Sep-05 20:28 
GeneralRe: Thanks! Pin
man2815-Jun-06 1:16
man2815-Jun-06 1:16 
GeneralUsing in C# Pin
DuongNguyen18-Apr-05 7:17
DuongNguyen18-Apr-05 7:17 
GeneralQuery regarding using SmartGraph Pin
krihtika7413-Jan-05 18:50
krihtika7413-Jan-05 18:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.