Back to Devexpress

CustomDrawSeriesEventArgsBase.DisposeLegendMarkerImage Property

corelibraries-devexpress-dot-xtracharts-dot-customdrawserieseventargsbase-7525ce67.md

latest22.0 KB
Original Source

CustomDrawSeriesEventArgsBase.DisposeLegendMarkerImage Property

Gets or sets the value specifying whether e.DXLegendMarkerImage should be disposed when drawing is finished.

Namespace : DevExpress.XtraCharts

Assembly : DevExpress.XtraCharts.v25.2.dll

NuGet Package : DevExpress.Charts

Declaration

csharp
public bool DisposeLegendMarkerImage { get; set; }
vb
Public Property DisposeLegendMarkerImage As Boolean

Property Value

TypeDescription
Boolean

true , to dispose the legend marker image settings; otherwise, false.

|

Remarks

Use the DisposeLegendMarkerImage property to release all the resources allocated to the object assigned to the e.DXLegendMarkerImage property.

Examples

Create Custom Legend Radio Buttons to Control Visibility of a Chart Series

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

Draw a Custom Legend Marker for a Series

This example demonstrates how to use the ChartControl.CustomDrawSeries event to modify the legend markers of bar series.

View Example

Assign a custom legend marker to the e.LegendMarkerImage property. Set the e.DisposeLegendMarkerImage property to true to avoid memory leaks. To customize options used to draw the series, cast e.SeriesDrawOptions to the DrawOptions class descendant that stores draw options of the required series view type.

cs
using CustomDrawingSample.Model;
using DevExpress.Drawing;
using DevExpress.XtraCharts;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;

namespace CustomDrawingSample {
    public partial class Form1 : DevExpress.XtraEditors.XtraForm {
        string trackedSeriesName;
        Dictionary<string, DXImage> photoCache = new Dictionary<string, DXImage>();

        #region #Constants
        const int borderSize = 5;
        const int scaledPhotoWidth = 48;
        const int scaledPhotoHeight = 51;
        // Width and height of scaled photo with border.
        const int totalWidth = 58;
        const int totalHeight = 61;

        // Rects required to create a custom legend series marker.
        static readonly Rectangle photoRect = new Rectangle(
                borderSize, borderSize,
                scaledPhotoWidth, scaledPhotoHeight);
        static readonly Rectangle totalRect = new Rectangle(
                0, 0,
                totalWidth, totalHeight);
        #endregion

        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            using (var context = new NwindDbContext()) {
                InitPhotoCache(context.Employees);
                chart.DataSource = context.Orders.ToList();
            }

            chart.SeriesDataMember = "Employee.FullName";
            chart.SeriesTemplate.ArgumentDataMember = "OrderDate";
            chart.SeriesTemplate.ValueDataMembers.AddRange("Freight");

            XYDiagram diagram = chart.Diagram as XYDiagram;
            if (diagram != null) {
                diagram.AxisX.DateTimeScaleOptions.AggregateFunction = AggregateFunction.Sum;
                diagram.AxisX.DateTimeScaleOptions.MeasureUnit = DateTimeMeasureUnit.Year;
            }

            chart.CustomDrawSeries += OnCustomDrawSeries;
            chart.ObjectHotTracked += OnObjectHotTracked;
        }

        private void OnObjectHotTracked(object sender, HotTrackEventArgs e) {
            trackedSeriesName = e.HitInfo.InSeries ? ((Series)e.HitInfo.Series).Name : null;
        }

        void InitPhotoCache(IEnumerable<Employee> employees) {
            photoCache.Clear();
            foreach (var employee in employees) {
                using (MemoryStream stream = new MemoryStream(employee.Photo)) {
                    if (!photoCache.ContainsKey(employee.FullName))
                        photoCache.Add(employee.FullName, DXImage.FromStream(stream));
                }
            }
        }

        #region #CustomDrawSeriesImplementation
        private void OnCustomDrawSeries(object sender, CustomDrawSeriesEventArgs e) {
            bool isSelected = e.Series.Name.Equals(trackedSeriesName);
            // Design a series marker image.
            DXBitmap image = new DXBitmap(totalWidth, totalHeight);
            using (DXGraphics graphics = DXGraphics.FromImage(image)) {
                using (var fillBrush = isSelected ? (DXBrush)new DXHatchBrush(DXHatchStyle.DarkDownwardDiagonal,
                                                                          e.LegendDrawOptions.Color,
                                                                          e.LegendDrawOptions.ActualColor2)
                                                  : (DXBrush)new DXSolidBrush(e.LegendDrawOptions.Color)) {
                    graphics.FillRectangle(fillBrush, totalRect);
                }
                DXImage photo;
                if (photoCache.TryGetValue(e.Series.Name, out photo))
                    graphics.DrawImage(photo, photoRect);
            }
            e.DXLegendMarkerImage = image;
            e.DisposeLegendMarkerImage = true;

            BarDrawOptions options = e.SeriesDrawOptions as BarDrawOptions;
            if (options != null && isSelected) {
                options.FillStyle.FillMode = DevExpress.XtraCharts.FillMode.Hatch;
                ((HatchFillOptions)options.FillStyle.Options).HatchStyle = HatchStyle.DarkDownwardDiagonal;
            }
        }
        #endregion
    }
}
vb
Imports CustomDrawingSample.Model
Imports DevExpress.Drawing
Imports DevExpress.XtraCharts
Imports System
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.IO
Imports System.Linq

Namespace CustomDrawingSample

    Public Partial Class Form1
        Inherits DevExpress.XtraEditors.XtraForm

        Private trackedSeriesName As String

        Private photoCache As Dictionary(Of String, DXImage) = New Dictionary(Of String, DXImage)()

