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   
GeneralMy vote of 5memberMember 967370511 Dec '12 - 12:32 
Exactly what I was looking for and easy to use. Thanks.
QuestionCan you update it to WindowsXP-64-bit or windows server 2003 - 64bit?memberrockcracks23 Jun '08 - 23:50 
Can you update it to WindowsXP-64-bit or windows server 2003 - 64bit?
When I set the project to 64bit, there were many problems. Of course, you can complete it under VS.net 2005.
AnswerRe: Can you update it to WindowsXP-64-bit or windows server 2003 - 64bit?memberHossein Khosravi24 Jun '08 - 20:36 
Sorry, I don't have experience on 64 bit platforms.
GeneralRe: Can you update it to WindowsXP-64-bit or windows server 2003 - 64bit?memberSaroj Acharya7 Dec '11 - 8:26 
Is this same as running in VISTA ? I was unable to install this DLL in Vista...and it could be because it is just 32 bit DLL.
 
Any advice ?
GeneralC# Implementationmemberworked on mine9 Oct '07 - 4:15 
Anyone have an implementation code sample for C#. I'm hung up on build error "cannot convert from 'double[]' to 'ref double'"
 
Steve

GeneralHelp me..memberChoi Sung Dae18 Sep '07 - 20:28 
compiler is Visual Studio 2005..
 
---------------------------
RegSvr32
---------------------------
C:\Project\exProject\3_2D Graph\SmartGraph_SRC\Debug\SmartGraph.dll의 DllRegisterServer fail.
error code: 0x8007000e
---------------------------
 


GeneralRe: Help me..memberHossein Khosravi18 Sep '07 - 21:50 
Do you have MSVCR71.dll in your system32?
GeneralRe: Help me..memberChoi Sung Dae20 Sep '07 - 15:08 
yes..
 
I have msvcr71.dll
and msvcr71.dll of position is c:\windows\system32..Sigh | :sigh:
GeneralRe: Help me..memberChoi Sung Dae20 Sep '07 - 21:34 
compiler is .net 2005 --> error
 
compiler is .net 2003 --> success.
 
^_^v
 
Have a good time~Laugh | :laugh:
 

GeneralVisual studio 2005memberkspalj13 Sep '07 - 23:14 
Registered .dll
Inserted Smartgraph in form (Visual C++ CLR project).
Used following code:
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
 
this->timer_protok->Interval = 1000;

nLen = 2;
trend1->SetPlotType(0);
trend1->xLable = ("Vrijeme");
trend1->yLable =("Amplituda");
trend1->Title =("Graf koji ne radi!!!");
}
private: System::Void timer_protok_Tick(System::Object^ sender, System::EventArgs^ e)
{
intTimerCounter++;

xdata = intTimerCounter;
ydata = 10 * Math::Sin(xdata);

trend1->SetData2(xdata,ydata,nLen,-25.0,100.0,-25.0,100.0,true);
trend1->UpdateGraph();
trend1->Refresh();
}
As result I got a straight line with a right end following the x and y data (as it should do), and left starting point with constant y value, and changing x value that behaves like y data of ending point (in this example oscilating around 0 value). In other words something like swinging lever...
 
Please help.
 

 
Kresimir
GeneralRe: Visual studio 2005memberHossein Khosravi14 Sep '07 - 21:07 
xData and yData must be pointers to the start of data, for example if xData = int [10] and yData = int [10], then you must call setData with &xData, and &yData as parameters. I think in C# you must do this in a "fixed" block
GeneralTruely Nice Work!memberOxfoX31 Jul '07 - 3:32 
Thanks!....
 
hu Zhenxing
GeneralUsing the SmartGraph control in a property pagememberpkam19 Jul '07 - 6:50 
Hi Hossein,
 
First of all, let me say thank you for this handy control. It is just what I was looking for for one of my dialog-based MFC applications. But I am having some trouble integrating this with the application.
 
