You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							356 lines
						
					
					
						
							17 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							356 lines
						
					
					
						
							17 KiB
						
					
					
				| using CommunityToolkit.Mvvm.ComponentModel; | |
| using DyeingComputer.Properties; | |
| using LiveChartsCore.Defaults; | |
| using LiveChartsCore.Kernel.Sketches; | |
| using LiveChartsCore.SkiaSharpView.Painting; | |
| using LiveChartsCore.SkiaSharpView; | |
| using LiveChartsCore; | |
| using SkiaSharp; | |
| using System; | |
| using System.Collections.Generic; | |
| using System.Collections.ObjectModel; | |
| using System.Data; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| using static DyeingComputer.UserClass.SqliteHelper; | |
| using System.Windows.Controls; | |
| using System.Windows.Threading; | |
| using System.Management.Instrumentation; | |
| 
 | |
| namespace DyeingComputer.ViewModel | |
| { | |
|     public partial class HistoryRecordsViewModel : ObservableObject | |
|     { | |
|         //数据 | |
|         public ISeries[] _series; | |
|         public ISeries[] Series { get { return _series; } set { _series = value;OnPropertyChanged(); } } | |
|         //x轴 | |
|         public ICartesianAxis[] _xAxes; | |
|         public ICartesianAxis[] XAxes { get {return _xAxes; } set { _xAxes = value; OnPropertyChanged(); } } | |
| 
 | |
|         //颜色 | |
|         private static readonly SKColor s_blue = new SKColor(25, 118, 210); | |
|         private static readonly SKColor s_blue_0 = new SKColor(25, 18, 210); | |
|         private static readonly SKColor s_blue_1 = new SKColor(25, 118, 170); | |
|         private static readonly SKColor s_blue_2 = new SKColor(25, 118, 130); | |
|         private static readonly SKColor s_blue_3 = new SKColor(25, 118, 90); | |
|         private static readonly SKColor s_blue_4 = new SKColor(25, 118, 50); | |
|         private static readonly SKColor s_red = new SKColor(229, 57, 53); | |
|         private static readonly SKColor s_red_1 = new SKColor(189, 57, 53); | |
|         private static readonly SKColor s_red_2 = new SKColor(149, 57, 53); | |
|         private static readonly SKColor s_red_3 = new SKColor(109, 57, 53); | |
|         private static readonly SKColor s_yellow = new SKColor(198, 167, 0); | |
|         public ICartesianAxis[] YAxes { get; set; } ={ | |
|         new Axis //y在此轴上缩放 | |
|         { | |
|             MinLimit =0, | |
|             MaxLimit =150, | |
|             Name = Properties.Resources.Temperature +"  (  °C  )", | |
|             NameTextSize = 24, | |
|             NamePaint = new SolidColorPaint(s_blue), | |
|             NamePadding = new LiveChartsCore.Drawing.Padding(0, 20), | |
|             Padding =  new LiveChartsCore.Drawing.Padding(0, 0, 20, 0), | |
|             TextSize = 16, | |
|             LabelsPaint = new SolidColorPaint(s_blue), | |
|             TicksPaint = new SolidColorPaint(s_blue), | |
|             SubticksPaint = new SolidColorPaint(s_blue), | |
|             DrawTicksPath = true, | |
|             CrosshairSnapEnabled = true, | |
|              Position = LiveChartsCore.Measure.AxisPosition.Start | |
|         },//温度轴 | |
|         new Axis // the "hundreds" series will be scaled on this axis | |
|         { | |
|             MinLimit =0, | |
|             Name =  Properties.Resources.WaterLevel +"  (  L  )", | |
|             NameTextSize = 24, | |
|             NamePaint = new SolidColorPaint(s_red), | |
|             NamePadding = new LiveChartsCore.Drawing.Padding(0, 20), | |
|             Padding =  new LiveChartsCore.Drawing.Padding(20, 0, 0, 0), | |
|             TextSize = 16, | |
|             LabelsPaint = new SolidColorPaint(s_red), | |
|             TicksPaint = new SolidColorPaint(s_red), | |
|             SubticksPaint = new SolidColorPaint(s_red), | |
|             DrawTicksPath = true, | |
|             CrosshairSnapEnabled = true, | |
|             Position = LiveChartsCore.Measure.AxisPosition.End | |
|         }, //液位轴 | |
|         new Axis //  | |
|         { | |
|             MinLimit =0, | |
|             MaxLimit =14, | |
|             Name =  "PH  (  pH  )", | |
|             NameTextSize = 16, | |
|             NamePaint = new SolidColorPaint(s_yellow), | |
|             NamePadding = new LiveChartsCore.Drawing.Padding(0, 20), | |
|             Padding =  new LiveChartsCore.Drawing.Padding(20, 0, 0, 0), | |
|             TextSize = 16, | |
|             LabelsPaint = new SolidColorPaint(s_yellow), | |
|             TicksPaint = new SolidColorPaint(s_yellow), | |
|             SubticksPaint = new SolidColorPaint(s_yellow), | |
|             DrawTicksPath = true, | |
|             CrosshairSnapEnabled = true, | |
|             Position = LiveChartsCore.Measure.AxisPosition.End | |
|         }, //PH轴 | |
|  | |
|         //new AnimatableAxisBounds             | |
|         }; | |
| 
 | |
|         private Random _r = new Random(); | |
|         private ObservableCollection<DateTimePoint> MTT_values; | |
|         private ObservableCollection<DateTimePoint> MTL_values; | |
|         private ObservableCollection<DateTimePoint> MTH_values; | |
|         private ObservableCollection<DateTimePoint> MST_values; | |
|         private ObservableCollection<DateTimePoint> MUT_values; | |
|         private ObservableCollection<DateTimePoint> STTA_values; | |
|         private ObservableCollection<DateTimePoint> STLA_values; | |
|         private ObservableCollection<DateTimePoint> STTB_values; | |
|         private ObservableCollection<DateTimePoint> STLB_values; | |
|         private ObservableCollection<DateTimePoint> STTC_values; | |
|         private ObservableCollection<DateTimePoint> STLC_values; | |
| 
 | |
|         private SQLiteHelper SQLiteHelpers = null;  //定义数据库 | |
|         private readonly string ChartAdress = Environment.CurrentDirectory + "\\DataBase\\Chart.db";  //数据库路径 | |
|         public DataTable CDB = new DataTable(); | |
|         private int CDB_Count;//长度 | |
|  | |
|         public HistoryRecordsViewModel() | |
|         { | |
|             string Sys_Time = DateTime.Now.AddDays(-2).ToString("yyyy/MM/dd HH:mm:ss"); | |
|             SQLiteHelpers = new SQLiteHelper(ChartAdress); //数据库连接路径 | |
|             SQLiteHelpers.Open();  //打开数据库 | |
|             CDB = SQLiteHelpers.ExecuteDataSet("select * from Chart where Time > '" + Sys_Time + "'order by Time", null).Tables[0]; | |
|             SQLiteHelpers.Close(); | |
|             CDB_Count = CDB.Rows.Count; | |
| 
 | |
|             Chart_View(); | |
|             CountDown(); | |
|         } | |
|         public static bool history_ = false; | |
|         public static string name; | |
|         public static string stime; | |
|         public static string etime; | |
|         public void History() | |
|         { | |
|             SQLiteHelpers = new SQLiteHelper(ChartAdress); //数据库连接路径 | |
|             SQLiteHelpers.Open();  //打开数据库 | |
|             CDB = SQLiteHelpers.ExecuteDataSet("select * from Chart where DYELOT = '" + name + "'order by Time", null).Tables[0]; | |
|             SQLiteHelpers.Close(); | |
|             CDB_Count = CDB.Rows.Count; | |
| 
 | |
|             Chart_View(); | |
|         } | |
|         public void CountDown() | |
|         { | |
|             DispatcherTimer timer1s = new DispatcherTimer//初始化循环,每1秒调用一次Tick | |
|             { | |
|                 Interval = TimeSpan.FromSeconds(1)//秒 | |
|             }; | |
|             timer1s.Tick += Tick_Event_1S; | |
|             timer1s.Start(); | |
|         } | |
|         void Tick_Event_1S(object sender, EventArgs e)//Tick_Event周期执行事件1S | |
|         { | |
|             if (history_)  | |
|             { | |
|                 History();                | |
|                 history_ = false; | |
|             } | |
|         } | |
| 
 | |
|         public void Chart_View()  | |
|         { | |
|             var MTT_items = new List<DateTimePoint>(); | |
|             var MTL_items = new List<DateTimePoint>(); | |
|             var MTH_items = new List<DateTimePoint>(); | |
|             var MST_items = new List<DateTimePoint>(); | |
|             var MUT_items = new List<DateTimePoint>(); | |
|             var STTA_items = new List<DateTimePoint>(); | |
|             var STLA_items = new List<DateTimePoint>(); | |
|             var STTB_items = new List<DateTimePoint>(); | |
|             var STLB_items = new List<DateTimePoint>(); | |
|             var STTC_items = new List<DateTimePoint>(); | |
|             var STLC_items = new List<DateTimePoint>(); | |
|             for (var i = 0; i < CDB_Count; i++) | |
|             { | |
|                 DateTime TIME = Convert.ToDateTime(CDB.Rows[i].Field<string>("Time")); | |
|                 MTT_items.Add(new DateTimePoint(TIME, CDB.Rows[i].Field<double>("MTT"))); | |
|                 MTL_items.Add(new DateTimePoint(TIME, CDB.Rows[i].Field<double>("MTL"))); | |
|                 MTH_items.Add(new DateTimePoint(TIME, CDB.Rows[i].Field<double>("MTH"))); | |
|                 MST_items.Add(new DateTimePoint(TIME, CDB.Rows[i].Field<double>("MST"))); | |
|                 MUT_items.Add(new DateTimePoint(TIME, CDB.Rows[i].Field<double>("MUT"))); | |
|                 if (MainWindowViewModel.SM01 == 1) | |
|                 { | |
|                     STTA_items.Add(new DateTimePoint(TIME, CDB.Rows[i].Field<double>("STTA"))); | |
|                     STLA_items.Add(new DateTimePoint(TIME, CDB.Rows[i].Field<double>("STLA"))); | |
|                 } | |
|                 if (MainWindowViewModel.SM02 == 1) | |
|                 { | |
|                     STTB_items.Add(new DateTimePoint(TIME, CDB.Rows[i].Field<double>("STTB"))); | |
|                     STLB_items.Add(new DateTimePoint(TIME, CDB.Rows[i].Field<double>("STLB"))); | |
|                 } | |
|                 if (MainWindowViewModel.SM03 == 1) | |
|                 { | |
|                     STTC_items.Add(new DateTimePoint(TIME, CDB.Rows[i].Field<double>("STTC"))); | |
|                     STLC_items.Add(new DateTimePoint(TIME, CDB.Rows[i].Field<double>("STLC"))); | |
|                 } | |
|             } | |
| 
 | |
|             MTT_values = new ObservableCollection<DateTimePoint>(MTT_items); | |
|             MTL_values = new ObservableCollection<DateTimePoint>(MTL_items); | |
|             MTH_values = new ObservableCollection<DateTimePoint>(MTH_items); | |
|             MST_values = new ObservableCollection<DateTimePoint>(MST_items); | |
|             MUT_values = new ObservableCollection<DateTimePoint>(MUT_items); | |
|             if (MainWindowViewModel.SM01 == 1) | |
|             { | |
|                 STTA_values = new ObservableCollection<DateTimePoint>(STTA_items); | |
|                 STLA_values = new ObservableCollection<DateTimePoint>(STLA_items); | |
|             } | |
|             if (MainWindowViewModel.SM02 == 1) | |
|             { | |
|                 STTB_values = new ObservableCollection<DateTimePoint>(STTB_items); | |
|                 STLB_values = new ObservableCollection<DateTimePoint>(STLB_items); | |
|             } | |
|             if (MainWindowViewModel.SM03 == 1) | |
|             { | |
|                 STTC_values = new ObservableCollection<DateTimePoint>(STTC_items); | |
|                 STLC_values = new ObservableCollection<DateTimePoint>(STLC_items); | |
|             } | |
|             //数据 | |
|             Series = new ISeries[] | |
|             { | |
|                     new LineSeries<DateTimePoint> | |
|                     { | |
|                         Name = Properties.Resources.MasterCylinder +"PH", | |
|                         Values = MTH_values, | |
|                         Stroke = new SolidColorPaint(s_yellow, 2), | |
|                         GeometrySize = 0, | |
|                         GeometryStroke = new SolidColorPaint(s_yellow, 2), | |
|                         Fill = null, | |
|                         LineSmoothness = 0, | |
|                         ScalesYAt = 2, | |
|                     },   //PH                | |
|                     new LineSeries<DateTimePoint> | |
|                     { | |
|                         Name = Resources.Target + Resources.Temperature, | |
|                         Values = MST_values, | |
|                         Stroke = new SolidColorPaint(s_blue_0, 2), | |
|                         GeometrySize = 0, | |
|                         GeometryStroke = new SolidColorPaint(s_blue_0, 2), | |
|                         Fill = null, | |
|                         LineSmoothness = 0, | |
|                         ScalesYAt = 0, | |
|                     },   //目标温度  | |
|                     new LineSeries<DateTimePoint> | |
|                     { | |
|                         Name = Properties.Resources.Temperature +"1("+Properties.Resources.MasterCylinder+")", | |
|                         Values = MTT_values, | |
|                         Stroke = new SolidColorPaint(s_blue, 2), | |
|                         GeometrySize = 0, | |
|                         GeometryStroke = new SolidColorPaint(s_blue, 2), | |
|                         Fill = null, | |
|                         LineSmoothness = 0, | |
|                         ScalesYAt = 0, | |
|                     },   //主缸     | |
|                     new LineSeries<DateTimePoint> | |
|                     { | |
|                         Name = Properties.Resources.Temperature +"2("+Properties.Resources.MasterCylinder+Properties.Resources.Bottom+")", | |
|                         Values = MUT_values, | |
|                         Stroke = new SolidColorPaint(s_blue_4, 2), | |
|                         GeometrySize = 0, | |
|                         GeometryStroke = new SolidColorPaint(s_blue_4, 2), | |
|                         Fill = null, | |
|                         LineSmoothness = 0, | |
|                         ScalesYAt = 0, | |
|                     },   //缸底                                                         | |
|                     new LineSeries<DateTimePoint> | |
|                     { | |
|                         Name = Properties.Resources.Temperature +"3("+Resources.Medicine + Resources.Tank +"1)", | |
|                         Values = STTA_values, | |
|                         Stroke = new SolidColorPaint(s_blue_1, 2), | |
|                         GeometrySize = 0, | |
|                         GeometryStroke = new SolidColorPaint(s_blue_1, 2), | |
|                         Fill = null, | |
|                         LineSmoothness = 0, | |
|                         ScalesYAt = 0, | |
|                     },   //附缸1         | |
|                     new LineSeries<DateTimePoint> | |
|                     { | |
|                         Name = Properties.Resources.Temperature +"4("+Resources.Medicine + Resources.Tank +"2)", | |
|                         Values = STTB_values, | |
|                         Stroke = new SolidColorPaint(s_blue_2, 2), | |
|                         GeometrySize = 0, | |
|                         GeometryStroke = new SolidColorPaint(s_blue_2, 2), | |
|                         Fill = null, | |
|                         LineSmoothness = 0, | |
|                         ScalesYAt = 0, | |
|                     },   //附缸2                  | |
|                     new LineSeries<DateTimePoint> | |
|                     { | |
|                         Name = Properties.Resources.Temperature +"5("+Resources.Medicine + Resources.Tank +"3)", | |
|                         Values = STTC_values, | |
|                         Stroke = new SolidColorPaint(s_blue_3, 2), | |
|                         GeometrySize = 0, | |
|                         GeometryStroke = new SolidColorPaint(s_blue_3, 2), | |
|                         Fill = null, | |
|                         LineSmoothness = 0, | |
|                         ScalesYAt = 0, | |
|                     },   //附缸3     | |
|                     new LineSeries<DateTimePoint> | |
|                     { | |
|                         Name = Properties.Resources.WaterLevel +"("+Properties.Resources.MasterCylinder+")", | |
|                         Values = MTL_values, | |
|                         Stroke = new SolidColorPaint(s_red, 2), | |
|                         GeometrySize = 0, | |
|                         GeometryStroke = new SolidColorPaint(s_red, 2), | |
|                         Fill = null, | |
|                         LineSmoothness = 0, | |
|                         ScalesYAt = 1, | |
|                     },   //主缸液位     | |
|                     new LineSeries<DateTimePoint> | |
|                     { | |
|                         Name = Properties.Resources.WaterLevel +"("+Resources.Medicine + Resources.Tank +"1)", | |
|                         Values = STLA_values, | |
|                         Stroke = new SolidColorPaint(s_red_1, 2), | |
|                         GeometrySize = 0, | |
|                         GeometryStroke = new SolidColorPaint(s_red_1, 2), | |
|                         Fill = null, | |
|                         LineSmoothness = 0, | |
|                         ScalesYAt = 1, | |
|                     },   //附缸1液位         | |
|                     new LineSeries<DateTimePoint> | |
|                     { | |
|                         Name = Properties.Resources.WaterLevel +"("+Resources.Medicine + Resources.Tank +"2)", | |
|                         Values = STLB_values, | |
|                         Stroke = new SolidColorPaint(s_red_2, 2), | |
|                         GeometrySize = 0, | |
|                         GeometryStroke = new SolidColorPaint(s_red_2, 2), | |
|                         Fill = null, | |
|                         LineSmoothness = 0, | |
|                         ScalesYAt = 1, | |
|                     },   //附缸2液位                | |
|                     new LineSeries<DateTimePoint> | |
|                     { | |
|                         Name = Properties.Resources.WaterLevel +"("+Resources.Medicine + Resources.Tank +"3)", | |
|                         Values = STLC_values, | |
|                         Stroke = new SolidColorPaint(s_red_3, 2), | |
|                         GeometrySize = 0, | |
|                         GeometryStroke = new SolidColorPaint(s_red_3, 2), | |
|                         Fill = null, | |
|                         LineSmoothness = 0, | |
|                         ScalesYAt = 1, | |
|                     },   //附缸3 液位  | |
|             }; | |
|             //x轴时间格式 | |
|             XAxes = new ICartesianAxis[] | |
|             { | |
|                 new DateTimeAxis(TimeSpan.FromSeconds(5) , date => date.ToString("yyyy-MM-dd HH:mm")) | |
|                 { | |
|                     CrosshairLabelsBackground = SKColors.DarkGray.AsLvcColor(), | |
|                     CrosshairLabelsPaint = new SolidColorPaint(SKColors.DarkSlateBlue, 1), | |
|                     CrosshairPaint = new SolidColorPaint(SKColors.DarkSlateGray, 1),           | |
|                 },     | |
|             }; | |
|         } | |
|     } | |
| }
 | |
| 
 |