diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 3da939d..2575b7a 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -4,16 +4,11 @@ using LiveCharts.Wpf; using SqlSugar; using System.Windows; using System.Windows.Controls; -using System.Windows.Media; -using System.Windows.Navigation; +using WpfApp.Services; using WpfApp.src.components; -using WpfApp.src.config; -using WpfApp.src.view; using WpfApp.src.controllers; using WpfApp.Utils; - - namespace WpfApp; /// @@ -25,6 +20,8 @@ public partial class MainWindow : Window private readonly DatabaseService db; private readonly EventService even; private readonly MenuController menu; + private readonly ChartManager chartManager; + private readonly CancellationTokenSource chartCts = new CancellationTokenSource(); public MainWindow(LogService logService, DatabaseService databaseService, EventService eventService) { @@ -33,8 +30,7 @@ public partial class MainWindow : Window log = logService; db = databaseService; even = eventService; - // 初始化菜单控制器 - menu = new MenuController(this); + menu = new MenuController(this);// 初始化菜单控制器 // 记录窗口初始化日志 log.Info("MainWindow", "主窗口已通过依赖注入初始化"); @@ -43,25 +39,17 @@ public partial class MainWindow : Window { log.Info($"接收到事件:{d.Data}"); }); - SetWindowTitle("FMSDGAUGE"); + + WindowHelper.SetWindowTitle(this, "FMSDGAUGE"); // 设置标题 - var labels = new[] { "13:27 23", "13:27 45", "13:27 49", "13:27 50", "13:27 55" }; + chartManager = new ChartManager(Dispatcher); + chartManager.InitCharts(myChart, myChart1, myChart2); // 初始化图表 + + // 启动实时数据刷新 + chartManager.StartAutoUpdate(myChart, chartCts.Token); + chartManager.StartAutoUpdate(myChart1, chartCts.Token); + chartManager.StartAutoUpdate(myChart2, chartCts.Token); - var config1 = new ChartConfig - { - ColumnData = new List { 20, 10, 12, 5, 9 }, - Labels = new List(labels), - LineConfigs = new List - { - new LineConfig { Title = "标准1", Values = new List { 5,5,5,5,5 }, Stroke = Brushes.Blue }, - new LineConfig { Title = "标准2", Values = new List { 10,10,10,10,10 }, Stroke = Brushes.Green }, - new LineConfig { Title = "标准3", Values = new List { 30,30,30,30,30 }, Stroke = Brushes.Red, Unit = "A" } - } - }; - InitChart.InitCartesianChart(myChart, config1); - InitChart.InitCartesianChart(myChart1, config1); - InitChart.InitCartesianChart(myChart2, config1); - } private void Button_Click(object sender, RoutedEventArgs e) @@ -74,15 +62,6 @@ public partial class MainWindow : Window { } - /// - /// 设置窗体标题的函数 - /// - /// - public void SetWindowTitle(string newTitle) - { - this.Title = newTitle; - } - @@ -103,11 +82,8 @@ public partial class MainWindow : Window /// /// /// - private void OnCloseMainWindowButtonClick(object sender, RoutedEventArgs e) - { - this.Close(); + private void OnCloseMainWindowButtonClick(object sender, RoutedEventArgs e) => WindowHelper.CloseWindow(this); - } /// /// 测试按钮 @@ -119,7 +95,7 @@ public partial class MainWindow : Window //LineBarChartDemo lineBarChartDemo = new LineBarChartDemo(); //lineBarChartDemo.Show(); } - + #endregion /// /// 等级选择下拉框变化事件处理 /// @@ -150,5 +126,5 @@ public partial class MainWindow : Window Sensor3.Level = selectedLevel; } } - #endregion + } \ No newline at end of file diff --git a/Services/ChartManager.cs b/Services/ChartManager.cs index 1ec731e..c93a1cc 100644 --- a/Services/ChartManager.cs +++ b/Services/ChartManager.cs @@ -1,11 +1,92 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using LiveCharts.Wpf; +using System.Windows.Media; +using System.Windows.Threading; +using WpfApp.src.config; +using WpfApp.Utils; -namespace WpfApp.Services; - -public class ChartManager +namespace WpfApp.Services { + /// + /// Chart 管理器 - 负责图表初始化和实时更新 + /// + public class ChartManager + { + private readonly Dispatcher dispatcher; + + public ChartManager(Dispatcher uiDispatcher) + { + dispatcher = uiDispatcher; + } + + /// + /// 初始化图表 + /// + public void InitCharts(params CartesianChart[] charts) + { + var labels = new[] { "13:27 23", "13:27 45", "13:27 49", "13:27 50", "13:27 55" }; + + var config = new ChartConfig + { + ColumnData = new List { 20, 10, 12, 5, 9 }, + Labels = labels.ToList(), + LineConfigs = new List + { + new LineConfig { Title = "标准1", Values = Enumerable.Repeat(5.0,5).ToList(), Stroke = Brushes.Blue }, + new LineConfig { Title = "标准2", Values = Enumerable.Repeat(10.0,5).ToList(), Stroke = Brushes.Green }, + new LineConfig { Title = "标准3", Values = Enumerable.Repeat(30.0,5).ToList(), Stroke = Brushes.Red, Unit="A" } + } + }; + + foreach (var chart in charts) + { + InitChart.InitCartesianChart(chart, config); + } + } + + /// + /// 动态更新图表数据(线程安全) + /// + public void UpdateChart(CartesianChart chart, List columnData, List lineData) + { + dispatcher.Invoke(() => + { + var config = new ChartConfig + { + ColumnData = columnData, + Labels = chart.AxisX[0].Labels.ToList(), + LineConfigs = lineData + }; + InitChart.InitCartesianChart(chart, config); + }); + } + + /// + /// 模拟后台数据采集并实时刷新图表 + /// + public void StartAutoUpdate(CartesianChart chart, CancellationToken token) + { + Task.Run(async () => + { + var rand = new Random(); + + while (!token.IsCancellationRequested) + { + // 模拟采集数据 + var columnData = Enumerable.Range(0, 5).Select(_ => rand.NextDouble() * 50).ToList(); + var lineData = new List + { + new LineConfig { Title = "标准1", Values = Enumerable.Range(0,5).Select(_ => rand.NextDouble()*20).ToList(), Stroke = Brushes.Blue }, + new LineConfig { Title = "标准2", Values = Enumerable.Range(0,5).Select(_ => rand.NextDouble()*30).ToList(), Stroke = Brushes.Green }, + new LineConfig { Title = "标准3", Values = Enumerable.Range(0,5).Select(_ => rand.NextDouble()*40).ToList(), Stroke = Brushes.Yellow } + + }; + + // 更新图表(通过 Dispatcher 保证线程安全) + UpdateChart(chart, columnData, lineData); + + await Task.Delay(5000); // 每秒更新一次 + } + }, token); + } + } } diff --git a/Utils/WindowHelper.cs b/Utils/WindowHelper.cs index 7c4dfd1..cad45c6 100644 --- a/Utils/WindowHelper.cs +++ b/Utils/WindowHelper.cs @@ -45,5 +45,27 @@ namespace WpfApp.Utils navWin.ShowDialog(); } + + /// + /// 设置指定窗口的标题 + /// + /// 目标窗口 + /// 新标题 + public static void SetWindowTitle(Window window, string newTitle) + { + if (window != null) + { + window.Title = newTitle; + } + } + + /// + /// 关闭窗口 + /// + /// + public static void CloseWindow(Window window) + { + window?.Close(); + } } }