using LiveChartsCore; using LiveChartsCore.Defaults; using LiveChartsCore.Kernel.Sketches; using LiveChartsCore.SkiaSharpView; using LiveChartsCore.SkiaSharpView.Painting; using Newtonsoft.Json.Linq; using ScottPlot; using ScottPlot.Plottables; using ScottPlot.Statistics; using SkiaSharp; using SunlightCentralizedControlManagement_SCCM_.ViewModel; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Data; using System.Data.Entity.Core.Common.CommandTrees.ExpressionBuilder; using System.Linq; using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Media.Imaging; using System.Windows.Media.Media3D; using System.Windows.Navigation; using System.Windows.Shapes; using System.Windows.Threading; namespace SunlightCentralizedControlManagement_SCCM_.UserControls { /// /// info.xaml 的交互逻辑 /// public partial class info : UserControl, INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } public void RaisePropertyChanged(string propertyName) { if (PropertyChanged != null) { if (propertyName != null) { PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } } private ObservableCollection MTH_values = new ObservableCollection(); public ObservableCollection _series { get; set; } public ObservableCollection Series { get => _series; set { _series = value; OnPropertyChanged(nameof(Series)); } } //x轴时间格式 public Axis[] XAxes { get; set; } = { new DateTimeAxis(TimeSpan.FromSeconds(5) , date => date.ToString("HH:mm")) { MinLimit = DateTime.Now.AddHours(-6).Ticks, MaxLimit = DateTime.Now.AddHours(0.5).Ticks, CrosshairLabelsBackground = SKColors.DarkGray.AsLvcColor(), CrosshairLabelsPaint = new SolidColorPaint(SKColors.DarkSlateBlue, 1), CrosshairPaint = new SolidColorPaint(SKColors.DarkSlateGray, 1), }, }; //颜色 private static readonly SKColor s_blue = new SKColor(25, 118, 210); private static readonly SKColor s_red = new SKColor(229, 57, 53); public ICartesianAxis[] YAxes { get; set; } ={ new Axis //y在此轴上缩放 { MinLimit =0, MaxLimit =160, NamePadding = new LiveChartsCore.Drawing.Padding(0, 20), Padding = new LiveChartsCore.Drawing.Padding(0, 0, 20, 0), TextSize = 12, LabelsPaint = new SolidColorPaint(s_blue), TicksPaint = new SolidColorPaint(s_blue), SubticksPaint = new SolidColorPaint(s_blue), DrawTicksPath = true, CrosshairSnapEnabled = true, Position = LiveChartsCore.Measure.AxisPosition.Start },//温度轴 }; public info() { InitializeComponent(); DataContext = this; } public void DataAdd(DateTime dateTime, double dat) { Dispatcher.Invoke(() => { // 添加新数据点 MTH_values.Add(new DateTimePoint(dateTime, dat)); // 更新X轴范围,始终保持最近6小时 XAxes[0].MinLimit = dateTime.AddHours(-6).Ticks; XAxes[0].MaxLimit = dateTime.AddMinutes(1).Ticks; // 移除6小时前的旧数据 while (MTH_values.Count > 0 && MTH_values[0].DateTime < dateTime.AddHours(-6)) { MTH_values.RemoveAt(0); } }); } public void DataROW(DataRow[] dataRows) { DateTime dateTime = DateTime.Now; foreach (DataRow row in dataRows) { // 添加新数据点 MTH_values.Add(new DateTimePoint((DateTime)row["DateTime"], (double)row["DAT"])); } Dispatcher.Invoke(() => { // 更新X轴范围,始终保持最近6小时 XAxes[0].MinLimit = dateTime.AddHours(-6).Ticks; XAxes[0].MaxLimit = dateTime.AddMinutes(1).Ticks; }); } private void UserControl_Loaded(object sender, RoutedEventArgs e) { // 初始化系列 Series = new ObservableCollection { new LineSeries { Name = Properties.Resources.Temperature, Values = MTH_values, Stroke = new SolidColorPaint(s_red, 2), GeometrySize = 0, GeometryStroke = new SolidColorPaint(s_red, 2), Fill = null, LineSmoothness = 0, ScalesYAt = 0, } }; } } }