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

2D Graph ActiveX Control

By , 5 Aug 2003
 

Sample Image - NTGraph_ActiveX.jpg

Introduction

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

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.SetFrameColor((RGB(0,0,0));
m_Graph.SetPlotAreaColor(RGB(212,222,200));

m_Graph.SetFrameStyle(2) // (1) - Flat
                         // (2) - Scope (raised frame and sunken plot area borders)
                         // (3) - 3DFrame (a bitmap frame picture)
    
m_Graph.SetGridColor(RGB(192,192,192));
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");
m_Graph.SetRange(0.,10,-1,1.);

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.
  
   m_Graph.SetElementLineColor(RGB(255,0,0));
   m_Graph.SetElementLinetype(0);
   m_Graph.SetElementWidth(1);

   m_Graph.SetElementPointColor(RGB(0,0,255);
   m_Graph.SetElementPointSymbol(3);
   m_Graph.SetElementSolidPoint(TRUE);
	

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

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

  // Now change again the properties of Element-1
  m_Graph.SetElement(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:

    Graph

  •  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

    Elements

  •  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

    Annotations

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

    Cursors

  •  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

    Format

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

Methods

    Graph

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

    Elements

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

    Annotations

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

    Cursors

  •  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!

Enjoy!

Send mail to nteofilov@yahoo.de with questions or comments about this article.

History

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Nikolai Teofilov
Researcher
Germany Germany
Member
No Biography provided

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   
Questionpossible to add a Costum point ?memberAlina K6 Feb '13 - 3:04 
Hi, Is there a way to add a custom point (shape cross / diamond ect'..) not on the existing plot. I am playing with the control and it seems that i cam add only plots or whole lines (part of add Cursors)
 
Thank you,
Alina
QuestionConverting Cursor Coordinates to Plot CoordinatesmemberSandthief12 Nov '12 - 10:58 
I'm trying to recover the plot's XY values shown in the tooltip that appears while tracking the cursor position. I've tried to use the GetCursor functions, but those return the graph values, not the actual plot values.
 
I've seen this GetElementXValue function, but I don't know how to generate the index from the data I've got.
 
Any suggestions?
 
Best regards.
QuestionCrash on multi core cpu computermemberlicheer10 Oct '12 - 5:39 
When my program using ntgraph.ocx runs on multi core cpu computer, somethings, the program crash.
I trace the source code, the memory read/write error happened in PlotElementLines().
How to solve it, should I provide more detail informations?
Please help me, thanks.
Questioncan not using in Win 64 bit ???membershizuoka201018 Sep '12 - 19:40 
I using VS2005 and Win 7 ( 64 bit ). So, i can not install with the command regsvr32 NTGraph.ocx
How i can registry in Win7 64bit ???
Thanks for read !!!!
AnswerRe: can not using in Win 64 bit ???memberNenad Medić14 Nov '12 - 7:02 
I am using Win 7 64 bit with VS2012 and this control. Only thing that isn't working is help file.
 
1. You must copy this NTGraph.ocx to windows\sysWOW64 dir.
2. After that you must register ocx from cmd window. To do this click on start button and type cmd, after that click with right button on cmd.exe and choose "Run as administrator" and run cmd as administrator. Change directory to windows\sysWOW64 and type regsvr32 NTGraph.ocx. press enter and ocx should register in your system.
That is all. Enyoj!
QuestionCPU usagememberrealtimedev28 Aug '12 - 10:20 
Hi,
 
I am using the graphing tool during the day. I plot once a second and continue throughout the day. By the end of the day the CPU on my box shows the graph using 30%. If I clear the graph it goes do 1% and then starts to grow again.
 
I dont want to clear the graph. What is going on that is using up the cpu?
 
Any ideas would be appreciated. Smile | :)
GeneralVS 2010???memberboblogan15 Jul '12 - 21:45 
I am porting an application from vs2005 to vs2010 that uses this control. Attempts to Register this on a windows 7 machine produces the following: The module "ntgraph.ocx" was loaded but the call to Dll RegisterServer failed with error code 0x80040200.
Is there a trick or previous report on how to get this registered on 64 bit OS?
 
Oh.. that is a manual register for now on the development machine....
Thanks
 
OH I GUESS I NEED SLEEP - Running as Admin did the trick..
BugBug Found - m_nCursorCountmemberwdolson15 Jun '12 - 15:13 
I just discovered it is not initialized. Set to zero in the constructor.
 
Bill
QuestionHow can I add my code to the .ocx file?memberanly8724 May '12 - 20:20 
I want to change some parameters, such as the coordinates
AnswerRe: How can I add my code to the .ocx file?memberwdolson15 Jun '12 - 15:09 
The source code is provided. Make your changes and recompile.
QuestionCan I touch the plot with the mouse and get the (x,y) coordinates of that point?memberFarzanehS6 May '12 - 21:40 
Thank for the practical program, but I have a problem with it,
Can I touch the plot with the mouse and get the (x,y) coordinates of that point?
Also I want to change cursor shape on the plot, what should I do?
would you please help me?
AnswerRe: Can I touch the plot with the mouse and get the (x,y) coordinates of that point?memberwdolson8 May '12 - 23:28 
If the track mode is set to Track, clicking on the graph will pop up a tooltip type box with the X,y coordinate. In my application I have code to get the X,Y value which I display on a side bar separate from the graph. In my form view I get the X and Y cursor location in OnLButtonDown if the click was in the graph area and display that on the side bar.
 
Do you want to change the mouse cursor when you are hovering over the graph area, or when you do something on the graph?
GeneralRe: Can I touch the plot with the mouse and get the (x,y) coordinates of that point?memberFarzanehS8 May '12 - 23:48 
I dont know how to change VC++ program, I used VB .net. and I want to disable Drag option for changing cursor line position and also I want to get x and y coordination in VB. what should I do?
yes I want to change the mouse cursor when user select track mode to zoom or pan or trak and is hovering over the graph.
GeneralRe: Can I touch the plot with the mouse and get the (x,y) coordinates of that point?memberwdolson9 May '12 - 0:00 
If you don't know how to edit the graph code itself you are probably out of luck disabling any features.
 
I only know a little VB, but in my program I have the following calls to get the cursor position:
 
x = m_Graph.GetCursorX();
y = m_Graph.GetCursorY();
 
The syntax is probably close in VB.
 
To change the cursor when hovering, you'll have to figure out what the equivalent functions are in VB to determine when the mouse is over the graph control, then call the handler function for mouse hover, and within that function change the cursor. Have the mouse hover function set the cursor to normal if the mouse is over anything else.
 
I don't know enough about VB to tell you the specifics. I've worked a little with VB6, but mostly I was hacking my way through because I had to (I got them to hire a VB programmer to do things right and free me up to work on the C and C++ parts of the project).
GeneralRe: Can I touch the plot with the mouse and get the (x,y) coordinates of that point?memberFarzanehS9 May '12 - 0:04 
Would you please tell me, how can I changing VC++ code and create a modified Activex?
GeneralRe: Can I touch the plot with the mouse and get the (x,y) coordinates of that point?memberwdolson9 May '12 - 14:22 
I can't give you a detailed step by step cookbook, but if you look at OnMouseMove in NTGraphCtl.cpp, you can comment out the calls to DoPan. The user will still be able to select pan mode, but it won't do anything.
 
Disabling the selections would be a bit more in depth.
 
Bill
QuestionNegative valuesmemberMember 773975323 Apr '12 - 10:16 
Hello, when using the bar line style, bars are not drawn from zero, but from the bottom of the graph. Since my y axis ranges +/- some range, it is very confusing. Is there a way to fix this?
QuestionWindows 7 supportmemberMundu18 Mar '12 - 16:53 
Hi does this control work on Windows 7 enterprise edition
 
thanks
Questionhow use different ElementLineColor in the same Graph?memberwudi108612 Mar '12 - 17:28 
I mean that how can i use different lineColor in a wave.
i use SetElementLineColor to change the colour,but the colour of wave is all change .
Smile | :) I only want to change that point not all point. Smile | :)
 
Thanks and warm Regards
QuestionHow to use xtime?memberhakha43 Feb '12 - 5:38 
Hi
Found this excellent component searching for a plotting tool but I'd like a better help file how to use properties. Using it with VB6. I'm logging temp data and it works just fine but I want to show real time (HH:MM:SS)in x-axis for each measurement. Now I'm using VB function Timer for displaying time but it's not that descriptive. Can anyone give me a hint how to use xtime property and how to set range according to time
Any help appreciated
Regards Hucke
QuestionScale troubles [modified]memberDmitry240327 Nov '11 - 19:29 
Here is a some strange troubles wiith "PanX" or "PanXY" scale. When i try to move my graph at VB6 in any axis its (graph) is become lost its oniginal shape. For example: i have a line started from 0,0 coordinates, and when a become to move line by "PanX" to the left or right (nevermind) a degrees between any axis and line become different than at original state. And line otherwise is started from 0,0. So when i drag a line one of the end always is glued to Y (if i drag by "PanX") Sigh | :sigh: Sigh | :sigh: Sigh | :sigh:
 
Have some body any ideas or solutions how to fix this bug?
P.S. sorry for my eng. i`m from Russia

modified 28 Nov '11 - 2:23.

QuestionUse NTGraph as a resizeable control in the CTabCtrl-class objectmemberMember 789153518 Sep '11 - 23:34 
Hi,
 
I got some problem when I tried to sue NTGraph as a control inside a CTabCtrl object.
Basically I am having a Tab control, and inside the Tab control I have different kind of control. For example:
For tab 1, I have NTGraph (Active X control); while for tab 2, I have List Control.
 
I am also using DlgResizeHelper class, available from:  
http://www.codeguru.com/cpp/w-d/dislog/resizabledialogs/article.php/c1913/
 
For some reason, I can't get the NTGraph Control resized properly (the other tab component: List Control, and it can be resized fine).
 
I have also tried using NTGraph Control as a stand-alone (NOT as a component in a Tab Control) and it works fine.
 
To be more specific about the problem, using the DlgResizeHelper I should be able to resize all the components inside my dialog window. I am able to resize the List Control (as an element in second Tab of my TabCtrl), however, I am NOT able to resize the NTGraph Control (as an element in the first Tab of my TabCtrl). It always gives me the original size of NTGraph Control.
 
Is there anyone out there who has experience similar to what I'm facing now or any idea that might enlighten me on this problem?
 
Thanks and best regards,
Willie
QuestionUse on a webpage - or rather, in a WebBrowser controlmemberwoo woo19 Aug '11 - 8:16 
Can this be used on a web page?
 
i'm using vb.net to programatically fill a page in a webbrowser control with various stats from existing calculated data. how would i go about assigning all the data to it.. presumably something like...
 
<OBJECT ID="MyObject1" CLASSID="CLSID:ID HERE">
<param name="foo" value="bar"
...
...
</OBJECT>
 
?
 
thanks,
 
chris
QuestionhimemberHaflBit3 Aug '11 - 20:15 
The component is very good.
Now I have a problem:
I want to use it in the MFC APP directly without the layer of "ActiveX",how can I do that?
Do you have the source that use the control directly under MFC App?
GeneralMy vote of 1memberibyk3015 Jun '11 - 14:23 
does not work (tested on several machines running WinXP)

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

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