剥离图表初始化和更新
This commit is contained in:
parent
6e14f38a41
commit
b19325cd2a
|
|
@ -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;
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -25,6 +20,8 @@ public partial class MainWindow : Window
|
|||
private readonly DatabaseService db;
|
||||
private readonly EventService<GeneralEventArgs> even;
|
||||
private readonly MenuController menu;
|
||||
private readonly ChartManager chartManager;
|
||||
private readonly CancellationTokenSource chartCts = new CancellationTokenSource();
|
||||
|
||||
public MainWindow(LogService logService, DatabaseService databaseService, EventService<GeneralEventArgs> 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<double> { 20, 10, 12, 5, 9 },
|
||||
Labels = new List<string>(labels),
|
||||
LineConfigs = new List<LineConfig>
|
||||
{
|
||||
new LineConfig { Title = "标准1", Values = new List<double> { 5,5,5,5,5 }, Stroke = Brushes.Blue },
|
||||
new LineConfig { Title = "标准2", Values = new List<double> { 10,10,10,10,10 }, Stroke = Brushes.Green },
|
||||
new LineConfig { Title = "标准3", Values = new List<double> { 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
|
|||
{
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// 设置窗体标题的函数
|
||||
/// </summary>
|
||||
/// <param name="newTitle"></param>
|
||||
public void SetWindowTitle(string newTitle)
|
||||
{
|
||||
this.Title = newTitle;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -103,11 +82,8 @@ public partial class MainWindow : Window
|
|||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void OnCloseMainWindowButtonClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
this.Close();
|
||||
private void OnCloseMainWindowButtonClick(object sender, RoutedEventArgs e) => WindowHelper.CloseWindow(this);
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试按钮
|
||||
|
|
@ -119,7 +95,7 @@ public partial class MainWindow : Window
|
|||
//LineBarChartDemo lineBarChartDemo = new LineBarChartDemo();
|
||||
//lineBarChartDemo.Show();
|
||||
}
|
||||
|
||||
#endregion
|
||||
/// <summary>
|
||||
/// 等级选择下拉框变化事件处理
|
||||
/// </summary>
|
||||
|
|
@ -150,5 +126,5 @@ public partial class MainWindow : Window
|
|||
Sensor3.Level = selectedLevel;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
|
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Chart 管理器 - 负责图表初始化和实时更新
|
||||
/// </summary>
|
||||
public class ChartManager
|
||||
{
|
||||
private readonly Dispatcher dispatcher;
|
||||
|
||||
public ChartManager(Dispatcher uiDispatcher)
|
||||
{
|
||||
dispatcher = uiDispatcher;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化图表
|
||||
/// </summary>
|
||||
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<double> { 20, 10, 12, 5, 9 },
|
||||
Labels = labels.ToList(),
|
||||
LineConfigs = new List<LineConfig>
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 动态更新图表数据(线程安全)
|
||||
/// </summary>
|
||||
public void UpdateChart(CartesianChart chart, List<double> columnData, List<LineConfig> lineData)
|
||||
{
|
||||
dispatcher.Invoke(() =>
|
||||
{
|
||||
var config = new ChartConfig
|
||||
{
|
||||
ColumnData = columnData,
|
||||
Labels = chart.AxisX[0].Labels.ToList(),
|
||||
LineConfigs = lineData
|
||||
};
|
||||
InitChart.InitCartesianChart(chart, config);
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 模拟后台数据采集并实时刷新图表
|
||||
/// </summary>
|
||||
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<LineConfig>
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,5 +45,27 @@ namespace WpfApp.Utils
|
|||
|
||||
navWin.ShowDialog();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定窗口的标题
|
||||
/// </summary>
|
||||
/// <param name="window">目标窗口</param>
|
||||
/// <param name="newTitle">新标题</param>
|
||||
public static void SetWindowTitle(Window window, string newTitle)
|
||||
{
|
||||
if (window != null)
|
||||
{
|
||||
window.Title = newTitle;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 关闭窗口
|
||||
/// </summary>
|
||||
/// <param name="window"></param>
|
||||
public static void CloseWindow(Window window)
|
||||
{
|
||||
window?.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user