|
| 1 | +using System.IO; |
| 2 | +using Syncfusion.DocIO; |
| 3 | +using Syncfusion.DocIO.DLS; |
| 4 | +using Syncfusion.OfficeChart; |
| 5 | + |
| 6 | +namespace Set_2D_array_data_in_combination_chart |
| 7 | +{ |
| 8 | + class Program |
| 9 | + { |
| 10 | + static void Main(string[] args) |
| 11 | + { |
| 12 | + //Define a 2D array with dimensions [3, 13] |
| 13 | + string[,] data = new string[3, 13] |
| 14 | + { |
| 15 | + { "Month", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }, |
| 16 | + { "Rainy Days", "12", "11", "10", "9", "8", "6", "4", "6", "7", "8", "10", "11" }, |
| 17 | + { "Profit", "3574", "4708", "5332", "6693", "8843", "12347", "15180", "11198", "9739", "9846", "6620", "5085" } |
| 18 | + }; |
| 19 | + //Create a new Word document. |
| 20 | + using (WordDocument document = new WordDocument()) |
| 21 | + { |
| 22 | + //Add a section to the document. |
| 23 | + IWSection section = document.AddSection(); |
| 24 | + //Add a paragraph to the section. |
| 25 | + IWParagraph paragraph = section.AddParagraph(); |
| 26 | + //Create and append the chart to the paragraph. |
| 27 | + WChart chart = paragraph.AppendChart(446, 270); |
| 28 | + //Set chart data using values from the 2D array. |
| 29 | + for (int i = 0; i < data.GetLength(0); i++) |
| 30 | + { |
| 31 | + for (int j = 0; j < data.GetLength(1); j++) |
| 32 | + { |
| 33 | + string value = data[i, j]; |
| 34 | + //Try to parse the value as an integer, if possible |
| 35 | + if (int.TryParse(value, out int intValue)) |
| 36 | + { |
| 37 | + //Set the integer value. |
| 38 | + chart.ChartData.SetValue(j + 1, i + 1, intValue); |
| 39 | + } |
| 40 | + else |
| 41 | + { |
| 42 | + //Set the string value. |
| 43 | + chart.ChartData.SetValue(j + 1, i + 1, value); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + //Set region of Chart data. |
| 48 | + chart.DataRange = chart.ChartData[1, 1, 13, 3]; |
| 49 | + //Set chart series in the column for assigned data region. |
| 50 | + chart.IsSeriesInRows = false; |
| 51 | + //Set a Chart Title. |
| 52 | + chart.ChartTitle = "Combination Chart"; |
| 53 | + //Set Datalabels. |
| 54 | + IOfficeChartSerie series1 = chart.Series[0]; |
| 55 | + IOfficeChartSerie series2 = chart.Series[1]; |
| 56 | + //Set Serie type. |
| 57 | + series1.SerieType = OfficeChartType.Column_Clustered; |
| 58 | + series2.SerieType = OfficeChartType.Line; |
| 59 | + series2.UsePrimaryAxis = false; |
| 60 | + //Set Datalabels. |
| 61 | + series1.DataPoints.DefaultDataPoint.DataLabels.IsValue = true; |
| 62 | + //Set legend. |
| 63 | + chart.HasLegend = true; |
| 64 | + chart.Legend.Position = OfficeLegendPosition.Bottom; |
| 65 | + //Set chart type. |
| 66 | + chart.ChartType = OfficeChartType.Combination_Chart; |
| 67 | + //Set secondary axis on right side. |
| 68 | + chart.SecondaryValueAxis.TickLabelPosition = OfficeTickLabelPosition.TickLabelPosition_High; |
| 69 | + //Create a file stream. |
| 70 | + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.docx"), FileMode.Create, FileAccess.ReadWrite)) |
| 71 | + { |
| 72 | + //Save the Word document to the file stream. |
| 73 | + document.Save(outputFileStream, FormatType.Docx); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments