Chart Legend in PowerPoint
2 Dec 202418 minutes to read
Legends are visual pictorial hints that provide a viewer information that helps them understand an chart. Using Presentation, 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.
//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("Data/Template.pptx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//Open an existing PowerPoint Presentation.
using (IPresentation pptxDoc = Presentation.Open(fileStreamPath))
{
//Gets the first slide.
ISlide slide = pptxDoc.Slides[0];
//Gets the chart in slide.
IPresentationChart chart = slide.Shapes[0] as IPresentationChart;
//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("Result.pptx", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
{
//Save the PowerPoint Presentation.
pptxDoc.Save(outputStream);
}
}
//Open an existing PowerPoint Presentation.
using (IPresentation pptxDoc = Presentation.Open("Template.pptx"))
{
//Gets the first slide.
ISlide slide = pptxDoc.Slides[0];
//Gets the chart in slide.
IPresentationChart chart = slide.Shapes[0] as IPresentationChart;
//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;
//Save the PowerPoint Presentation.
pptxDoc.Save("Result.pptx");
}
' Open an existing PowerPoint Presentation.
Using pptxDoc As IPresentation = Presentation.Open("Template.pptx")
' Gets the first slide.
Dim slide As ISlide = pptxDoc.Slides(0)
' Gets the chart in the slide.
Dim chart As IPresentationChart = TryCast(slide.Shapes(0), IPresentationChart)
' Enable the legend.
chart.HasLegend = True
' Set the position of the 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
' Save the PowerPoint Presentation.
pptxDoc.Save("Result.pptx")
End Using
You can download a complete working sample from GitHub.