Skip to main content
A newer version of this page is available. .

How to: Custom Draw a Crosshair Cursor

  • 6 minutes to read

This example demonstrates one of several possible way of using the ChartControl.CustomDrawCrosshair event. In this sample, the event is used to modify the legend markers of bar series.

To access crosshair element groups, use the CustomDrawCrosshairEventArgs.CrosshairElementGroups property. Elements are separated into several groups when crosshair labels are displayed for each pane. For each group, it is possible to customize the header using the CrosshairElementGroup.HeaderElement property and elements representing series using the CrosshairElementGroup.CrosshairElements property. F‌or each element representing a series, you can customize its label representation using the CrosshairElementBase.LabelElement property and axis label using the CrosshairElementBase.AxisLabelElement property. For example, to modify the legend marker image, use the CrosshairLabelElement.MarkerImage property.

using CustomDrawCrosshairSample.Model;
using DevExpress.XtraCharts;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace CustomDrawCrosshairSample {
    public partial class Form1 : Form {
        Dictionary<string, Image> photoCache = new Dictionary<string, Image>();

        #region #Constants
        const int borderSize = 2;
        const int scaledPhotoWidth = 32;
        const int scaledPhotoHeight = 34;
        // Width and height of scaled photo with border.
        const int totalWidth = 36;
        const int totalHeight = 38;

        // 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.CustomDrawCrosshair += OnCustomDrawCrosshair;
        }

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

        #region #CustomDrawCrosshairHandler
        private void OnCustomDrawCrosshair(object sender, CustomDrawCrosshairEventArgs e) {
            foreach (CrosshairElementGroup group in e.CrosshairElementGroups) {
                if (group.CrosshairElements[0] != null)
                    group.HeaderElement.Text = String.Format("Sales in {0:yyyy}", group.CrosshairElements[0].SeriesPoint.DateTimeArgument);
                foreach (CrosshairElement element in group.CrosshairElements) {
                    Bitmap image = new Bitmap(totalWidth, totalHeight);
                    using (Graphics graphics = Graphics.FromImage(image)) {
                        graphics.FillRectangle(new SolidBrush(element.LabelElement.MarkerColor), totalRect);
                        Image photo;
                        if (photoCache.TryGetValue(element.Series.Name, out photo))
                            graphics.DrawImage(photo, photoRect);
                    }
                    element.LabelElement.MarkerImage = image;
                    element.LabelElement.MarkerSize = new Size(totalWidth, totalHeight);
                }
            }
        }
        #endregion 
    }
}