Click here to Skip to main content
12,304,652 members (63,925 online)
Click here to Skip to main content
Add your own
alternative version


328 bookmarked

2D Graph ActiveX Control

, 5 Aug 2003 MIT
Rate this:
Please Sign up or sign in to vote.
An ActiveX control for 2D data visualisation

Sample Image - NTGraph_ActiveX.jpg

<STYLE> <!-- { list-style-image: url(NTGraph_ActiveX/prop.gif) } LI.method { list-style-image: url(NTGraph_ActiveX/gmethod.gif) } --> </STYLE>


This is a simple OCX control, which allows you to plot two-dimensional data. Despite the large set of controls that comes with VC++, there is no out-of-the-box control that provides a simple and straightforward 2D data visualization. The ActiveX control tutorial by Kapil Chaturvedi inspired me to write my own control, mostly because I wanted to customize the source code when needed. Over time, the functionality of the ActiveX control became more elaborate, and finally I made decision to publish what I have in hand.

What Can It Do?

The control is able to plot a large number of points and updating one or more plots on the graph with new data, replacing the old plot with the new plot. Multiple plots with individual properties such as name, line and point style, width, could be customized at runtime. At runtime, the control is capable of displaying its own property pages (double click on the control area or by invoking the ShowProperties method) and showing short help as a result of the user pressing F1 while the control has the focus. By setting the TrackMode property you should be able to switch between a different modes such as tracking cursor coordinates while moving (left mouse button pressed), zooming, XY-, X-, and Y-panning. Finally the control snapshot could be copied to the clipboard, printed, or saved as a bitmap file.

What doesn't it do?

You cannot plot 3D data, but you can use the NTGraph3D ATL/STL/OpenGL activeX control to do that Smile | :) !

What's New?

  • The Log Axes Mode works now, showing the log10 grid, and appropriated labels, it also converts the graph element's data
  • The control's snapshot could be now saved as a bitmap file, many thanks to Robert Harber for providing the code!
  • Added abilities to dynamically creating annotation labels, that can be on different colors, orientations, and also could be hidden or visible.
  • Added "Annotations" property page that provides fully access to the annotation list in the real/design mode.
  • Added abilities to dynamically drawing of multiply cursors, with a different colors, crosshair styles, floating/fixed, or snapped to the currently selected element!
  • Added "Cursors" property page that provides fully access to the cursor list in the real/design mode.
  • Added axis formatting, that allows a customization of the bottom and left axis labels.
  • Added "Format" property page that provides access to the axis format properties, and a templates for of commonly used data formats such a: Numbers, Exponential, Symbolic, Date, and Time!
  • Added Time format for the graph axes. To use it, you should set the XTime/YTime property to True.
    You also have to convert the date/time data to double format. The Date/Time format is implemented as a as a floating-point value, measuring days from midnight, 30 December 1899. So, midnight, 31 December 1899 is represented by 1.0. Similarly, 6 AM, 1 January 1900 is represented by 2.25, and midnight, 29 December 1899 is 1.0. However, 6 AM, 29 December 1899 is 1.25.
    For more info refer to MSDN for the class:COleDateTime!

How to test the control

You can use the ActiveX Control Test Container, and load the Test.dsm macro from the menu Tools\Macros... You can write your own routines to test the control behavior (look at Test.dsm macro)

How to use the control

To use this OCX control, embed it in an application that supports the use of OCX controls. Microsoft Visual Basic applications, Office applications and applications created with the Microsoft Developer Studio’s AppWizard can support the use of OCX controls. There are two files required to use this control. They are:
  • NTGraph.hlp -The help file for this control.
  • NTGraph.ocx -The NTGraph controls code and data.

Before the ActiveX control can be used in your application, it must be registered as a COM Component in the system registry. This is a self registering control. This means that to register the control in the system registry you only need to have an application load the control and call the control’s exported function DllRegisterServer. You can use the REGSVR32 utility or have your setup program do this.

How to use the REGSVR32 utility?

Copy NTGraph.ocx to your directory and type:

regsvr32 NTGraph.ocx
regsvr32 /u NTGraph.ocx (Unregister server)

Customizing The Control

You can change the properties of this control during design time, or in run time to affect how the control will plot the data.
Use the new control property pages:

Graph Property Page

Graph Property Page

Elements Property Page

Elements Property Page

Annotations Property Page

Annotations Property Page

Cursors Property Page

Cursors Property Page

Format Property Page

Cursors Property Page

You can include the control in your project by following the standard steps for ActiveX controls:

  1. Create MFC Dialog project or MDI/SDI project with View class derived from CFormView
  2. Choose menu Project|Add To Project|Components and Controls...
  3. Open the Registered ActiveX Control gallery
  4. Choose the NTGraph Control and click Insert
  5. Visual C++ will generate the class CNTGraph
  6. Then you can define variable of the type as CNTGraph.

The control's customization options are straightforward:

// Customize Graph Properties 

m_Graph.SetBackColor (RGB(0,0,0));
m_Graph.SetAxisColor (RGB(0,255,0));
m_Graph.SetLabelColor (RGB(128,255,255));

// Control's Frame and Plot area options

m_Graph.SetFrameStyle(2) // (1) - Flat
                         // (2) - Scope (raised frame and sunken plot area borders)
                         // (3) - 3DFrame (a bitmap frame picture)
m_Graph.SetShowGrid (TRUE);

m_Graph.SetCursorColor (RGB(255,0,0));
m_Graph.SetTrackMode (1);

m_Graph.SetGraphTitle("XY Plot");
m_Graph.SetXLabel ("X Axis");
m_Graph.SetYLabel("Y Axis");

You don't need to call the control Invalidate() function every time you change a Graph property. The changes are automatically reflected on the control appearance.

To load the data into the control...

// Customize Graph Elements 
   // The Graph elements are dynamically allocated! 
   // Element 0 is allocated by default
   // Even after a call to the ClearGraph method,
   // the Element-0  is automaticaly allocated.


   // Allocate a new element: Element-1
   m_Graph.SetElementColor (RGB(0,255,0));
   // Allocate a new element: Element-2

   m_Graph.SetElementColor (RGB(0,0,255));

  // Now change again the properties of Element-1
  m_Graph.SetElementColor (RGB(0,0,255)); 

// Load Data int the Graph Elements 

  double y;
  for (int i = 0; i < NumberOfElements; i++) 
 	for (int x = 0; x < NumberOfPoints; x++) 
	   y = (double)rand() / RAND_MAX * 10.0;
	   y = y / 3 + 10.0 / 2 * i + 1;
           m_Graph.PlotXY(x, y, i); 
           // or PlotY(double data, long ElementID) 

The same story for Visual Basic Users:

With NTGraph1
     .PlotAreaColor = vbBlack
     .FrameStyle = Frame
     .Caption = ""
     .XLabel = ""
     .YLabel = ""

     .ClearGraph 'delete all elements and create a new one
     .ElementLineColor = RGB(255, 255, 0)
     .AddElement  ' Add second elements
     .ElementLineColor = vbGreen

     For X = 0 To 100
          Y = Sin(X / 3.15) * Rnd - 1
          .PlotY Y, 0
           Y = Cos(X / 3.15) * Rnd + 1
          .PlotXY X, Y, 1
          .SetRange 0, 100, -3, 3
      Next X
End With

