Back to Devexpress

ChartControl.CustomDrawCrosshair Event

wpf-devexpress-dot-xpf-dot-charts-dot-chartcontrol-7c651d33.md

latest16.0 KB
Original Source

ChartControl.CustomDrawCrosshair Event

Occurs before crosshair items are drawn when the chart’s contents are being drawn.

Namespace : DevExpress.Xpf.Charts

Assembly : DevExpress.Xpf.Charts.v25.2.dll

NuGet Package : DevExpress.Wpf.Charts

Declaration

csharp
public event CustomDrawCrosshairEventHandler CustomDrawCrosshair
vb
Public Event CustomDrawCrosshair As CustomDrawCrosshairEventHandler

Event Data

The CustomDrawCrosshair event's data class is CustomDrawCrosshairEventArgs. The following properties provide information specific to this event:

PropertyDescription
CrosshairAxisLabelElementsGets the settings of crosshair axis label elements to customize their appearance.
CrosshairElementGroupsProvides access to the settings of crosshair elements and crosshair group header elements to customize their appearance.
CrosshairElementsObsolete. Gets crosshair elements (crosshair value lines, crosshair value labels, crosshair labels) to customize their appearance.
CrosshairGroupHeaderElementsObsolete. Gets the settings of crosshair group header elements to customize their appearance.
CrosshairLegendElementsReturns crosshair legend elements to customize their appearance.
CrosshairLineElementGets the settings of a crosshair line element to customize its appearance (color, line style).
HandledGets or sets a value that indicates the present state of the event handling for a routed event as it travels the route. Inherited from RoutedEventArgs.
IndicatorLegendElementsReturns all indicator elements that the Crosshair Cursor shows in a legend.
OriginalSourceGets the original reporting source as determined by pure hit testing, before any possible Source adjustment by a parent class. Inherited from RoutedEventArgs.
RoutedEventGets or sets the RoutedEvent associated with this RoutedEventArgs instance. Inherited from RoutedEventArgs.
SourceGets or sets a reference to the object that raised the event. Inherited from RoutedEventArgs.

The event data class exposes the following methods:

MethodDescription
InvokeEventHandler(Delegate, Object)When overridden in a derived class, provides a way to invoke event handlers in a type-specific way, which can increase efficiency over the base implementation. Inherited from RoutedEventArgs.
OnSetSource(Object)When overridden in a derived class, provides a notification callback entry point whenever the value of the Source property of an instance changes. Inherited from RoutedEventArgs.

Remarks

Use the CustomDrawCrosshair event to create a custom appearance for crosshair items.

Example

This example shows how to use the ChartControl.CustomDrawCrosshair event to customize the crosshair cursor. For example, you can use this event to paint crosshair label items depending on the highlighted point value. In this example, the ChartControl.CustomDrawCrosshair event is invoked when you click the Custom Draw Crosshair Cursor button on the form.

Enable the following properties before the crosshair is customized to display crosshair axis lines and labels:

View Example

xaml
<Window x:Class="CrosshairCustomDraw.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/charts"
        Title="MainWindow" Height="414" Width="525">
    <Grid>
        <StackPanel>
            <dxc:ChartControl Name="chartControl1" Height="340" 
                              CustomDrawCrosshair="chartControl1_CustomDrawCrosshair">
                <dxc:ChartControl.CrosshairOptions>
                    <dxc:CrosshairOptions ShowArgumentLabels="True" 
                                          ShowArgumentLine="True" 
                                          ShowValueLabels="True" 
                                          ShowValueLine="True"/>
                </dxc:ChartControl.CrosshairOptions>
                <dxc:XYDiagram2D>
                    <dxc:XYDiagram2D.AxisX>
                        <dxc:AxisX2D>
                            <dxc:AxisX2D.CrosshairAxisLabelOptions>
                                <dxc:CrosshairAxisLabelOptions Pattern="{}{V:f1}"/>
                            </dxc:AxisX2D.CrosshairAxisLabelOptions>
                        </dxc:AxisX2D>
                    </dxc:XYDiagram2D.AxisX>
                    <dxc:LineSeries2D DisplayName="Series1">
                        <dxc:SeriesPoint Argument="1" Value="50"/>
                        <dxc:SeriesPoint Argument="2" Value="44"/>
                        <dxc:SeriesPoint Argument="3" Value="55"/>
                        <dxc:SeriesPoint Argument="4" Value="77"/>
                    </dxc:LineSeries2D>
                    <dxc:LineSeries2D DisplayName="Series2">
                        <dxc:SeriesPoint Argument="1" Value="22"/>
                        <dxc:SeriesPoint Argument="2" Value="11"/>
                        <dxc:SeriesPoint Argument="3" Value="9"/>
                        <dxc:SeriesPoint Argument="4" Value="8"/>
                    </dxc:LineSeries2D>
                </dxc:XYDiagram2D>
            </dxc:ChartControl>
            <Button Height="35" Content="Custom Draw Crosshair Cursor" Click="Button_Click"></Button>
        </StackPanel>
    </Grid>
</Window>
csharp
using DevExpress.Xpf.Charts;
using System.Windows;
using System.Windows.Media;

namespace CrosshairCustomDraw {
    public partial class MainWindow : Window {
        bool handleCustomDraw;
        public MainWindow() {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e) {
            handleCustomDraw = true;
            chartControl1.CustomDrawCrosshair += chartControl1_CustomDrawCrosshair;
        }
        private void chartControl1_CustomDrawCrosshair(object sender, CustomDrawCrosshairEventArgs e) {

            if (!handleCustomDraw) return;

            // Specify the crosshair argument line color, dash style and thickness.
            e.CrosshairLineElement.Brush = Brushes.Aqua;
            e.CrosshairLineElement.LineStyle.DashStyle = DashStyles.DashDot;
            e.CrosshairLineElement.LineStyle.Thickness = 3;

            // Specify the back color for crosshair argument label. 
            foreach (CrosshairAxisLabelElement axisLabelElement in e.CrosshairAxisLabelElements)
                axisLabelElement.Background = Brushes.Blue;

            foreach (CrosshairElementGroup group in e.CrosshairElementGroups) {
                CrosshairElement element = group.CrosshairElements[0];

                // Specify the color, dash style and thickness for the crosshair value lines. 
                element.LineElement.Brush = Brushes.DarkViolet;
                element.LineElement.LineStyle.DashStyle = DashStyles.Dash;
                element.LineElement.LineStyle.Thickness = 2;

                // Specify the font size and background for the crosshair value labels.
                element.AxisLabelElement.FontSize = 14;
                element.AxisLabelElement.Background = Brushes.Red;

                // Specify the foreground and font size for the crosshair cursor label that shows series. 
                if (element.SeriesPoint.Value > 50) {
                    element.LabelElement.Foreground = Brushes.Green;
                    element.LabelElement.FontSize = 14;
                }
            }

            foreach (CrosshairElementGroup group in e.CrosshairElementGroups) {
                CrosshairGroupHeaderElement groupHeaderElement = group.HeaderElement;

                // Specify the text, text color and font for the crosshair group header element. 
                groupHeaderElement.Text = "Custom draw";
                groupHeaderElement.Foreground = Brushes.Red;
                groupHeaderElement.FontSize = 15;
            }
        }
    }
}
vb
Imports DevExpress.Xpf.Charts
Imports System.Windows
Imports System.Windows.Media

Namespace CrosshairCustomDraw

    Public Partial Class MainWindow
        Inherits Window

        Private handleCustomDraw As Boolean

        Public Sub New()
            Me.InitializeComponent()
        End Sub

        Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            handleCustomDraw = True
            AddHandler Me.chartControl1.CustomDrawCrosshair, AddressOf Me.chartControl1_CustomDrawCrosshair
        End Sub

        Private Sub chartControl1_CustomDrawCrosshair(ByVal sender As Object, ByVal e As CustomDrawCrosshairEventArgs)
            If Not handleCustomDraw Then Return
            ' Specify the crosshair argument line color, dash style and thickness.
            e.CrosshairLineElement.Brush = Brushes.Aqua
            e.CrosshairLineElement.LineStyle.DashStyle = DashStyles.DashDot
            e.CrosshairLineElement.LineStyle.Thickness = 3
            ' Specify the back color for crosshair argument label. 
            For Each axisLabelElement As CrosshairAxisLabelElement In e.CrosshairAxisLabelElements
                axisLabelElement.Background = Brushes.Blue
            Next

            For Each group As CrosshairElementGroup In e.CrosshairElementGroups
                Dim element As CrosshairElement = group.CrosshairElements(0)
                ' Specify the color, dash style and thickness for the crosshair value lines. 
                element.LineElement.Brush = Brushes.DarkViolet
                element.LineElement.LineStyle.DashStyle = DashStyles.Dash
                element.LineElement.LineStyle.Thickness = 2
                ' Specify the font size and background for the crosshair value labels.
                element.AxisLabelElement.FontSize = 14
                element.AxisLabelElement.Background = Brushes.Red
                ' Specify the foreground and font size for the crosshair cursor label that shows series. 
                If element.SeriesPoint.Value > 50 Then
                    element.LabelElement.Foreground = Brushes.Green
                    element.LabelElement.FontSize = 14
                End If
            Next

            For Each group As CrosshairElementGroup In e.CrosshairElementGroups
                Dim groupHeaderElement As CrosshairGroupHeaderElement = group.HeaderElement
                ' Specify the text, text color and font for the crosshair group header element. 
                groupHeaderElement.Text = "Custom draw"
                groupHeaderElement.Foreground = Brushes.Red
                groupHeaderElement.FontSize = 15
            Next
        End Sub
    End Class
End Namespace

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the CustomDrawCrosshair event.

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.

wpf-charts-custom-draw-crosshair-cursor/CS/CrosshairCustomDraw/MainWindow.xaml#L9

xml
<dxc:ChartControl Name="chartControl1" Height="340"
                  CustomDrawCrosshair="chartControl1_CustomDrawCrosshair">
    <dxc:ChartControl.CrosshairOptions>

wpf-charts-custom-draw-crosshair-cursor/CS/CrosshairCustomDraw/MainWindow.xaml.cs#L13

csharp
handleCustomDraw = true;
    chartControl1.CustomDrawCrosshair += chartControl1_CustomDrawCrosshair;
}

wpf-charts-custom-draw-crosshair-cursor/VB/CrosshairCustomDraw/MainWindow.xaml.vb#L18

vb
handleCustomDraw = True
    AddHandler Me.chartControl1.CustomDrawCrosshair, AddressOf Me.chartControl1_CustomDrawCrosshair
End Sub

See Also

Crosshair Cursor

ChartControl Class

ChartControl Members

DevExpress.Xpf.Charts Namespace