|
| 1 | +using System.IO; |
| 2 | +using Syncfusion.DocIO; |
| 3 | +using Syncfusion.DocIO.DLS; |
| 4 | +using Syncfusion.OfficeChart; |
| 5 | + |
| 6 | +namespace Create_box_and_whisker_chart |
| 7 | +{ |
| 8 | + class Program |
| 9 | + { |
| 10 | + static void Main(string[] args) |
| 11 | + { |
| 12 | + using (WordDocument document = new WordDocument()) |
| 13 | + { |
| 14 | + IWSection section = document.AddSection(); |
| 15 | + IWParagraph paragraph = section.AddParagraph(); |
| 16 | + |
| 17 | + WChart chart = paragraph.AppendChart(500f, 300f); |
| 18 | + chart.ChartType = OfficeChartType.BoxAndWhisker; |
| 19 | + chart.ChartTitle = "Box and whisker chart"; |
| 20 | + |
| 21 | + // Set headers |
| 22 | + chart.ChartData.SetValue(1, 1, "Course"); |
| 23 | + chart.ChartData.SetValue(1, 2, "School A"); |
| 24 | + chart.ChartData.SetValue(1, 3, "School B"); |
| 25 | + chart.ChartData.SetValue(1, 4, "School C"); |
| 26 | + |
| 27 | + // Add data rows |
| 28 | + string[,] data = { |
| 29 | + {"English", "63", "53", "45"}, |
| 30 | + {"Physics", "61", "55", "65"}, |
| 31 | + {"English", "63", "50", "65"}, |
| 32 | + {"Math", "62", "51", "64"}, |
| 33 | + {"English", "46", "53", "66"}, |
| 34 | + {"English", "58", "56", "67"}, |
| 35 | + {"Math", "60", "51", "67"}, |
| 36 | + {"Math", "62", "53", "66"}, |
| 37 | + {"English", "63", "54", "64"}, |
| 38 | + {"English", "63", "52", "67"}, |
| 39 | + {"Physics", "60", "56", "64"}, |
| 40 | + {"English", "60", "56", "67"}, |
| 41 | + {"Math", "61", "56", "45"}, |
| 42 | + {"Math", "63", "58", "64"}, |
| 43 | + {"English", "59", "54", "65"} |
| 44 | + }; |
| 45 | + |
| 46 | + for (int i = 0; i < data.GetLength(0); i++) |
| 47 | + { |
| 48 | + chart.ChartData.SetValue(i + 2, 1, data[i, 0]); // Course name |
| 49 | + chart.ChartData.SetValue(i + 2, 2, int.Parse(data[i, 1])); // School A |
| 50 | + chart.ChartData.SetValue(i + 2, 3, int.Parse(data[i, 2])); // School B |
| 51 | + chart.ChartData.SetValue(i + 2, 4, int.Parse(data[i, 3])); // School C |
| 52 | + } |
| 53 | + |
| 54 | + // Set data range and chart properties |
| 55 | + chart.DataRange = chart.ChartData[2, 1, data.GetLength(0) + 1, 4]; |
| 56 | + |
| 57 | + chart.PrimaryCategoryAxis.Title = "Subjects"; |
| 58 | + chart.PrimaryValueAxis.Title = "Scores"; |
| 59 | + chart.PrimaryValueAxis.MinimumValue = 0; |
| 60 | + chart.PrimaryValueAxis.MaximumValue = 70; |
| 61 | + |
| 62 | + IOfficeChartSerie series = chart.Series[0]; |
| 63 | + series.SerieFormat.ShowOutlierPoints = true; |
| 64 | + series.SerieFormat.ShowMeanMarkers = true; |
| 65 | + |
| 66 | + series = chart.Series[1]; |
| 67 | + series.SerieFormat.ShowOutlierPoints = true; |
| 68 | + series.SerieFormat.ShowMeanMarkers = true; |
| 69 | + |
| 70 | + series = chart.Series[2]; |
| 71 | + series.SerieFormat.ShowOutlierPoints = true; |
| 72 | + series.SerieFormat.ShowMeanMarkers = true; |
| 73 | + |
| 74 | + // Set legend. |
| 75 | + chart.HasLegend = true; |
| 76 | + chart.Legend.Position = OfficeLegendPosition.Bottom; |
| 77 | + |
| 78 | + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.docx"), FileMode.Create, FileAccess.ReadWrite)) |
| 79 | + { |
| 80 | + document.Save(outputFileStream, FormatType.Docx); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments