Back to Devexpress

LegendBase.AlignmentHorizontal Property

corelibraries-devexpress-dot-xtracharts-dot-legendbase.md

latest15.0 KB
Original Source

LegendBase.AlignmentHorizontal Property

Gets or sets the legend’s horizontal alignment within the chart control.

Namespace : DevExpress.XtraCharts

Assembly : DevExpress.XtraCharts.v25.2.dll

NuGet Package : DevExpress.Charts

Declaration

csharp
[XtraChartsLocalizableCategory(XtraChartsCategory.Behavior)]
public LegendAlignmentHorizontal AlignmentHorizontal { get; set; }
vb
<XtraChartsLocalizableCategory(XtraChartsCategory.Behavior)>
Public Property AlignmentHorizontal As LegendAlignmentHorizontal

Property Value

TypeDescription
LegendAlignmentHorizontal

A LegendAlignmentHorizontal enumeration member which specifies the legend’s horizontal alignment within the chart control.

|

Available values:

NameDescription
LeftOutside

The legend is positioned to the left of the Chart control.

| | Left |

The legend is positioned on the left side of the Diagram.

| | Center |

Centers a legend horizontally within the Chart control.

| | Right |

The legend is positioned on the right side of the Diagram.

| | RightOutside |

The legend is positioned on the right side of the Chart control.

|

Remarks

To specify the legend’s vertical alignment use the LegendBase.AlignmentVertical property.

For more information, refer to Legends.

Example

This example demonstrates how to access a chart’s Legend and customize its appearance at runtime.

To specify whether series, indicators, Constant Lines, and Strips should be shown in the legend, set their SeriesBase.ShowInLegend, Indicator.ShowInLegend, ConstantLine.ShowInLegend, and Strip.ShowInLegend properties to true.

View Example

cs
using System;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.Drawing;
using DevExpress.XtraCharts;

namespace ChartLegend {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e) {
            // Create an empty chart.
            ChartControl chartControl1 = new ChartControl();

            // Create a series and add points to it.
            Series series1 = new Series("Series 1", ViewType.Bar);
            series1.Points.Add(new SeriesPoint("A", new double[] { 4 }));
            series1.Points.Add(new SeriesPoint("B", new double[] { 2 }));
            series1.Points.Add(new SeriesPoint("C", new double[] { 17 }));
            series1.Points.Add(new SeriesPoint("D", new double[] { 4 }));
            series1.Points.Add(new SeriesPoint("E", new double[] { 17 }));
            series1.Points.Add(new SeriesPoint("F", new double[] { 12 }));
            series1.Points.Add(new SeriesPoint("G", new double[] { 15 }));

            // Add the series to the chart.
            chartControl1.Series.Add(series1);

            // Create a constant line (optional).
            ConstantLine constantLine1 = new ConstantLine("Constant Line 1");
            ((XYDiagram)chartControl1.Diagram).AxisY.ConstantLines.Add(constantLine1);
            constantLine1.AxisValue = 4.5;

            // Create a strip (optional).
            Strip strip1 = new Strip("Strip 1");
            ((XYDiagram)chartControl1.Diagram).AxisY.Strips.Add(strip1);
            strip1.MaxLimit.AxisValue = 15;
            strip1.MinLimit.AxisValue = 7.5;

            // Customize the strip's and constant line's legend appearance.
            strip1.ShowInLegend = true;
            constantLine1.ShowInLegend = true;
            constantLine1.LegendText = "Some Threshold";

            // Display the chart control's legend.
            chartControl1.Legend.Visibility = DevExpress.Utils.DefaultBoolean.True;

            // Define its horizontal and vertical alignment.
            chartControl1.Legend.AlignmentHorizontal = LegendAlignmentHorizontal.RightOutside;
            chartControl1.Legend.AlignmentVertical = LegendAlignmentVertical.Top;