#Region "#Constants"
        Const borderSize As Integer = 5

        Const scaledPhotoWidth As Integer = 48

        Const scaledPhotoHeight As Integer = 51

        ' Width and height of scaled photo with border.
        Const totalWidth As Integer = 58

        Const totalHeight As Integer = 61

        ' Rects required to create a custom legend series marker.
        Private Shared ReadOnly photoRect As Rectangle = New Rectangle(borderSize, borderSize, scaledPhotoWidth, scaledPhotoHeight)

        Private Shared ReadOnly totalRect As Rectangle = New Rectangle(0, 0, totalWidth, totalHeight)

#End Region
        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
            Using context = New NwindDbContext()
                InitPhotoCache(context.Employees)
                chart.DataSource = context.Orders.ToList()
            End Using

            chart.SeriesDataMember = "Employee.FullName"
            chart.SeriesTemplate.ArgumentDataMember = "OrderDate"
            chart.SeriesTemplate.ValueDataMembers.AddRange("Freight")
            Dim diagram As XYDiagram = TryCast(chart.Diagram, XYDiagram)
            If diagram IsNot Nothing Then
                diagram.AxisX.DateTimeScaleOptions.AggregateFunction = AggregateFunction.Sum
                diagram.AxisX.DateTimeScaleOptions.MeasureUnit = DateTimeMeasureUnit.Year
            End If

            AddHandler chart.CustomDrawSeries, AddressOf OnCustomDrawSeries
            AddHandler chart.ObjectHotTracked, AddressOf OnObjectHotTracked
        End Sub

        Private Sub OnObjectHotTracked(ByVal sender As Object, ByVal e As HotTrackEventArgs)
            trackedSeriesName = If(e.HitInfo.InSeries, CType(e.HitInfo.Series, Series).Name, Nothing)
        End Sub

        Private Sub InitPhotoCache(ByVal employees As IEnumerable(Of Employee))
            photoCache.Clear()
            For Each employee In employees
                Using stream As MemoryStream = New MemoryStream(employee.Photo)
                    If Not photoCache.ContainsKey(employee.FullName) Then photoCache.Add(employee.FullName, DXImage.FromStream(stream))
                End Using
            Next
        End Sub

#Region "#CustomDrawSeriesImplementation"
        Private Sub OnCustomDrawSeries(ByVal sender As Object, ByVal e As CustomDrawSeriesEventArgs)
            Dim isSelected As Boolean = e.Series.Name.Equals(trackedSeriesName)
            ' Design a series marker image.
            Dim image As DXBitmap = New DXBitmap(totalWidth, totalHeight)
            Using graphics As DXGraphics = DXGraphics.FromImage(image)
                Using fillBrush = If(isSelected, CType(New DXHatchBrush(DXHatchStyle.DarkDownwardDiagonal, e.LegendDrawOptions.Color, e.LegendDrawOptions.ActualColor2), DXBrush), CType(New DXSolidBrush(e.LegendDrawOptions.Color), DXBrush))
                    graphics.FillRectangle(fillBrush, totalRect)
                End Using

                Dim photo As DXImage
                If photoCache.TryGetValue(e.Series.Name, photo) Then graphics.DrawImage(photo, photoRect)
            End Using

            e.DXLegendMarkerImage = image
            e.DisposeLegendMarkerImage = True
            Dim options As BarDrawOptions = TryCast(e.SeriesDrawOptions, BarDrawOptions)
            If options IsNot Nothing AndAlso isSelected Then
                options.FillStyle.FillMode = DevExpress.XtraCharts.FillMode.Hatch
                CType(options.FillStyle.Options, HatchFillOptions).HatchStyle = HatchStyle.DarkDownwardDiagonal
            End If
        End Sub
#End Region
    End Class
End Namespace

The following code snippets (auto-collected from DevExpress Examples) contain references to the DisposeLegendMarkerImage 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#L62

csharp
}
e.DisposeLegendMarkerImage = true;
e.DXLegendMarkerImage = bitmap;

winforms-chart-draw-a-custom-legend-marker-for-a-series-point/CS/CustomSeriesPointDrawingSample/Form1.cs#L85

csharp
e.DXLegendMarkerImage = image;
e.DisposeLegendMarkerImage = true;

winforms-chart-draw-a-custom-legend-marker-for-a-series/CS/CustomDrawingSample/Form1.cs#L88

csharp
e.DXLegendMarkerImage = image;
e.DisposeLegendMarkerImage = true;

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

vb
e.DisposeLegendMarkerImage = True
e.DXLegendMarkerImage = bitmap

winforms-chart-draw-a-custom-legend-marker-for-a-series-point/VB/CustomSeriesPointDrawingSample/Form1.vb#L88

vb
e.DXLegendMarkerImage = image
e.DisposeLegendMarkerImage = True
Dim options As DevExpress.XtraCharts.PieDrawOptions = TryCast(e.SeriesDrawOptions, DevExpress.XtraCharts.PieDrawOptions)

winforms-chart-draw-a-custom-legend-marker-for-a-series/VB/CustomDrawingSample/Form1.vb#L89

vb
e.DXLegendMarkerImage = image
e.DisposeLegendMarkerImage = True
Dim options As BarDrawOptions = TryCast(e.SeriesDrawOptions, BarDrawOptions)

See Also

DisposeLegendFont

CustomDrawSeriesEventArgsBase Class

CustomDrawSeriesEventArgsBase Members

DevExpress.XtraCharts Namespace