using CommunityToolkit.Mvvm.ComponentModel; using ivf_tl_Operate.Converts; using OxyPlot; using OxyPlot.Axes; using OxyPlot.Legends; using OxyPlot.Series; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace ivf_tl_Operate.ViewModel { public partial class ChartPageViewModel : ObservableObject { public MainPageViewModel mainPageViewModel { get; set; } public bool isClose = false; public ChartPageViewModel(int newHouseId, MainPageViewModel _vm) { CurrentHouseId = newHouseId; mainPageViewModel = _vm; Init(); Task.Run(() => { decimal temprature = 0; decimal pressure = 0; double minT = 0; double minP = 0; double maxT = 0; double maxP = 0; bool dataAny = false; while (true) { if (isClose) return; temprature = 0; pressure = 0; switch (CurrentHouseId) { case 1: temprature = mainPageViewModel.ExHouse1.temperature; pressure = mainPageViewModel.ExHouse1.pressure; break; case 2: temprature = mainPageViewModel.ExHouse2.temperature; pressure = mainPageViewModel.ExHouse2.pressure; break; case 3: temprature = mainPageViewModel.ExHouse3.temperature; pressure = mainPageViewModel.ExHouse3.pressure; break; case 4: temprature = mainPageViewModel.ExHouse4.temperature; pressure = mainPageViewModel.ExHouse4.pressure; break; case 5: temprature = mainPageViewModel.ExHouse5.temperature; pressure = mainPageViewModel.ExHouse5.pressure; break; case 6: temprature = mainPageViewModel.ExHouse6.temperature; pressure = mainPageViewModel.ExHouse6.pressure; break; case 7: temprature = mainPageViewModel.ExHouse7.temperature; pressure = mainPageViewModel.ExHouse7.pressure; break; case 8: temprature = mainPageViewModel.ExHouse8.temperature; pressure = mainPageViewModel.ExHouse8.pressure; break; case 9: temprature = mainPageViewModel.ExHouse9.temperature; pressure = mainPageViewModel.ExHouse9.pressure; break; case 10: temprature = mainPageViewModel.ExHouse10.temperature; pressure = mainPageViewModel.ExHouse10.pressure; break; default: break; } if (temprature >= 35 && temprature <= 38) { TemperatureRealDataPoints.Add(DateTimeAxis.CreateDataPoint(DateTime.Now, (double)temprature)); } PressureRealDataPoints.Add(DateTimeAxis.CreateDataPoint(DateTime.Now, (double)pressure)); //if (pressure >= 35 && pressure <= 55) //{ // PressureRealDataPoints.Add(DateTimeAxis.CreateDataPoint(DateTime.Now, (double)pressure)); //} if (TemperatureRealDataPoints.Count > 3000) { TemperatureRealDataPoints.RemoveAt(0); PressureRealDataPoints.RemoveAt(0); } if (RealModel.Axes[1] is DateTimeAxis temperatureDateTimeAxis) { if (TemperatureRealDataPoints.Any()) { minT = TemperatureRealDataPoints.First().X; maxT = TemperatureRealDataPoints.Last().X; } if (PressureRealDataPoints.Any()) { minP = PressureRealDataPoints.First().X; maxP = PressureRealDataPoints.Last().X; } if (minT != 0 && minP != 0) { temperatureDateTimeAxis.Minimum = minT < minP ? minT : minP; } else if (minT != 0 && minP == 0) { temperatureDateTimeAxis.Minimum = minT; } else if (minT == 0 && minP != 0) { temperatureDateTimeAxis.Minimum = minP; } if (maxT != 0 && maxP != 0) { temperatureDateTimeAxis.Maximum = maxT > maxP ? maxT : maxP; } else if (maxT != 0 && maxP == 0) { temperatureDateTimeAxis.Maximum = maxT; } else if (maxT == 0 && maxP != 0) { temperatureDateTimeAxis.Maximum = maxP; } } RealModel.ResetAllAxes(); RealModel.InvalidatePlot(true); Thread.Sleep(1000); } }); } public int CurrentHouseId { get; set; } public string tlSn { get; set; } [ObservableProperty] private string leftDateString; [ObservableProperty] private string rightDateString; private DateTime leftDate; public DateTime LeftDate { get { return leftDate; } set { leftDate = value; LeftDateString = $"{leftDate.Year}-{leftDate.Month.ToString().PadLeft(2, '0')}-{leftDate.Day.ToString().PadLeft(2, '0')}"; } } private DateTime rightDate; public DateTime RightDate { get { return rightDate; } set { rightDate = value; RightDateString = $"{rightDate.Year}-{rightDate.Month.ToString().PadLeft(2, '0')}-{rightDate.Day.ToString().PadLeft(2, '0')}"; } } [ObservableProperty] private string historyTitle = "";//1号舱室温压历史曲线 [ObservableProperty] private string realTitle = "";//1号舱室温压实时曲线 /// /// 历史曲线模型 /// [ObservableProperty] private PlotModel historyModel; /// /// 气压历史曲线数据 /// [ObservableProperty] private ObservableCollection pressureHistoryDataPoints = new ObservableCollection(); /// /// 温度历史曲线数据 /// [ObservableProperty] private ObservableCollection temperatureHistoryDataPoints = new ObservableCollection(); /// /// 实时曲线模型 /// [ObservableProperty] private PlotModel realModel; /// /// 气压实时曲线数据 /// [ObservableProperty] private ObservableCollection pressureRealDataPoints = new ObservableCollection(); /// /// 温度实时曲线数据 /// [ObservableProperty] private ObservableCollection temperatureRealDataPoints = new ObservableCollection(); public void RefHistroy() { var a = AppData.Instance.HttpHelper.GetHouseEnvironmentListApi(tlSn, CurrentHouseId, LeftDate.ToString("yyyy-MM-dd HH:mm:ss"), RightDate.ToString("yyyy-MM-dd HH:mm:ss")); TemperatureHistoryDataPoints.Clear(); if (a.temperatureList != null) foreach (var item in a.temperatureList) TemperatureHistoryDataPoints.Add(OxyPlot.Axes.DateTimeAxis.CreateDataPoint(item.createTime, (double)item.temperature)); PressureHistoryDataPoints.Clear(); if (a.pressureList != null) foreach (var item in a.pressureList) PressureHistoryDataPoints.Add(DateTimeAxis.CreateDataPoint(item.createTime, item.pressure)); if (HistoryModel.Axes[1] is DateTimeAxis temperatureDateTimeAxis) { temperatureDateTimeAxis.Minimum = DateTimeAxis.ToDouble(LeftDate); temperatureDateTimeAxis.Maximum = DateTimeAxis.ToDouble(RightDate); } HistoryModel.ResetAllAxes(); HistoryModel.InvalidatePlot(true); } private void Init() { string wendu = KeyToStringConvert.GetLanguageStringByKey("C0073"); string shij = KeyToStringConvert.GetLanguageStringByKey("C0284"); string qiya = KeyToStringConvert.GetLanguageStringByKey("C0285"); string yali= KeyToStringConvert.GetLanguageStringByKey("C0074"); double StrokeThicknessD = 3; HistoryModel = new PlotModel(); RealModel = new PlotModel(); string TrackerFormatString = shij + ":{2:MM-dd HH:mm}\n" + wendu + ":{4:0.00}℃"; string TrackerFormatString1 = shij + ":{2:MM-dd HH:mm}\n" + yali + ":{4:0.00}mbar"; HistoryModel.Series.Add(new LineSeries { Title = wendu, ItemsSource = TemperatureHistoryDataPoints, InterpolationAlgorithm = InterpolationAlgorithms.CatmullRomSpline, TrackerFormatString = TrackerFormatString, MarkerType = MarkerType.None, Color = OxyColor.FromRgb(169, 201, 238), StrokeThickness = StrokeThicknessD }); RealModel.Axes.Add(new LinearAxis() { Position = AxisPosition.Left, Minimum = 0, Maximum = 60 }); HistoryModel.Axes.Add(new LinearAxis() { Position = AxisPosition.Left, Minimum = 0, Maximum = 60, IsPanEnabled = true, IsZoomEnabled = true }); RealModel.Series.Add(new LineSeries { Title = wendu, ItemsSource = TemperatureRealDataPoints, InterpolationAlgorithm = InterpolationAlgorithms.CatmullRomSpline, TrackerFormatString = TrackerFormatString, MarkerType = MarkerType.None, Color = OxyColor.FromRgb(106, 170, 245), StrokeThickness = StrokeThicknessD }); HistoryModel.Axes.Add(new DateTimeAxis() { Position = AxisPosition.Bottom, StringFormat = "MM-dd\nHH:mm" }); RealModel.Series.Add(new LineSeries { Title = qiya, ItemsSource = PressureRealDataPoints, InterpolationAlgorithm = InterpolationAlgorithms.CatmullRomSpline, TrackerFormatString = TrackerFormatString1, MarkerType = MarkerType.None, Color = OxyColor.FromRgb(157, 119, 238), StrokeThickness = StrokeThicknessD }); HistoryModel.Series.Add(new LineSeries { Title = qiya, ItemsSource = PressureHistoryDataPoints, InterpolationAlgorithm = InterpolationAlgorithms.CatmullRomSpline, TrackerFormatString = TrackerFormatString1, MarkerType = MarkerType.None, Color = OxyColor.FromRgb(197, 175, 244), StrokeThickness = StrokeThicknessD }); RealModel.Axes.Add(new DateTimeAxis() { Position = AxisPosition.Bottom, StringFormat = "HH:mm", MinorIntervalType = DateTimeIntervalType.Seconds, IntervalType = DateTimeIntervalType.Seconds });//, MinimumMajorStep = 0.01, MajorStep = 0.5 RealModel.Legends.Add(new Legend() { LegendPosition = LegendPosition.BottomCenter, LegendOrientation = LegendOrientation.Horizontal, LegendFontSize = 25, LegendPlacement = LegendPlacement.Outside, LegendSymbolLength = 60 }); HistoryModel.Legends.Add(new Legend() { LegendPosition = LegendPosition.BottomCenter, LegendOrientation = LegendOrientation.Horizontal, LegendFontSize = 25, LegendPlacement = LegendPlacement.Outside, LegendSymbolLength = 60 }); //HistoryModel.LegendPosition = LegendPosition.BottomCenter; //HistoryModel.LegendOrientation = LegendOrientation.Horizontal; //HistoryModel.LegendFontSize = 25; //HistoryModel.LegendPlacement = LegendPlacement.Outside; ////RealModel.LegendItemSpacing = 100; //HistoryModel.LegendSymbolLength = 60; } } }