using BPASmartClient.Academy._50L; using LiveCharts.Definitions.Charts; using OxyPlot; using OxyPlot.Axes; using OxyPlot.Series; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Shapes; using System.Diagnostics; using ThingsGateway.Foundation.Extension.Generic; using static System.Windows.Forms.LinkLabel; using System.Windows; using System.Windows.Media; using OxyPlot.Wpf; namespace BPASmartClient.Academy.ViewModel { internal class HistoryChart50LViewModel : NotifyBase { public HistoryChart50LViewModel() { LoadNames(SqliteOperate.GetInstance.FindNames(SelectTime)); Select = new BPARelayCommand(() => { LoadNames(SqliteOperate.GetInstance.FindNames(SelectTime)); }); FindProductCommand = new BPARelayCommand(() => { LoadNames(SqliteOperate.GetInstance.FindNames(ProductNum)); }); InspectDataCommand = new BPARelayCommand(s => { OxyInit(s); }); CheckedCommand = new BPARelayCommand(async s => { if (int.TryParse(s, out int index)) { if (VisStatus.Length == OxyDataModels.Count && index >= 0 && index < VisStatus.Length) { IsEnable = false; await Task.Run(() => { sw.Start(); if (VisStatus[index]) OxyModel.Series.Add(OxyDataModels[index].Line); else OxyModel.Series.Remove(OxyDataModels[index].Line); OxyModel.InvalidatePlot(true); //重新加载曲线 IsEnable = true; Debug.WriteLine(sw.ElapsedMilliseconds); sw.Restart(); }); } } }); } Stopwatch sw = new Stopwatch(); private void OxyInit(string num) { OxyModel.Series.Clear(); OxyDataModels.Clear(); OxyModel = new PlotModel() { Title = num }; OxyDataModels.Add(new OxyDataModel(CreateLine("反应釜温度"))); OxyDataModels.Add(new OxyDataModel(CreateLine("反应釜蒸汽压力"))); OxyDataModels.Add(new OxyDataModel(CreateLine("反应釜蒸汽流量"))); OxyDataModels.Add(new OxyDataModel(CreateLine("冷凝水罐温度"))); OxyDataModels.Add(new OxyDataModel(CreateLine("冷凝水罐湿度"))); OxyDataModels.Add(new OxyDataModel(CreateLine("负压流量"))); OxyDataModels.Add(new OxyDataModel(CreateLine("称重水罐重量"))); OxyDataModels.Add(new OxyDataModel(CreateLine("比例阀实际开度"))); SqliteOperate.GetInstance.QueryableByNum(num).ForEach(item => { double x = DateTimeAxis.ToDouble(item.Createtime); OxyDataModels[0].dataPoints.Add(new DataPoint(x, item.Temperature)); OxyDataModels[1].dataPoints.Add(new DataPoint(x, item.SteamPressure)); OxyDataModels[2].dataPoints.Add(new DataPoint(x, item.SteamFlowRate)); OxyDataModels[3].dataPoints.Add(new DataPoint(x, item.CondensateWaterTemperature)); OxyDataModels[4].dataPoints.Add(new DataPoint(x, item.CondensateWaterHumidity)); OxyDataModels[5].dataPoints.Add(new DataPoint(x, item.NegativePressureFlowRate)); OxyDataModels[6].dataPoints.Add(new DataPoint(x, item.WeighingWaterTankWeight)); OxyDataModels[7].dataPoints.Add(new DataPoint(x, item.ProportionalValveOpening)); }); OxyDataModels.ForEach(item => { item.AddRange(); }); if (!(OxyModel.Axes.Count > 0)) { OxyModel.Axes.Add(new DateTimeAxis() { Position = OxyPlot.Axes.AxisPosition.Bottom, Title = "时间", StringFormat = "hh:mm:ss", AxislineColor = OxyColor.FromRgb(84, 164, 227) }); OxyModel.Axes.Add(new LinearAxis() { Key = "y1", Title = "温度/℃\r\n压力/kap\r\n流量/m³", Position = OxyPlot.Axes.AxisPosition.Left, LabelFormatter = value => value.ToString(), AxislineColor = OxyColor.Parse("#9C9C9C"), AxislineStyle = LineStyle.Solid, AxislineThickness = 1, }); } } private LineSeries CreateLine(string title) { var scb = Application.Current.TryFindResource(title) as SolidColorBrush; scb.ToOxyColor(); return new LineSeries() { LineStyle = LineStyle.Solid, Title = title, MarkerFill = scb.ToOxyColor(), MarkerSize = 2, LineLegendPosition = LineLegendPosition.End }; } private void LoadNames(List data) { if (data == null) return; if (data.Count <= 0) { MessageNotify.GetInstance.OpenMsg("未查询到有效记录"); return; } RecipeCharts.Clear(); data.ForEach(item => { RecipeCharts.Add(new RecipeChart() { Num = RecipeCharts.Count + 1, CreateTime = item.Createtime, Name = item.ProductNumber }); }); } public List OxyDataModels { get; set; } = new List(); public ObservableCollection RecipeCharts { get; set; } = new ObservableCollection(); public DateTime SelectTime { get { return _mSelectTime; } set { _mSelectTime = value; OnPropertyChanged(); } } private DateTime _mSelectTime = DateTime.Now; public string ProductNum { get { return _mProductNum; } set { _mProductNum = value; OnPropertyChanged(); } } private string _mProductNum; public PlotModel OxyModel { get { return _mOxyModel; } set { _mOxyModel = value; OnPropertyChanged(); } } private PlotModel _mOxyModel = new PlotModel(); public bool[] VisStatus { get { return _mVisStatus; } set { _mVisStatus = value; OnPropertyChanged(); } } private bool[] _mVisStatus = new bool[8]; public bool IsEnable { get { return _mIsEnable; } set { _mIsEnable = value; LoadingVis = value ? Visibility.Collapsed : Visibility.Visible; OnPropertyChanged(); } } private bool _mIsEnable = true; public Visibility LoadingVis { get { return _mLoadingVis; } set { _mLoadingVis = value; OnPropertyChanged(); } } private Visibility _mLoadingVis = Visibility.Collapsed; public BPARelayCommand Select { get; set; } public BPARelayCommand FindProductCommand { get; set; } public BPARelayCommand InspectDataCommand { get; set; } public BPARelayCommand CheckedCommand { get; set; } } public class OxyDataModel { public LineSeries Line { get; set; } = new LineSeries(); public List dataPoints { get; set; } = new List(); public void AddRange() { Line.Points.AddRange(dataPoints); } public OxyDataModel(LineSeries line) { Line = line; } } }