windowsforms-devexpress-dot-xtracharts-dot-chartcontrol-df54e3cc.md
Occurs before crosshair items are drawn when the chart’s contents are being drawn.
Namespace : DevExpress.XtraCharts
Assembly : DevExpress.XtraCharts.v25.2.UI.dll
NuGet Package : DevExpress.Win.Charts
public event CustomDrawCrosshairEventHandler CustomDrawCrosshair
Public Event CustomDrawCrosshair As CustomDrawCrosshairEventHandler
The CustomDrawCrosshair event's data class is CustomDrawCrosshairEventArgs. The following properties provide information specific to this event:
| Property | Description |
|---|---|
| CrosshairAxisLabelElements | Gets the settings of crosshair axis label elements to customize their appearance. |
| CrosshairElementGroups | Provides access to the settings of crosshair elements and crosshair group header elements to customize their appearance. |
| CrosshairLegendElements | Returns crosshair legend elements to customize their appearance. |
| CrosshairLineElement | Gets the settings of a crosshair line element to customize its appearance (color, line style). |
| IndicatorLegendElements | Returns all indicator elements that the Crosshair Cursor shows in a legend. |
Use the CustomDrawCrosshair event to create a custom appearance for crosshair items.
Review the following help topic for information about scenarios you can implement when you handle CustomDrawCrosshair: Customize the Crosshair Cursor at Runtime.
This example shows how to use the ChartControl.CustomDrawCrosshair event to create a custom appearance for the crosshair cursor. This event is invoked when you select the Custom Draw Crosshair Cursor check box.
If you want to display crosshair axis lines and labels on a chart before drawing the crosshair cursor, set the CrosshairOptions.ShowArgumentLine, CrosshairOptions.ShowArgumentLabels, CrosshairOptions.ShowValueLabels, and CrosshairOptions.ShowValueLine properties to true.
Note that the crosshair cursor customization is available for the CrosshairOptions.SnapMode property when it is set to NearestArgument.
using System;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.Drawing;
using DevExpress.XtraCharts;
namespace CustomDrawCrosshairCursor {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void OnCheckEditCheckedChanged(object sender, EventArgs e) {
if (checkEdit1.Checked)
chartControl1.CustomDrawCrosshair += OnChartControlCustomDrawCrosshair;
else
chartControl1.CustomDrawCrosshair -= OnChartControlCustomDrawCrosshair;
}
private void OnChartControlCustomDrawCrosshair(object sender, CustomDrawCrosshairEventArgs e) {
// Specify the crosshair argument line color, dash style and thickness.
e.CrosshairLineElement.Color = Color.Green;
e.CrosshairLineElement.LineStyle.DashStyle = DashStyle.DashDot;
e.CrosshairLineElement.LineStyle.Thickness = 3;
// Specify the back color for the crosshair argument axis label.
foreach (CrosshairAxisLabelElement axisLabelElement in e.CrosshairAxisLabelElements)
axisLabelElement.BackColor = Color.Blue;
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.TextColor = Color.Green;
groupHeaderElement.DXFont = new DXFont("SegoeUI", 12, DXFontStyle.Bold);
// Obtain the first series.
CrosshairElement element = group.CrosshairElements[0];
// Specify the color, dash style and thickness for the crosshair value lines.
element.LineElement.Color = Color.DarkViolet;
element.LineElement.LineStyle.DashStyle = DashStyle.Dash;
element.LineElement.LineStyle.Thickness = 2;
// Specify the text color and back color for the crosshair value labels.
element.AxisLabelElement.TextColor = Color.Red;
element.AxisLabelElement.BackColor = Color.Yellow;
// Format the text shown for the series in the crosshair cursor label. Specify the text color and marker size.
element.LabelElement.TextColor = Color.Red;
element.LabelElement.MarkerSize = new Size(15, 15);
element.LabelElement.Text = string.Format("{0}: A={1}; V={2}", element.Series.Name, element.SeriesPoint.Argument, element.SeriesPoint.Values[0]);
}
}
}
}
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports DevExpress.Drawing
Imports DevExpress.XtraCharts
Namespace CustomDrawCrosshairCursor
Public Partial Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
Private Sub OnCheckEditCheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
If checkEdit1.Checked Then
AddHandler chartControl1.CustomDrawCrosshair, AddressOf OnChartControlCustomDrawCrosshair
Else
RemoveHandler chartControl1.CustomDrawCrosshair, AddressOf OnChartControlCustomDrawCrosshair
End If
End Sub
Private Sub OnChartControlCustomDrawCrosshair(ByVal sender As Object, ByVal e As CustomDrawCrosshairEventArgs)
' Specify the crosshair argument line color, dash style and thickness.
e.CrosshairLineElement.Color = Color.Green
e.CrosshairLineElement.LineStyle.DashStyle = DashStyle.DashDot
e.CrosshairLineElement.LineStyle.Thickness = 3
' Specify the back color for the crosshair argument axis label.
For Each axisLabelElement As CrosshairAxisLabelElement In e.CrosshairAxisLabelElements
axisLabelElement.BackColor = Color.Blue
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.TextColor = Color.Green
groupHeaderElement.DXFont = New DXFont("SegoeUI", 12, DXFontStyle.Bold)
' Obtain the first series.
Dim element As CrosshairElement = group.CrosshairElements(0)
' Specify the color, dash style and thickness for the crosshair value lines.
element.LineElement.Color = Color.DarkViolet
element.LineElement.LineStyle.DashStyle = DashStyle.Dash
element.LineElement.LineStyle.Thickness = 2
' Specify the text color and back color for the crosshair value labels.
element.AxisLabelElement.TextColor = Color.Red
element.AxisLabelElement.BackColor = Color.Yellow
' Format the text shown for the series in the crosshair cursor label. Specify the text color and marker size.
element.LabelElement.TextColor = Color.Red
element.LabelElement.MarkerSize = New Size(15, 15)
element.LabelElement.Text = String.Format("{0}: A={1}; V={2}", element.Series.Name, element.SeriesPoint.Argument, element.SeriesPoint.Values(0))
Next
End Sub
End Class
End Namespace
The following code snippets (auto-collected from DevExpress Examples) contain references 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.
winforms-chart-draw-a-crosshair-cursor/CS/CustomDrawCrosshairCursor/Form1.cs#L14
if (checkEdit1.Checked)
chartControl1.CustomDrawCrosshair += OnChartControlCustomDrawCrosshair;
else
chart.CustomDrawCrosshair += OnCustomDrawCrosshair;
}
winforms-chart-draw-a-crosshair-cursor/VB/CustomDrawCrosshairCursor/Form1.vb#L18
If checkEdit1.Checked Then
AddHandler chartControl1.CustomDrawCrosshair, AddressOf OnChartControlCustomDrawCrosshair
Else
AddHandler chart.CustomDrawCrosshair, AddressOf OnCustomDrawCrosshair
End Sub
See Also