            // Define the layout of items within the legend.
            chartControl1.Legend.Direction = LegendDirection.LeftToRight;
            chartControl1.Legend.EquallySpacedItems = true;
            chartControl1.Legend.HorizontalIndent = 4;
            chartControl1.Legend.VerticalIndent = 4;
            chartControl1.Legend.TextOffset = 4;

            // Define the limits for the legend to occupy the chart's space.
            chartControl1.Legend.MaxHorizontalPercentage = 50;
            chartControl1.Legend.MaxVerticalPercentage = 50;

            // Define the legend markers' options.
            chartControl1.Legend.MarkerVisible = true;
            chartControl1.Legend.MarkerSize = new Size(20, 20);

            // Customize the legend appearance.
            chartControl1.Legend.BackColor = Color.LightBlue;
            chartControl1.Legend.FillStyle.FillMode = FillMode.Empty;

            chartControl1.Legend.Border.Color = Color.DarkBlue;
            chartControl1.Legend.Border.Thickness = 2;
            chartControl1.Legend.Border.Visibility = DevExpress.Utils.DefaultBoolean.True;

            chartControl1.Legend.Shadow.Visible = true;
            chartControl1.Legend.Shadow.Color = Color.LightGray;
            chartControl1.Legend.Shadow.Size = 2;

            // Customize the legend text properties.
            chartControl1.Legend.EnableAntialiasing = DevExpress.Utils.DefaultBoolean.False;
            chartControl1.Legend.DXFont = new DXFont("Tahoma", 9, DXFontStyle.Bold);
            chartControl1.Legend.TextColor = Color.DarkBlue;

            // Add the chart to the form.
            chartControl1.Dock = DockStyle.Fill;
            this.Controls.Add(chartControl1);
        }
    }
}
vb
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports DevExpress.Drawing
Imports DevExpress.XtraCharts

Namespace ChartLegend

    Public Partial Class Form1
        Inherits Form

        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
            ' Create an empty chart.
            Dim chartControl1 As ChartControl = New ChartControl()
            ' Create a series and add points to it.
            Dim series1 As Series = New Series("Series 1", ViewType.Bar)
            series1.Points.Add(New SeriesPoint("A", New Double() {4}))
            series1.Points.Add(New SeriesPoint("B", New Double() {2}))
            series1.Points.Add(New SeriesPoint("C", New Double() {17}))
            series1.Points.Add(New SeriesPoint("D", New Double() {4}))
            series1.Points.Add(New SeriesPoint("E", New Double() {17}))
            series1.Points.Add(New SeriesPoint("F", New Double() {12}))
            series1.Points.Add(New SeriesPoint("G", New Double() {15}))
            ' Add the series to the chart.
            chartControl1.Series.Add(series1)
            ' Create a constant line (optional).
            Dim constantLine1 As ConstantLine = New ConstantLine("Constant Line 1")
            CType(chartControl1.Diagram, XYDiagram).AxisY.ConstantLines.Add(constantLine1)
            constantLine1.AxisValue = 4.5
            ' Create a strip (optional).
            Dim strip1 As Strip = New Strip("Strip 1")
            CType(chartControl1.Diagram, XYDiagram).AxisY.Strips.Add(strip1)
            strip1.MaxLimit.AxisValue = 15
            strip1.MinLimit.AxisValue = 7.5
            ' Customize the strip's and constant line's legend appearance.
            strip1.ShowInLegend = True
            constantLine1.ShowInLegend = True
            constantLine1.LegendText = "Some Threshold"
            ' Display the chart control's legend.
            chartControl1.Legend.Visibility = DevExpress.Utils.DefaultBoolean.True
            ' Define its horizontal and vertical alignment.
            chartControl1.Legend.AlignmentHorizontal = LegendAlignmentHorizontal.RightOutside
            chartControl1.Legend.AlignmentVertical = LegendAlignmentVertical.Top
            ' Define the layout of items within the legend.
            chartControl1.Legend.Direction = LegendDirection.LeftToRight
            chartControl1.Legend.EquallySpacedItems = True
            chartControl1.Legend.HorizontalIndent = 4
            chartControl1.Legend.VerticalIndent = 4
            chartControl1.Legend.TextOffset = 4
            ' Define the limits for the legend to occupy the chart's space.
            chartControl1.Legend.MaxHorizontalPercentage = 50
            chartControl1.Legend.MaxVerticalPercentage = 50
            ' Define the legend markers' options.
            chartControl1.Legend.MarkerVisible = True
            chartControl1.Legend.MarkerSize = New Size(20, 20)
            ' Customize the legend appearance.
            chartControl1.Legend.BackColor = Color.LightBlue
            chartControl1.Legend.FillStyle.FillMode = FillMode.Empty
            chartControl1.Legend.Border.Color = Color.DarkBlue
            chartControl1.Legend.Border.Thickness = 2
            chartControl1.Legend.Border.Visibility = DevExpress.Utils.DefaultBoolean.True
            chartControl1.Legend.Shadow.Visible = True
            chartControl1.Legend.Shadow.Color = Color.LightGray
            chartControl1.Legend.Shadow.Size = 2
            ' Customize the legend text properties.
            chartControl1.Legend.EnableAntialiasing = DevExpress.Utils.DefaultBoolean.False
            chartControl1.Legend.DXFont = New DXFont("Tahoma", 9, DXFontStyle.Bold)
            chartControl1.Legend.TextColor = Color.DarkBlue
            ' Add the chart to the form.
            chartControl1.Dock = DockStyle.Fill
            Me.Controls.Add(chartControl1)
        End Sub
    End Class
End Namespace

The following code snippets (auto-collected from DevExpress Examples) contain references to the AlignmentHorizontal property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-visualize-data-grid-rows-in-a-chart/CS/ControlRowSourceSample/MainForm.cs#L38

csharp
chartControl.Legend.AlignmentHorizontal = LegendAlignmentHorizontal.Right;
chartControl.Legend.AlignmentVertical = LegendAlignmentVertical.Top;

winforms-charts-create-stacked-bar-chart/CS/Series_StackedBarChart/Form1.cs#L42

csharp
chart.Legend.MarkerMode = LegendMarkerMode.CheckBoxAndMarker;
chart.Legend.AlignmentHorizontal = LegendAlignmentHorizontal.Center;
chart.Legend.AlignmentVertical = LegendAlignmentVertical.TopOutside;

winforms-chart-add-an-additional-legend-to-a-chart/CS/LegendsSamples/Form1.cs#L38

csharp
macdLegend.DockTarget = diagram.Panes.GetPaneByName("macdPane");
macdLegend.AlignmentHorizontal = LegendAlignmentHorizontal.Left;
macdLegend.AlignmentVertical = LegendAlignmentVertical.Top;

winforms-visualize-data-grid-rows-in-a-chart/VB/ControlRowSourceSample/MainForm.vb#L34

vb
diagram.AxisY.Label.TextPattern = "{V:C0}"
chartControl.Legend.AlignmentHorizontal = LegendAlignmentHorizontal.Right
chartControl.Legend.AlignmentVertical = LegendAlignmentVertical.Top

winforms-charts-create-stacked-bar-chart/VB/Series_StackedBarChart/Form1.vb#L39

vb
chart.Legend.MarkerMode = LegendMarkerMode.CheckBoxAndMarker
chart.Legend.AlignmentHorizontal = LegendAlignmentHorizontal.Center
chart.Legend.AlignmentVertical = LegendAlignmentVertical.TopOutside

winforms-chart-add-an-additional-legend-to-a-chart/VB/LegendsSamples/Form1.vb#L40

vb
macdLegend.DockTarget = diagram.Panes.GetPaneByName("macdPane")
macdLegend.AlignmentHorizontal = LegendAlignmentHorizontal.Left
macdLegend.AlignmentVertical = LegendAlignmentVertical.Top

See Also

AlignmentVertical

Legends

LegendBase Class

LegendBase Members

DevExpress.XtraCharts Namespace