本文概述
当你需要处理科学主题, 数据可视化(或最简单的为仪表盘创建图表)时, 饼图无疑是显示简单数据的最常用和最广为人知的方法之一。饼图用于显示某个值的总和的百分比, 因此所有切片(段)的总和必须为100%。如果你愿意在WinForms应用程序中显示此类图表, 建议你为此使用一个库, 而不是使用.NET的Drawing类进行绘制
在本文中, 我们将向你展示如何使用LiveCharts库在带有C#的WinForms应用程序中显示饼图。
1.安装LiveCharts
要在Visual Studio中的项目上安装此软件包, 请转到解决方案资源管理器, 然后右键单击你的项目。从下拉列表中选择管理NuGet软件包选项:
在管理器中, 转到浏览选项卡并搜索LiveCharts包:
选择Beto Rodriguez的WinForms软件包并将其安装在你的项目中。安装完成后, 你将可以使用该库在代码上呈现图表。有关此库的更多信息, 请访问Github上的官方存储库。
2.添加一个PieChart组件
在项目中安装LiveCharts库后, Visual Studio会自动添加一组新控件, 使你可以将图表添加到WinForms布局中。打开表单设计器时, 可以在Visual Studio的左侧找到它。为了开始本文的示例, 我们将添加一个PieChart项, 因此只需将其拖放到表单中即可:
这将添加默认图像, 以让你知道该组件正在占用空间(数据没有意义)。默认情况下, 第一个项目在代码中将被命名为pieChart1, 但是你可以稍后更改组件的名称。在我们的示例中, 我们将保留第一个元素的相同名称。
3.在PieChart上显示数据
现在你的表单以图形方式显示了一个PieChart, 你需要设置一些将要绘制的数据。首先, 你需要定义一个包含PieSeries元素的数据集合, 这些元素可以在SeriesCollection对象中进行动态或静态定义。每个PieSeries对象至少需要定义Title, LabelPoint和Values选项才能正确显示。可以使用ChartValues类的新实例及其类型和原始值来定义每个项目的值。
可以根据需要在集合上添加数据, 也可以立即在SeriesCollection构造函数中对其进行定义:
// Include the required namespace of LiveCharts
using LiveCharts;
using LiveCharts.Wpf;
// It's important to run the code inside the load event of the form, so the component will be drawn after this correctly
private void Form1_Load(object sender, EventArgs e)
{
// Define the label that will appear over the piece of the chart
// in this case we'll show the given value and the percentage e.g 123 (8%)
Func<ChartPoint, string> labelPoint = chartPoint => string.Format("{0} ({1:P})", chartPoint.Y, chartPoint.Participation);
// Define a collection of items to display in the chart
SeriesCollection piechartData = new SeriesCollection
{
new PieSeries
{
Title = "First Item", Values = new ChartValues<double> {25}, DataLabels = true, LabelPoint = labelPoint
}, new PieSeries
{
Title = "Second Item", Values = new ChartValues<double> {25}, DataLabels = true, LabelPoint = labelPoint
}, new PieSeries
{
Title = "Third Item", Values = new ChartValues<double> {25}, DataLabels = true, LabelPoint = labelPoint
}
};
// You can add a new item dinamically with the add method of the collection
// Useful when you define the data dinamically and not statically
piechartData.Add(
new PieSeries{
Title = "Fourth Item", Values = new ChartValues<double> { 25 }, DataLabels = true, LabelPoint = labelPoint, Fill = System.Windows.Media.Brushes.Gray
}
);
// Define the collection of Values to display in the Pie Chart
pieChart1.Series = piechartData;
// Set the legend location to appear in the Right side of the chart
pieChart1.LegendLocation = LegendLocation.Right;
}
如前所述, 代码需要在表单加载事件之后/期间执行。以下示例显示了文章开头显示的图形, 该图形显示了该国核弹头的数量和百分比:
using System;
using System.Windows.Forms;
// Include the required namespace of LiveCharts
using LiveCharts;
using LiveCharts.Wpf;
namespace Sandbox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Define the label that will appear over the piece of the chart
// in this case we'll show the given value and the percentage e.g 123 (8%)
Func<ChartPoint, string> labelPoint = chartPoint => string.Format("{0} ({1:P})", chartPoint.Y, chartPoint.Participation);
// Define the collection of Values to display in the Pie Chart
pieChart1.Series = new SeriesCollection
{
new PieSeries
{
Title = "USA", Values = new ChartValues<double> {6450}, DataLabels = true, LabelPoint = labelPoint, }, new PieSeries
{
Title = "Russia", Values = new ChartValues<double> {6850}, DataLabels = true, LabelPoint = labelPoint
}, new PieSeries
{
Title = "United Kingdom", Values = new ChartValues<double> {215}, DataLabels = true, LabelPoint = labelPoint
}, new PieSeries
{
Title = "France", Values = new ChartValues<double> {300}, DataLabels = true, LabelPoint = labelPoint
}, new PieSeries
{
Title = "China", Values = new ChartValues<double> {280}, DataLabels = true, LabelPoint = labelPoint
}
};
// Set the legend location to appear in the bottom of the chart
pieChart1.LegendLocation = LegendLocation.Bottom;
}
}
}
4.自定义PieChart
对于定制, 几乎每个人都需要的是基本功能。例如, 要更改颜色, 你需要定义System.Windows.Media.Brushes密封类的填充颜色:
new PieSeries
{
Title = "Second Item", Values = new ChartValues<double> {25}, DataLabels = true, LabelPoint = labelPoint, // Define the piece with a green color
Fill = System.Windows.Media.Brushes.Green
}
或者, 如果要突出显示饼图中的某段, 请使用PushOut选项:
new PieSeries
{
Title = "Second Item", Values = new ChartValues<double> {25}, DataLabels = true, LabelPoint = labelPoint, // Highligh this piece of the chart
PushOut = 15, }
如下代码:
// Define the label that will appear over the piece of the chart
// in this case we'll show the given value and the percentage e.g 123 (8%)
Func<ChartPoint, string> labelPoint = chartPoint => string.Format("{0} ({1:P})", chartPoint.Y, chartPoint.Participation);
SeriesCollection piechartData = new SeriesCollection
{
new PieSeries
{
Title = "First Item", Values = new ChartValues<double> {25}, DataLabels = true, LabelPoint = labelPoint, // Define a custom Color
Fill = System.Windows.Media.Brushes.Black
}, new PieSeries
{
Title = "Second Item", Values = new ChartValues<double> {25}, DataLabels = true, LabelPoint = labelPoint, Fill = System.Windows.Media.Brushes.Green, PushOut = 15
}, new PieSeries
{
Title = "Third Item", Values = new ChartValues<double> {25}, DataLabels = true, LabelPoint = labelPoint, Fill = System.Windows.Media.Brushes.Pink
}
};
// You can add a new item dinamically with the add method of the collection
piechartData.Add(
new PieSeries{
Title = "Fourth Item", Values = new ChartValues<double> { 25 }, DataLabels = true, LabelPoint = labelPoint, Fill = System.Windows.Media.Brushes.Gray
}
);
// Define the collection of Values to display in the Pie Chart
pieChart1.Series = piechartData;
// Set the legend location to appear in the bottom of the chart
pieChart1.LegendLocation = LegendLocation.Right;
将生成以下图表:
编码愉快!
评论前必须登录!
注册