using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Timers; using GenLogic; namespace RealTimeChartSimple { public partial class GlgRTChartForm : Form { public GlgChart glg_chart; int UpdateInterval = 100; public GlgRTChartForm() { InitializeComponent(); // Assign GLG drawing name to be displayed in the GLG control. String drawing_name = "chart2.g"; String drawing_file = Path.Combine( Application.StartupPath, drawing_name ); // Create a GlgControl, specifying a drawing name to be displayed in the control. glg_chart = new GlgChart( drawing_file, true /* prefill chart with data */ ); // Add GLG control to the form. Controls.Add( glg_chart ); // Position the GLG control. ResizeGLGControl(); // Start periodic updates. glg_chart.StartUpdates( UpdateInterval ); } ////////////////////////////////////////////////////////////////////// // Set width and height of the GLG control. ////////////////////////////////////////////////////////////////////// private void ResizeGLGControl() { // Resize GLG control according to the form's client size. glg_chart.Location = new Point(0, 0); glg_chart.Size = ClientSize; } private void GlgRTChartForm_FormClosed( object sender, FormClosedEventArgs e ) { glg_chart.StopUpdates(); } private void GlgRTChartForm_Resize( object sender, EventArgs e ) { ResizeGLGControl(); } } ////////////////////////////////////////////////////////////////////////// // Extend GlgControl and define a custom class GlgChart. ////////////////////////////////////////////////////////////////////////// public class GlgChart : GlgControl { // Number of lines in a chart. May be overriden by the parent object. public int NumPlots = 3; const double TIME_SPAN = 60.0; // Initial Time Span in sec. // Low and High range of the incoming data values. double Low = 0.0; double High = 10.0; private static System.Timers.Timer timer1; // Flag to prevent a race condition when the timer is stopped. public bool Timer1Enabled = false; // Update interval in msec. int UpdateInterval = 100; double TimeSpan = TIME_SPAN; // Store object IDs for each plot. // Used for performance optimization in the chart data feed. GlgObject [] Plots; // Number of plots as defined in the drawing. int num_plots_drawing; Random random = new Random(); Boolean Ready = false; // Used in GetCurrTime(). Epoch_time is a DateTime object // representing Epoch time starting January 1, 1970 at 0:0:0, // based on UTC. // static DateTime Epoch_time = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); // Used in GetPlotValue() to generate demo data. int counter = 0; // Constructor. public GlgChart( String drawing_file, Boolean prefill_data ) { // Set DrawingFile property for the GlgControl. DrawingFile = drawing_file; // Disable automatic update for input events to avoid slowing down // real-time chart updates. SetAutoUpdateOnInput( false ); } // HCallback is invoked before the drawing is displayed, and before // hierarchy setup. public override void HCallback( GlgObject viewport ) { base.HCallback( viewport ); double major_interval, minor_interval; // Retreive number of plots defined in .g file. num_plots_drawing = (int) GetDResource( "Chart/NumPlots" ); // Set new number of plots. SetDResource( "Chart/NumPlots", NumPlots ); // Set Time Span for the X axis. SetDResource( "Chart/XAxis/Span", TimeSpan ); // Set tick intervals for the Time axis. // Use positive values for absolute time interval, for example // set major_interval = 10 for a major tick every 10 sec. // major_interval = -6; // 6 major intervals minor_interval = -5; // 5 minor intervals SetDResource( "Chart/XAxis/MajorInterval", major_interval ); SetDResource( "Chart/XAxis/MinorInterval", minor_interval ); // Set data value range. Since the graph has one Y axis and // common data range for the plots, Low/High data range is // set on the YAxis level. // SetDResource( "Chart/YAxis/Low", Low ); SetDResource( "Chart/YAxis/High", High ); } // VCallback is invoked before the drawing is displayed, but after // hierarchy setup. public override void VCallback( GlgObject viewport ) { base.VCallback( viewport ); // Store objects IDs for each plot. Plots = new GlgObject[ NumPlots ]; for( int i=0; i