|
| 1 | +using System.IO; |
| 2 | +using Syncfusion.DocIO; |
| 3 | +using Syncfusion.DocIO.DLS; |
| 4 | +using Syncfusion.OfficeChart; |
| 5 | + |
| 6 | +namespace Create_pie_chart |
| 7 | +{ |
| 8 | + class Program |
| 9 | + { |
| 10 | + static void Main(string[] args) |
| 11 | + { |
| 12 | + //Create a new Word document. |
| 13 | + using (WordDocument document = new WordDocument()) |
| 14 | + { |
| 15 | + //Add a section to the document. |
| 16 | + IWSection section = document.AddSection(); |
| 17 | + //Add a paragraph to the section. |
| 18 | + IWParagraph paragraph = section.AddParagraph(); |
| 19 | + //Create and append the chart to the paragraph. |
| 20 | + WChart chart = paragraph.AppendChart(446, 270); |
| 21 | + //Set chart type. |
| 22 | + chart.ChartType = OfficeChartType.Pie; |
| 23 | + //Set chart data. |
| 24 | + chart.ChartData.SetValue(2, 1, "Food"); |
| 25 | + chart.ChartData.SetValue(3, 1, "Fruits"); |
| 26 | + chart.ChartData.SetValue(4, 1, "Vegetables"); |
| 27 | + chart.ChartData.SetValue(5, 1, "Dairy"); |
| 28 | + chart.ChartData.SetValue(6, 1, "Protein"); |
| 29 | + chart.ChartData.SetValue(7, 1, "Grains"); |
| 30 | + chart.ChartData.SetValue(2, 2, "Percentage"); |
| 31 | + chart.ChartData.SetValue(3, 2, 36); |
| 32 | + chart.ChartData.SetValue(4, 2, 14); |
| 33 | + chart.ChartData.SetValue(5, 2, 13); |
| 34 | + chart.ChartData.SetValue(6, 2, 28); |
| 35 | + chart.ChartData.SetValue(7, 2, 9); |
| 36 | + //Set region of Chart data. |
| 37 | + chart.DataRange = chart.ChartData[2, 1, 7, 2]; |
| 38 | + //Set chart series in the column for assigned data region. |
| 39 | + chart.IsSeriesInRows = false; |
| 40 | + //Set a Chart Title. |
| 41 | + chart.ChartTitle = "Pie Chart"; |
| 42 | + //Set Datalabels. |
| 43 | + IOfficeChartSerie serie = chart.Series[0]; |
| 44 | + |
| 45 | + serie.DataPoints.DefaultDataPoint.DataLabels.IsValue = true; |
| 46 | + //Set legend. |
| 47 | + chart.HasLegend = true; |
| 48 | + chart.Legend.Position = OfficeLegendPosition.Bottom; |
| 49 | + //Create a file stream. |
| 50 | + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.docx"), FileMode.Create, FileAccess.ReadWrite)) |
| 51 | + { |
| 52 | + //Save the Word document to the file stream. |
| 53 | + document.Save(outputFileStream, FormatType.Docx); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | +} |
0 commit comments