Chart Legend in Excel document
29 Oct 202413 minutes to read
Legends are visual pictorial hints that provide a viewer information that helps them understand an chart. Using XlsIO, you can customize the legend in the chart.
Add
The following code snippet illustrates how to add the legend.
//Add the legend
chart.HasLegend = true;
//Add the legend
chart.HasLegend = true;
'Add the legend
chart.HasLegend = False
Formatting
Border
The following code snippet illustrates how to format the border of the legend.
//Set the border
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.DashDot;
chart.Legend.FrameFormat.Border.LineWeight = ExcelChartLineWeight.Narrow;
//Set the border
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.DashDot;
chart.Legend.FrameFormat.Border.LineWeight = ExcelChartLineWeight.Narrow;
'Set the border
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.DashDot
chart.Legend.FrameFormat.Border.LineWeight = ExcelChartLineWeight.Narrow
Color
The following code snippet illustrates how to format the color of the legend.
//Set the color
chart.Legend.TextArea.Color = ExcelKnownColors.Pink;
//Set the color
chart.Legend.TextArea.Color = ExcelKnownColors.Pink;
'Set the color
chart.Legend.TextArea.Color = ExcelKnownColors.Pink
The following code snippet illustrates how to format the background color of the legend.
//Set the background color
chart.Legend.FrameFormat.Fill.ForeColorIndex = ExcelKnownColors.Yellow;
//Set the background color
chart.Legend.FrameFormat.Fill.ForeColorIndex = ExcelKnownColors.Yellow;
'Set the background color
chart.Legend.FrameFormat.Fill.ForeColorIndex = ExcelKnownColors.Yellow;
Font
The following code snippet illustrates how to format the font of the legend.
//Set the font
chart.Legend.TextArea.Bold = true;
chart.Legend.TextArea.FontName = "Times New Roman";
chart.Legend.TextArea.Size = 10;
chart.Legend.TextArea.Strikethrough = false;
//Set the font
chart.Legend.TextArea.Bold = true;
chart.Legend.TextArea.FontName = "Times New Roman";
chart.Legend.TextArea.Size = 10;
chart.Legend.TextArea.Strikethrough = false;
'Set the font
chart.Legend.TextArea.Bold = true
chart.Legend.TextArea.FontName = "Times New Roman"
chart.Legend.TextArea.Size = 10
chart.Legend.TextArea.Strikethrough = false
Set Position
The following code snippet illustrates how to set the position of the legend.
//Set the position
chart.Legend.Position = ExcelLegendPosition.Bottom;
//Set the position
chart.Legend.Position = ExcelLegendPosition.Bottom;
'Set the position
chart.Legend.Position = ExcelLegendPosition.Bottom
View horizontally
The following code snippet illustrates how to view the legend horizontally.
//View legend horizontally
chart.Legend.IsVerticalLegend = false;
//View legend horizontally
chart.Legend.IsVerticalLegend = false;
'View legend horizontally
chart.Legend.IsVerticalLegend = False
Layout Inclusion
The following code snippet illustrates how to prevent the legend from overlapping the chart.
//Set Legend without overlapping the chart
chart.Legend.IncludeInLayout = true;
//Set Legend without overlapping the chart
chart.Legend.IncludeInLayout = true;
'Set Legend without overlapping the chart
chart.Legend.IncludeInLayout = true
Remove
The following code snippet illustrates how to remove the legend.
//Remove the legend
chart.Legend.LegendEntries[0].Delete();
//Remove the legend
chart.Legend.LegendEntries[0].Delete();
'Remove the legend
chart.Legend.LegendEntries(0).Delete()
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 worksheet = workbook.Worksheets[0];
IChartShape chart = worksheet.Charts[0];
//Add the legend
chart.HasLegend = true;
//Set the position
chart.Legend.Position = ExcelLegendPosition.Bottom;
//View legend horizontally
chart.Legend.IsVerticalLegend = false;
//Set the border
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.DashDot;
chart.Legend.FrameFormat.Border.LineWeight = ExcelChartLineWeight.Narrow;
//Set the color
chart.Legend.TextArea.Color = ExcelKnownColors.Pink;
//Set the font
chart.Legend.TextArea.Bold = true;
chart.Legend.TextArea.FontName = "Times New Roman";
chart.Legend.TextArea.Size = 10;
chart.Legend.TextArea.Strikethrough = false;
//Remove the legend
chart.Legend.LegendEntries[0].IsDeleted = true;
//Set Legend without overlapping the chart
chart.Legend.IncludeInLayout = true;
//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];
//Add the legend
chart.HasLegend = true;
//Set the position
chart.Legend.Position = ExcelLegendPosition.Bottom;
//View legend horizontally
chart.Legend.IsVerticalLegend = false;
//Set the border
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.DashDot;
chart.Legend.FrameFormat.Border.LineWeight = ExcelChartLineWeight.Narrow;
//Set the color
chart.Legend.TextArea.Color = ExcelKnownColors.Pink;
//Set the background color
chart.Legend.FrameFormat.Fill.ForeColorIndex = ExcelKnownColors.Yellow;
//Set the font
chart.Legend.TextArea.Bold = true;
chart.Legend.TextArea.FontName = "Times New Roman";
chart.Legend.TextArea.Size = 10;
chart.Legend.TextArea.Strikethrough = false;
//Remove the legend
chart.Legend.LegendEntries[0].Delete();
//Set Legend without overlapping the chart
chart.Legend.IncludeInLayout = true;
//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)
'Add the legend
chart.HasLegend = True
'Set the position
chart.Legend.Position = ExcelLegendPosition.Bottom
'View legend horizontally
chart.Legend.IsVerticalLegend = False
'Set the border
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.DashDot
chart.Legend.FrameFormat.Border.LineWeight = ExcelChartLineWeight.Narrow
'Set the color
chart.Legend.TextArea.Color = ExcelKnownColors.Pink
'Set the background color
chart.Legend.FrameFormat.Fill.ForeColorIndex = ExcelKnownColors.Yellow
'Set the font
chart.Legend.TextArea.Bold = True
chart.Legend.TextArea.FontName = "Times New Roman"
chart.Legend.TextArea.Size = 10
chart.Legend.TextArea.Strikethrough = False
'Remove the legend
chart.Legend.LegendEntries(0).Delete()
'Set Legend without overlapping the chart
chart.Legend.IncludeInLayout = True
'Saving the workbook
workbook.SaveAs("Output.xlsx")
End Using
A complete working example for the chart legend in C# is present on this GitHub page.