Academic Company Events NI Developer Zone Support Solutions Products & Services Contact NI MyNI
What is Developer Zone?
United States

Document TypeTutorial
NI Supported: Yes
Publish Date: Sep 10, 2009


Feedback


Yes No

Related Categories

Related Links - Developer Zone

Related Links -Products and Services

Creating a Custom Cursor for Measurement Studio Windows Forms Scatter and Waveform Graphs

0 Ratings | 0.00 out of 5
 Print |  PDF

Overview

With the Measurement Studio scatter and waveform graph .NET controls, you can easily leverage custom drawing services and helper methods to extend the functionality of the cursor. The XYPlot base class provides several members to help you create custom cursors, including the following events:

  • AfterDraw event—Occurs immediately after the cursor is drawn. You typically handle this event if you want to augment the default visual representation of the cursor with additional custom drawing. For more information, refer to Augmenting the Default Drawing of the Cursor.
  • BeforeDraw event—Occurs immediately before the cursor is drawn. You typically handle this event if you want to completely handle the way that the cursor is drawn. If you completely handle the implementation for drawing the cursor, you can set the Cancel property of the event arguments to true to indicate that you have completely handled the implementation and that the cursor should not perform any additional drawing. For more information, refer to Completely Implementing the Cursor Drawing.

Note: It is not always necessary to use the BeforeDraw and AfterDraw events to extend the functionality of the cursor. You can customize the PointStyle of the cursor and the crosshair LineStyle without using these events. For more information, refer to Creating a Custom Line Style for the Measurement Studio Windows Forms Graph .NET Controls in the NI Measurement Studio Help.

Augmenting the Default Drawing of the Cursor

The attached example creates a DisplayPositionCursor class, which derives from XYCursor. This class overrides the protected virtual OnAfterDraw method, which the AfterDraw event is raised through, instead of registering for the event. Overriding this method has the advantage of encapsulating the implementation. This example uses the AfterDraw event to demonstrate how to do additional drawing on a cursor that is already drawn.

The next example creates an instance of the DisplayPositionCursor class, assigns it a plot, and adds it to the graph Cursors collection:

VB.NET

Dim data As Double() = BasicFunctionGenerator.GenerateSineWave(1.0, 1.0, 0.0, 0.0, 100.0, 100)
WaveformGraph1.PlotY(data)

Dim plot As WaveformPlot = WaveformGraph1.Plots(0)
Dim positionCursor As DisplayPositionCursor = New DisplayPositionCursor(plot)
WaveformGraph1.Cursors.Add(positionCursor)
positionCursor.XPosition = 10.0

C#

double[] data = BasicFunctionGenerator.GenerateSineWave(1.0, 1.0, 0.0, 0.0, 100.0, 100);
waveformGraph1.PlotY(data);

WaveformPlot plot = waveformGraph1.Plots[0];
DisplayPositionCursor positionCursor = new DisplayPositionCursor(plot);
waveformGraph1.Cursors.Add(positionCursor);
positionCursor.XPosition = 10.0;

The following image shows the plotted data on waveformPlot1:

As you move the cursor on the plot, the cursor position updates because AfterDraw is called each time the cursor draws. You can use DisplayPositionCursor with the scatter and waveform graphs.

Completely Implementing the Cursor Drawing

The attached example creates a DoubleLineCursor class, which derives from XYCursor. This class overrides the protected virtual OnBeforeDraw method, which the BeforeDraw event is raised through, instead of registering for the event. Overriding this method has the advantage of encapsulating the implementation. This example uses BeforeDraw to demonstrate how to take full control of drawing the cursor by canceling the event.

The next example creates an instance of the DoubleLineCursor class, assigns it a plot, and adds it to the graph Cursors collection.

VB.NET

Dim data As Double() = BasicFunctionGenerator.GenerateSineWave(1.0, 1.0, 0.0, 0.0, 100.0, 100)
WaveformGraph1.PlotY(data)

Dim plot As WaveformPlot = WaveformGraph1.Plots(0)
Dim doubleLineCursor As DoubleLineCursor = New DoubleLineCursor(plot)
WaveformGraph1.Cursors.Add(doubleLineCursor)
doubleLineCursor.XPosition = 10.0

 

C#

double[] data = BasicFunctionGenerator.GenerateSineWave(1.0, 1.0, 0.0, 0.0, 100.0, 100);
waveformGraph1.PlotY(data);

WaveformPlot plot = waveformGraph1.Plots[0];
DoubleLineCursor doubleLineCursor = new DoubleLineCursor(plot);
waveformGraph1.Cursors.Add(doubleLineCursor);
doubleLineCursor.XPosition = 10.0;

The following image shows DoubleLineCursor:

 

 

 

 

 


Reader Comments | Submit a comment »

 

Legal
This tutorial (this "tutorial") was developed by National Instruments ("NI"). Although technical support of this tutorial may be made available by National Instruments, the content in this tutorial may not be completely tested and verified, and NI does not guarantee its quality in any way or that NI will continue to support this content with each new revision of related products and drivers. THIS TUTORIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND AND SUBJECT TO CERTAIN RESTRICTIONS AS MORE SPECIFICALLY SET FORTH IN NI.COM'S TERMS OF USE (http://ni.com/legal/termsofuse/unitedstates/us/).