This example shows how to provide a custom appearance for the crosshair cursor using the DevExpress.XtraCharts.ChartControl.CustomDrawCrosshair event. This event is invoked when you click the Custom Draw Crosshair Cursor button on the form.
If you wish to display crosshair axis lines and labels on a chart before custom drawing the crosshair cursor, set the CrosshairOptions.ShowArgumentLine, CrosshairOptions.ShowArgumentLabels, CrosshairOptions.ShowValueLabels and CrosshairOptions.ShowValueLine properties to true.
Note that the customization of the crosshair cursor is provided for the CrosshairOptions.SnapMode property set to NearestArgument.
Form1.cs |
using System;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraCharts;
namespace CustomDrawCrosshairCursor {
public partial class Form1 : Form {
bool handleCustomDraw;
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) {
handleCustomDraw = true;
chartControl1.CustomDrawCrosshair += chartControl1_CustomDrawCrosshair;
}
private void chartControl1_CustomDrawCrosshair(object sender, CustomDrawCrosshairEventArgs e) {
if (!handleCustomDraw) return;
e.CrosshairLineElement.Color = Color.Green;
e.CrosshairLineElement.LineStyle.DashStyle = DashStyle.DashDot;
e.CrosshairLineElement.LineStyle.Thickness = 3;
foreach (CrosshairAxisLabelElement axisLabelElement in e.CrosshairAxisLabelElements)
axisLabelElement.BackColor = Color.Blue;
foreach (CrosshairElementGroup group in e.CrosshairElementGroups) {
CrosshairElement element = group.CrosshairElements[0];
element.LineElement.Color = Color.DarkViolet;
element.LineElement.LineStyle.DashStyle = DashStyle.Dash;
element.LineElement.LineStyle.Thickness = 2;
element.AxisLabelElement.TextColor = Color.Red;
element.AxisLabelElement.BackColor = Color.Yellow;
element.LabelElement.TextColor = Color.Red;
element.LabelElement.MarkerSize = new Size(15, 15);
}
foreach (CrosshairElementGroup group in e.CrosshairElementGroups) {
CrosshairGroupHeaderElement groupHeaderElement = group.HeaderElement;
groupHeaderElement.Text = "Custom draw";
groupHeaderElement.TextColor = Color.Green;
groupHeaderElement.Font = new Font(this.Font, FontStyle.Bold);
}
}
}
}
|
Form1.vb |
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports DevExpress.XtraCharts
Namespace CustomDrawCrosshairCursor
Partial Public Class Form1
Inherits Form
Private handleCustomDraw As Boolean
Public Sub New()
InitializeComponent()
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
handleCustomDraw = True
AddHandler chartControl1.CustomDrawCrosshair, AddressOf chartControl1_CustomDrawCrosshair
End Sub
Private Sub chartControl1_CustomDrawCrosshair(ByVal sender As Object, ByVal e As CustomDrawCrosshairEventArgs) Handles chartControl1.CustomDrawCrosshair
If Not handleCustomDraw Then
Return
End If
' 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 crosshair argument label.
For Each axisLabelElement As CrosshairAxisLabelElement In e.CrosshairAxisLabelElements
axisLabelElement.BackColor = Color.Blue
Next axisLabelElement
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.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
' Specify the text color and marker size for the crosshair cursor label that shows series.
element.LabelElement.TextColor = Color.Red
element.LabelElement.MarkerSize = New Size(15, 15)
Next group
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.Font = New Font(Me.Font, FontStyle.Bold)
Next group
End Sub
End Class
End Namespace
|