我正在使用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赋给图表时代码崩溃的原因。以下是我填充PieChart的尝试:
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的其他示例代码,有人知道如何做到这一点吗?
https://stackoverflow.com/questions/41558338
复制相似问题