Chart Title in an Excel document
24 Jul 20267 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.
Add
The following code snippet illustrates how to add the chart title.
//Set the chart title
chart.ChartTitle = "Purchase Details";//Set the chart title
chart.ChartTitle = "Purchase Details";'Set the chart title
chart.ChartTitle = "Purchase Details"Formatting
Color
The following code snippet illustrates how to format the color of the chart title.
//Set the color
chart.ChartTitleArea.Color = ExcelKnownColors.Black;//Set the color
chart.ChartTitleArea.Color = ExcelKnownColors.Black;'Set the color
chart.ChartTitleArea.Color = ExcelKnownColors.BlackFont
The following code snippet illustrates how to format the font of the chart title.
//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 = 15Set 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 = 20The complete code snippet illustrating the above options is shown below.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"), 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
workbook.SaveAs(Path.GetFullPath("Output/Output.xlsx"));
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open("InputTemplate.xlsx", ExcelOpenType.Automatic);
IWorksheet sheet = workbook.Worksheets[0];
IChartShape chart = sheet.Charts[0];
//Set the chart title
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", ExcelOpenType.Automatic)
Dim sheet As IWorksheet = workbook.Worksheets(0)
Dim chart As IChartShape = sheet.Charts(0)
'Set the chart title
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 the chart title
chart.ChartTitleArea.Layout.Left = 20
'Saving the workbook
workbook.SaveAs("Output.xlsx")
End UsingA 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 by clearing its text. The title frame is preserved but rendered without any text.
//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