我正在使用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的任何其他示例代码,有人知道怎么做吗?
答案 0 :(得分:3)
所以我终于明白了:
foreach (var n in classChartData.Slice)
{
areaChart.Series.Add(new PieSeries
{
Title = n.Key,
Values = new ChartValues<double> { n.Value }
});
}
areaChart.LegendLocation = LegendLocation.Bottom;
classChartData
private Dictionary<string, double> slice = new Dictionary<string, double>();
public Dictionary<string, double> Slice
{
get { return slice; }
set { slice = value; }
}
public void AddSlice(string slicename, double slicevalue)
{
slice.Add(slicename, slicevalue);
}
我希望这可以帮助任何人使用LiveCharts。
答案 1 :(得分:0)
如果有人仍在寻找解决方案,这就是我解决问题的方法
Func<ChartPoint, string> labelPoint = chartPoint =>
string.Format("{0} ({1:P})", chartPoint.Y, chartPoint.Participation);
ChartValues<double> cht_y_values = new ChartValues<double>();
LiveCharts.SeriesCollection series = new LiveCharts.SeriesCollection();
foreach (DataRow dr in dt.Rows)
{
PieSeries ps = new PieSeries
{
Title = dr[x_column_name].ToString(),
Values = new ChartValues<double> {
double.Parse(dr[y1_column_name].ToString())},
DataLabels = true,
LabelPoint = labelPoint
};
series.Add(ps);
}
pieChart.Series = series;
答案 2 :(得分:0)
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;
}
}
}