我使用LiveCharts創建餅圖。我有一張我想在餅圖中表示的雙打名單。問題是,列表的值可以,並且會改變,因此我希望能夠相應地更改圖表。在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
只接受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任何其他示例代碼,沒有人知道如何做到這一點?