2025-10-05
C#
00

WinForms中使用OxyPlot创建面积图的详细指南。面积图是一种非常有用的图表类型,可以直观地展示数据随时间的变化趋势,并且能够很好地表现数据的累积效果。

准备工作

首先,我们需要在项目中添加OxyPlot的引用。您可以通过NuGet包管理器来安装OxyPlot。在Visual Studio中,右击您的项目,选择"管理NuGet包",然后搜索并安装以下包:

  • OxyPlot.Core
  • OxyPlot.WindowsForms

安装完成后,在您的代码文件顶部添加以下using语句:

C#
using OxyPlot; using OxyPlot.Series; using OxyPlot.WindowsForms;

创建基本面积图

让我们从一个简单的面积图开始。假设我们要展示某产品五年的销售数据。

C#
public partial class Form1 : Form { public Form1() { InitializeComponent(); CreateBasicAreaChart(); } private void CreateBasicAreaChart() { var plotView = new PlotView(); plotView.Dock = DockStyle.Fill; var model = new PlotModel { Title = "产品销售趋势" }; var areaSeries = new AreaSeries { Color = OxyColors.LightBlue, StrokeThickness = 2 }; // 添加数据点 areaSeries.Points.Add(new DataPoint(2018, 100)); areaSeries.Points.Add(new DataPoint(2019, 150)); areaSeries.Points.Add(new DataPoint(2020, 200)); areaSeries.Points.Add(new DataPoint(2021, 180)); areaSeries.Points.Add(new DataPoint(2022, 250)); model.Series.Add(areaSeries); plotView.Model = model; this.Controls.Add(plotView); } }

image.png

2025-10-05
C#
00

OxyPlot是一个功能强大的跨平台绘图库,可以在WinForms应用程序中创建各种类型的图表,包括饼状图。本指南将介绍如何在WinForms应用程序中使用OxyPlot创建和自定义饼状图。

环境设置

首先,确保您的WinForms项目中已安装OxyPlot库。您可以通过NuGet包管理器安装:

C#
Install-Package OxyPlot.WindowsForms

基本饼状图

让我们从一个简单的饼状图开始:

C#
using OxyPlot; using OxyPlot.Series; using OxyPlot.WindowsForms; private void CreateBasicPieChart() { var model = new PlotModel { Title = "基本饼状图" }; var series = new PieSeries { StrokeThickness = 2.0, InsideLabelPosition = 0.8, AngleSpan = 360, StartAngle = 0 }; series.Slices.Add(new PieSlice("苹果", 3) { IsExploded = true }); series.Slices.Add(new PieSlice("香蕉", 4)); series.Slices.Add(new PieSlice("橙子", 2)); model.Series.Add(series); var plotView = new PlotView { Model = model, Dock = DockStyle.Fill }; this.Controls.Add(plotView); }

image.png

2025-10-05
C#
00

OxyPlot是一个强大的跨平台绘图库,可以在WinForms应用程序中创建高质量的图表。本指南将重点介绍如何使用OxyPlot在WinForms中创建各种类型的柱状图。

环境设置

首先,您需要在您的WinForms项目中安装OxyPlot。您可以通过NuGet包管理器安装以下包:

  • OxyPlot.Core
  • OxyPlot.WindowsForms

image.png

在Visual Studio中,右键点击您的项目,选择"管理NuGet包",然后搜索并安装这两个包。

安装完成后,在您的代码中添加以下using语句:

C#
using OxyPlot; using OxyPlot.Series; using OxyPlot.WindowsForms;
2025-10-05
C#
00

在Windows Forms应用程序中,ListView是一个非常实用的控件,用于显示数据列表。但默认情况下,ListView并不支持点击列头进行排序。本文将介绍如何开发一个可点击列头排序的ListView控件。

创建自定义比较器

ListViewItemSorter 类是一个用于排序 ListView 控件中项目的自定义比较器。它实现了 IComparer<ListViewItem> 接口,可以按照指定的列、排序顺序和数据类型对 ListViewItem 进行排序。

C#
public class ListViewItemSorter : IComparer<ListViewItem> { private int _columnIndex; private SortOrder _sortOrder; private ColumnDataType _dataType; public ListViewItemSorter(int columnIndex, SortOrder sortOrder, ColumnDataType dataType) { _columnIndex = columnIndex; _sortOrder = sortOrder; _dataType = dataType; } public int Compare(ListViewItem x, ListViewItem y) { string textX = x.SubItems[_columnIndex].Text; string textY = y.SubItems[_columnIndex].Text; int result; switch (_dataType) { case ColumnDataType.Number: if (double.TryParse(textX, out double numX) && double.TryParse(textY, out double numY)) { result = numX.CompareTo(numY); } else { result = string.Compare(textX, textY, StringComparison.OrdinalIgnoreCase); } break; case ColumnDataType.Date: if (DateTime.TryParse(textX, out DateTime dateX) && DateTime.TryParse(textY, out DateTime dateY)) { result = dateX.CompareTo(dateY); } else { result = string.Compare(textX, textY, StringComparison.OrdinalIgnoreCase); } break; case ColumnDataType.Text: default: result = string.Compare(textX, textY, StringComparison.OrdinalIgnoreCase); break; } return _sortOrder == SortOrder.Ascending ? result : -result; } }
  1. 构造函数接受三个参数:
    • columnIndex:要排序的列的索引
    • sortOrder:排序顺序(升序或降序)
    • dataType:列数据的类型(数字、日期或文本)
  2. Compare 方法实现了实际的比较逻辑:
    • 根据指定的列索引获取要比较的文本
    • 根据数据类型进行相应的比较:
      • 对于数字类型,尝试将文本转换为 double 进行比较
      • 对于日期类型,尝试将文本转换为 DateTime 进行比较
      • 对于文本类型或无法转换的情况,使用字符串比较
  3. 比较结果会根据指定的排序顺序进行调整(升序或降序)
2025-10-05
C#
00

OxyPlot是一个强大的跨平台绘图库,非常适合在WinForms应用程序中创建各种类型的图表。本指南将重点介绍如何使用OxyPlot创建各种线图,并提供多个完整的示例。

环境设置

首先,确保您已经安装了OxyPlot。在Visual Studio中,通过NuGet包管理器安装以下包:

  • OxyPlot.Core
  • OxyPlot.WindowsForms

或者在包管理器控制台中运行:

C#
Install-Package OxyPlot.Core Install-Package OxyPlot.WindowsForms

image.png 在您的Form类中,添加以下using语句:

C#
using OxyPlot; using OxyPlot.Series; using OxyPlot.WindowsForms;

基本线图

让我们从一个基本的线图开始。这个例子展示了如何创建一个简单的正弦波线图。

C#
public partial class Form1 : Form { public Form1() { InitializeComponent(); CreateBasicLineChart(); } private void CreateBasicLineChart() { var plotView = new PlotView(); plotView.Dock = DockStyle.Fill; this.Controls.Add(plotView); var plotModel = new PlotModel { Title = "Basic Line Chart" }; var series = new LineSeries { Title = "sin(x)" }; for (double x = 0; x < 10; x += 0.1) { series.Points.Add(new DataPoint(x, Math.Sin(x))); } plotModel.Series.Add(series); plotView.Model = plotModel; } }

image.png