This commit is contained in:
YONGYE 2025-10-11 15:36:43 +08:00
parent 18a63a1c1d
commit 0a2798497f
5 changed files with 757 additions and 814 deletions

View File

@ -4,13 +4,16 @@ using LiveCharts.Wpf;
using SqlSugar; using SqlSugar;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Navigation; using System.Windows.Navigation;
using WpfApp.src.components; using WpfApp.src.components;
using WpfApp.src.config;
using WpfApp.src.view; using WpfApp.src.view;
using WpfApp.Utils;
namespace WpfApp namespace WpfApp;
{
/// <summary> /// <summary>
/// Interaction logic for MainWindow.xaml /// Interaction logic for MainWindow.xaml
/// </summary> /// </summary>
@ -35,173 +38,25 @@ namespace WpfApp
{ {
log.Info($"接收到事件:{d.Data}"); log.Info($"接收到事件:{d.Data}");
}); });
#region 线
SetWindowTitle("FMSDGAUGE"); SetWindowTitle("FMSDGAUGE");
// 传感器1 var labels = new[] { "13:27 23", "13:27 45", "13:27 49", "13:27 50", "13:27 55" };
// 初始化Series
myChart.Series = new SeriesCollection var config1 = new ChartConfig
{ {
// 柱状图 ColumnData = new List<double> { 20, 10, 12, 5, 9 },
new ColumnSeries Labels = new List<string>(labels),
LineConfigs = new List<LineConfig>
{ {
Title = "柱状数据", new LineConfig { Title = "标准1", Values = new List<double> { 5,5,5,5,5 }, Stroke = Brushes.Blue },
Values = new ChartValues<double> { 20, 10, 12, 5, 9 } 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" }
// 标准1
new LineSeries
{
Title = "标准1",
Values = new ChartValues<double> { 5, 5, 5, 5, 5 },
DataLabels = true,
LabelPoint = value => value.Y.ToString("0.##") + " Hz",
StrokeThickness = 2,
Fill = System.Windows.Media.Brushes.Transparent
},
// 标准2
new LineSeries
{
Title = "标准2",
Values = new ChartValues<double> { 10, 10, 10, 10, 10 },
DataLabels = true,
LabelPoint = value => value.Y.ToString("0.##") + " Hz",
//Foreground = System.Windows.Media.Brushes.Blue,// 标签颜色
//FontSize = 14,// 字体大小
// StrokeThickness = 2, // 线条粗细
StrokeThickness = 2,
Stroke = System.Windows.Media.Brushes.Green,
Fill = System.Windows.Media.Brushes.Transparent
},
// 标准3
new LineSeries
{
Title = "标准3",
Values = new ChartValues<double> { 30, 30, 30, 30, 30 },
DataLabels = true,
LabelPoint = value => value.Y.ToString("0.##") + " Hz",
StrokeThickness = 2,
Stroke = System.Windows.Media.Brushes.Red,
Fill = System.Windows.Media.Brushes.Transparent
} }
}; };
InitChart.InitCartesianChart(myChart, config1);
InitChart.InitCartesianChart(myChart1, config1);
InitChart.InitCartesianChart(myChart2, config1);
// 设置X轴标签
myChart.AxisX.Add(new Axis
{
Title = "",
Labels = new[] { "13:27 23", "13:27 45", "13:27 49", "13:27 50", "13:27 55" }
});
// 设置Y轴
myChart.AxisY.Add(new Axis
{
Title = ""
});
// 传感器2
// 初始化Series
myChart1.Series = new SeriesCollection
{
// 柱状图
new ColumnSeries
{
Title = "柱状数据",
Values = new ChartValues<double> { 20, 10, 12, 5, 9 }
},
// 标准1
new LineSeries
{
Title = "标准1",
Values = new ChartValues<double> { 5, 5, 5, 5, 5 },
StrokeThickness = 2,
Fill = System.Windows.Media.Brushes.Transparent
},
// 标准2
new LineSeries
{
Title = "标准2",
Values = new ChartValues<double> { 10, 10, 10, 10, 10 },
StrokeThickness = 2,
Stroke = System.Windows.Media.Brushes.Green,
Fill = System.Windows.Media.Brushes.Transparent
},
// 标准3
new LineSeries
{
Title = "标准3",
Values = new ChartValues<double> { 30, 30, 30, 30, 30 },
StrokeThickness = 2,
Stroke = System.Windows.Media.Brushes.Red,
Fill = System.Windows.Media.Brushes.Transparent
}
};
// 设置X轴标签
myChart1.AxisX.Add(new Axis
{
Title = "",
Labels = new[] { "13:27 23", "13:27 45", "13:27 49", "13:27 50", "13:27 55" }
});
// 设置Y轴
myChart1.AxisY.Add(new Axis
{
Title = ""
});
// 传感器3
// 初始化Series
myChart2.Series = new SeriesCollection
{
// 柱状图
new ColumnSeries
{
Title = "柱状数据",
Values = new ChartValues<double> { 20, 10, 12, 5, 9 }
},
// 标准1
new LineSeries
{
Title = "标准1",
Values = new ChartValues<double> { 5, 5, 5, 5, 5 },
StrokeThickness = 2,
Fill = System.Windows.Media.Brushes.Transparent
},
// 标准2
new LineSeries
{
Title = "标准2",
Values = new ChartValues<double> { 10, 10, 10, 10, 10 },
StrokeThickness = 2,
Stroke = System.Windows.Media.Brushes.Green,
Fill = System.Windows.Media.Brushes.Transparent
},
// 标准3
new LineSeries
{
Title = "标准3",
Values = new ChartValues<double> { 30, 30, 30, 30, 30 },
StrokeThickness = 2,
Stroke = System.Windows.Media.Brushes.Red,
Fill = System.Windows.Media.Brushes.Transparent
}
};
// 设置X轴标签
myChart2.AxisX.Add(new Axis
{
Title = "",
Labels = new[] { "13:27 23", "13:27 45", "13:27 49", "13:27 50", "13:27 55" }
});
// 设置Y轴
myChart2.AxisY.Add(new Axis
{
Title = ""
});
#endregion
} }
private void Button_Click(object sender, RoutedEventArgs e) private void Button_Click(object sender, RoutedEventArgs e)
@ -442,4 +297,3 @@ namespace WpfApp
} }
#endregion #endregion
} }
}

