图表x轴显示优化

This commit is contained in:
hsc
2026-07-17 09:51:11 +08:00
parent 74d544c288
commit c4ec348217
2 changed files with 13 additions and 14 deletions

View File

@@ -222,10 +222,8 @@ namespace MonitorModule.ViewModels
if (channel == null) return;
double time = _stopwatch.Elapsed.TotalSeconds;
// 记录数据点线程安全Record 内部 ConcurrentQueue + Series 操作)
channel.Record(time, args.Value);
channel.Record(args.Time, args.Value);
// 入队数据库批量写入
// CreateTime 使用 args.Time采样触发时刻而不是 DateTime.Now设备响应到达时刻
@@ -252,13 +250,12 @@ namespace MonitorModule.ViewModels
{
if (!_stopwatch.IsRunning) return;
double elapsed = _stopwatch.Elapsed.TotalSeconds;
var now = DateTime.Now;
var xAxis = Plot.Axes.FirstOrDefault(a => a.Position == AxisPosition.Bottom);
if (xAxis != null)
{
double window = 10000 * 0.1; // 20s 视窗
xAxis.Minimum = Math.Max(0, elapsed - window);
xAxis.Maximum = elapsed + 0.5;
xAxis.Minimum = DateTimeAxis.ToDouble(now.AddSeconds(-20));
xAxis.Maximum = DateTimeAxis.ToDouble(now.AddSeconds(0.5));
}
Plot.InvalidatePlot(true);
}
@@ -370,10 +367,11 @@ namespace MonitorModule.ViewModels
PlotAreaBorderColor = OxyColors.LightGray,
Background = OxyColors.White
};
pm.Axes.Add(new LinearAxis
pm.Axes.Add(new DateTimeAxis
{
Position = AxisPosition.Bottom,
Title = "时间 (s)",
Title = "时间",
StringFormat = "HH:mm:ss",
MajorGridlineStyle = LineStyle.Dot,
MinorGridlineStyle = LineStyle.None
});
@@ -822,7 +820,7 @@ namespace MonitorModule.ViewModels
var recent = channel.DataPoints.ToArray();
var startIdx = Math.Max(0, recent.Length - 10000);
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.InvalidatePlot(true);
@@ -846,7 +844,7 @@ namespace MonitorModule.ViewModels
channel.Series.Points.Clear();
var startIdx = Math.Max(0, points.Length - 10000);
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);
}
#endregion

View File

@@ -1,5 +1,6 @@
using OxyPlot.Series;
using OxyPlot;
using System;
using System.Collections.Concurrent;
using Prism.Mvvm;
using NCalc;
@@ -52,7 +53,7 @@ namespace UIShare.UIViewModel
// ===== 数据存储 =====
/// <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>
public int MaxBuffer { get; set; } = 5000;
@@ -67,7 +68,7 @@ namespace UIShare.UIViewModel
// ===== 辅助方法 =====
/// <summary>记录一个数据点RawValue 经数学变换后得到 DisplayValue一并入队</summary>
public void Record(double time, double rawValue)
public void Record(DateTime time, double rawValue)
{
double displayValue = rawValue;
@@ -82,7 +83,7 @@ namespace UIShare.UIViewModel
// 如果正在显示,同步到 Series
if (_series != null)
{
_series.Points.Add(new DataPoint(time, displayValue));
_series.Points.Add(new DataPoint(time.ToOADate(), displayValue));
while (_series.Points.Count > 10000)
{
_series.Points.RemoveAt(0);