Skip to content

Commit

Permalink
Merge pull request #147 from SyncfusionExamples/917563-Chart-XlsIO
Browse files Browse the repository at this point in the history
917563 - Add Blog Sample for Chart
  • Loading branch information
Mohan2401 authored Oct 25, 2024
2 parents ee17705 + 1bfa927 commit c4f109c
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35417.141 d17.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Create Chart and Customize", "Create Chart and Customize\Create Chart and Customize.csproj", "{DCBE2799-4105-45DD-AA01-9B3B6230E436}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DCBE2799-4105-45DD-AA01-9B3B6230E436}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DCBE2799-4105-45DD-AA01-9B3B6230E436}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DCBE2799-4105-45DD-AA01-9B3B6230E436}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DCBE2799-4105-45DD-AA01-9B3B6230E436}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Create_Chart_and_Customize</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="*" />
</ItemGroup>

<ItemGroup>
<None Update="Data\*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Output\*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
using Syncfusion.XlsIO;
using Syncfusion.Drawing;

namespace Create_Chart_and_Customize
{
class Program
{
public static void Main(string[] args)
{
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;

//Load an existing Excel file
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
IWorkbook workbook = application.Workbooks.Open(inputStream);
IWorksheet worksheet = workbook.Worksheets[0];

//Create a Chart
IChartShape chart = worksheet.Charts.Add();

//Set the Chart Type
chart.ChartType = ExcelChartType.Column_Clustered;

//Set data range in the worksheet
chart.DataRange = worksheet.Range["A1:C6"];

//Specify that the series are in columns
chart.IsSeriesInRows = false;

//Positioning the chart in the worksheet
chart.TopRow = 8;
chart.LeftColumn = 1;
chart.BottomRow = 23;
chart.RightColumn = 8;

//Set the chart title
chart.ChartTitle = "Purchase Details";

//Format chart title color and font
chart.ChartTitleArea.Color = ExcelKnownColors.Black;

chart.ChartTitleArea.FontName = "Calibri";
chart.ChartTitleArea.Bold = true;
chart.ChartTitleArea.Underline = ExcelUnderline.Single;
chart.ChartTitleArea.Size = 15;

//Format Chart Area
IChartFrameFormat chartArea = chart.ChartArea;

//Format chart area border and color
chartArea.Border.LinePattern = ExcelChartLinePattern.Solid;
chartArea.Border.LineColor = Color.Pink;
chartArea.Border.LineWeight = ExcelChartLineWeight.Hairline;

chartArea.Fill.FillType = ExcelFillType.Gradient;
chartArea.Fill.GradientColorType = ExcelGradientColor.TwoColor;
chartArea.Fill.BackColor = Color.FromArgb(205, 217, 234);
chartArea.Fill.ForeColor = Color.White;

//Format Plot Area
IChartFrameFormat chartPlotArea = chart.PlotArea;

//Format plot area border and color
chartPlotArea.Border.LinePattern = ExcelChartLinePattern.Solid;
chartPlotArea.Border.LineColor = Color.Pink;
chartPlotArea.Border.LineWeight = ExcelChartLineWeight.Hairline;

chartPlotArea.Fill.FillType = ExcelFillType.Gradient;
chartPlotArea.Fill.GradientColorType = ExcelGradientColor.TwoColor;
chartPlotArea.Fill.BackColor = Color.FromArgb(205, 217, 234);
chartPlotArea.Fill.ForeColor = Color.White;

//Format Series
IChartSerie serie1 = chart.Series[0];
IChartSerie serie2 = chart.Series[1];

//Format series border and color
serie1.SerieFormat.LineProperties.LineColor = Color.Pink;
serie1.SerieFormat.LineProperties.LinePattern = ExcelChartLinePattern.Dot;
serie1.SerieFormat.LineProperties.LineWeight = ExcelChartLineWeight.Narrow;

serie2.SerieFormat.LineProperties.LineColor = Color.Pink;
serie2.SerieFormat.LineProperties.LinePattern = ExcelChartLinePattern.Dot;
serie2.SerieFormat.LineProperties.LineWeight = ExcelChartLineWeight.Narrow;

serie1.SerieFormat.Fill.FillType = ExcelFillType.Gradient;
serie1.SerieFormat.Fill.GradientColorType = ExcelGradientColor.TwoColor;
serie1.SerieFormat.Fill.BackColor = Color.FromArgb(205, 217, 234);
serie1.SerieFormat.Fill.ForeColor = Color.Pink;

serie2.SerieFormat.Fill.FillType = ExcelFillType.Gradient;
serie2.SerieFormat.Fill.GradientColorType = ExcelGradientColor.TwoColor;
serie2.SerieFormat.Fill.BackColor = Color.FromArgb(205, 217, 234);
serie2.SerieFormat.Fill.ForeColor = Color.Pink;

//Set Datalabel
serie1.DataPoints.DefaultDataPoint.DataLabels.IsValue = true;
serie2.DataPoints.DefaultDataPoint.DataLabels.IsValue = true;
serie1.DataPoints.DefaultDataPoint.DataLabels.Position = ExcelDataLabelPosition.Outside;
serie2.DataPoints.DefaultDataPoint.DataLabels.Position = ExcelDataLabelPosition.Outside;

//Format data labels color and font
serie1.DataPoints.DefaultDataPoint.DataLabels.Color = ExcelKnownColors.Black;
serie2.DataPoints.DefaultDataPoint.DataLabels.Color = ExcelKnownColors.Black;

serie1.DataPoints.DefaultDataPoint.DataLabels.Size = 10;
serie1.DataPoints.DefaultDataPoint.DataLabels.FontName = "calibri";
serie1.DataPoints.DefaultDataPoint.DataLabels.Bold = true;

serie2.DataPoints.DefaultDataPoint.DataLabels.Size = 10;
serie2.DataPoints.DefaultDataPoint.DataLabels.FontName = "calibri";
serie2.DataPoints.DefaultDataPoint.DataLabels.Bold = true;

//Set Legend
chart.HasLegend = true;
chart.Legend.Position = ExcelLegendPosition.Bottom;

//Format legend border, color, and font
chart.Legend.FrameFormat.Border.AutoFormat = false;
chart.Legend.FrameFormat.Border.IsAutoLineColor = false;
chart.Legend.FrameFormat.Border.LineColor = Color.Black;
chart.Legend.FrameFormat.Border.LinePattern = ExcelChartLinePattern.LightGray;
chart.Legend.FrameFormat.Border.LineWeight = ExcelChartLineWeight.Narrow;

chart.Legend.TextArea.Color = ExcelKnownColors.Black;

chart.Legend.TextArea.Bold = true;
chart.Legend.TextArea.FontName = "Calibri";
chart.Legend.TextArea.Size = 8;
chart.Legend.TextArea.Strikethrough = false;

//Set axis title
chart.PrimaryCategoryAxis.Title = "Items";
chart.PrimaryValueAxis.Title = "Amount in($) and counts";

//Format chart axis border and font
chart.PrimaryCategoryAxis.Border.LinePattern = ExcelChartLinePattern.CircleDot;
chart.PrimaryCategoryAxis.Border.LineColor = Color.Pink;
chart.PrimaryCategoryAxis.Border.LineWeight = ExcelChartLineWeight.Hairline;

chart.PrimaryValueAxis.Border.LinePattern = ExcelChartLinePattern.CircleDot;
chart.PrimaryValueAxis.Border.LineColor = Color.Pink;
chart.PrimaryValueAxis.Border.LineWeight = ExcelChartLineWeight.Hairline;

chart.PrimaryCategoryAxis.Font.Color = ExcelKnownColors.Black;
chart.PrimaryCategoryAxis.Font.FontName = "Calibri";
chart.PrimaryCategoryAxis.Font.Bold = true;
chart.PrimaryCategoryAxis.Font.Size = 8;

chart.PrimaryValueAxis.Font.Color = ExcelKnownColors.Black;
chart.PrimaryValueAxis.Font.FontName = "Calibri";
chart.PrimaryValueAxis.Font.Bold = true;
chart.PrimaryValueAxis.Font.Size = 8;

//Saving the workbook
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Chart.xlsx"), FileMode.Create, FileAccess.Write);
workbook.SaveAs(outputStream);

//Dispose stream
inputStream.Dispose();
outputStream.Dispose();
}
}
}
}

0 comments on commit c4f109c

Please sign in to comment.