Chart Area in Excel document
14 Oct 20247 minutes to read
Chart area refers to the space that contains the entire chart or graph within a document. It includes all elements of the chart, such as data points, labels, axes, and the plot area. Using XlsIO, you can customize various aspects of the chart area in the chart.
Formatting
Border
The following code snippet illustrates how to format the border of the chart area.
//Format Chart Area
IChartFrameFormat chartArea = chart.ChartArea;
//Set the border
chartArea.Border.LinePattern = ExcelChartLinePattern.Solid;
chartArea.Border.LineColor = Color.Blue;
chartArea.Border.LineWeight = ExcelChartLineWeight.Hairline;
//Format Chart Area
IChartFrameFormat chartArea = chart.ChartArea;
//Set the border
chartArea.Border.LinePattern = ExcelChartLinePattern.Solid;
chartArea.Border.LineColor = Color.Blue;
chartArea.Border.LineWeight = ExcelChartLineWeight.Hairline;
'Format Chart Area
IChartFrameFormat chartArea = chart.ChartArea
'Set the border
chartArea.Border.LinePattern = ExcelChartLinePattern.Solid
chartArea.Border.LineColor = Color.Blue
chartArea.Border.LineWeight = ExcelChartLineWeight.Hairline
Color
The following code snippet illustrates how to format the color of the chart area.
//Set the color
chartArea.Fill.FillType = ExcelFillType.Gradient;
chartArea.Fill.GradientColorType = ExcelGradientColor.TwoColor;
chartArea.Fill.BackColor = Color.FromArgb(205, 217, 234);
chartArea.Fill.ForeColor = Color.White;
//Set the color
chartArea.Fill.FillType = ExcelFillType.Gradient;
chartArea.Fill.GradientColorType = ExcelGradientColor.TwoColor;
chartArea.Fill.BackColor = Color.FromArgb(205, 217, 234);
chartArea.Fill.ForeColor = Color.White;
'Set the color
chartArea.Fill.FillType = ExcelFillType.Gradient
chartArea.Fill.GradientColorType = ExcelGradientColor.TwoColor
chartArea.Fill.BackColor = Color.FromArgb(205, 217, 234)
chartArea.Fill.ForeColor = Color.White
The complete code snippet illustrating the above options is shown below.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
IWorkbook workbook = application.Workbooks.Open(inputStream);
IWorksheet sheet = workbook.Worksheets[0];
IChartShape chart = sheet.Charts[0];
//Format Chart Area
IChartFrameFormat chartArea = chart.ChartArea;
//Set the border
chartArea.Border.LinePattern = ExcelChartLinePattern.Solid;
chartArea.Border.LineColor = Color.Blue;
chartArea.Border.LineWeight = ExcelChartLineWeight.Hairline;
//Set the color
chartArea.Fill.FillType = ExcelFillType.Gradient;
chartArea.Fill.GradientColorType = ExcelGradientColor.TwoColor;
chartArea.Fill.BackColor = Color.FromArgb(205, 217, 234);
chartArea.Fill.ForeColor = Color.White;
//Saving the workbook as stream
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create, FileAccess.ReadWrite);
workbook.SaveAs(outputStream);
//Dispose streams
outputStream.Dispose();
inputStream.Dispose();
}
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open("InputTemplate.xlsx");
IWorksheet sheet = workbook.Worksheets[0];
IChartShape chart = sheet.Charts[0];
//Format Chart Area
IChartFrameFormat chartArea = chart.ChartArea;
//Set the border
chartArea.Border.LinePattern = ExcelChartLinePattern.Solid;
chartArea.Border.LineColor = Color.Blue;
chartArea.Border.LineWeight = ExcelChartLineWeight.Hairline;
//Set the color
chartArea.Fill.FillType = ExcelFillType.Gradient;
chartArea.Fill.GradientColorType = ExcelGradientColor.TwoColor;
chartArea.Fill.BackColor = Color.FromArgb(205, 217, 234);
chartArea.Fill.ForeColor = Color.White;
//Saving the workbook
workbook.SaveAs("Output.xlsx");
}
Using excelEngine As ExcelEngine = New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Open("InputTemplate.xlsx")
Dim sheet As IWorksheet = workbook.Worksheets(0)
Dim chart As IChartShape = sheet.Charts(0)
'Format Chart Area
Dim chartArea As IChartFrameFormat = chart.ChartArea
'Set the border
chartArea.Border.LinePattern = ExcelChartLinePattern.Solid
chartArea.Border.LineColor = Color.Blue
chartArea.Border.LineWeight = ExcelChartLineWeight.Hairline
'Set the color
chartArea.Fill.FillType = ExcelFillType.Gradient
chartArea.Fill.GradientColorType = ExcelGradientColor.TwoColor
chartArea.Fill.BackColor = Color.FromArgb(205, 217, 234)
chartArea.Fill.ForeColor = Color.White
'Saving the workbook
workbook.SaveAs("Output.xlsx")
End Using
A complete working example for the chart area in C# is present on this GitHub page.
Transparency
The following code snippet illustrates how to apply transparency in the chart area.
//Set the transparency
chartArea.Fill.Transparency = 0.5;
//Set the transparency
chartArea.Fill.Transparency = 0.5;
'Set the transparency
chartArea.Fill.Transparency = 0.5
NOTE
Transparency is only applicable when FillType is set as SolidColor. Color-shaded fill is represented as a floating-point value ranging from 0.0 (Clear) to 1.0 (Opaque).