using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using WeifenLuo.WinFormsUI.Docking; using LiveCharts; using LiveCharts.Wpf; using LiveCharts.WinForms; using LiveCharts.Defaults; using System.Windows.Media; namespace friction { public partial class PanelTrendGraph : DockContent { MainForm m_Owner = null; DataHandler m_DataHandler = null; string m_CurSpring = ""; string m_CurTable = ""; bool m_bHumidity = false; public PanelTrendGraph(MainForm owner, DataHandler data) { InitializeComponent(); m_Owner = owner; m_DataHandler = data; Theme.Apply(this); Theme.Apply(trendChart); Theme.Apply(groupBox1); Theme.Apply(rbHumidity); Theme.Apply(rbTemp); trendChart.AxisX.Add(new Axis { Title = "Humidity", }); trendChart.AxisY.Add(new Axis { Title = "RPN", MinValue = 0, MaxValue = 10, Sections = new SectionsCollection { new AxisSection { Value = 5, SectionWidth = 5, Fill = new SolidColorBrush { Color = System.Windows.Media.Color.FromArgb(Theme.Red.A, Theme.Red.R, Theme.Red.G, Theme.Red.B), Opacity = .75 } }, new AxisSection { //Label = "Good", Value = 3, SectionWidth = 2, Fill = new SolidColorBrush { Color = System.Windows.Media.Color.FromArgb(Theme.Yellow.A, Theme.Yellow.R, Theme.Yellow.G, Theme.Yellow.B), Opacity = .75 } }, new AxisSection { //Label = "Bad", Value = 0, SectionWidth = 3, Fill = new SolidColorBrush { Color = System.Windows.Media.Color.FromArgb(Theme.Green.A, Theme.Green.R, Theme.Green.G, Theme.Green.B), Opacity = .75 } } } }); trendChart.LegendLocation = LegendLocation.Right; trendChart.DataClick += TrendChart_DataClick; } public void Reset() { m_CurSpring = ""; m_CurTable = ""; m_bHumidity = false; } private void TrendChart_DataClick(object sender, ChartPoint chartPoint) { } public void UpdateGraph() { string strSpring = m_DataHandler.GetCurSpring(); string strTable = m_DataHandler.GetCurTable(); if (m_CurSpring == strSpring && m_CurTable == strTable && rbHumidity.Checked == m_bHumidity) return; var Chart = m_DataHandler.GetHumidityChart(strSpring, strTable); if (Chart.Count <= 0) return; string strTitle = ""; double dMin = 0; double dMax = 0; if (rbHumidity.Checked == true) { strTitle = "Humidity"; trendChart.AxisX[0].Title = strTitle; ChartValues[] Points = { new ChartValues(), new ChartValues(), new ChartValues() }; var Values = new List(); foreach (var pnt in Chart) { if(pnt.TEMPERATURE < 0) Points[0].Add(new ScatterPoint(pnt.HUMIDITY, pnt.RPN, 2)); else if(pnt.TEMPERATURE > 30) Points[2].Add(new ScatterPoint(pnt.HUMIDITY, pnt.RPN, 2)); else Points[1].Add(new ScatterPoint(pnt.HUMIDITY, pnt.RPN, 2)); Values.Add(new TrendLine.POINT { X = pnt.HUMIDITY, Y = pnt.RPN }); } Values.Sort((a, b) => (a.X == b.X) ? 0 : (a.X < b.X) ? -1 : 1); TrendLine trendline = new TrendLine(Values); ChartValues TrendPoints = new ChartValues(); TrendPoints.Add(new ScatterPoint(Values[0].X, trendline.GetY(Values[0].X))); TrendPoints.Add(new ScatterPoint(Values[Values.Count / 2].X, trendline.GetY(Values[Values.Count / 2].X))); TrendPoints.Add(new ScatterPoint(Values[Values.Count - 1].X, trendline.GetY(Values[Values.Count - 1].X))); System.Windows.Media.SolidColorBrush[] brushes = { System.Windows.Media.Brushes.DodgerBlue.Clone(), System.Windows.Media.Brushes.Green.Clone(), System.Windows.Media.Brushes.IndianRed.Clone() }; foreach (var brush in brushes) brush.Opacity = 0.75; trendChart.Series = new SeriesCollection { new ScatterSeries { Title = "~ 0℃", Values = Points[0], Fill = brushes[0], Foreground = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 255, 255, 255)), }, new ScatterSeries { Title = "0℃ ~ 30℃", Values = Points[1], Fill = brushes[1], }, new ScatterSeries { Title = "30℃ ~", Values = Points[2], Fill = brushes[2], }, new LineSeries { Title = "Trend", Values = TrendPoints, PointGeometry = DefaultGeometries.None, StrokeThickness = 4, }, }; dMin = Chart.Min(r => r.HUMIDITY - 1); dMax = Chart.Max(r => r.HUMIDITY - 1); } else if(rbTemp.Checked == true) { strTitle = "Temperature"; trendChart.AxisX[0].Title = strTitle; ChartValues[] Points = { new ChartValues(), new ChartValues() }; var Values = new List(); foreach (var pnt in Chart) { if(pnt.HUMIDITY <= 60) Points[0].Add(new ScatterPoint(pnt.TEMPERATURE, pnt.RPN, 2)); else Points[1].Add(new ScatterPoint(pnt.TEMPERATURE, pnt.RPN, 2)); Values.Add(new TrendLine.POINT { X = pnt.TEMPERATURE, Y = pnt.RPN }); } Values.Sort((a, b) => (a.X == b.X) ? 0 : (a.X TrendPoints = new ChartValues(); TrendPoints.Add(new ScatterPoint(Values[0].X, trendline.GetY(Values[0].X))); TrendPoints.Add(new ScatterPoint(Values[Values.Count / 2].X, trendline.GetY(Values[Values.Count / 2].X))); TrendPoints.Add(new ScatterPoint(Values[Values.Count - 1].X, trendline.GetY(Values[Values.Count - 1].X))); trendChart.Series = new SeriesCollection { new ScatterSeries { Title = "~ 60%", Values = Points[0], Foreground = System.Windows.Media.Brushes.SkyBlue, }, new ScatterSeries { Title = "60% ~", Values = Points[1], Foreground = System.Windows.Media.Brushes.DarkBlue, }, new LineSeries { Title = "Trend", Values = TrendPoints, PointGeometry = DefaultGeometries.None, StrokeThickness = 4, }, }; dMin = Chart.Min(r => r.TEMPERATURE - 1); dMax = Chart.Max(r => r.TEMPERATURE - 1); } trendChart.AxisX[0].MinValue = dMin; trendChart.AxisX[0].MaxValue = dMax; m_CurSpring = strSpring; m_CurTable = strTable; m_bHumidity = rbHumidity.Checked; } private void rbHumidity_CheckedChanged(object sender, EventArgs e) { UpdateGraph(); } private void rbTemp_CheckedChanged(object sender, EventArgs e) { UpdateGraph(); } } }