图表x轴显示优化
This commit is contained in:
@@ -222,10 +222,8 @@ namespace MonitorModule.ViewModels
|
|||||||
|
|
||||||
if (channel == null) return;
|
if (channel == null) return;
|
||||||
|
|
||||||
double time = _stopwatch.Elapsed.TotalSeconds;
|
|
||||||
|
|
||||||
// 记录数据点(线程安全:Record 内部 ConcurrentQueue + Series 操作)
|
// 记录数据点(线程安全:Record 内部 ConcurrentQueue + Series 操作)
|
||||||
channel.Record(time, args.Value);
|
channel.Record(args.Time, args.Value);
|
||||||
|
|
||||||
// 入队数据库批量写入
|
// 入队数据库批量写入
|
||||||
// CreateTime 使用 args.Time(采样触发时刻),而不是 DateTime.Now(设备响应到达时刻),
|
// CreateTime 使用 args.Time(采样触发时刻),而不是 DateTime.Now(设备响应到达时刻),
|
||||||
@@ -252,13 +250,12 @@ namespace MonitorModule.ViewModels
|
|||||||
{
|
{
|
||||||
if (!_stopwatch.IsRunning) return;
|
if (!_stopwatch.IsRunning) return;
|
||||||
|
|
||||||
double elapsed = _stopwatch.Elapsed.TotalSeconds;
|
var now = DateTime.Now;
|
||||||
var xAxis = Plot.Axes.FirstOrDefault(a => a.Position == AxisPosition.Bottom);
|
var xAxis = Plot.Axes.FirstOrDefault(a => a.Position == AxisPosition.Bottom);
|
||||||
if (xAxis != null)
|
if (xAxis != null)
|
||||||
{
|
{
|
||||||
double window = 10000 * 0.1; // 20s 视窗
|
xAxis.Minimum = DateTimeAxis.ToDouble(now.AddSeconds(-20));
|
||||||
xAxis.Minimum = Math.Max(0, elapsed - window);
|
xAxis.Maximum = DateTimeAxis.ToDouble(now.AddSeconds(0.5));
|
||||||
xAxis.Maximum = elapsed + 0.5;
|
|
||||||
}
|
}
|
||||||
Plot.InvalidatePlot(true);
|
Plot.InvalidatePlot(true);
|
||||||
}
|
}
|
||||||
@@ -370,10 +367,11 @@ namespace MonitorModule.ViewModels
|
|||||||
PlotAreaBorderColor = OxyColors.LightGray,
|
PlotAreaBorderColor = OxyColors.LightGray,
|
||||||
Background = OxyColors.White
|
Background = OxyColors.White
|
||||||
};
|
};
|
||||||
pm.Axes.Add(new LinearAxis
|
pm.Axes.Add(new DateTimeAxis
|
||||||
{
|
{
|
||||||
Position = AxisPosition.Bottom,
|
Position = AxisPosition.Bottom,
|
||||||
Title = "时间 (s)",
|
Title = "时间",
|
||||||
|
StringFormat = "HH:mm:ss",
|
||||||
MajorGridlineStyle = LineStyle.Dot,
|
MajorGridlineStyle = LineStyle.Dot,
|
||||||
MinorGridlineStyle = LineStyle.None
|
MinorGridlineStyle = LineStyle.None
|
||||||
});
|
});
|
||||||
@@ -822,7 +820,7 @@ namespace MonitorModule.ViewModels
|
|||||||
var recent = channel.DataPoints.ToArray();
|
var recent = channel.DataPoints.ToArray();
|
||||||
var startIdx = Math.Max(0, recent.Length - 10000);
|
var startIdx = Math.Max(0, recent.Length - 10000);
|
||||||
for (int i = startIdx; i < recent.Length; i++)
|
for (int i = startIdx; i < recent.Length; i++)
|
||||||
channel.Series.Points.Add(new DataPoint(recent[i].Time, recent[i].DisplayValue));
|
channel.Series.Points.Add(new DataPoint(recent[i].Time.ToOADate(), recent[i].DisplayValue));
|
||||||
|
|
||||||
Plot.Series.Add(channel.Series);
|
Plot.Series.Add(channel.Series);
|
||||||
Plot.InvalidatePlot(true);
|
Plot.InvalidatePlot(true);
|
||||||
@@ -846,7 +844,7 @@ namespace MonitorModule.ViewModels
|
|||||||
channel.Series.Points.Clear();
|
channel.Series.Points.Clear();
|
||||||
var startIdx = Math.Max(0, points.Length - 10000);
|
var startIdx = Math.Max(0, points.Length - 10000);
|
||||||
for (int i = startIdx; i < points.Length; i++)
|
for (int i = startIdx; i < points.Length; i++)
|
||||||
channel.Series.Points.Add(new DataPoint(points[i].Time, points[i].DisplayValue));
|
channel.Series.Points.Add(new DataPoint(points[i].Time.ToOADate(), points[i].DisplayValue));
|
||||||
Plot.InvalidatePlot(true);
|
Plot.InvalidatePlot(true);
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using OxyPlot.Series;
|
using OxyPlot.Series;
|
||||||
using OxyPlot;
|
using OxyPlot;
|
||||||
|
using System;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using Prism.Mvvm;
|
using Prism.Mvvm;
|
||||||
using NCalc;
|
using NCalc;
|
||||||
@@ -52,7 +53,7 @@ namespace UIShare.UIViewModel
|
|||||||
|
|
||||||
// ===== 数据存储 =====
|
// ===== 数据存储 =====
|
||||||
/// <summary>所有采样数据点(线程安全),与 OxyPlot 无关</summary>
|
/// <summary>所有采样数据点(线程安全),与 OxyPlot 无关</summary>
|
||||||
public ConcurrentQueue<(double Time, double RawValue, double DisplayValue)> DataPoints { get; } = new();
|
public ConcurrentQueue<(DateTime Time, double RawValue, double DisplayValue)> DataPoints { get; } = new();
|
||||||
|
|
||||||
/// <summary>最大缓冲数据点数,超出则丢弃最旧的</summary>
|
/// <summary>最大缓冲数据点数,超出则丢弃最旧的</summary>
|
||||||
public int MaxBuffer { get; set; } = 5000;
|
public int MaxBuffer { get; set; } = 5000;
|
||||||
@@ -67,7 +68,7 @@ namespace UIShare.UIViewModel
|
|||||||
|
|
||||||
// ===== 辅助方法 =====
|
// ===== 辅助方法 =====
|
||||||
/// <summary>记录一个数据点:RawValue 经数学变换后得到 DisplayValue,一并入队</summary>
|
/// <summary>记录一个数据点:RawValue 经数学变换后得到 DisplayValue,一并入队</summary>
|
||||||
public void Record(double time, double rawValue)
|
public void Record(DateTime time, double rawValue)
|
||||||
{
|
{
|
||||||
double displayValue = rawValue;
|
double displayValue = rawValue;
|
||||||
|
|
||||||
@@ -82,7 +83,7 @@ namespace UIShare.UIViewModel
|
|||||||
// 如果正在显示,同步到 Series
|
// 如果正在显示,同步到 Series
|
||||||
if (_series != null)
|
if (_series != null)
|
||||||
{
|
{
|
||||||
_series.Points.Add(new DataPoint(time, displayValue));
|
_series.Points.Add(new DataPoint(time.ToOADate(), displayValue));
|
||||||
while (_series.Points.Count > 10000)
|
while (_series.Points.Count > 10000)
|
||||||
{
|
{
|
||||||
_series.Points.RemoveAt(0);
|
_series.Points.RemoveAt(0);
|
||||||
|
|||||||
Reference in New Issue
Block a user