分类: C/C++
2008-09-14 19:52:37
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 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.
XTime/YTime
property to True
. 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.COleDateTime
! 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.
Copy NTGraph.ocx to your directory and type:
regsvr32 NTGraph.ocx
regsvr32 /u NTGraph.ocx
(Unregister server)
You can include the control in your project by following the standard steps for ActiveX controls:
CFormView
NTGraph
Control and click Insert
CNTGraph
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)
}
}
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
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)
Enjoy!
Send mail to with questions or comments about this article.
22 Nov 2002 - v1.0 Initial release
01 Dec 2002 - v1.1
26 Jan 2003 - v2.0 (Flicker Free versiton of the control)
Autorange
.
ElementLinewidth
.
ElementLinetype
.
09 Mar 2003 - v2.1
SetElementColor
, SetElementLinewidth
, SetElementLinetype
, so that they accept as a first parameter the ElementID.
01 Jun 2003 - v3.0 New release!
02 Aug 2003 - v4.0 Final release!
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.