Chart Title in Excel document
14 Oct 20247 minutes to read
Chart title is a brief description at the top of a chart, offering context and clarity for the data displayed. Using XlsIO, you can customize the chart title in the chart.
Add
The following code snippet illustrates how to add the chart title.
//Set chart name and title
chart.Name = "Purchase Details";
chart.ChartTitle = "Purchase Details";
//Set chart name and title
chart.Name = "Purchase Details";
chart.ChartTitle = "Purchase Details";
'Set chart name and title
chart.Name = "Purchase Details"
chart.ChartTitle = "Purchase Details"
Formatting
Color
The following code snippet illustrates how to format the color of the chart area.
//Set the color
chart.ChartTitleArea.Color = ExcelKnownColors.Black;
//Set the color
chart.ChartTitleArea.Color = ExcelKnownColors.Black;
'Set the color
chart.ChartTitleArea.Color = ExcelKnownColors.Black
Font
The following code snippet illustrates how to format the font of the legend.
//Set the font
chart.ChartTitleArea.FontName = "Calibri";
chart.ChartTitleArea.Bold = true;
chart.ChartTitleArea.Underline = ExcelUnderline.Single;
chart.ChartTitleArea.Size = 15;
//Set the font
chart.ChartTitleArea.FontName = "Calibri";
chart.ChartTitleArea.Bold = true;
chart.ChartTitleArea.Underline = ExcelUnderline.Single;
chart.ChartTitleArea.Size = 15;
'Set the font
chart.ChartTitleArea.FontName = "Calibri"
chart.ChartTitleArea.Bold = true
chart.ChartTitleArea.Underline = ExcelUnderline.Single
chart.ChartTitleArea.Size = 15
Set Position
The following code snippet illustrates how to set the position of the chart title.
//Set the position
chart.ChartTitleArea.Layout.Left = 20;
//Set the position
chart.ChartTitleArea.Layout.Left = 20;
'Set the position
chart.ChartTitleArea.Layout.Left = 20
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, ExcelOpenType.Automatic);
IWorksheet sheet = workbook.Worksheets[0];
IChartShape chart = sheet.Charts[0];
//Set chart name and title
chart.Name = "Purchase Details";
chart.ChartTitle = "Purchase Details";
//Formatting chart title area
chart.ChartTitleArea.FontName = "Calibri";
chart.ChartTitleArea.Bold = true;
chart.ChartTitleArea.Color = ExcelKnownColors.Black;
chart.ChartTitleArea.Underline = ExcelUnderline.Single;
chart.ChartTitleArea.Size = 15;
//Manually resizing chart title area using Layout.
chart.ChartTitleArea.Layout.Left = 20;
//Saving the workbook as stream
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create, FileAccess.ReadWrite);
workbook.SaveAs(outputStream);
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];
//Set chart name and title
chart.Name = "Purchase Details";
chart.ChartTitle = "Purchase Details";
//Set the color
chart.ChartTitleArea.Color = ExcelKnownColors.Black;
//Set the font
chart.ChartTitleArea.FontName = "Calibri";
chart.ChartTitleArea.Bold = true;
chart.ChartTitleArea.Underline = ExcelUnderline.Single;
chart.ChartTitleArea.Size = 15;
//Set the position
chart.ChartTitleArea.Layout.Left = 20;
//Saving the workbook
workbook.SaveAs("Output.xlsx");
}
Using excelEngine As 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)
'Set chart name and title
chart.Name = "Purchase Details"
chart.ChartTitle = "Purchase Details"
'Set the color
chart.ChartTitleArea.Color = ExcelKnownColors.Black
'Set the font
chart.ChartTitleArea.FontName = "Calibri"
chart.ChartTitleArea.Bold = True
chart.ChartTitleArea.Underline = ExcelUnderline.Single
chart.ChartTitleArea.Size = 15
'Set the position of
chart.ChartTitleArea.Layout.Left = 20
'Saving the workbook as stream
workbook.SaveAs("Output.xlsx")
End Using
A complete working example for the chart title in C# is present on this GitHub page.
Remove
The following code snippet illustrates how to remove the chart title.
//Remove the chart title
chart.ChartTitleArea.Text = String.Empty;
//Remove the chart title
chart.ChartTitleArea.Text = String.Empty;
'Remove the chart title
chart.ChartTitleArea.Text = String.Empty