Click here to Skip to main content
Click here to Skip to main content

A 2D Graph Component With Zoom Capability

By , 21 Dec 2004
 

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)

About the Author

Hossein Khosravi
Software Developer (Senior) Persia Dade Pardaz - داده پردازی پرشیا
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
Member
I am an electronic engineer interested in Pattern Recognition (specially OCR), Neural Networks, Image Processing and of course PROGRAMMING. I was born in Khezri, a small town in Khorasan, the largest province of Iran, I have taken My BS. from Sharif University of technology, MSc. and PhD. from Tarbiat Modarres University.
Currently I'm working in Shahrood university as an assistant professor in Faculty of Electrical & Robotic.
 
Hossein Khosravi's Blog: Andisheh Online
 
Educational Site: C++ Tutorial - آموزش برنامه نویسی سی پلاس پلاس
 
Other Sites: Free Farsi OCR Datasets, Farsi OCR Software

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralRe: why do you not give the project file(.dsw) int demo?memberyejianhui1688 Apr '07 - 2:12 
Thank you,I see.
Smile | :) Big Grin | :-D
QuestionHow can i update the graph?membermah_gh18 Dec '06 - 1:24 
I have the loop in my code ,in whitch i want to plot new data every time.
i use SmartGraph, but the graph don't update every loop and show only initial data, i try to use below code, but i can't succeed.
{
.............
SetData(xdata,ydata,t, true);
ShowWindow(SW_SHOW);
UpdateWindow();
m_myError1.UpdateGraph();
UpdateWindow();
}

 
mh

AnswerRe: How can i update the graph?memberHossein Khosravi18 Dec '06 - 21:23 
Hi,
did you set its parent window?
what IDE are you using?
GeneralRe: How can i update the graph?membermah_gh21 Dec '06 - 0:42 
thanks i can solve this problem only by this code: RedrawWindow()
 
mahdieh

QuestionHow can i update the graph?membermah_gh18 Dec '06 - 1:23 
I have the loop in my code ,in whitch i want to plot new data every time.
i use SmartGraph, but the graph don't update every loop and show only initial data, i try to use below code, but i can't succeed.
{
.............
myPlotError->m_myError1.SetData(xdata,ydata,t, true);
myPlotError->ShowWindow(SW_SHOW);
myPlotError->UpdateWindow();
myPlotError->m_myError1.UpdateGraph();
myPlotError->UpdateWindow();
}

 
mh

QuestionDoesn`t WORK VC++ NET 2005..can u pls hlpmemberEngAbdALLA13 Dec '06 - 7:37 
i register it no problem and add it to TOOLbox no problem too but no response at all
 
Graph1->SetParentWnd(this->Handle.ToInt32() );
Graph1->SetPlotType(0);// Regular Graph
Graph1->xLable="Time"; // put_xLable()<-method doesn`t found
Graph1->yLable="Amplitude";
 
Graph1->SetData(*X,*Y,1000,true);
Graph1->UpdateGraph();
Confused | :confused: Confused | :confused: Confused | :confused: Confused | :confused: Confused | :confused:
 
Eng_AbdALLA

AnswerRe: Doesn`t WORK VC++ NET 2005..can u pls hlpmemberHossein Khosravi15 Dec '06 - 19:02 
I didn't test it with .NET 2005! it may be incompatible.
QuestionHow to include SmartGraph.dll in my VC++ 6 project?memberLiu xiaoyan1 Dec '06 - 7:17 
I am new to VC. please help me out.
I want to use SmartGraph.dll in my current project. I had registered dll file. but I do not know how to add the dll into my project. do I still need any .h and . lib file?
 
Thanks
AnswerRe: How to include SmartGraph.dll in my VC++ 6 project?memberHossein Khosravi2 Dec '06 - 1:59 
Hello,
this component is an ActiveX. To use it, you must at first register it using "regsvr32", then right click on your dialog and select "insert ActiveX". A list of all activex controls, installed on your pc, will be appeared, now select SmartGraph(or Graph). it will appear on the dialog...finished!
GeneralRe: How to include SmartGraph.dll in my VC++ 6 project?memberLiu xiaoyan4 Dec '06 - 7:37 
Hi,Hossein:
Thanks for your reply.
It works now, but when I try to add a variable m_Graph to the IDC_SmartGraph through ClassWizard, the Developer Studio generate a C++ wrapper class for ActiveX Smartgraph automically. When I rebuild project, I get a lot of compile errors for undeclared identifier, such as, VIS_I4 DISPID_BACKCOLOR...
 
By the way, I used VC++6.0 and is SmartGraph compatible with VC++ 6?
 
Thanks again and have a nice day.

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

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