Customize Chart Legend
29 Nov 202418 minutes to read
Legends are visual pictorial hints that provide a viewer information that helps them understand an chart. Using DocIO, you can customize the legend in the chart.
Set the Position of Legend
The following code snippet illustrates how to set the legend position in chart.
NOTE
Refer to the appropriate tabs in the code snippets section: C# [Cross-platform] for ASP.NET Core, Blazor, Xamarin, UWP, .NET MAUI, and WinUI; C# [Windows-specific] for WinForms and WPF; VB.NET [Windows-specific] for VB.NET applications.
//Set the position of legend.
chart.Legend.Position = OfficeLegendPosition.Bottom;
//Set the position of legend.
chart.Legend.Position = OfficeLegendPosition.Bottom;
' Set the position of legend.
chart.Legend.Position = OfficeLegendPosition.Bottom
Set the layout Inclusion
The following code snippet illustrates how to prevent the overlapping the legend in chart.
//Legend without overlapping the chart.
chart.Legend.IncludeInLayout = true;
//Set the position of legend.
chart.Legend.Position = OfficeLegendPosition.Bottom;
' Set the position of legend.
chart.Legend.Position = OfficeLegendPosition.Bottom
Customization of Border
The following code snippet illustrates how to modify the border of the legend in chart.
//Sets the legend border format - color, pattern, weight.
chart.Legend.FrameFormat.Border.AutoFormat = false;
chart.Legend.FrameFormat.Border.IsAutoLineColor = false;
chart.Legend.FrameFormat.Border.LineColor = Syncfusion.Drawing.Color.Blue;
chart.Legend.FrameFormat.Border.LinePattern = OfficeChartLinePattern.DashDot;
chart.Legend.FrameFormat.Border.LineWeight = OfficeChartLineWeight.Wide;
//Sets the legend border format - color, pattern, weight.
chart.Legend.FrameFormat.Border.AutoFormat = false;
chart.Legend.FrameFormat.Border.IsAutoLineColor = false;
chart.Legend.FrameFormat.Border.LineColor = Color.Blue;
chart.Legend.FrameFormat.Border.LinePattern = OfficeChartLinePattern.DashDot;
chart.Legend.FrameFormat.Border.LineWeight = OfficeChartLineWeight.Wide;
' Sets the legend border format - color, pattern, weight.
chart.Legend.FrameFormat.Border.AutoFormat = False
chart.Legend.FrameFormat.Border.IsAutoLineColor = False
chart.Legend.FrameFormat.Border.LineColor = Color.Blue
chart.Legend.FrameFormat.Border.LinePattern = OfficeChartLinePattern.DashDot
chart.Legend.FrameFormat.Border.LineWeight = OfficeChartLineWeight.Wide
Customization of Text Area
The following code snippet illustrates how to modify the text area of the legend in chart.
//Set the legend's text area formatting - font name, weight, color, size.
chart.Legend.TextArea.Bold = true;
chart.Legend.TextArea.Color = OfficeKnownColors.Bright_green;
chart.Legend.TextArea.FontName = "Times New Roman";
chart.Legend.TextArea.Size = 20;
chart.Legend.TextArea.Strikethrough = true;
//Set the legend's text area formatting - font name, weight, color, size.
chart.Legend.TextArea.Bold = true;
chart.Legend.TextArea.Color = OfficeKnownColors.Bright_green;
chart.Legend.TextArea.FontName = "Times New Roman";
chart.Legend.TextArea.Size = 20;
chart.Legend.TextArea.Strikethrough = true;
' Set the legend's text area formatting - font name, weight, color, size.
chart.Legend.TextArea.Bold = True
chart.Legend.TextArea.Color = OfficeKnownColors.Bright_green
chart.Legend.TextArea.FontName = "Times New Roman"
chart.Legend.TextArea.Size = 20
chart.Legend.TextArea.Strikethrough = True
Modify the Legend Entry
The following code snippet illustrates how to modify the legend entry.
//Modify the legend entry.
chart.Legend.LegendEntries[0].IsDeleted = true;
//Modify the legend entry.
chart.Legend.LegendEntries[0].IsDeleted = true;
' Modify the legend entry.
chart.Legend.LegendEntries(0).IsDeleted = True
Manage Legend Visibility
The following code snippet illustrates how to hide the legend in chart.
//Hiding the legend.
chart.HasLegend = false;
//Hiding the legend.
chart.HasLegend = false;
' Hiding the legend.
chart.HasLegend = False
View the Legend Horizontally
The following code snippet illustrates how to view legend horizontally.
//View legend horizontally.
chart.Legend.IsVerticalLegend = false;
//View legend horizontally.
chart.Legend.IsVerticalLegend = false;
' View legend horizontally.
chart.Legend.IsVerticalLegend = False
Set the Position using Layout
The following code snippet illustrates how to position the legend using layout.
//Manually positioning chart legend area using Layout.
chart.Legend.Layout.LeftMode = LayoutModes.edge;
chart.Legend.Layout.TopMode = LayoutModes.edge;
//Manually positioning chart legend area using Manual Layout.
chart.Legend.Layout.ManualLayout.LeftMode = LayoutModes.edge;
chart.Legend.Layout.ManualLayout.TopMode = LayoutModes.edge;
//Manually positioning chart legend area using Layout.
chart.Legend.Layout.LeftMode = LayoutModes.edge;
chart.Legend.Layout.TopMode = LayoutModes.edge;
//Manually positioning chart legend area using Manual Layout.
chart.Legend.Layout.ManualLayout.LeftMode = LayoutModes.edge;
chart.Legend.Layout.ManualLayout.TopMode = LayoutModes.edge;
' Manually positioning chart legend area using Layout.
chart.Legend.Layout.LeftMode = LayoutModes.Edge
chart.Legend.Layout.TopMode = LayoutModes.Edge
' Manually positioning chart legend area using Manual Layout.
chart.Legend.Layout.ManualLayout.LeftMode = LayoutModes.Edge
chart.Legend.Layout.ManualLayout.TopMode = LayoutModes.Edge
Resize the Legend
The following code snippet illustrates how to resize the legend in chart.
//Manually resizing chart legend area using Layout.
chart.Legend.Layout.Left = 400;
chart.Legend.Layout.Top = 150;
chart.Legend.Layout.Width = 150;
chart.Legend.Layout.Height = 100;
//Manually resizing chart legend area using Manual Layout.
chart.Legend.Layout.ManualLayout.Height = 0.09;
chart.Legend.Layout.ManualLayout.Width = 0.30;
chart.Legend.Layout.ManualLayout.Top = 0.36;
chart.Legend.Layout.ManualLayout.Left = 0.68;
//Manually resizing chart legend area using Layout.
chart.Legend.Layout.Left = 400;
chart.Legend.Layout.Top = 150;
chart.Legend.Layout.Width = 150;
chart.Legend.Layout.Height = 100;
//Manually resizing chart legend area using Manual Layout.
chart.Legend.Layout.ManualLayout.Height = 0.09;
chart.Legend.Layout.ManualLayout.Width = 0.30;
chart.Legend.Layout.ManualLayout.Top = 0.36;
chart.Legend.Layout.ManualLayout.Left = 0.68;
' Manually resizing chart legend area using Layout.
chart.Legend.Layout.Left = 400
chart.Legend.Layout.Top = 150
chart.Legend.Layout.Width = 150
chart.Legend.Layout.Height = 100
' Manually resizing chart legend area using Manual Layout.
chart.Legend.Layout.ManualLayout.Height = 0.09
chart.Legend.Layout.ManualLayout.Width = 0.30
chart.Legend.Layout.ManualLayout.Top = 0.36
chart.Legend.Layout.ManualLayout.Left = 0.68
The complete code snippet illustrating the above options is shown below.
FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"../../../Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//Open an existing document from file system through constructor of WordDocument class.
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
{
//Get the paragraph.
WParagraph paragraph = document.LastParagraph;
//Get the chart entity.
WChart chart = paragraph.ChildEntities[0] as WChart;
//Enable the legend.
chart.HasLegend = true;
//Set the position of legend.
chart.Legend.Position = OfficeLegendPosition.Right;
//Legend without overlapping the chart.
chart.Legend.IncludeInLayout = true;
chart.Legend.FrameFormat.Border.AutoFormat = false;
chart.Legend.FrameFormat.Border.IsAutoLineColor = false;
chart.Legend.FrameFormat.Border.LineColor = Syncfusion.Drawing.Color.Black;
chart.Legend.FrameFormat.Border.LinePattern = OfficeChartLinePattern.DashDot;
chart.Legend.FrameFormat.Border.LineWeight = OfficeChartLineWeight.Hairline;
//Set the legend's text area formatting - font name, weight, color, size.
chart.Legend.TextArea.Bold = true;
chart.Legend.TextArea.Color = OfficeKnownColors.Pink;
chart.Legend.TextArea.FontName = "Times New Roman";
chart.Legend.TextArea.Size = 10;
chart.Legend.TextArea.Strikethrough = false;
//View legend in vertical.
chart.Legend.IsVerticalLegend = true;
//Modifies the legend entry.
chart.Legend.LegendEntries[0].IsDeleted = true;
//Manually resizing chart legend area using Layout.
chart.Legend.Layout.Left = 0.2;
chart.Legend.Layout.Top = 5;
chart.Legend.Layout.Width = 40;
chart.Legend.Layout.Height = 40;
//Legend without overlapping the chart.
chart.Legend.IncludeInLayout = true;
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"../../../Sample.docx"), FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
{
//Save the Word file.
document.Save(outputStream, FormatType.Docx);
}
}
using (WordDocument document = new WordDocument("Template.docx"))
{
//Get the paragraph.
WParagraph paragraph = document.LastParagraph;
//Get the chart entity.
WChart chart = paragraph.ChildEntities[0] as WChart;
//Enable the legend.
chart.HasLegend = true;
//Set the position of legend.
chart.Legend.Position = OfficeLegendPosition.Right;
//Legend without overlapping the chart.
chart.Legend.IncludeInLayout = true;
chart.Legend.FrameFormat.Border.AutoFormat = false;
chart.Legend.FrameFormat.Border.IsAutoLineColor = false;
chart.Legend.FrameFormat.Border.LineColor = Color.Black;
chart.Legend.FrameFormat.Border.LinePattern = OfficeChartLinePattern.DashDot;
chart.Legend.FrameFormat.Border.LineWeight = OfficeChartLineWeight.Hairline;
//Set the legend's text area formatting - font name, weight, color, size.
chart.Legend.TextArea.Bold = true;
chart.Legend.TextArea.Color = OfficeKnownColors.Pink;
chart.Legend.TextArea.FontName = "Times New Roman";
chart.Legend.TextArea.Size = 10;
chart.Legend.TextArea.Strikethrough = false;
//View legend in vertical.
chart.Legend.IsVerticalLegend = true;
//Modifies the legend entry.
chart.Legend.LegendEntries[0].IsDeleted = true;
//Manually resizing chart legend area using Layout.
chart.Legend.Layout.Left = 0.2;
chart.Legend.Layout.Top = 5;
chart.Legend.Layout.Width = 40;
chart.Legend.Layout.Height = 40;
//Legend without overlapping the chart.
chart.Legend.IncludeInLayout = true;
//Save the Word file.
document.Save("Sample.docx");
}
Using document As New WordDocument("Template.docx")
' Get the paragraph.
Dim paragraph As WParagraph = document.LastParagraph
' Get the chart entity.
Dim chart As WChart = TryCast(paragraph.ChildEntities(0), WChart)
' Enable the legend.
chart.HasLegend = True
' Set the position of legend.
chart.Legend.Position = OfficeLegendPosition.Right
' Legend without overlapping the chart.
chart.Legend.IncludeInLayout = True
chart.Legend.FrameFormat.Border.AutoFormat = False
chart.Legend.FrameFormat.Border.IsAutoLineColor = False
chart.Legend.FrameFormat.Border.LineColor = Color.Black
chart.Legend.FrameFormat.Border.LinePattern = OfficeChartLinePattern.DashDot
chart.Legend.FrameFormat.Border.LineWeight = OfficeChartLineWeight.Hairline
' Set the legend's text area formatting - font name, weight, color, size.
chart.Legend.TextArea.Bold = True
chart.Legend.TextArea.Color = OfficeKnownColors.Pink
chart.Legend.TextArea.FontName = "Times New Roman"
chart.Legend.TextArea.Size = 10
chart.Legend.TextArea.Strikethrough = False
' View legend in vertical.
chart.Legend.IsVerticalLegend = True
' Modifies the legend entry.
chart.Legend.LegendEntries(0).IsDeleted = True
' Manually resizing chart legend area using Layout.
chart.Legend.Layout.Left = 0.2
chart.Legend.Layout.Top = 5
chart.Legend.Layout.Width = 40
chart.Legend.Layout.Height = 40
' Legend without overlapping the chart.
chart.Legend.IncludeInLayout = True
' Save the Word file.
document.Save("Sample.docx")
End Using
You can download a complete working sample from GitHub.