|
| 1 | +using Syncfusion.XlsIO; |
| 2 | +using Syncfusion.Drawing; |
| 3 | + |
| 4 | +namespace Create_Chart_and_Customize |
| 5 | +{ |
| 6 | + class Program |
| 7 | + { |
| 8 | + public static void Main(string[] args) |
| 9 | + { |
| 10 | + using (ExcelEngine excelEngine = new ExcelEngine()) |
| 11 | + { |
| 12 | + IApplication application = excelEngine.Excel; |
| 13 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 14 | + |
| 15 | + //Load an existing Excel file |
| 16 | + FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read); |
| 17 | + IWorkbook workbook = application.Workbooks.Open(inputStream); |
| 18 | + IWorksheet worksheet = workbook.Worksheets[0]; |
| 19 | + |
| 20 | + //Create a Chart |
| 21 | + IChartShape chart = worksheet.Charts.Add(); |
| 22 | + |
| 23 | + //Set the Chart Type |
| 24 | + chart.ChartType = ExcelChartType.Column_Clustered; |
| 25 | + |
| 26 | + //Set data range in the worksheet |
| 27 | + chart.DataRange = worksheet.Range["A1:C6"]; |
| 28 | + |
| 29 | + //Specify that the series are in columns |
| 30 | + chart.IsSeriesInRows = false; |
| 31 | + |
| 32 | + //Positioning the chart in the worksheet |
| 33 | + chart.TopRow = 8; |
| 34 | + chart.LeftColumn = 1; |
| 35 | + chart.BottomRow = 23; |
| 36 | + chart.RightColumn = 8; |
| 37 | + |
| 38 | + //Set the chart title |
| 39 | + chart.ChartTitle = "Purchase Details"; |
| 40 | + |
| 41 | + //Format chart title color and font |
| 42 | + chart.ChartTitleArea.Color = ExcelKnownColors.Black; |
| 43 | + |
| 44 | + chart.ChartTitleArea.FontName = "Calibri"; |
| 45 | + chart.ChartTitleArea.Bold = true; |
| 46 | + chart.ChartTitleArea.Underline = ExcelUnderline.Single; |
| 47 | + chart.ChartTitleArea.Size = 15; |
| 48 | + |
| 49 | + //Format Chart Area |
| 50 | + IChartFrameFormat chartArea = chart.ChartArea; |
| 51 | + |
| 52 | + //Format chart area border and color |
| 53 | + chartArea.Border.LinePattern = ExcelChartLinePattern.Solid; |
| 54 | + chartArea.Border.LineColor = Color.Pink; |
| 55 | + chartArea.Border.LineWeight = ExcelChartLineWeight.Hairline; |
| 56 | + |
| 57 | + chartArea.Fill.FillType = ExcelFillType.Gradient; |
| 58 | + chartArea.Fill.GradientColorType = ExcelGradientColor.TwoColor; |
| 59 | + chartArea.Fill.BackColor = Color.FromArgb(205, 217, 234); |
| 60 | + chartArea.Fill.ForeColor = Color.White; |
| 61 | + |
| 62 | + //Format Plot Area |
| 63 | + IChartFrameFormat chartPlotArea = chart.PlotArea; |
| 64 | + |
| 65 | + //Format plot area border and color |
| 66 | + chartPlotArea.Border.LinePattern = ExcelChartLinePattern.Solid; |
| 67 | + chartPlotArea.Border.LineColor = Color.Pink; |
| 68 | + chartPlotArea.Border.LineWeight = ExcelChartLineWeight.Hairline; |
| 69 | + |
| 70 | + chartPlotArea.Fill.FillType = ExcelFillType.Gradient; |
| 71 | + chartPlotArea.Fill.GradientColorType = ExcelGradientColor.TwoColor; |
| 72 | + chartPlotArea.Fill.BackColor = Color.FromArgb(205, 217, 234); |
| 73 | + chartPlotArea.Fill.ForeColor = Color.White; |
| 74 | + |
| 75 | + //Format Series |
| 76 | + IChartSerie serie1 = chart.Series[0]; |
| 77 | + IChartSerie serie2 = chart.Series[1]; |
| 78 | + |
| 79 | + //Format series border and color |
| 80 | + serie1.SerieFormat.LineProperties.LineColor = Color.Pink; |
| 81 | + serie1.SerieFormat.LineProperties.LinePattern = ExcelChartLinePattern.Dot; |
| 82 | + serie1.SerieFormat.LineProperties.LineWeight = ExcelChartLineWeight.Narrow; |
| 83 | + |
| 84 | + serie2.SerieFormat.LineProperties.LineColor = Color.Pink; |
| 85 | + serie2.SerieFormat.LineProperties.LinePattern = ExcelChartLinePattern.Dot; |
| 86 | + serie2.SerieFormat.LineProperties.LineWeight = ExcelChartLineWeight.Narrow; |
| 87 | + |
| 88 | + serie1.SerieFormat.Fill.FillType = ExcelFillType.Gradient; |
| 89 | + serie1.SerieFormat.Fill.GradientColorType = ExcelGradientColor.TwoColor; |
| 90 | + serie1.SerieFormat.Fill.BackColor = Color.FromArgb(205, 217, 234); |
| 91 | + serie1.SerieFormat.Fill.ForeColor = Color.Pink; |
| 92 | + |
| 93 | + serie2.SerieFormat.Fill.FillType = ExcelFillType.Gradient; |
| 94 | + serie2.SerieFormat.Fill.GradientColorType = ExcelGradientColor.TwoColor; |
| 95 | + serie2.SerieFormat.Fill.BackColor = Color.FromArgb(205, 217, 234); |
| 96 | + serie2.SerieFormat.Fill.ForeColor = Color.Pink; |
| 97 | + |
| 98 | + //Set Datalabel |
| 99 | + serie1.DataPoints.DefaultDataPoint.DataLabels.IsValue = true; |
| 100 | + serie2.DataPoints.DefaultDataPoint.DataLabels.IsValue = true; |
| 101 | + serie1.DataPoints.DefaultDataPoint.DataLabels.Position = ExcelDataLabelPosition.Outside; |
| 102 | + serie2.DataPoints.DefaultDataPoint.DataLabels.Position = ExcelDataLabelPosition.Outside; |
| 103 | + |
| 104 | + //Format data labels color and font |
| 105 | + serie1.DataPoints.DefaultDataPoint.DataLabels.Color = ExcelKnownColors.Black; |
| 106 | + serie2.DataPoints.DefaultDataPoint.DataLabels.Color = ExcelKnownColors.Black; |
| 107 | + |
| 108 | + serie1.DataPoints.DefaultDataPoint.DataLabels.Size = 10; |
| 109 | + serie1.DataPoints.DefaultDataPoint.DataLabels.FontName = "calibri"; |
| 110 | + serie1.DataPoints.DefaultDataPoint.DataLabels.Bold = true; |
| 111 | + |
| 112 | + serie2.DataPoints.DefaultDataPoint.DataLabels.Size = 10; |
| 113 | + serie2.DataPoints.DefaultDataPoint.DataLabels.FontName = "calibri"; |
| 114 | + serie2.DataPoints.DefaultDataPoint.DataLabels.Bold = true; |
| 115 | + |
| 116 | + //Set Legend |
| 117 | + chart.HasLegend = true; |
| 118 | + chart.Legend.Position = ExcelLegendPosition.Bottom; |
| 119 | + |
| 120 | + //Format legend border, color, and font |
| 121 | + chart.Legend.FrameFormat.Border.AutoFormat = false; |
| 122 | + chart.Legend.FrameFormat.Border.IsAutoLineColor = false; |
| 123 | + chart.Legend.FrameFormat.Border.LineColor = Color.Black; |
| 124 | + chart.Legend.FrameFormat.Border.LinePattern = ExcelChartLinePattern.LightGray; |
| 125 | + chart.Legend.FrameFormat.Border.LineWeight = ExcelChartLineWeight.Narrow; |
| 126 | + |
| 127 | + chart.Legend.TextArea.Color = ExcelKnownColors.Black; |
| 128 | + |
| 129 | + chart.Legend.TextArea.Bold = true; |
| 130 | + chart.Legend.TextArea.FontName = "Calibri"; |
| 131 | + chart.Legend.TextArea.Size = 8; |
| 132 | + chart.Legend.TextArea.Strikethrough = false; |
| 133 | + |
| 134 | + //Set axis title |
| 135 | + chart.PrimaryCategoryAxis.Title = "Items"; |
| 136 | + chart.PrimaryValueAxis.Title = "Amount in($) and counts"; |
| 137 | + |
| 138 | + //Format chart axis border and font |
| 139 | + chart.PrimaryCategoryAxis.Border.LinePattern = ExcelChartLinePattern.CircleDot; |
| 140 | + chart.PrimaryCategoryAxis.Border.LineColor = Color.Pink; |
| 141 | + chart.PrimaryCategoryAxis.Border.LineWeight = ExcelChartLineWeight.Hairline; |
| 142 | + |
| 143 | + chart.PrimaryValueAxis.Border.LinePattern = ExcelChartLinePattern.CircleDot; |
| 144 | + chart.PrimaryValueAxis.Border.LineColor = Color.Pink; |
| 145 | + chart.PrimaryValueAxis.Border.LineWeight = ExcelChartLineWeight.Hairline; |
| 146 | + |
| 147 | + chart.PrimaryCategoryAxis.Font.Color = ExcelKnownColors.Black; |
| 148 | + chart.PrimaryCategoryAxis.Font.FontName = "Calibri"; |
| 149 | + chart.PrimaryCategoryAxis.Font.Bold = true; |
| 150 | + chart.PrimaryCategoryAxis.Font.Size = 8; |
| 151 | + |
| 152 | + chart.PrimaryValueAxis.Font.Color = ExcelKnownColors.Black; |
| 153 | + chart.PrimaryValueAxis.Font.FontName = "Calibri"; |
| 154 | + chart.PrimaryValueAxis.Font.Bold = true; |
| 155 | + chart.PrimaryValueAxis.Font.Size = 8; |
| 156 | + |
| 157 | + //Saving the workbook |
| 158 | + FileStream outputStream = new FileStream(Path.GetFullPath("Output/Chart.xlsx"), FileMode.Create, FileAccess.Write); |
| 159 | + workbook.SaveAs(outputStream); |
| 160 | + |
| 161 | + //Dispose stream |
| 162 | + inputStream.Dispose(); |
| 163 | + outputStream.Dispose(); |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments