Back to Devexpress

LegendItemCheckedEventArgs.CheckedElement Property

corelibraries-devexpress-dot-xtracharts-dot-legenditemcheckedeventargs.md

latest11.6 KB
Original Source

LegendItemCheckedEventArgs.CheckedElement Property

Gets a value that defines which chart element has been checked on a legend by a legend check box.

Namespace : DevExpress.XtraCharts

Assembly : DevExpress.XtraCharts.v25.2.dll

NuGet Package : DevExpress.Charts

Declaration

csharp
public ChartElement CheckedElement { get; }
vb
Public ReadOnly Property CheckedElement As ChartElement

Property Value

TypeDescription
ChartElement

The ChartElement value that is the chart element for which the legend check box has been checked.

|

Remarks

Use the CheckedElement property to define a chart element for which the legend check box has been checked when the ChartControl.LegendItemChecked (WebChartControl.LegendItemChecked) event is handled.

Note

Set the Legend.UseCheckBoxes property to CheckBox , CheckBoxAndMarker or MarkerAndCheckBox to use legend check boxes.

For more information on legend check boxes, see the corresponding section of the Legend Check Box topic.

Example

This example demonstrates how to show chart series (Point, Line, or Area) depending on the selection state of a custom radio button in the chart legend.

View Example

Use the ChartControl.CustomDrawSeries event handler to create a custom appearance for radio buttons based on the color of a selected series. Handle the ChartControl.LegendItemChecked event and use e.CheckedElement and SeriesBase.CheckedInLegend proeprties to show (or hide) the chart series when you switch between radio buttons.

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

namespace CustomCheckboxesInLegendViewAndBehavior {
    public partial class mainForm : Form {
        const int LegendRadioSide = 17;
        const int LegendRadioInnerPointBoundsSide = 8;
        const float LegendRadioWidth = 1.5f;
        bool initializationFlag = false;

        public mainForm() {
            InitializeComponent();
            chartControl.BeginInit(); {
                chartControl.LegendItemChecked += OnLegendItemChecked;
                chartControl.CustomDrawSeries += OnCustomDrawSeries;
                chartControl.Legend.UseCheckBoxes = true;
                chartControl.Series["Point"].CheckedInLegend = false;
                chartControl.Series["Line"].CheckedInLegend = true;
                chartControl.Series["Area"].CheckedInLegend = false;
            }
            chartControl.EndInit();
            initializationFlag = false;
        }

        void OnLegendItemChecked(object sender, LegendItemCheckedEventArgs e) {
            if (initializationFlag == true)
                return;
            initializationFlag = true; {
                Series checkedSeries = e.CheckedElement as Series;
                if (checkedSeries == null)
                    throw new Exception("Expected series only");
                foreach (Series series in chartControl.Series)
                    series.CheckedInLegend = false;
                checkedSeries.CheckedInLegend = true;
                chartControl.Titles[0].Text = checkedSeries.Name;
            }
            initializationFlag = false;
        }

        void OnCustomDrawSeries(object sender, CustomDrawSeriesEventArgs e) {
            DXBitmap bitmap = new DXBitmap(LegendRadioSide, LegendRadioSide);
            using (DXGraphics graphics = DXGraphics.FromImage(bitmap)) {
                graphics.SmoothingMode = DXSmoothingMode.HighQuality;
                Color seriesColor = GetSeriesColor(e.Series, chartControl);
                using (DXPen radioPen = new DXPen(seriesColor, LegendRadioWidth)) {
                    int radioRadius = LegendRadioSide - 3;
                    Rectangle radioRectangle = new Rectangle(1, 1, radioRadius, radioRadius);
                    graphics.DrawEllipse(radioPen, radioRectangle);
                }
                if (e.Series.CheckedInLegend) {
                    using (DXBrush brush = new DXSolidBrush(seriesColor)) {
                        int coord = (LegendRadioSide - LegendRadioInnerPointBoundsSide) / 2;
                        Rectangle filledEllipseBounds = new Rectangle(coord, coord,
                              LegendRadioInnerPointBoundsSide, LegendRadioInnerPointBoundsSide);
                        graphics.FillEllipse(brush, filledEllipseBounds);
                    }
                }
            }
            e.DisposeLegendMarkerImage = true;
            e.DXLegendMarkerImage = bitmap;
        }

        Color GetSeriesColor(Series series, ChartControl chartControl) {
            int seriesIndex = chartControl.Series.IndexOf(series);
            string paletteName = chartControl.PaletteName;
            Palette currentPalette = chartControl.PaletteRepository[paletteName];
            PaletteEntry paletteEntryAccordingToSeries = currentPalette[seriesIndex];
            Color result = paletteEntryAccordingToSeries.Color;
            return result;
        }
    }
}
vb
Imports DevExpress.Drawing
Imports DevExpress.XtraCharts
Imports System
Imports System.Drawing
Imports System.Windows.Forms

Namespace CustomCheckboxesInLegendViewAndBehavior

    Public Partial Class mainForm
        Inherits Form

        Const LegendRadioSide As Integer = 17

        Const LegendRadioInnerPointBoundsSide As Integer = 8

        Const LegendRadioWidth As Single = 1.5F

        Private initializationFlag As Boolean = False

        Public Sub New()
            InitializeComponent()
            chartControl.BeginInit()
            If True Then
                AddHandler chartControl.LegendItemChecked, AddressOf OnLegendItemChecked
                AddHandler chartControl.CustomDrawSeries, AddressOf OnCustomDrawSeries
                chartControl.Legend.UseCheckBoxes = True
                chartControl.Series("Point").CheckedInLegend = False
                chartControl.Series("Line").CheckedInLegend = True
                chartControl.Series("Area").CheckedInLegend = False
            End If

            chartControl.EndInit()
            initializationFlag = False
        End Sub

        Private Sub OnLegendItemChecked(ByVal sender As Object, ByVal e As LegendItemCheckedEventArgs)
            If initializationFlag = True Then Return
            initializationFlag = True
            If True Then
                Dim checkedSeries As Series = TryCast(e.CheckedElement, Series)
                If checkedSeries Is Nothing Then Throw New Exception("Expected series only")
                For Each series As Series In chartControl.Series
                    series.CheckedInLegend = False
                Next

                checkedSeries.CheckedInLegend = True
                chartControl.Titles(0).Text = checkedSeries.Name
            End If

            initializationFlag = False
        End Sub

        Private Sub OnCustomDrawSeries(ByVal sender As Object, ByVal e As CustomDrawSeriesEventArgs)
            Dim bitmap As DXBitmap = New DXBitmap(LegendRadioSide, LegendRadioSide)
            Using graphics As DXGraphics = DXGraphics.FromImage(bitmap)
                graphics.SmoothingMode = DXSmoothingMode.HighQuality
                Dim seriesColor As Color = GetSeriesColor(e.Series, chartControl)
                Using radioPen As DXPen = New DXPen(seriesColor, LegendRadioWidth)
                    Dim radioRadius As Integer = LegendRadioSide - 3
                    Dim radioRectangle As Rectangle = New Rectangle(1, 1, radioRadius, radioRadius)
                    graphics.DrawEllipse(radioPen, radioRectangle)
                End Using

                If e.Series.CheckedInLegend Then
                    Using brush As DXBrush = New DXSolidBrush(seriesColor)
                        Dim coord As Integer =(LegendRadioSide - LegendRadioInnerPointBoundsSide) \ 2
                        Dim filledEllipseBounds As Rectangle = New Rectangle(coord, coord, LegendRadioInnerPointBoundsSide, LegendRadioInnerPointBoundsSide)
                        graphics.FillEllipse(brush, filledEllipseBounds)
                    End Using
                End If
            End Using

            e.DisposeLegendMarkerImage = True
            e.DXLegendMarkerImage = bitmap
        End Sub

        Private Function GetSeriesColor(ByVal series As Series, ByVal chartControl As ChartControl) As Color
            Dim seriesIndex As Integer = chartControl.Series.IndexOf(series)
            Dim paletteName As String = chartControl.PaletteName
            Dim currentPalette As Palette = chartControl.PaletteRepository(paletteName)
            Dim paletteEntryAccordingToSeries As PaletteEntry = currentPalette(seriesIndex)
            Dim result As Color = paletteEntryAccordingToSeries.Color
            Return result
        End Function
    End Class
End Namespace

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the CheckedElement 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-chart-create-custom-legend-radio-buttons-to-control-visibility-of-a-chart-series/CS/MainForm.cs#L32

csharp
initializationFlag = true; {
    Series checkedSeries = e.CheckedElement as Series;
    if (checkedSeries == null)

winforms-chart-create-custom-legend-radio-buttons-to-control-visibility-of-a-chart-series/VB/MainForm.vb#L40

vb
If True Then
    Dim checkedSeries As Series = TryCast(e.CheckedElement, Series)
    If checkedSeries Is Nothing Then Throw New Exception("Expected series only")

See Also

LegendItemCheckedEventArgs Class

LegendItemCheckedEventArgs Members

DevExpress.XtraCharts Namespace