将图形库替换为LiveChartsCore.SkiaSharpView.WPF

This commit is contained in:
2025-09-22 06:24:08 +08:00
parent bcdb119d11
commit 0f869cf410
3 changed files with 62 additions and 55 deletions

View File

@@ -161,11 +161,11 @@
<PackageReference Include="HandyControl" Version="3.5.1" />
<PackageReference Include="Hardcodet.NotifyIcon.Wpf" Version="2.0.1" />
<PackageReference Include="iNKORE.UI.WPF.Modern" Version="0.10.0" />
<PackageReference Include="LiveChartsCore.SkiaSharpView.WPF" Version="2.0.0-rc2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.5" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.5" />
<PackageReference Include="NLog" Version="6.0.0" />
<PackageReference Include="ObservableCollections" Version="3.3.4" />
<PackageReference Include="LiveCharts.Wpf" Version="0.9.7" />
</ItemGroup>

View File

@@ -1,3 +1,4 @@
using System.Windows.Media;
using AutoMapper;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
@@ -7,10 +8,13 @@ using DMS.Application.Interfaces.Database;
using DMS.Core.Events;
using DMS.WPF.Interfaces;
using DMS.WPF.ViewModels.Items;
using LiveCharts;
using LiveCharts.Defaults;
using LiveCharts.Wpf;
using LiveChartsCore;
using LiveChartsCore.Defaults;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Painting;
using LiveChartsCore.SkiaSharpView.WPF;
using ObservableCollections;
using SkiaSharp;
namespace DMS.WPF.ViewModels;
@@ -54,13 +58,13 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable
public NotifyCollectionChangedSynchronizedViewList<VariableHistoryDto> VariableHistories { get; }
// 折线图相关属性
public SeriesCollection LineSeriesCollection { get; set; }
public ISeries[] LineSeriesCollection { get; set; }
[ObservableProperty]
private AxesCollection _lineAxisX;
private Axis[] _lineAxisX;
[ObservableProperty]
private AxesCollection _lineAxisY;
private Axis[] _lineAxisY;
private readonly ObservableList<VariableHistoryDto> _variableHistoryList;
private readonly ISynchronizedView<VariableHistoryDto, VariableHistoryDto> _variableHistorySynchronizedView;
@@ -89,14 +93,14 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable
_allVariableHistories = new List<VariableHistoryDto>();
// 初始化默认值
_historyLimit = 1000; // 默认限制1000条记录
_historyLimit = 50; // 默认限制1000条记录
_startTime = null;
_endTime = null;
// 初始化图表属性
LineAxisX = new AxesCollection { new Axis() };
LineAxisY = new AxesCollection { new Axis() };
LineSeriesCollection = new SeriesCollection();
LineAxisX = new Axis[] { new Axis() };
LineAxisY = new Axis[] { new Axis() };
LineSeriesCollection = new ISeries[0];
_eventService.OnVariableValueChanged += OnVariableValueChanged;
}
@@ -116,6 +120,8 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable
};
_variableHistoryList.Add(variableHistory);
// 更新图表数据
UpdateChartData();
}
/// <summary>
@@ -229,13 +235,13 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable
if (_variableHistoryList == null || _variableHistoryList.Count == 0)
{
// 清空图表数据
LineSeriesCollection?.Clear();
LineSeriesCollection = new ISeries[0];
OnPropertyChanged(nameof(LineSeriesCollection));
return;
}
// 创建数值点集合
var values = new ChartValues<DateTimePoint>();
var values = new List<DateTimePoint>();
foreach (var history in _variableHistoryList)
{
@@ -246,40 +252,35 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable
}
}
// 确保LineSeriesCollection已初始化
if (LineSeriesCollection == null)
// 创建线性序列
var series = new LineSeries<DateTimePoint>
{
LineSeriesCollection = new SeriesCollection();
}
// 清空旧数据并添加新系列
LineSeriesCollection.Clear();
LineSeriesCollection.Add(new LineSeries
{
Title = "变量值",
Name = "变量值",
Values = values,
PointGeometry = null, // 不显示数据点,只显示线条
LineSmoothness = 0.5, // 设置线条平滑度
StrokeThickness = 2
});
Fill = null,
Stroke = new SolidColorPaint(new SKColor(41, 128, 185)) { StrokeThickness = 2 },
GeometrySize = 0 // 不显示数据点,只显示线条
};
// 确保坐标轴集合已初始化
if (LineAxisX == null || LineAxisX.Count == 0)
// 更新序列集合
LineSeriesCollection = new ISeries[] { series };
// 更新坐标轴
LineAxisX = new Axis[]
{
LineAxisX = new AxesCollection { new Axis() };
}
if (LineAxisY == null || LineAxisY.Count == 0)
new Axis
{
LineAxisY = new AxesCollection { new Axis() };
Labeler = value => new DateTime((long)value).ToString("MM-dd HH:mm:ss")
}
};
// 设置坐标轴
// X轴使用时间格式化
LineAxisX[0].LabelFormatter = value => new DateTime((long)value).ToString("MM-dd HH:mm:ss");
// Y轴使用数值格式化
LineAxisY[0].Title = "值";
LineAxisY[0].LabelFormatter = value => value.ToString("F2");
LineAxisY = new Axis[]
{
new Axis
{
Name = "值"
}
};
// 通知属性更改
OnPropertyChanged(nameof(LineSeriesCollection));

View File

@@ -15,7 +15,7 @@
xmlns:valueConverts="clr-namespace:DMS.WPF.ValueConverts"
xmlns:vm="clr-namespace:DMS.WPF.ViewModels"
xmlns:converters="clr-namespace:DMS.WPF.Converters"
xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
xmlns:lvc="clr-namespace:LiveChartsCore.SkiaSharpView.WPF;assembly=LiveChartsCore.SkiaSharpView.WPF"
d:DataContext="{d:DesignInstance vm:VariableHistoryViewModel}"
d:DesignHeight="600"
d:DesignWidth="800"
@@ -106,8 +106,11 @@
</StackPanel>
<!-- 变量历史记录列表和图表 -->
<hc:TabControl Margin="20">
<hc:TabItem Header="数据表格">
<TabControl Margin="20">
<TabItem Header="数据表格">
<ui:TabItemHelper.Icon>
<ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Settings}"/>
</ui:TabItemHelper.Icon>
<DataGrid
AutoGenerateColumns="False"
CanUserDeleteRows="False"
@@ -124,13 +127,16 @@
IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
</hc:TabItem>
<hc:TabItem Header="数据图表">
<lvc:CartesianChart Series="{Binding LineSeriesCollection}"
AxisX="{Binding LineAxisX}"
AxisY="{Binding LineAxisY}"
</TabItem>
<TabItem Header="数据图表">
<ui:TabItemHelper.Icon>
<ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Settings}"/>
</ui:TabItemHelper.Icon>
<lvc:CartesianChart ZoomMode="X" Series="{Binding LineSeriesCollection}"
XAxes="{Binding LineAxisX}"
YAxes="{Binding LineAxisY}"
Margin="10"/>
</hc:TabItem>
</hc:TabControl>
</TabItem>
</TabControl>
</DockPanel>
</UserControl>