58
Utils/InitChart.cs Normal file
View File

@ -0,0 +1,58 @@
using LiveCharts;
using LiveCharts.Wpf;
using System.Windows.Media;
using WpfApp.src.config;
namespace WpfApp.Utils;
public static class InitChart
{
public static void InitCartesianChart(CartesianChart chart, ChartConfig config)
{
chart.Series = new SeriesCollection();
// 柱状图
if (config.ColumnData != null && config.ColumnData.Count > 0)
{
chart.Series.Add(new ColumnSeries
{
Title = "柱状数据",
Values = new ChartValues<double>(config.ColumnData)
});
}
// 标准线条
foreach (var line in config.LineConfigs)
{
var lineSeries = new LineSeries
{
Title = line.Title,
Values = new ChartValues<double>(line.Values),
Stroke = line.Stroke ?? Brushes.Black,
StrokeThickness = 2,
Fill = Brushes.Transparent,
DataLabels = line.ShowLabel,
};
if (line.ShowLabel)
lineSeries.LabelPoint = value => $"{value.Y:0.##} {line.Unit}";
chart.Series.Add(lineSeries);
}
// X轴
chart.AxisX.Clear();
chart.AxisX.Add(new Axis
{
Title = config.XTitle ?? "",
Labels = config.Labels
});
// Y轴
chart.AxisY.Clear();
chart.AxisY.Add(new Axis
{
Title = config.YTitle ?? ""
});
}
}

View File

@ -1,12 +1,10 @@
using System;
using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Media; using System.Windows.Media;
namespace WpfApp.src.components namespace WpfApp.src.components;
{
/// <summary> /// <summary>
/// 刻度线数据结构 /// 刻度线数据结构
/// </summary> /// </summary>
@ -399,4 +397,13 @@ namespace WpfApp.src.components
#endregion #endregion
} }
/// <summary>
/// 传感器等级枚举
/// </summary>
public enum SensorLevel
{
Low, // 低等:-20到20
Medium, // 中等:-40到40
High // 高等:-60到60
} }

View File

@ -1,12 +0,0 @@
namespace WpfApp.src.components
{
/// <summary>
/// 传感器等级枚举
/// </summary>
public enum SensorLevel
{
Low, // 低等:-20到20
Medium, // 中等:-40到40
High // 高等:-60到60
}
}

36
src/config/ChartConfig.cs Normal file
View File

@ -0,0 +1,36 @@
using System.Windows.Media;
namespace WpfApp.src.config;
public class ChartConfig
{
public List<double> ColumnData { get; set; } = new();
public List<LineConfig> LineConfigs { get; set; } = new();
public List<string> Labels { get; set; } = new();
public string XTitle { get; set; }
public string YTitle { get; set; }
}
public class LineConfig
{
/// <summary>
/// 曲线标题
/// </summary>
public string Title { get; set; }
/// <summary>
/// 曲线的值
/// </summary>
public List<double> Values { get; set; }
/// <summary>
/// 曲线颜色
/// </summary>
public Brush Stroke { get; set; } = Brushes.Black;
/// <summary>
/// 是否显示数据点上的标签
/// </summary>
public bool ShowLabel { get; set; } = true;
/// <summary>
/// 单位
/// </summary>
public string Unit { get; set; } = "Hz";
}