|
1 | | -using Syncfusion.DocIO; |
2 | | -using Syncfusion.DocIO.DLS; |
| 1 | +using Syncfusion.DocIO.DLS; |
3 | 2 | using Syncfusion.OfficeChart; |
4 | | -using System.IO; |
| 3 | +using Syncfusion.Drawing; |
5 | 4 |
|
6 | | -namespace Create_3D_pie_chart |
| 5 | + |
| 6 | +// Create a new Word document |
| 7 | +WordDocument document = new WordDocument(); |
| 8 | +IWSection section = document.AddSection(); |
| 9 | +section.PageSetup.PageSize = PageSize.A3; |
| 10 | + |
| 11 | +// Add title paragraphs |
| 12 | +IWParagraph titlePara1 = section.AddParagraph(); |
| 13 | +titlePara1.AppendText("DUKE ENERGY FLORIDA").CharacterFormat.Bold = true; |
| 14 | +titlePara1.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center; |
| 15 | + |
| 16 | +IWParagraph titlePara2 = section.AddParagraph(); |
| 17 | +titlePara2.AppendText("ACCOUNT 352 STRUCTURES AND IMPROVEMENTS").CharacterFormat.Bold = true; |
| 18 | +titlePara2.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center; |
| 19 | + |
| 20 | +IWParagraph titlePara3 = section.AddParagraph(); |
| 21 | +titlePara3.AppendText("ORIGINAL AND SMOOTH SURVIVOR CURVES").CharacterFormat.Bold = true; |
| 22 | +titlePara3.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center; |
| 23 | + |
| 24 | +// Add paragraph for chart |
| 25 | +IWParagraph chartPara = section.AddParagraph(); |
| 26 | + |
| 27 | +// Create chart |
| 28 | +WChart chart = chartPara.AppendChart(700, 350); |
| 29 | +chart.ChartType = OfficeChartType.Scatter_SmoothedLine; |
| 30 | +chart.ChartTitle = "Smooth Curve chart"; |
| 31 | + |
| 32 | +#region Define Chart Data |
| 33 | +int[] ages = { 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 }; |
| 34 | +int[] originalCurve = { 100, 99, 97, 93, 85, 65, 40, 20, 8, 2, 0 }; |
| 35 | + |
| 36 | +// Set up the chart data |
| 37 | +// First row with headers |
| 38 | +chart.ChartData.SetValue(1, 1, "ORIGINAL CURVE"); |
| 39 | +chart.ChartData.SetValue(1, 2, "1943-2022 Experience\n1925-2022 Placements"); |
| 40 | +chart.ChartData.SetValue(1, 3, "1943-2022 Experience\n1925-2022 Placements"); |
| 41 | +chart.ChartData.SetValue(1, 4, "1943-2022 Experience\n1925-2022 Placements"); |
| 42 | +chart.ChartData.SetValue(1, 5, "1943-2022 Experience\n1925-2022 Placements"); |
| 43 | +chart.ChartData.SetValue(1, 6, "1943-2022 Experience\n1925-2022 Placements"); |
| 44 | + |
| 45 | +// Fill data rows - note we're only filling the original curve column |
| 46 | +for (int i = 0; i < ages.Length; i++) |
7 | 47 | { |
8 | | - class Program |
| 48 | + chart.ChartData.SetValue(i + 2, 1, ages[i]); |
| 49 | + chart.ChartData.SetValue(i + 2, 3, originalCurve[i]); |
| 50 | + |
| 51 | + if (i % 2 == 0) // Add some scattered points for other series |
9 | 52 | { |
10 | | - static void Main(string[] args) |
| 53 | + chart.ChartData.SetValue(i + 2, 2, originalCurve[i] - 3); |
| 54 | + chart.ChartData.SetValue(i + 2, 4, originalCurve[i] + 2); |
| 55 | + } |
| 56 | + if (i % 3 == 0) |
| 57 | + { |
| 58 | + chart.ChartData.SetValue(i + 2, 5, originalCurve[i] - 5); |
| 59 | + chart.ChartData.SetValue(i + 2, 6, originalCurve[i] + 4); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// Set data range |
| 64 | +chart.DataRange = chart.ChartData[1, 1, ages.Length + 1, 6]; |
| 65 | +#endregion |
| 66 | + |
| 67 | + |
| 68 | +#region Configure Series |
| 69 | +for (int i = 0; i < chart.Series.Count; i++) |
| 70 | +{ |
| 71 | + // Set X values to use the ages column |
| 72 | + chart.Series[i].CategoryLabels = chart.ChartData[2, 1, ages.Length + 1, 1]; |
| 73 | + |
| 74 | + // Set Y values to use the respective data column |
| 75 | + chart.Series[i].Values = chart.ChartData[2, i + 2, ages.Length + 1, i + 2]; |
| 76 | + |
| 77 | + // Configure series appearance |
| 78 | + if (i == 1) // Original curve (index 1 corresponds to column 3) |
| 79 | + { |
| 80 | + chart.Series[i].SerieFormat.LineProperties.LinePattern = OfficeChartLinePattern.Solid; |
| 81 | + chart.Series[i].SerieFormat.IsSmoothedLine = true; |
| 82 | + chart.Series[i].SerieFormat.LineProperties.LineWeight = OfficeChartLineWeight.Narrow; |
| 83 | + chart.Series[i].SerieFormat.MarkerStyle = OfficeChartMarkerType.None; |
| 84 | + chart.Series[i].SerieFormat.LineProperties.LineColor = Syncfusion.Drawing.Color.Black; |
| 85 | + |
| 86 | + chart.Series[i].DataPoints.DefaultDataPoint.DataLabels.IsValue = false; |
| 87 | + |
| 88 | + IOfficeChartDataPoint point = chart.Series[i].DataPoints[5]; // 0-based index |
| 89 | + point.DataLabels.IsSeriesName = true; |
| 90 | + point.DataLabels.Position = OfficeDataLabelPosition.Above; |
| 91 | + point.DataLabels.Layout.Left = 80; |
| 92 | + point.DataLabels.Layout.Top = 0.5; |
| 93 | + point.DataLabels.ShowLeaderLines = true; |
| 94 | + |
| 95 | + } |
| 96 | + else |
| 97 | + { |
| 98 | + // For other series, show only markers |
| 99 | + chart.Series[i].SerieFormat.LineProperties.LinePattern = OfficeChartLinePattern.None; |
| 100 | + |
| 101 | + // Set different marker styles |
| 102 | + switch (i) |
11 | 103 | { |
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_3D; |
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 = "3D 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 | | - //Set elevation. |
50 | | - chart.Elevation = 30; |
51 | | - //Create a file stream. |
52 | | - using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.docx"), FileMode.Create, FileAccess.ReadWrite)) |
53 | | - { |
54 | | - //Save the Word document to the file stream. |
55 | | - document.Save(outputFileStream, FormatType.Docx); |
56 | | - } |
57 | | - } |
58 | | - } |
59 | | - } |
| 104 | + case 0: chart.Series[i].SerieFormat.MarkerStyle = OfficeChartMarkerType.Triangle; break; |
| 105 | + case 2: chart.Series[i].SerieFormat.MarkerStyle = OfficeChartMarkerType.Circle; break; |
| 106 | + case 3: chart.Series[i].SerieFormat.MarkerStyle = OfficeChartMarkerType.Diamond; break; |
| 107 | + case 4: chart.Series[i].SerieFormat.MarkerStyle = OfficeChartMarkerType.Square; break; |
| 108 | + default: chart.Series[i].SerieFormat.MarkerStyle = OfficeChartMarkerType.Star; break; |
| 109 | + } |
| 110 | + chart.Series[i].SerieFormat.MarkerSize = 5; |
| 111 | + } |
60 | 112 | } |
| 113 | +#endregion |
| 114 | + |
| 115 | + |
| 116 | +#region Configure Chart Axes and Legend |
| 117 | +chart.Legend.Position = OfficeLegendPosition.Top; |
| 118 | + |
| 119 | +// Add vertical gridlines |
| 120 | +chart.PrimaryCategoryAxis.HasMajorGridLines = true; |
| 121 | +chart.PrimaryCategoryAxis.MajorGridLines.Border.LinePattern = OfficeChartLinePattern.Solid; |
| 122 | +chart.PrimaryCategoryAxis.MajorGridLines.Border.LineWeight = OfficeChartLineWeight.Hairline; |
| 123 | +chart.PrimaryCategoryAxis.MajorGridLines.Border.LineColor = Color.Black; |
| 124 | + |
| 125 | +chart.PrimaryCategoryAxis.TickLabelSpacing = 10; |
| 126 | + |
| 127 | +// Configure the value axis (Y-axis) |
| 128 | +chart.PrimaryValueAxis.MinimumValue = 0; |
| 129 | +chart.PrimaryValueAxis.MaximumValue = 100; |
| 130 | +chart.PrimaryValueAxis.MajorUnit = 10; |
| 131 | +chart.PrimaryValueAxis.HasMajorGridLines = true; |
| 132 | +chart.PrimaryValueAxis.MajorGridLines.Border.LinePattern = OfficeChartLinePattern.Solid; |
| 133 | +chart.PrimaryValueAxis.MajorGridLines.Border.LineWeight = OfficeChartLineWeight.Hairline; |
| 134 | +chart.PrimaryValueAxis.MajorGridLines.Border.LineColor = Color.Black; |
| 135 | +chart.PrimaryValueAxis.Title = "PERCENT SURVIVING"; |
| 136 | + |
| 137 | +// Configure the category axis (X-axis) |
| 138 | +chart.PrimaryCategoryAxis.MinimumValue = 0; |
| 139 | +chart.PrimaryCategoryAxis.MaximumValue = 120; |
| 140 | +chart.PrimaryCategoryAxis.MajorUnit = 10; |
| 141 | +chart.PrimaryCategoryAxis.Title = "AGE IN YEARS"; |
| 142 | +#endregion |
| 143 | + |
| 144 | +document.Save(@"../../../Output/SurvivorCurves.docx"); |
| 145 | +document.Close(); |
0 commit comments