Click here to Skip to main content
11,933,621 members (55,265 online)
Click here to Skip to main content
Add your own
alternative version


163 bookmarked

A 2D Graph Component With Zoom Capability

, 21 Dec 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
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.


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:
    /*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_Title("Graph Test");

    and when your data is ready, use:


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.


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)
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

You may also be interested in...

Comments and Discussions

GeneralRe: How to install and use it in Visual C++? Pin
Zeus 198130-Oct-06 1:59
memberZeus 198130-Oct-06 1:59 
GeneralRe: How to install and use it in Visual C++? Pin
Hossein Khosravi30-Oct-06 18:43
memberHossein Khosravi30-Oct-06 18:43 
GeneralThanks! Pin
warrenblackwell1-Sep-05 13:11
memberwarrenblackwell1-Sep-05 13:11 
AnswerRe: Thanks! Pin
Hossein Khosravi2-Sep-05 21:28
memberHossein Khosravi2-Sep-05 21:28 
GeneralRe: Thanks! Pin
man2815-Jun-06 2:16
memberman2815-Jun-06 2:16 
GeneralUsing in C# Pin
DuongNguyen18-Apr-05 8:17
memberDuongNguyen18-Apr-05 8:17 
GeneralQuery regarding using SmartGraph Pin
krihtika7413-Jan-05 19:50
memberkrihtika7413-Jan-05 19:50 
QuestionHow to use it (zooming in/out) in VB? Pin
hyxkona@yahoo.com4-Jan-05 12:56
memberhyxkona@yahoo.com4-Jan-05 12:56 
AnswerRe: How to use it (zooming in/out) in VB? Pin
anonymous17-Jan-05 1:01
memberanonymous17-Jan-05 1:01 
GeneralCheers Pin
nice_boy24730-Dec-04 6:38
membernice_boy24730-Dec-04 6:38 
GeneralHI Pin
MohammadAmiry22-Dec-04 3:41
memberMohammadAmiry22-Dec-04 3:41 
GeneralRe: HI Pin
Anonymous22-Dec-04 20:53
sussAnonymous22-Dec-04 20:53 
GeneralHighlighter issue Pin
sad61t6-Dec-04 1:03
membersad61t6-Dec-04 1:03 
GeneralRe: Highlighter issue Pin
Hossein Khosravi13-Dec-04 20:48
memberHossein Khosravi13-Dec-04 20:48 
Thaks for your attendance,
I will solve this problem very soon.Cool | :cool:
QuestionI am very happy? Pin
Zero_One_ADO6-Oct-04 0:33
memberZero_One_ADO6-Oct-04 0:33 
AnswerRe: I am very happy? Pin
Hossein Khosravi8-Oct-04 20:25
memberHossein Khosravi8-Oct-04 20:25 
AnswerRe: I am very happy? Pin
Anonymous30-Dec-04 2:51
sussAnonymous30-Dec-04 2:51 
GeneralRe: I am very happy? Pin
Neta Zmora30-Dec-04 2:53
sussNeta Zmora30-Dec-04 2:53 
GeneralProblem with smartgraph Pin
bollons17-Sep-04 5:06
memberbollons17-Sep-04 5:06 
GeneralRe: Problem with smartgraph Pin
Hossein Khosravi18-Sep-04 18:53
memberHossein Khosravi18-Sep-04 18:53 
General&amp;#1583;&amp;#1605;&amp;#1578; &amp;#1711;&amp;#1585;&amp;#1605; Pin
vazan11-Sep-04 4:10
membervazan11-Sep-04 4:10 
GeneralRe: &amp;#1583;&amp;#1605;&amp;#1578; &amp;#1711;&amp;#1585;&amp;#1605; Pin
Hossein Khosravi11-Sep-04 4:57
memberHossein Khosravi11-Sep-04 4:57 
Questionwhy not change it into c#? Pin
dxhdxh8-Sep-04 10:11
memberdxhdxh8-Sep-04 10:11 
AnswerRe: why not change it into c#? Pin
Hossein Khosravi8-Sep-04 18:46
memberHossein Khosravi8-Sep-04 18:46 
GeneralExcellent! Pin
A. Riazi1-Sep-04 9:36
memberA. Riazi1-Sep-04 9:36 
GeneralHi please help... Pin
venkyhyd14-Jan-05 16:52
membervenkyhyd14-Jan-05 16:52 

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.

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