I have added this control to a property page but as soon as I click the tab for this particular property page, it disappears. The application and the other property pages work fine even after that. The constructor for the property page is called, but OnInitDialog is never called. It looks like the page receives a AfxSig_v_v_v signal and OnDestroy is called to process the signal. I have added the statements you have given in your article to the InitDialog function and I have also taken a look at the demo project.
 
Do you know what could be the problem here? Am I missing something very obvious? I am using Visual Studio .Net 2003 and the programming language is C/C++.
 
Thanks,
Priya

GeneralRe: Using the SmartGraph control in a property pagememberHossein Khosravi21 Jul '07 - 0:33 
Hello, the only thing that you may forgotten is the registration of the ActiveX. Do you register it without any error? If so, unfortunately, I don't have any idea!!
GeneralRe: Using the SmartGraph control in a property pagememberpkam22 Jul '07 - 23:22 
Hi Hossein,
 
Thank you for your response. I did register the dll successfully. I'll keep debugging the problem and will post the solution here if I do solve it.
 
Thanks,
Priya

GeneralRe: Using the SmartGraph control in a property pagememberpkam24 Jul '07 - 6:39 
Phew...finally figured it out. I don't think anyone else will make such an obvious mistake, but in case they do, I hope this helps. I had forgotten to add AfxEnableControlContainer() call to the InitInstance() function for the application. Now everything works like a charm.

GeneralRe: Using the SmartGraph control in a property pagememberHossein Khosravi24 Jul '07 - 18:42 
Hi, congratulation...
every one may make such mistakes, I don't know this trick!! because its only the case were someone like you need to insert it in a property page.
Good luckSmile | :)
QuestionactiveX registrartionmembermickael_biardeau20 Jun '07 - 2:32 
Hi,
i work with VS2005.
i try to open your dialog IDD_SMARTGRAPHDEMO_FORM with the resource viewer and i have this message:
The activeX control "{A2919A75-B379-4BBD-8D95-15BEB3BAA140}" is not registered. Register the control and try again.
 
With activeX i have to register/Where is it?
 
mike
AnswerRe: activeX registrartionmemberHossein Khosravi20 Jun '07 - 3:12 
The ActiveX is graph.dll, you can find it in binary files. register it using the following command in Run menu:
Regsvr32
you must have some dlls to register it, they are included in binary files, I think.
good luck

General'double %'memberSnowstarTeam17 May '07 - 13:01 
SetData takes two, "double %"s, an int and a bool. Could someone tell me what a "double %" is, I've never seen something that looks like this before. This may be a stupid question, but I've never seen a "double %" before.
GeneralRe: 'double %'memberHossein Khosravi17 May '07 - 21:12 
When I was writing the code, I wrote "double *", now I'm completely confused to see "double %" instead!!OMG | :OMG:
GeneralRe: 'double %'memberSnowstarTeam18 May '07 - 10:05 
Yeah, I looked at the source and found it's supposed to be a "double *", but for some reason I can't figure out, when I add the compiled component into my project visual studio claims it's a 'double %'.
GeneralRe: 'double %'memberjcnhal15 Oct '07 - 19:57 
did you ever solve this one?
I'm getting the same problem.
Questionwhy do you not give the project file(.dsw) int demo?memberyejianhui1686 Apr '07 - 4:29 
sorry,I can not find the project file(*.dsw),So i cant not compile the project..
 
Laugh | :laugh: Laugh | :laugh:
AnswerRe: why do you not give the project file(.dsw) int demo?memberHossein Khosravi6 Apr '07 - 5:25 
its .net solution, not vc 6 project.
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.
GeneralRe: How to include SmartGraph.dll in my VC++ 6 project?memberHossein Khosravi4 Dec '06 - 18:25 
Hi again,
I work with VC 2003, but to check this problem, I tried smartGraph on my friend PC which works with VC 6.0 (SP6), I do the same steps as wrote before. then I add smartgraph to my project and used ClassWizard to create a variable. I used some functions(SetTitle, SetData, UpdateGraph). The project compiled without any error and all things ok. The only think remained is that SP6 has been installed here and I hope if you install SP6, your problem will be answered.
GeneralRe: How to include SmartGraph.dll in my VC++ 6 project?memberLiu xiaoyan5 Dec '06 - 11:48 
Thanks very much for your reply.
I solve the problem by new another project, but I do not know how to show smartgraph in view window of MDI. I add a variable m_graph in the myview.h, but I do not know how to make it visble?
 
