数据图表ScottPlot.WPF用法示例
- 软件开发
- 2025-09-17 21:27:02

目录
一、添加 NuGet 程序包(5.0.47)
二、MainWindow.xaml中添加引用
三、MainWindow.xaml.cs 具体使用代码
图表示例:
一、添加 NuGet 程序包(5.0.47) 二、MainWindow.xaml中添加引用 <Window x:Class="jssc_analysis2.MainWindow" xmlns="http://schemas.microsoft /winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft /winfx/2006/xaml" xmlns:d="http://schemas.microsoft /expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" xmlns:ScottPlot="clr-namespace:ScottPlot.WPF;assembly=ScottPlot.WPF" Title="数据分析" Width="1280" MinWidth="1280" Height="725" MinHeight="725" Topmost="False" Background="Gray" WindowStartupLocation="CenterScreen"> <Grid x:Name="control_grid"> <Grid.ColumnDefinitions> <ColumnDefinition></ColumnDefinition> </Grid.ColumnDefinitions> <ScottPlot:WpfPlot x:Name="wpfPlot" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/> </Grid> </Window> 三、MainWindow.xaml.cs 具体使用代码 private List<DateTime> timeData; private List<double> priceData; private Scatter scatterPlot; private Crosshair crosshair; private Annotation highlightAnnotation; //文本提示框 private async void Btn_startOnClick(object sender, RoutedEventArgs e) { timeData = new List<DateTime>(); priceData = new List<double>(); for (int i = 0; i < pointCount; i++) { timeData.Add(startTime.AddMinutes(i * 5)); // 每 5 分钟一个点 priceData.Add(Math.Sin(i * 0.1) * 100 + rand.Next(-5, 5)); // 模拟盈亏金额走势 } // 转换时间为 double(OADate 格式) double[] xs = timeData.ConvertAll(t => t.ToOADate()).ToArray(); double[] ys = priceData.ToArray(); // 添加曲线 scatterPlot = wpfPlot.Plot.Add.Scatter(xs, ys, ScottPlot.Colors.Red); scatterPlot.LineWidth = 2; scatterPlot.MarkerSize = 4; scatterPlot.MarkerShape = MarkerShape.FilledCircle; crosshair = wpfPlot.Plot.Add.Crosshair(0, 0); crosshair.IsVisible = false; crosshair.MarkerShape = MarkerShape.FilledCircle; crosshair.MarkerSize = 15; // 添加鼠标悬停的文本提示 (Annotation) highlightAnnotation = wpfPlot.Plot.Add.Annotation(""); highlightAnnotation.LabelOffsetX = 0; highlightAnnotation.LabelOffsetY = 0; highlightAnnotation.LabelBackgroundColor = ScottPlot.Colors.LightYellow; highlightAnnotation.LabelBorderColor = ScottPlot.Colors.Black; highlightAnnotation.LabelFontSize = 15; highlightAnnotation.IsVisible = false; // 初始时不显示 // 配置 X 轴显示时间格式 wpfPlot.Plot.Axes.DateTimeTicksBottom(); wpfPlot.Refresh(); // 绑定鼠标移动事件 wpfPlot.MouseMove += WpfPlot_MouseMove; } private void WpfPlot_MouseMove(object sender, System.Windows.Input.MouseEventArgs e) { // 获取鼠标位置(需转换为图表像素坐标) Pixel mousePixel = new Pixel( x: (int)e.GetPosition(wpfPlot).X, y: (int)e.GetPosition(wpfPlot).Y ); Coordinates mouseLocation = wpfPlot.Plot.GetCoordinates(mousePixel); DataPoint nearest = scatterPlot.Data.GetNearest(mouseLocation, wpfPlot.Plot.LastRender); // 将十字线置于突出显示的点上 if (nearest.IsReal) { crosshair.IsVisible = true; crosshair.Position = nearest.Coordinates; highlightAnnotation.Text = $"Index:{nearest.Index}\r\nNumber:{nearest.Y:0.##}\r\nTime:{DateTime.FromOADate(nearest.X)}"; highlightAnnotation.IsVisible = true; } // 当未选择任何点时隐藏十字准线 if (!nearest.IsReal && crosshair.IsVisible) { crosshair.IsVisible = false; highlightAnnotation.Text = $""; highlightAnnotation.IsVisible = false; } wpfPlot.Refresh(); // 刷新图表 }另:Annotation暂时无法支持中文,有解决办法的话欢迎留言
数据图表ScottPlot.WPF用法示例由讯客互联软件开发栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“数据图表ScottPlot.WPF用法示例”