我正在使用 LiveCharts 创建饼图。我有一个要在饼图中表示的双精度列表。问题是列表的值可以并且将会改变,因此我希望能够相应地更改图表。
以下是 LiveCharts 网站上的一些示例代码:
using System;
using System.Windows.Forms;
using LiveCharts;
using LiveCharts.Wpf;
namespace Winforms.PieChart
{
public partial class PieChartExample : Form
{
public PieChartExample()
{
InitializeComponent();
Func<ChartPoint, string> labelPoint = chartPoint =>
string.Format("{0} ({1:P})", chartPoint.Y, chartPoint.Participation);
pieChart1.Series = new SeriesCollection
{
new PieSeries
{
Title = "Maria",
Values = new ChartValues<double> {3},
PushOut = 15,
DataLabels = true,
LabelPoint = labelPoint
},
new PieSeries
{
Title = "Charles",
Values = new ChartValues<double> {4},
DataLabels = true,
LabelPoint = labelPoint
},
new PieSeries
{
Title = "Frida",
Values = new ChartValues<double> {6},
DataLabels = true,
LabelPoint = labelPoint
},
new PieSeries
{
Title = "Frederic",
Values = new ChartValues<double> {2},
DataLabels = true,
LabelPoint = labelPoint
}
};
pieChart1.LegendLocation = LegendLocation.Bottom;
}
}
}
本质上,我想做同样的事情,但相反,迭代列表并为饼图创建适当数量的切片。LiveCharts 提供PieSlices
但PieChart
Control
只接受 a SeriesCollection
,这就是为什么我的代码在分配pieChartData
给图表时崩溃的原因。这是我填充饼图的尝试:
LiveCharts.Wpf.PieChart pieChartData = new LiveCharts.Wpf.PieChart();
foreach (var n in areavalues)
{
pieChartData.AddToView(new PieSlice
{
PieceValue = n
});
}
areaChart.Series.Add(new PieSeries(pieChartData)); //<-- CRASH
我很难找到 LiveCharts 的任何其他示例代码,有人知道该怎么做吗?