郑州做网站优化公司,重庆沙坪坝地图全图,c++怎么做网站,中国最新消息新闻报道C# WPF 折线图制作笔记#xff08;LiveCharts 库#xff09;
一、核心信息
1. 开发环境 .NET Framework 4.7.2 WPF 应用程序 核心库#xff1a;LiveCharts、LiveCharts.Wpf#xff08;需通过 NuGet 安装#xff09;
2. 核心组件说明
组件作用CartesianChartLiveCha…C# WPF 折线图制作笔记LiveCharts 库一、核心信息1. 开发环境.NET Framework 4.7.2WPF 应用程序核心库LiveCharts、LiveCharts.Wpf需通过 NuGet 安装2. 核心组件说明组件作用CartesianChartLiveCharts 的核心图表控件用于绘制笛卡尔坐标系图表折线图、柱状图等SeriesCollection图表系列集合存放多条折线的数据LineSeries折线系列单条折线的样式、数据配置ChartValuesdouble折线的数值集合存储具体数据点Axis坐标轴配置X 轴 / Y 轴支持标题、标签、格式化器二、实现步骤拆解步骤 1安装依赖库通过 NuGet 包管理器安装LiveCharts核心逻辑LiveCharts.WpfWPF 控件封装步骤 2创建折线图用户控件UCLineChart2.1 XAML 布局UCLineChart.xamlUserControl x:ClassLineChart2.UCLineChart xmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:xhttp://schemas.microsoft.com/winfx/2006/xaml xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006 xmlns:dhttp://schemas.microsoft.com/expression/blend/2008 xmlns:localclr-namespace:LineChart2 !-- 引入LiveCharts.Wpf命名空间 -- xmlns:lvcclr-namespace:LiveCharts.Wpf;assemblyLiveCharts.Wpf mc:Ignorabled d:DesignHeight450 d:DesignWidth800 Grid !-- 核心图表控件 -- lvc:CartesianChart Series{Binding SeriesCollection} !-- 绑定折线系列集合 -- LegendLocationRight !-- 图例位置右侧 -- !-- Y轴配置 -- lvc:CartesianChart.AxisY lvc:Axis TitleSales !-- Y轴标题 -- LabelFormatter{Binding YFormatter} !-- Y轴数值格式化 -- /lvc:Axis /lvc:CartesianChart.AxisY !-- X轴配置 -- lvc:CartesianChart.AxisX lvc:Axis TitleMonth !-- X轴标题 -- Labels{Binding Labels} !-- X轴标签文本 -- /lvc:Axis /lvc:CartesianChart.AxisX /lvc:CartesianChart /Grid /UserControl2.2 后台逻辑UCLineChart.xaml.csusing LiveCharts; using LiveCharts.Wpf; using System.Windows; using System.Windows.Controls; namespace LineChart2 { public partial class UCLineChart : UserControl { // 绑定属性折线系列集合 public SeriesCollection SeriesCollection { get; set; } // 绑定属性X轴标签 public string[] Labels { get; set; } // 绑定属性Y轴数值格式化器 public Funcdouble, string YFormatter { get; set; } public UCLineChart() { InitializeComponent(); // 1. 初始化折线系列集合 SeriesCollection new SeriesCollection { // 第一条折线 new LineSeries { Title Series 1, // 折线名称图例显示 Values new ChartValuesdouble { 4, 6, 5, 2, 7 } // 数据点 }, // 第二条折线 new LineSeries { Title Series 2, Values new ChartValuesdouble { 6, 7, 3, 4, 6 } } }; // 2. 配置X轴标签 Labels new[] { Jan, Feb, Mar, Apr, May }; // 3. 配置Y轴格式化器货币格式 YFormatter value value.ToString(C); // 4. 动态添加第三条折线支持动画更新 SeriesCollection.Add(new LineSeries { Values new ChartValuesdouble { 5, 3, 2, 4 }, LineSmoothness 0 // 折线平滑度0直线1最平滑 }); // 5. 动态追加数据点支持动画更新 SeriesCollection[2].Values.Add(5d); // 6. 设置数据上下文绑定生效 DataContext this; } } }步骤 3在主窗口引用用户控件MainWindow.xamlWindow x:ClassLineChart2.MainWindow xmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:xhttp://schemas.microsoft.com/winfx/2006/xaml xmlns:dhttp://schemas.microsoft.com/expression/blend/2008 xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006 xmlns:localclr-namespace:LineChart2 mc:Ignorabled TitleMainWindow Height450 Width800 Grid Grid Margin10 !-- 引用自定义折线图控件 -- local:UCLineChart x:NameucLineChart HorizontalAlignmentStretch !-- 水平拉伸 -- VerticalAlignmentStretch !-- 垂直拉伸 -- Background#F5F5F5 !-- 背景色 -- BorderBrush#E0E0E0 !-- 边框色 -- BorderThickness1 / !-- 边框厚度 -- /Grid /Grid /Window三、关键特性说明1. 动态更新数据LiveCharts 支持动画更新修改SeriesCollection或ChartValues会自动触发图表动画// 动态添加折线 SeriesCollection.Add(new LineSeries { Values new ChartValuesdouble { 1, 2, 3 } }); // 动态追加数据点 SeriesCollection[0].Values.Add(8d); // 动态修改数据点 SeriesCollection[1].Values[2] 9d;2. 折线样式配置LineSeries常用属性属性作用示例值LineSmoothness折线平滑度0-10直线、0.5Stroke折线颜色Brushes.RedStrokeThickness折线粗细2PointGeometry数据点形状圆形 / 方形等DefaultGeometries.CirclePointForeground数据点填充色Brushes.Blue示例自定义折线样式new LineSeries { Title Custom Series, Values new ChartValuesdouble { 2, 5, 3, 8 }, LineSmoothness 0.8, Stroke Brushes.Orange, StrokeThickness 3, PointGeometry DefaultGeometries.Triangle, PointForeground Brushes.Purple };3. 坐标轴自定义// 自定义X轴 var axisX new Axis { Title 时间, Labels new[] { 周一, 周二, 周三 }, LabelRotation 15, // 标签旋转角度 Foreground Brushes.Gray // 标签颜色 }; // 自定义Y轴 var axisY new Axis { Title 数值, MinValue 0, // 最小值 MaxValue 10, // 最大值 LabelFormatter v ${v} 个 // 格式化标签 }; // 应用到图表 myCartesianChart.AxisX.Add(axisX); myCartesianChart.AxisY.Add(axisY);四、完整示例扩展版需求制作一个带动态数据、自定义样式的月度销量折线图支持实时追加数据。完整代码1. UCLineChart.xamlUserControl x:ClassLineChart2.UCLineChart xmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:xhttp://schemas.microsoft.com/winfx/2006/xaml xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006 xmlns:dhttp://schemas.microsoft.com/expression/blend/2008 xmlns:localclr-namespace:LineChart2 xmlns:lvcclr-namespace:LiveCharts.Wpf;assemblyLiveCharts.Wpf mc:Ignorabled d:DesignHeight450 d:DesignWidth800 Grid Grid.RowDefinitions RowDefinition Height*/ RowDefinition HeightAuto/ /Grid.RowDefinitions lvc:CartesianChart x:Namechart Series{Binding SeriesCollection} LegendLocationTop BackgroundWhite lvc:CartesianChart.AxisY lvc:Axis Title销量件 LabelFormatter{Binding YFormatter} Foreground#333333/ /lvc:CartesianChart.AxisY lvc:CartesianChart.AxisX lvc:Axis Title月份 Labels{Binding Labels} Foreground#333333/ /lvc:CartesianChart.AxisX /lvc:CartesianChart !-- 追加数据按钮 -- Button Grid.Row1 Content追加6月数据 Width120 Height30 Margin10 ClickBtnAddData_Click/ /Grid /UserControl2. UCLineChart.xaml.csusing LiveCharts; using LiveCharts.Defaults; using LiveCharts.Wpf; using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace LineChart2 { public partial class UCLineChart : UserControl { public SeriesCollection SeriesCollection { get; set; } public string[] Labels { get; set; } public Funcdouble, string YFormatter { get; set; } // 模拟销量数据 private readonly Random _random new Random(); public UCLineChart() { InitializeComponent(); // 初始化系列 SeriesCollection new SeriesCollection { // 2024年销量 new LineSeries { Title 2024年, Values new ChartValuesdouble { 120, 150, 110, 180, 160 }, LineSmoothness 0.3, Stroke Brushes.DodgerBlue, StrokeThickness 2, PointGeometry DefaultGeometries.Circle, PointGeometrySize 8, PointForeground Brushes.DodgerBlue }, // 2023年销量 new LineSeries { Title 2023年, Values new ChartValuesdouble { 100, 130, 140, 150, 130 }, LineSmoothness 0.3, Stroke Brushes.OrangeRed, StrokeThickness 2, PointGeometry DefaultGeometries.Square, PointGeometrySize 8, PointForeground Brushes.OrangeRed } }; // X轴标签1-5月 Labels new[] { 1月, 2月, 3月, 4月, 5月 }; // Y轴格式化显示整数 YFormatter value ${value:N0}; DataContext this; } // 追加6月数据按钮点击事件 private void BtnAddData_Click(object sender, RoutedEventArgs e) { // 生成随机销量100-200 double sales2024 _random.Next(100, 200); double sales2023 _random.Next(100, 200); // 追加数据点 SeriesCollection[0].Values.Add(sales2024); SeriesCollection[1].Values.Add(sales2023); // 更新X轴标签 // ArrResize(out Labels, Labels.Length 1); Labels[Labels.Length - 1] 6月; // 刷新绑定触发UI更新 chart.AxisX[0].Labels Labels; } } }3. MainWindow.xaml无修改Window x:ClassLineChart2.MainWindow xmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:xhttp://schemas.microsoft.com/winfx/2006/xaml xmlns:dhttp://schemas.microsoft.com/expression/blend/2008 xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006 xmlns:localclr-namespace:LineChart2 mc:Ignorabled TitleMainWindow Height450 Width800 Grid Grid Margin10 !-- 自定义折线图用户控件 -- local:UCLineChart x:NameucLineChart HorizontalAlignmentStretch VerticalAlignmentStretch Background#F5F5F5 BorderBrush#E0E0E0 BorderThickness1 / /Grid /Grid /Window保持原代码不变运行后可看到包含 2023/2024 年 1-5 月销量的折线图点击 “追加 6 月数据” 按钮动态添加 6 月随机销量数据图表自动更新并播放动画五、常见问题解决NuGet 安装失败确保选择与.NET Framework 4.7.2 兼容的 LiveCharts 版本推荐 0.9.7。绑定不生效检查DataContext this;是否执行属性是否为公共属性public。图表无显示确保SeriesCollection有数据且Labels长度与数据点数量匹配。动画卡顿大量数据时可关闭动画chart.DisableAnimations true;。六、扩展方向绑定 MVVM 架构将SeriesCollection等属性移到 ViewModel 中实现数据与 UI 分离。数据导出结合 Excel 库如 EPPlus将图表数据导出为 Excel。交互增强添加数据点点击事件、图例隐藏 / 显示、坐标轴缩放等功能。样式定制通过Chart.Style或自定义模板修改图表整体样式。
做网站怎样和客户沟通,企业宣传片策划公司,外贸网站海外推广3个必去网站,王也踏青图照片目录具体实现截图项目开发技术介绍PHP核心代码部分展示系统结论源码获取/同行可拿货,招校园代理具体实现截图