Excel文件有4列数据,前两列为一条曲线,后两列为一条曲线,
时间: 2024-02-06 08:04:13 浏览: 114
如果你想在同一张图上绘制两条曲线,可以使用Chart.js或LiveCharts等绘图库。以下是使用LiveCharts绘制两条曲线的代码示例:
首先,你需要在你的C#项目中安装LiveCharts。
接下来,你需要将Excel数据转换为适当的格式。以下是一个示例:
```
using Excel = Microsoft.Office.Interop.Excel;
using LiveCharts;
using LiveCharts.Defaults;
Excel.Application excelApp = new Excel.Application();
Excel.Workbook workbook = excelApp.Workbooks.Open(@"C:\your\file\path.xlsx");
Excel.Worksheet sheet = workbook.Sheets[1];
Excel.Range range = sheet.UsedRange;
int rowCount = range.Rows.Count;
int columnCount = range.Columns.Count;
// 从Excel中读取数据并将其转换为ChartValues类型
ChartValues<ObservablePoint> series1 = new ChartValues<ObservablePoint>();
ChartValues<ObservablePoint> series2 = new ChartValues<ObservablePoint>();
for (int i = 1; i <= rowCount; i++)
{
double x = range.Cells[i, 1].Value2;
double y = range.Cells[i, 2].Value2;
series1.Add(new ObservablePoint(x, y));
x = range.Cells[i, 3].Value2;
y = range.Cells[i, 4].Value2;
series2.Add(new ObservablePoint(x, y));
}
workbook.Close();
excelApp.Quit();
```
接下来,你可以使用以下代码将两个系列绘制到同一张图上:
```
using LiveCharts.Wpf;
// 创建一个新的CartesianChart
CartesianChart chart = new CartesianChart();
// 将两个系列添加到图表中
LineSeries series1 = new LineSeries
{
Title = "Series 1",
Values = series1
};
chart.Series.Add(series1);
LineSeries series2 = new LineSeries
{
Title = "Series 2",
Values = series2
};
chart.Series.Add(series2);
// 设置图表的其他属性
chart.AxisX.Add(new Axis
{
Title = "X Axis Title"
});
chart.AxisY.Add(new Axis
{
Title = "Y Axis Title"
});
// 将图表添加到窗口中
myWindow.Content = chart;
```
这将在窗口中创建一个新的CartesianChart,并将两个系列添加到图表中。你可以根据需要调整图表的其他属性,例如轴标题、颜色、线条样式等。
阅读全文