251 lines
7.4 KiB
C#
251 lines
7.4 KiB
C#
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
|
||
{
|
||
/// <summary>
|
||
/// 曲线实时系列模型
|
||
/// </summary>
|
||
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);
|
||
}
|
||
|
||
///// <summary>
|
||
///// 名称
|
||
///// </summary>
|
||
//public string? Name { get; set; }
|
||
|
||
///// <summary>
|
||
///// 值
|
||
///// </summary>
|
||
//public double Value { get; set; }
|
||
|
||
///// <summary>
|
||
///// Max
|
||
///// </summary>
|
||
//public double Max { get; set; }
|
||
|
||
///// <summary>
|
||
///// Min
|
||
///// </summary>
|
||
//public double Min { get; set; }
|
||
|
||
///// <summary>
|
||
///// 单位
|
||
///// </summary>
|
||
//public string? Unit { get; set; }
|
||
|
||
private string? _Name;
|
||
/// <summary>
|
||
/// 名称
|
||
/// </summary>
|
||
public string? Name
|
||
{
|
||
get { return _Name; }
|
||
set { _Name = value; RaisePropertyChanged(); }
|
||
}
|
||
|
||
private double _Value;
|
||
/// <summary>
|
||
/// 值
|
||
/// </summary>
|
||
public double Value
|
||
{
|
||
get { return _Value; }
|
||
set { _Value = value; RaisePropertyChanged(); }
|
||
}
|
||
|
||
private double _Max;
|
||
/// <summary>
|
||
/// Max
|
||
/// </summary>
|
||
public double Max
|
||
{
|
||
get { return _Max; }
|
||
set { _Max = value; RaisePropertyChanged(); }
|
||
}
|
||
|
||
private double _Min;
|
||
/// <summary>
|
||
/// Min
|
||
/// </summary>
|
||
public double Min
|
||
{
|
||
get { return _Min; }
|
||
set { _Min = value; RaisePropertyChanged(); }
|
||
}
|
||
|
||
|
||
private string? _Unit;
|
||
/// <summary>
|
||
/// 单位
|
||
/// </summary>
|
||
public string? Unit
|
||
{
|
||
get { return _Unit; }
|
||
set { _Unit = value; RaisePropertyChanged(); }
|
||
}
|
||
|
||
/// <summary>
|
||
/// 曲线
|
||
/// </summary>
|
||
public PointLineSeries? ChartPointLineSeries { get; set; }
|
||
|
||
/// <summary>
|
||
/// Y轴
|
||
/// </summary>
|
||
public AxisY? ChartAxisY { get; set; }
|
||
|
||
/// <summary>
|
||
/// X轴
|
||
/// </summary>
|
||
public AxisX? ChartAxisX { get; set; }
|
||
|
||
/// <summary>
|
||
/// LightningChart 实例
|
||
/// </summary>
|
||
public LightningChart LightningChartInstance { get; set; }
|
||
|
||
/// <summary>
|
||
/// X轴滚动
|
||
/// </summary>
|
||
private bool ScrollEnable = true;
|
||
|
||
|
||
/// <summary>
|
||
/// Chart图表X轴的跨度-秒
|
||
/// </summary>
|
||
private int ChartXDateTimeRange = 3600;
|
||
|
||
/// <summary>
|
||
/// 增加数据
|
||
/// </summary>
|
||
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();
|
||
|
||
//}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Solve value from series data points array. Accurate method, but slower than SolveValueCoarse
|
||
/// </summary>
|
||
/// <param name="series">Series</param>
|
||
/// <param name="xValue">X value</param>
|
||
/// <param name="yValue">Output Y value</param>
|
||
/// <returns>Success status</returns>
|
||
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;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Solve value from screen coordinates. Faster method, but not less accurateValue than SolveValueAccurate
|
||
/// </summary>
|
||
/// <param name="series">Series</param>
|
||
/// <param name="xValue">X value</param>
|
||
/// <param name="yValue">Output Y value</param>
|
||
/// <returns>Success status</returns>
|
||
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;
|
||
}
|
||
|
||
}
|
||
}
|