Click here to Skip to main content
15,498,622 members
Articles / Desktop Programming / ATL
Posted 31 Aug 2004


166 bookmarked

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

Written By
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 , Islamic Quotes

Company Site: Shahaab-co
My old Site: Farsi OCR

Comments and Discussions

Questionwindows 10 Pin
Bahar Shahsavarani15-Aug-16 11:10
MemberBahar Shahsavarani15-Aug-16 11:10 
AnswerRe: windows 10 Pin
Bahar Shahsavarani17-Aug-16 14:37
MemberBahar Shahsavarani17-Aug-16 14:37 
PraiseThank you! Pin
Bahar Shahsavarani29-Jul-16 7:43
MemberBahar Shahsavarani29-Jul-16 7:43 
GeneralRe: Thank you! Pin
Hossein Khosravi30-Jul-16 20:11
MemberHossein Khosravi30-Jul-16 20:11 
GeneralMy vote of 5 Pin
Member 967370511-Dec-12 13:32
MemberMember 967370511-Dec-12 13:32 
QuestionCan you update it to WindowsXP-64-bit or windows server 2003 - 64bit? Pin
rockcracks24-Jun-08 0:50
Memberrockcracks24-Jun-08 0:50 
AnswerRe: Can you update it to WindowsXP-64-bit or windows server 2003 - 64bit? Pin
Hossein Khosravi24-Jun-08 21:36
MemberHossein Khosravi24-Jun-08 21:36 
GeneralRe: Can you update it to WindowsXP-64-bit or windows server 2003 - 64bit? Pin
Saroj Acharya7-Dec-11 9:26
MemberSaroj Acharya7-Dec-11 9:26 
GeneralC# Implementation Pin
worked on mine9-Oct-07 5:15
Memberworked on mine9-Oct-07 5:15 
GeneralHelp me.. Pin
Choi Sung Dae18-Sep-07 21:28
MemberChoi Sung Dae18-Sep-07 21:28 
GeneralRe: Help me.. Pin
Hossein Khosravi18-Sep-07 22:50
MemberHossein Khosravi18-Sep-07 22:50 
GeneralRe: Help me.. Pin
Choi Sung Dae20-Sep-07 16:08
MemberChoi Sung Dae20-Sep-07 16:08 
GeneralRe: Help me.. Pin
Choi Sung Dae20-Sep-07 22:34
MemberChoi Sung Dae20-Sep-07 22:34 
GeneralVisual studio 2005 Pin
kspalj14-Sep-07 0:14
Memberkspalj14-Sep-07 0:14 
GeneralRe: Visual studio 2005 Pin
Hossein Khosravi14-Sep-07 22:07
MemberHossein Khosravi14-Sep-07 22:07 
GeneralTruely Nice Work! Pin
OxfoX31-Jul-07 4:32
MemberOxfoX31-Jul-07 4:32 
GeneralUsing the SmartGraph control in a property page Pin
tufkap19-Jul-07 7:50
Membertufkap19-Jul-07 7:50 
GeneralRe: Using the SmartGraph control in a property page Pin
Hossein Khosravi21-Jul-07 1:33
MemberHossein Khosravi21-Jul-07 1:33 
GeneralRe: Using the SmartGraph control in a property page Pin
tufkap23-Jul-07 0:22
Membertufkap23-Jul-07 0:22 
GeneralRe: Using the SmartGraph control in a property page Pin
tufkap24-Jul-07 7:39
Membertufkap24-Jul-07 7:39 
GeneralRe: Using the SmartGraph control in a property page Pin
Hossein Khosravi24-Jul-07 19:42
MemberHossein Khosravi24-Jul-07 19:42 
QuestionactiveX registrartion Pin
mickael_biardeau20-Jun-07 3:32
Membermickael_biardeau20-Jun-07 3:32 
AnswerRe: activeX registrartion Pin
Hossein Khosravi20-Jun-07 4:12
MemberHossein Khosravi20-Jun-07 4:12 
General'double %' Pin
SnowstarTeam17-May-07 14:01
MemberSnowstarTeam17-May-07 14:01 
GeneralRe: 'double %' Pin
Hossein Khosravi17-May-07 22:12
MemberHossein Khosravi17-May-07 22:12 

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.