Thanks for your help.
QuestionHow to install and use it in Visual C++?memberZeus 198126 Oct '06 - 13:15 
hi
Its a great program, I was looking for one just like this.
i tried installing as per the steps you have suggested, and I am using Visual C++, but it did not worked.Confused | :confused:
can you help me in installing and using the code.
aslo i would like to know what kind of input data is it? and what format is it?
 
thanks
Zues 1981
AnswerRe: How to install and use it in Visual C++?memberHossein Khosravi27 Oct '06 - 21:12 
you may need MSvcr71.dll to install the component. input data is (double*), a pointer to the start of your data. the provided sample application may be usefull.
GeneralRe: How to install and use it in Visual C++?memberZeus 198130 Oct '06 - 0:59 
in the install process i m not able to understand the second and third steps.
i will be thankful to you on helping me out.
 
zeus 1981
GeneralRe: How to install and use it in Visual C++?memberHossein Khosravi30 Oct '06 - 17:43 
what version of VC you have? VC 6.0 or .net?
GeneralThanks!memberwarrenblackwell1 Sep '05 - 12:11 
This is exactly what I needed and have searched for quite a long time.
 
W
AnswerRe: Thanks!memberHossein Khosravi2 Sep '05 - 20:28 
your welcome Cool | :cool:
GeneralRe: Thanks!memberman2815 Jun '06 - 1:16 
i can not download the sourse code ,can you send me a copy of the source code ,thanks !!my email:zjh_hua@hotmail.com
GeneralUsing in C#memberDuongNguyen18 Apr '05 - 7:17 
Anyone used this control in C#?
I could not use it. it always gives error: Can not convert from 'double*' to 'ref double' in the method SetData when i use this fragment
 
double[] x = ....
double[] y =....
 
unsafe
{
fixed(double* xx = x, yy = y)
myGraph.SetData(xx, yy, 10, true);
}
GeneralQuery regarding using SmartGraphmemberkrihtika7413 Jan '05 - 18:50 
Hi,
 
We are having chart control which does real time graphing. I need to include Zoomin Zoomout capabilities. I thought your code must be usefule. Would you please give me your email so that I can ask you in much detail.
 
My query is the chart control is in MFC, VC++ 7. Can I use this Smartgraph control developed using ATL in my ocx project code? If so how to do this?
 
Please help me
 
Please let me know if this is not possible and if I requred to post my qurey in this dicussion board itself.
 
Thanks,
 
Krithika

QuestionHow to use it (zooming in/out) in VB?memberhyxkona@yahoo.com4 Jan '05 - 11:56 
Thanks! It is a great chart control!
How to use it (zooming in/out) in VB? Any sample?...
-Kona
AnswerRe: How to use it (zooming in/out) in VB?memberanonymous17 Jan '05 - 0:01 
I've contacted the author and it seems not to work in VB. This graph component needs some pointers which is not supported by VB. My suggestion was to make it an OCX to overcome this problem perhaps. Unfortunately, I did not receive his opinion about this.
GeneralCheersmembernice_boy24730 Dec '04 - 5:38 
Very useful indeed! I would like to contact you for some image processing questions I have since it looks like you have some experiance in this field. Please let me know by email if that is OK.
 
Regards,
 

 
Behrouz Heshmatipour
behrouz@rayane.com
GeneralHImemberMohammadAmiry22 Dec '04 - 2:41 
Nice Job Hossein!

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

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