using Arction.Wpf.Charting; using Arction.Wpf.Charting.Annotations; using Arction.Wpf.Charting.Axes; using Arction.Wpf.Charting.Series3D; using Arction.Wpf.Charting.SeriesXY; using Arction.Wpf.Charting.Views.ViewXY; using ImTools; using Prism.Mvvm; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml.Linq; namespace CapMachine.Wpf.Models { /// /// 曲线实时系列模型 /// public class ChartRtSeries:BindableBase { public ChartRtSeries(AxisY axisY, LightningChart lightningChart, string SeriesName) { //var CreateAxisY = new AxisY(); //CreateAxisY.Title.Text = Name; //CreateAxisY.Title.AllowDragging = false; //CreateAxisY.SetRange(0, 100); //CreateAxisY.MajorGrid.Visible = false; ////取消滚轮缩放 //CreateAxisY.ZoomingEnabled = false; //模型赋值 LightningChartInstance = lightningChart; ChartAxisY = axisY; ChartAxisX = lightningChart.ViewXY.XAxes[0]; Name = SeriesName; Unit = "℃"; //判断别重复,因为可能共用Y轴的信息 //绑定lightningChart lightningChart.ViewXY.YAxes.Add(axisY); ChartPointLineSeries = new PointLineSeries(lightningChart.ViewXY, lightningChart.ViewXY.XAxes[0], axisY); ChartPointLineSeries.Title.Text = SeriesName; //绑定lightningChart lightningChart.ViewXY.PointLineSeries.Add(ChartPointLineSeries); } ///// ///// 名称 ///// //public string? Name { get; set; } ///// ///// 值 ///// //public double Value { get; set; } ///// ///// Max ///// //public double Max { get; set; } ///// ///// Min ///// //public double Min { get; set; } ///// ///// 单位 ///// //public string? Unit { get; set; } private string? _Name; /// /// 名称 /// public string? Name { get { return _Name; } set { _Name = value; RaisePropertyChanged(); } } private double _Value; /// /// 值 /// public double Value { get { return _Value; } set { _Value = value; RaisePropertyChanged(); } } private double _Max; /// /// Max /// public double Max { get { return _Max; } set { _Max = value; RaisePropertyChanged(); } } private double _Min; /// /// Min /// public double Min { get { return _Min; } set { _Min = value; RaisePropertyChanged(); } } private string? _Unit; /// /// 单位 /// public string? Unit { get { return _Unit; } set { _Unit = value; RaisePropertyChanged(); } } /// /// 曲线 /// public PointLineSeries? ChartPointLineSeries { get; set; } /// /// Y轴 /// public AxisY? ChartAxisY { get; set; } /// /// X轴 /// public AxisX? ChartAxisX { get; set; } /// /// LightningChart 实例 /// public LightningChart LightningChartInstance { get; set; } /// /// X轴滚动 /// private bool ScrollEnable = true; /// /// Chart图表X轴的跨度-秒 /// private int ChartXDateTimeRange = 3600; /// /// 增加数据 /// public void AddValue(double Data) { //foreach (var item in lightningChart1.ViewXY.PointLineSeries) //{ //Disable updates, to prevent several extra refreshes LightningChartInstance.BeginUpdate(); //Array for 1 point SeriesPoint[] points = new SeriesPoint[1]; //Convert 'Now' to X value var _previousX = LightningChartInstance.ViewXY.XAxes[0].DateTimeToAxisValue(DateTime.Now); points[0].X = _previousX; points[0].Y = Data; //Add the new point into end of first PointLineSeries ChartPointLineSeries.AddPoints(points, false); ChartPointLineSeries.DeletePointsBeforeX(_previousX - ChartXDateTimeRange); //item.DeletePointsBeforeX(_previousX - 100); if (ScrollEnable) { LightningChartInstance.ViewXY.XAxes[0].ScrollPosition = _previousX; } //Allow updates again, and update LightningChartInstance.EndUpdate(); //} } /// /// Solve value from series data points array. Accurate method, but slower than SolveValueCoarse /// /// Series /// X value /// Output Y value /// Success status private bool SolveValueAccurate(PointLineSeries series, double xValue, out double yValue) { AxisY axisY = LightningChartInstance.ViewXY.YAxes[series.AssignYAxisIndex]; yValue = 0; LineSeriesValueSolveResult result = series.SolveYValueAtXValue(xValue); if (result.SolveStatus == LineSeriesSolveStatus.OK) { //PointLineSeries may have two or more points at same X value. If so, center it between min and max yValue = (result.YMax + result.YMin) / 2.0; return true; } else { return false; } } /// /// Solve value from screen coordinates. Faster method, but not less accurateValue than SolveValueAccurate /// /// Series /// X value /// Output Y value /// Success status private bool SolveValueCoarse(PointLineSeries series, double xValue, out double yValue) { AxisY axisY = LightningChartInstance.ViewXY.YAxes[series.AssignYAxisIndex]; float coordX = LightningChartInstance.ViewXY.XAxes[0].ValueToCoord(xValue); float coordY; yValue = 0; LineSeriesCoordinateSolveResult result = series.SolveYCoordAtXCoord(coordX); if (result.SolveStatus == LineSeriesSolveStatus.OK) { coordY = (result.CoordBottom + result.CoordTop) / 2f; if (axisY.CoordToValue((int)Math.Round(coordY), out yValue) == false) { return false; } } else { return false; } return true; } } }