wpf对excel中的二维浮点数据进行拟合,输出函数方程和函数曲线图,写出xaml代码和后端代码,列出需要的nuget库
时间: 2024-02-16 17:03:24 浏览: 133
dotnet 读 WPF 源代码笔记 默认的 Main 函数是在哪创建的.rar
需要的 NuGet 库:MathNet.Numerics、ExcelDataReader和OxyPlot.Wpf。
下面是 XAML 代码:
```xaml
<Window x:Class="ExcelFitting.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:oxy="http://oxyplot.org/wpf"
Title="Excel Fitting" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,10">
<Button Content="Open Excel File" Margin="10" Click="OpenExcelFile_Click"/>
<Button Content="Fit Data" Margin="10" Click="FitData_Click"/>
</StackPanel>
<oxy:Plot Grid.Row="1" Margin="10" Model="{Binding PlotModel}"/>
</Grid>
</Window>
```
下面是后端代码:
```csharp
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Windows;
using ExcelDataReader;
using MathNet.Numerics.LinearRegression;
using OxyPlot;
using OxyPlot.Axes;
using OxyPlot.Series;
namespace ExcelFitting
{
public partial class MainWindow : Window
{
private DataTable data;
private List<double> xValues;
private List<double> yValues;
public PlotModel PlotModel { get; set; }
public MainWindow()
{
InitializeComponent();
PlotModel = new PlotModel
{
Axes =
{
new LinearAxis { Position = AxisPosition.Bottom },
new LinearAxis { Position = AxisPosition.Left },
}
};
DataContext = this;
}
private void OpenExcelFile_Click(object sender, RoutedEventArgs e)
{
var dialog = new Microsoft.Win32.OpenFileDialog
{
DefaultExt = ".xlsx",
Filter = "Excel Files (*.xlsx)|*.xlsx|All Files (*.*)|*.*"
};
if (dialog.ShowDialog() == true)
{
var stream = File.Open(dialog.FileName, FileMode.Open, FileAccess.Read);
var reader = ExcelReaderFactory.CreateReader(stream);
data = reader.AsDataSet().Tables[0];
xValues = data.Rows.Cast<DataRow>().Select(row => Convert.ToDouble(row[0])).ToList();
yValues = data.Rows.Cast<DataRow>().Select(row => Convert.ToDouble(row[1])).ToList();
}
}
private void FitData_Click(object sender, RoutedEventArgs e)
{
if (xValues == null || yValues == null)
{
MessageBox.Show("Please select an Excel file first!");
return;
}
var coefficients = Fit.Line(xValues.ToArray(), yValues.ToArray());
var a = coefficients.Item2;
var b = coefficients.Item1;
var series = new LineSeries
{
Title = $"y = {a:F2}x + {b:F2}",
MarkerType = MarkerType.Circle,
MarkerSize = 4,
MarkerStroke = OxyColors.White
};
for (var x = xValues.Min(); x <= xValues.Max(); x += 0.1)
{
var y = a * x + b;
series.Points.Add(new DataPoint(x, y));
}
PlotModel.Series.Clear();
PlotModel.Series.Add(series);
PlotModel.InvalidatePlot(true);
}
}
}
```
在上面的代码中,`OpenExcelFile_Click` 方法用于打开 Excel 文件,获取数据并将其存储在 `xValues` 和 `yValues` 中。`FitData_Click` 方法对这些数据进行拟合,并将拟合结果绘制在 `PlotModel` 上。
注意,在 `FitData_Click` 方法中,我们使用 `Fit.Line` 方法进行线性拟合。如果需要进行其他类型的拟合,可以使用 `Fit.Polynomial` 或 `Fit.LinearCombination` 等方法。
阅读全文