NTGraph Properties:


  •  short Appearance
  •  BSTR Caption
  •  short Appearance
  •  BSTR Caption
  •  BSTR XLabel
  •  BSTR YLabel
  •  OLE_COLOR ControlFrameColor
  •  OLE_COLOR PlotAreaColor
  •  OLE_COLOR AxisColor
  •  OLE_COLOR GridColor
  •  OLE_COLOR LabelColor
  •  OLE_COLOR CursorColor
  •  IPictureDisp* ControlFramePicture
  •  IPictureDisp* PlotAreaPicture
  •  IFontDisp*LabelFont
  •  IFontDisp* TickFont
  •  IFontDisp* TitleFont
  •  IFontDisp* IdentFont
  •  FrameType FrameStyle
  •  short XGridNumber
  •  short YGridNumber
  •  boolean ShowGrid
  •  boolean XLog
  •  boolean YLog
  •  double XCursor
  •  double YCursor


  •  short Element
  •  short ElementCount
  •  OLE_COLOR ElementLineColor
  •  OLE_COLOR ElementPointColor
  •  LineType ElementLinetype
  •  short ElementWidth
  •  SymbolType ElementPointSymbol
  •  boolean ElementSolidPoint
  •  boolean ElementShow
  •  TrackModeState TrackMode
  •  BSTR ElementName
  •  boolean ElementIdent


  •  short Annotation
  •  short AnnoCount
  •  BSTR AnnoLabelCaption
  •  double AnnoLabelX
  •  double AnnoLabelY
  •  OLE_COLOR AnnoLabelColor
  •  boolean AnnoLabelHorizontal
  •  boolean AnnoVisible


  •  short Cursor
  •  short CursorCount
  •  short CursorMode (0 - Fixed; 1 - Floating; 2 - Snapped to currentlly selected element)
  •  double CursorX
  •  double CursorY
  •  OLE_COLOR CursorColor
  •  short CursorStyle (0 - Crosshair; 1 - X hairline only; 2 - Y hairline only;)
  •  boolean CursorVisible


  •  boolean XTime
  •  boolean YTime
  •  BSTR FormatAxisBottom
  •  BSTR FormatAxisLeft



  •  void SetRange(double xmin, double xmax, double ymin, double ymax)
  •  void AutoRange()
  •  void CopyToClipboard()
  •  void PrintGraph()
  •  void ShowProperties()


  •  void AddElement()
  •  void DeleteElement(short ElementID)
  •  void ClearGraph()
  •  double GetElementXValue(short index, short ElementID)
  •  void SetElementXValue(short index, short ElementID, double newValue)
  •  double GetElementYValue(short index, short ElementID)
  •  void SetElementYValue(short index, short ElementID, double newValue)
  •  void PlotXY(double X, double Y, short ElementID)
  •  void PlotY(double Y, short ElementID)


  •  void AddAnnotation()
  •  void DeleteAnnotation(short AnnotationID)


  •  void AddCursor()
  •  void DeleteCursor(short CursorID)

Tracking Mode constants

  • None = 0
  • Track = 1 Track cursor position (hold mouse button pressed)
  • Cursor = 2 Cursor position by single click
  • Zoom = 3 Unzoom (right mouse button click)
  • PanXY = 4
  • PanX = 5
  • PanY = 6

Frame Style Constants

  • Flat = 0
  • Scope = 1 (raised frame and sunken plot area borders)
  • 3DFrame = 2 (a bitmap frame picture)

Line style constants

  • Solid = 0
  • Dash = 1
  • Dot = 2
  • DashDot = 3
  • DashDotDot = 4
  • Null = 5
  • XYStep = 6
  • YXStep = 7
  • Bars = 8
  • Stick = 9

Symbol style constants

  • Nosym = 0
  • Dots = 1
  • Rectangles = 2
  • Diamonds = 3
  • Asterisk = 4
  • DownTriangles = 5
  • UpTriangles = 6
  • LeftTriangles = 7
  • RightTriangles = 8

Yep, that's it!


Send mail to with questions or comments about this article.


22 Nov 2002 - v1.0 Initial release

01 Dec 2002 - v1.1

  • Added new method copy2clipboard.
  • Added the ability to draw elements with a different number of points. (by A.Hoffman)
  • New method added by A.Hoffman to Show/Hide the Graph Element
  • Bug fix. Thanks to A.Hofmann for help.
  • Fixed some drawing problems. Thanks to Judd.
  • Added custom font support.
  • Zoom Mode: Not implemented yet, but reserved.

26 Jan 2003 - v2.0 (Flicker Free versiton of the control)

  • Thanks to Keith Rule for the class CMemDC
  • ZoomMode Implemented.
  • Added tooltip, showing current cursor position.
  • Added new method Autorange.
  • Added new property ElementLinewidth.
  • Added new property ElementLinetype.
  • Fixed some drawing problems.
  • Added (Test.htm) a brief info on how to add the control to your web page.

09 Mar 2003 - v2.1

  • PanMode Implemented.
  • Modified SetElementColor, SetElementLinewidth, SetElementLinetype, so that they accept as a first parameter the ElementID.
  • Fixed some drawing problems. Should be clean now.
  • GDI leak Fixed.

01 Jun 2003 - v3.0 New release!

02 Aug 2003 - v4.0 Final release!

  • Thanks to Robert Harber for the useful discussions, ideas and code.
  • Thanks to tagi1 for fixing the printing font problem.
  • Thanks to Judd for testing the control.

Note that since there are significant changes in the last release you should remove (first unregister and than delete) all old versions of the control from your projects! The VBA users who use the control inside of Office applications should also remove the following file NTGRAPHLib.exd in the Temp directory of your computer. This file is automatically created by the Office application and saves properties of the former created control instances. You have to delete this file before you insert the new version of the control, in order to get correctly names in the property browser.


This article, along with any associated source code and files, is licensed under The MIT License


About the Author

Nikolai Teofilov
Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

Questionhi Pin
HaflBit3-Aug-11 20:15
memberHaflBit3-Aug-11 20:15 
GeneralMy vote of 1 Pin
ibyk3015-Jun-11 14:23
memberibyk3015-Jun-11 14:23 
GeneralRe: My vote of 1 Pin
K ARUN KUMAR3-Feb-12 23:30
memberK ARUN KUMAR3-Feb-12 23:30 
GeneralHow can i hide a trace (element of the graph)??? [modified] Pin
boris724-May-11 23:32
memberboris724-May-11 23:32 
GeneralAbout licence ... Pin
R. Choudhury24-Mar-11 19:34
memberR. Choudhury24-Mar-11 19:34 
GeneralCant install this on VS2010 Ultimate 94 bit Pin
Red&Black9-Mar-11 9:03
memberRed&Black9-Mar-11 9:03 
GeneralCannot implement this control in VS2005 Pin
Morten Dramstad2-Jan-11 22:17
memberMorten Dramstad2-Jan-11 22:17 
GeneralRe: Cannot implement this control in VS2005 Pin
Member 333157027-Apr-11 23:10
memberMember 333157027-Apr-11 23:10 
GeneralRe: Cannot implement this control in VS2005 Pin
Member 1012805526-Jun-13 7:50
memberMember 1012805526-Jun-13 7:50 
QuestionHow to track the data at real time Pin
xuebond17-Sep-10 18:17
memberxuebond17-Sep-10 18:17 
GeneralProblem of YLog for small values Pin
Fuzzier2-Sep-10 22:10
memberFuzzier2-Sep-10 22:10 
GeneralPlotAreaPicture Pin
mg658123-Aug-10 3:06
membermg658123-Aug-10 3:06 
GeneralRe-size Fonts Pin
dswansonma2-Aug-10 6:30
memberdswansonma2-Aug-10 6:30 
GeneralMy vote of 5 Pin
guetpeek23-Jul-10 16:42
memberguetpeek23-Jul-10 16:42 
QuestionControl ProperyPage Pin
Member 386989117-Jul-10 5:30
memberMember 386989117-Jul-10 5:30 
AnswerRe: Control ProperyPage Pin
dswansonma2-Aug-10 6:31
memberdswansonma2-Aug-10 6:31 
QuestionRe: Control ProperyPage Pin
Member 38698912-Aug-10 11:32
memberMember 38698912-Aug-10 11:32 
AnswerRe: Control ProperyPage Pin
yangjsh12-Nov-10 19:01
memberyangjsh12-Nov-10 19:01 
GeneralRe: Control ProperyPage Pin
Member 386989112-Nov-10 23:51
memberMember 386989112-Nov-10 23:51 
QuestionHow to use the REGSVR32 utility on 64 bit System. Pin
Divyang Mithaiwala12-Jul-10 20:28
memberDivyang Mithaiwala12-Jul-10 20:28 
QuestionProblem with NTGraph Pin
leixint7-Jul-10 16:17
memberleixint7-Jul-10 16:17 
QuestionHow to select portion of graph in drawn full graph? Pin
Member 42014258-Jun-10 20:50
memberMember 42014258-Jun-10 20:50 
Question2 Plots with two XY axes? Pin
luheng7-Jun-10 21:31
memberluheng7-Jun-10 21:31 
QuestionHow do I display ElementName in the different position of PlotArea? Pin
libing022412-Apr-10 18:23
memberlibing022412-Apr-10 18:23 
GeneralHide an element Pin
julgood4-Mar-10 23:13
memberjulgood4-Mar-10 23:13 

