We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Sample code for Pie Chart

Hi,


I would just like for a sample code on how to supply data for a pie chart.

Thank you.

Hi Joemelle,


Thanks for your inquiry. Please see Working with Charts section of documentation and try using the following code:

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);<o:p></o:p>

<o:p> </o:p>

Shape shape = builder.InsertChart(ChartType.Pie, 432, 252);<o:p></o:p>

Chart chart = shape.Chart;<o:p></o:p>

<o:p> </o:p>

ChartSeriesCollection seriesColl = chart.Series;<o:p></o:p>

seriesColl.Clear();<o:p></o:p>

<o:p> </o:p>

string[] categories = new string[] { “Grapefruit”, “Oranges”, “Plums”, “Pears”, “Apples” };<o:p></o:p>

// Specify all values as 20%<o:p></o:p>

ChartSeries series = seriesColl.Add(“Imported Fruits”, categories, new double[] { 2, 2, 2, 2, 2 });<o:p></o:p>

<o:p> </o:p>

ChartDataLabelCollection dataLabelCollection = series.DataLabels;<o:p></o:p>

<o:p> </o:p>

ChartDataLabel chartDataLabel00 = dataLabelCollection.Add(0);<o:p></o:p>

ChartDataLabel chartDataLabel01 = dataLabelCollection.Add(1);<o:p></o:p>

ChartDataLabel chartDataLabel02 = dataLabelCollection.Add(2);<o:p></o:p>

ChartDataLabel chartDataLabel03 = dataLabelCollection.Add(3);<o:p></o:p>

ChartDataLabel chartDataLabel04 = dataLabelCollection.Add(4);<o:p></o:p>

<o:p> </o:p>

chartDataLabel00.ShowCategoryName = true;<o:p></o:p>

chartDataLabel01.ShowCategoryName = true;<o:p></o:p>

chartDataLabel02.ShowCategoryName = true;<o:p></o:p>

chartDataLabel03.ShowCategoryName = true;<o:p></o:p>

chartDataLabel04.ShowCategoryName = true;<o:p></o:p>

<o:p> </o:p>

chart.Legend.Position = LegendPosition.Left;<o:p></o:p>

<o:p> </o:p>

doc.Save(MyDir + @“15.12.0.docx”);<o:p></o:p>


Hope, this helps.

Best regards,