using LiveCharts; using LiveCharts.Wpf; using LiveCharts.WinForms; using LiveCharts.Configurations; using System; using System.Windows.Forms; using System.Windows; using LiveCharts.Geared; using Brushes = System.Windows.Media.Brushes; namespace CPFConsole { //Needed for data visualization off thread processing public class GlobalVars { public double GlobalBellowsPos { get; set; } public double GlobalPressure { get; set; } public double GlobalModelPressure { get; set; } public double GlobalPlatformVelocity { get; set; } public double GlobalModelVelocity { get; set; } } partial class Form1 { public DataVisualizer _viewModel; //flags private static bool firstDataFlag = true; private static bool readModelVelocity = false; private static bool firstPressureRead = true; private static bool firstVelocityRead = true; private static bool firstBellowsRead = true; private double resetView; public void InitLiveChart() { _viewModel = new DataVisualizer(this); //////Set up LiveChart //var mapper = Mappers.Xy() // .X(model => model.DateTime.Ticks) //use DateTime.Ticks as X // .Y(model => model.Value); //use the value property as Y //////save the mapper globally. //Charting.For(mapper); BellowsSeries = new GLineSeries { Title = "Bellows Position", Values = _viewModel.BellowsValues, LineSmoothness = 0, Fill = Brushes.Transparent, PointGeometry = null, StrokeThickness = 2, ScalesYAt = 0 }; PressureSeries = new GLineSeries { Title = "Measured Pressure", Values = _viewModel.PressureValues, LineSmoothness = 0, Fill = Brushes.Transparent, PointGeometry = null, StrokeThickness = 2, ScalesYAt = 1 }; CommandedPressureSeries = new GLineSeries { Title = "Commanded Pressure", Values = _viewModel.ModelPressureValues, LineSmoothness = 0, Fill = Brushes.Transparent, PointGeometry = null, StrokeThickness = 2, ScalesYAt = 1 }; VelocitySeries = new GLineSeries { Title = "Platform Velocity", Values = _viewModel.PlatformVelocity, LineSmoothness = 0, Fill = Brushes.Transparent, PointGeometry = null, StrokeThickness = 2, ScalesYAt = 2 }; ModelVelocitySeries = new GLineSeries { Title = "Model Velocity", Values = _viewModel.ModelVelocity, LineSmoothness = 0, Fill = Brushes.Transparent, PointGeometry = null, StrokeThickness = 2, ScalesYAt = 2 }; cartesianChart1.Series = new SeriesCollection { BellowsSeries, PressureSeries, CommandedPressureSeries, VelocitySeries, ModelVelocitySeries }; //cartesianChart1.DisableAnimations = true; _viewModel.Read(); cartesianChart1.AxisX.Add(new Axis { //LabelFormatter = val => new DateTime((long)val).ToString("t") LabelFormatter = val => new System.DateTime((long)val).ToString("mm:ss") //Separator = new Separator //{ // Step = TimeSpan.FromSeconds(1).Ticks //} }); cartesianChart1.AxisY.Add(new Axis { //LabelFormatter = val => val.ToString() Foreground = System.Windows.Media.Brushes.DodgerBlue, Title = "Bellows Position" }); cartesianChart1.AxisY.Add(new Axis { Foreground = System.Windows.Media.Brushes.IndianRed, Title = "Pressure Measurements", Position = AxisPosition.RightTop }); cartesianChart1.AxisY.Add(new Axis { Title = "Velocity", Position = AxisPosition.RightTop }); cartesianChart1.DisableAnimations = true; cartesianChart1.Hoverable = false; cartesianChart1.DataTooltip = null; cartesianChart1.LegendLocation = LegendLocation.Right; //SetAxisLimits(System.DateTime.Now); resetView = System.DateTime.Now.Ticks; cartesianChart1.AxisX[0].MinValue = resetView; cartesianChart1.Zoom = ZoomingOptions.X; SetAxisLimits(System.DateTime.Now); } public GLineSeries BellowsSeries { get; set; } public GLineSeries PressureSeries { get; set; } public GLineSeries CommandedPressureSeries { get; set; } public GLineSeries VelocitySeries { get; set; } public GLineSeries ModelVelocitySeries { get; set; } private void SetAxisLimits(System.DateTime now) { cartesianChart1.AxisX[0].MaxValue = now.Ticks + TimeSpan.FromSeconds(1).Ticks; // lets force the axis to be 100ms ahead //cartesianChart1.AxisX[0].MinValue = now.Ticks - TimeSpan.FromSeconds(30).Ticks; //we only care about the last 8 seconds } #region Visibility Controls private void ToggleBellows_CheckedChanged(object sender, EventArgs e) { BellowsSeries.Visibility = BellowsSeries.Visibility == Visibility.Visible ? Visibility.Hidden : Visibility.Visible; } private void PressureToggle_CheckedChanged(object sender, EventArgs e) { PressureSeries.Visibility = PressureSeries.Visibility == Visibility.Visible ? Visibility.Hidden : Visibility.Visible; } private void CommandPressureToggle_CheckedChanged(object sender, EventArgs e) { CommandedPressureSeries.Visibility = CommandedPressureSeries.Visibility == Visibility.Visible ? Visibility.Hidden : Visibility.Visible; } private void PVToggle_CheckedChanged(object sender, EventArgs e) { VelocitySeries.Visibility = VelocitySeries.Visibility == Visibility.Visible ? Visibility.Hidden : Visibility.Visible; } private void ModelVelocityToggle_CheckedChanged(object sender, EventArgs e) { ModelVelocitySeries.Visibility = ModelVelocitySeries.Visibility == Visibility.Visible ? Visibility.Hidden : Visibility.Visible; } #endregion private void rtbCTDPressure_TextChanged(object sender, EventArgs e) { if (enableVisualizer.Checked == true) { var now = System.DateTime.Now; try { Globals.GlobalPressure = Convert.ToDouble(rtbCTDPressure.Text); _viewModel.PressureNewDataReady = true; if (firstPressureRead) { cartesianChart1.AxisY[1].MaxValue = 33; cartesianChart1.AxisY[1].MinValue = 0; firstPressureRead = false; } if (firstDataFlag) { cartesianChart1.AxisX[0].MinValue = System.DateTime.Now.Ticks; resetView = System.DateTime.Now.Ticks; firstDataFlag = false; } if (FollowBox.Checked) SetAxisLimits(System.DateTime.Now); } catch (FormatException) { } } } private void rtbBellowsPosition_TextChanged(object sender, EventArgs e) { if (enableVisualizer.Checked == true) { var now = System.DateTime.Now; try { Globals.GlobalBellowsPos = Convert.ToDouble(rtbBellowsPosition.Text); _viewModel.BellowsNewDataReady = true; if (firstBellowsRead) { cartesianChart1.AxisY[0].MaxValue = 115.0; cartesianChart1.AxisY[0].MinValue = 100.0; firstBellowsRead = false; } if (firstDataFlag) { cartesianChart1.AxisX[0].MinValue = System.DateTime.Now.Ticks; resetView = System.DateTime.Now.Ticks; firstDataFlag = false; } if (FollowBox.Checked) SetAxisLimits(System.DateTime.Now); } catch (FormatException) { } } } private void rtbDynamicModelDepth_TextChanged(object sender, EventArgs e) { //the rest is taken care of in the depth command rtb text change } private void rtbPIDPV_TextChanged(object sender, EventArgs e) { try { Globals.GlobalPlatformVelocity = Convert.ToDouble(rtbPIDPV.Text); _viewModel.PVNewDataReady = true; if (firstVelocityRead) { cartesianChart1.AxisY[2].MaxValue = 0.8; cartesianChart1.AxisY[2].MinValue = -0.5; firstVelocityRead = false; } readModelVelocity = true; if (firstDataFlag) { cartesianChart1.AxisX[0].MinValue = System.DateTime.Now.Ticks; resetView = System.DateTime.Now.Ticks; firstDataFlag = false; } if (FollowBox.Checked) SetAxisLimits(System.DateTime.Now); } catch (FormatException) { } } private void rtbDynamicModelVelocity_TextChanged(object sender, EventArgs e) { if (readModelVelocity) { try { Globals.GlobalModelVelocity = Convert.ToDouble(rtbDynamicModelVelocity.Text); _viewModel.ModelVelocityNewDataReady = true; if (firstDataFlag) { cartesianChart1.AxisX[0].MinValue = System.DateTime.Now.Ticks; resetView = System.DateTime.Now.Ticks; firstDataFlag = false; } if (FollowBox.Checked) SetAxisLimits(System.DateTime.Now); } catch (FormatException) { } } } private void clearChart_Click(object sender, EventArgs e) { _viewModel.Clear(); firstDataFlag = true; } private void ResetView_btn_Click(object sender, EventArgs e) { cartesianChart1.AxisX[0].MaxValue = System.DateTime.Now.Ticks + TimeSpan.FromSeconds(1).Ticks; cartesianChart1.AxisX[0].MinValue = resetView; } } } //using LiveCharts; //using LiveCharts.Wpf; //using LiveCharts.WinForms; //using LiveCharts.Configurations; //using System; //using System.Windows.Forms; //namespace CPFConsole //{ // partial class Form1 // { // public void InitLiveChart() // { // //Set up LiveChart // var mapper = Mappers.Xy() // .X(model => model.DateTime.Ticks) //use DateTime.Ticks as X // .Y(model => model.Value); //use the value property as Y // //save the mapper globally. // Charting.For(mapper); // //the ChartValues property will store our values array // BellowsValues = new ChartValues(); // PositionValues = new ChartValues(); // ModelPositionValues = new ChartValues(); // cartesianChart1.Series = new SeriesCollection // { // new LineSeries // { // Values = BellowsValues, // PointGeometrySize = 2, // StrokeThickness = 2, // ScalesYAt = 0 // }, // new LineSeries // { // Values = PositionValues, // PointGeometrySize = 2, // StrokeThickness = 2, // ScalesYAt = 1 // }, // new LineSeries // { // Values = ModelPositionValues, // PointGeometrySize = 2, // StrokeThickness = 2, // ScalesYAt = 1 // } // }; // getModelDepth = new Timer // { // Interval = 3000 // }; // getModelDepth.Tick += DepthUpdateTick; // //cartesianChart1.AxisX.Add(new Axis // //{ // // DisableAnimations = true, // // LabelFormatter = value => new System.DateTime((long)value).ToString("mm:ss"), // // Separator = new Separator // // { // // Step = TimeSpan.FromSeconds(1).Ticks // // } // //}); // cartesianChart1.AxisX.Add(new Axis // { // LabelFormatter = val => new System.DateTime((long)val).ToString("mm:ss") // //Separator = new Separator // //{ // // Step = TimeSpan.FromSeconds(1).Ticks // //} // }); // cartesianChart1.AxisY.Add(new Axis // { // LabelFormatter = val => val.ToString() // }); // cartesianChart1.AxisY.Add(new Axis // { // Foreground = System.Windows.Media.Brushes.IndianRed, // Title = "Red Axis", // Position = AxisPosition.RightTop // }); // cartesianChart1.DisableAnimations = true; // cartesianChart1.Hoverable = false; // cartesianChart1.DataTooltip = null; // SetAxisLimits(System.DateTime.Now); // cartesianChart1.AxisX[0].MinValue = System.DateTime.Now.Ticks; // cartesianChart1.Zoom = ZoomingOptions.Xy; // //SetAxisLimits(System.DateTime.Now); // } // public ChartValues BellowsValues { get; set; } // public ChartValues PositionValues { get; set; } // public ChartValues ModelPositionValues { get; set; } // public Timer getModelDepth { get; set; } // private void SetAxisLimits(System.DateTime now) // { // cartesianChart1.AxisX[0].MaxValue = now.Ticks + TimeSpan.FromSeconds(1).Ticks; // lets force the axis to be 100ms ahead // //cartesianChart1.AxisX[0].MinValue = now.Ticks - TimeSpan.FromSeconds(30).Ticks; //we only care about the last 8 seconds // } // private void rtbCTDPressure_TextChanged(object sender, EventArgs e) // { // if (enableVisualizer.Checked == true) // { // var now = System.DateTime.Now; // try // { // PositionValues.Add(new MeasureModel // { // DateTime = now, // Value = Convert.ToDouble(rtbCTDPressure.Text) // }); // cartesianChart1.AxisY[1].MaxValue = 30.0; // cartesianChart1.AxisY[1].MinValue = 0.0; // if (FollowBox.Checked == true) // SetAxisLimits(now); // //lets only use the last 30 values // //if (PositionValues.Count > 100) PositionValues.RemoveAt(0); // } // catch (FormatException) // { // } // } // } // private void rtbBellowsPosition_TextChanged(object sender, EventArgs e) // { // if (enableVisualizer.Checked == true) // { // var now = System.DateTime.Now; // try // { // BellowsValues.Add(new MeasureModel // { // DateTime = now, // Value = Convert.ToDouble(rtbBellowsPosition.Text) // }); // cartesianChart1.AxisY[0].MaxValue = 115.0; // cartesianChart1.AxisY[0].MinValue = 98.0; // if (FollowBox.Checked == true) // SetAxisLimits(now); // //lets only use the last 100 values // //if (BellowsValues.Count > 100) BellowsValues.RemoveAt(0); // } // catch (FormatException) // { // } // } // } // private void rtbDynamicModelDepth_TextChanged(object sender, EventArgs e) // { // if (CheckBoxToAutomate.Checked == true) // getModelDepth.Start(); // } // private void DepthUpdateTick(object sender, EventArgs eventArgs) // { // if (enableVisualizer.Checked == true) // { // if (CheckBoxToAutomate.Checked == false) // getModelDepth.Stop(); // var now = System.DateTime.Now; // try // { // ModelPositionValues.Add(new MeasureModel // { // DateTime = now, // Value = Math.Abs(Convert.ToDouble(rtbDynamicModelDepth.Text)) // }); // if (FollowBox.Checked == true) // SetAxisLimits(now); // //lets only use the last 30 values // //if (ModelPositionValues.Count > 100) ModelPositionValues.RemoveAt(0); // } // catch (FormatException) // { // } // } // } // } //}