I want to hide an element of the graph in function of its elementID parameter,

How should I do?
GeneralPLEASE HELP ME! Problems with regsvr32 ntgraph.ocx with windows XP Pin
boris723-Mar-10 10:15
memberboris723-Mar-10 10:15 
GeneralRe: PLEASE HELP ME! Problems with regsvr32 ntgraph.ocx with windows XP Pin
Carcan29-Mar-10 22:15
memberCarcan29-Mar-10 22:15 
GeneralProblems with managed c++ Pin
juanvo8-Feb-10 11:05
memberjuanvo8-Feb-10 11:05 
QuestionAbout registering activex Pin
swapnilb131-Jul-09 1:16
groupswapnilb131-Jul-09 1:16 
GeneralQuestion about the license ... Pin
sayed_716-Jun-09 4:44
membersayed_716-Jun-09 4:44 
GeneralActive X Control insert Problem Pin
supersuakii15-Jun-09 1:18
membersupersuakii15-Jun-09 1:18 
GeneralTotally useless... Pin
kilt1-Jun-09 1:24
memberkilt1-Jun-09 1:24 
GeneralRe: Totally useless... Pin
DavidCrow14-Sep-09 4:20
mvpDavidCrow14-Sep-09 4:20 
GeneralRe: Totally useless... Pin
Nuri Ismail14-Sep-09 4:24
memberNuri Ismail14-Sep-09 4:24 
GeneralRe: Horsehocky Pin
Member 21454371-Dec-09 11:12
memberMember 21454371-Dec-09 11:12 
GeneralRe: Totally useless... Pin
rjgarnett20-Feb-10 20:37
memberrjgarnett20-Feb-10 20:37 
Questionsave graph!!!! Pin
whynot12345620-May-09 22:25
memberwhynot12345620-May-09 22:25 
AnswerRe: save graph!!!! Pin
Member 21454371-Dec-09 11:19
memberMember 21454371-Dec-09 11:19 
Questionhow to make a graph real time? Pin
whynot12345618-May-09 2:59
memberwhynot12345618-May-09 2:59 
Generallog plot help please! Pin
Rohit11317-May-09 20:04
memberRohit11317-May-09 20:04 
GeneralLicense Pin
clintM22-Apr-09 2:50
memberclintM22-Apr-09 2:50 
Generalvery good Pin
leihuajun200897-Apr-09 23:12
memberleihuajun200897-Apr-09 23:12 
QuestionUsing .csv Data Files Pin
heron200219-Mar-09 23:44
memberheron200219-Mar-09 23:44 
AnswerRe: Using .csv Data Files Pin
heron200220-Mar-09 4:22
memberheron200220-Mar-09 4:22 
General3D cone Pin
dipurb6-Feb-09 0:33
memberdipurb6-Feb-09 0:33 
General3D cone Pin
dipurb6-Feb-09 0:32
memberdipurb6-Feb-09 0:32 
QuestionProblems to start the demo under VS2005 Pin
Dieter6821-Jan-09 10:33
memberDieter6821-Jan-09 10:33 
GeneralAnnotation Problem Pin
danjswift9-Nov-08 8:30
memberdanjswift9-Nov-08 8:30 
AnswerRe: Annotation Problem Pin
georgeo11-Nov-08 23:05
membergeorgeo11-Nov-08 23:05 
GeneralRe: Annotation Problem Pin
danjswift26-Dec-08 6:50
memberdanjswift26-Dec-08 6: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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160530.1 | Last Updated 6 Aug 2003
Article Copyright 2002 by Nikolai Teofilov
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid