Skip to content

Commit 1bfa927

Browse files
committed
Create and Edit Charts
1 parent ee17705 commit 1bfa927

File tree

5 files changed

+212
-0
lines changed

5 files changed

+212
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35417.141 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Create Chart and Customize", "Create Chart and Customize\Create Chart and Customize.csproj", "{DCBE2799-4105-45DD-AA01-9B3B6230E436}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{DCBE2799-4105-45DD-AA01-9B3B6230E436}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{DCBE2799-4105-45DD-AA01-9B3B6230E436}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{DCBE2799-4105-45DD-AA01-9B3B6230E436}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{DCBE2799-4105-45DD-AA01-9B3B6230E436}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Create_Chart_and_Customize</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\*">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Output\*">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
</ItemGroup>
23+
</Project>

Create and Edit Charts/Create Chart and Customize/.NET/Create Chart and Customize/Create Chart and Customize/Output/.gitkeep

Whitespace-only changes.
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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

Comments
 (0)