Required Namespaces
using System.Windows.Controls.DataVisualization.Charting;
using System.Windows.Controls.DataVisualization;
first create a chart using the chart class and create the pie series
Chart piechart = new Chart();
PieSeries ps = new PieSeries();
Then map the pieseries to data,here i have used four records ,so the pie chart
will have four slices.
List<data> DAT = new List<data>();
DAT.Add(new data() { project="project1",completed=50 });
DAT.Add(new data() { project = " project2 ", completed = 100 });
DAT.Add(new data() { project = " project3 ", completed = 20 });
DAT.Add(new data() { project = " project4 ", completed = 10 });
ps.IndependentValuePath = "project";
ps.DependentValuePath = "completed";
ps.ItemsSource = DAT;
public class data
{
public string project{get;set;}
public int completed{get;set;}
}
Now u need to create multiple styles for the pie chart DataPoint,
each style will be applied to each slice in the pie chart.
Each Style must be enclosed in ResourceDictionary class
ResourceDictionary rs1 = new ResourceDictionary();
Style datapointstyle1 = new Style(typeof(DataPoint));
datapointstyle1.Setters.Add(new Setter(BackgroundProperty, "#FF42B444"));
datapointstyle1.Setters.Add(new Setter(OpacityProperty,"0.8"));
rs1.Add("DataPointStyle",datapointstyle1);
ResourceDictionary rs2 = new ResourceDictionary();
Style datapointstyle2 = new Style(typeof(DataPoint));
datapointstyle2.Setters.Add(new Setter(BackgroundProperty, "#FFF73110"));
datapointstyle2.Setters.Add(new Setter(OpacityProperty, "0.8"));
rs2.Add("DataPointStyle", datapointstyle2);
ResourceDictionary rs3 = new ResourceDictionary();
Style datapointstyle3 = new Style(typeof(DataPoint));
datapointstyle3.Setters.Add(new Setter(BackgroundProperty, "Blue"));
datapointstyle3.Setters.Add(new Setter(OpacityProperty, "0.8"));
rs3.Add("DataPointStyle", datapointstyle3);
ResourceDictionary rs4 = new ResourceDictionary();
Style datapointstyle4 = new Style(typeof(DataPoint));
datapointstyle4.Setters.Add(new Setter(BackgroundProperty, "Gray"));
datapointstyle4.Setters.Add(new Setter(OpacityProperty, "0.8"));
rs4.Add("DataPointStyle", datapointstyle4);
Now we add all the resource dictionaries to a single collection,the
ResourceDictionaryCollection
ResourceDictionaryCollection coll = new ResourceDictionaryCollection();
coll.Add(rs1);
coll.Add(rs2);
coll.Add(rs3);
coll.Add(rs4);
finally we are going to apply the styles to the piechart
piechart.Pallete = coll;
Now add the Pie Series to the PieChart
piechart.Series.Add(ps);