Skip to main content

Hit Information

  • 6 minutes to read

The Chart control can process hit information to identify a chart element at the specified screen coordinates. This allows you to provide custom actions when an user clicks or hovers the mouse pointer over a specific element. You can handle a control’s mouse events to obtain the mouse pointer’s coordinates, for example, ChartControl.MouseMove or ChartControl.Click. To obtain hit information about the test point with these coordinates, use the ChartControl.CalcHitInfo method.

hit-testing-preview

Run Demo: Hit Testing

The document consists of the following sections:

Overview

Enable the ChartControl.RuntimeHitTesting property to allow a chart to collect and process hit information.

Design time

Expand the Chart Control’s Behavior properties’ group in the Properties window. Then, select True in the RuntimeHitTesting property’s drop-down list.

enable-runtime-hit-testing

Runtime

Enable RuntimeHitTesting at runtime on during a chart’s initialization, for example, in the Form’s constructor after the components’ initialization or in the Form’s Load event handler.

private void Form_Load(object sender, EventArgs e) {
    chartControl.RuntimeHitTesting = true;
    //...
}

Important

When RuntimeHitTesting is enabled, the Chart Control uses more memory resources and its performance can decrease. Refer to Performance for more information.

The Chart control collects information about the hit-tested points regardless of whether RuntimeHitTesting is enabled when you handle the ChartControl.ObjectHotTracked or ChartControl.ObjectSelected events, or Selection is enabled (see the ChartControl.SelectionMode property).

To hit-test series points, ensure that a series view’s markers are visible. To make them visible, specify the series view’s MarkerVisibility property to true. For example, set the LineSeriesView.MarkerVisibility property to true to show a line series view‘s markers.

You can receive hit information about a series point even if MarkerVisibility is disabled for the series. To do this, hit-test crosshair markers displayed when you hover the Crosshair Cursor over a series point. (Note that the following image shows the Crosshair Cursor with a custom tooltip while the default label is hidden.)

image

Use the ChartControl.CalcHitInfo method to obtain information about a point in a chart. The method returns a ChartHitInfo object that uses the following properties to store the hit information:

How to: Determine a Chart Element in a Test Point

The following example shows how to obtain information about the chart element under the mouse pointer and use the ToolTipController Component to display this information.

ToolTipController toolTipController = new ToolTipController();
//....
private void chartControl_MouseMove(object sender, MouseEventArgs e) {
    ChartHitInfo hitInfo = chartControl.CalcHitInfo(e.Location);
    StringBuilder builder = new StringBuilder();
    if(hitInfo.InDiagram)
        builder.AppendLine("In diagram");
    if(hitInfo.InNonDefaultPane)
        builder.AppendLine("In non-default pane: " + hitInfo.NonDefaultPane.Name);
    if(hitInfo.InAxis) {
        builder.AppendLine("In axis: " + hitInfo.Axis.Name);
        if(hitInfo.AxisLabelItem != null)
            builder.AppendLine("  Label item: " + hitInfo.AxisLabelItem.Text);
        if(hitInfo.AxisTitle != null)
            builder.AppendLine("  Axis title: " + hitInfo.AxisTitle.Text);
    }
    if(hitInfo.InChartTitle)
        builder.AppendLine("In chart title: " + hitInfo.ChartTitle.Text);
    if(hitInfo.InLegend) {
        builder.AppendLine("In legend");
        if(hitInfo.Series != null && !hitInfo.InSeries)
            builder.AppendLine("  Series: " + ((Series)hitInfo.Series).Name);
    }
    if(hitInfo.InSeries)
        builder.AppendLine("In series: " + ((Series)hitInfo.Series).Name);
    if(hitInfo.InSeriesLabel) {
        builder.AppendLine("In series label");
        builder.AppendLine("  Series: " + ((Series)hitInfo.Series).Name);
    }
    if(hitInfo.InSeriesPoint) {
        builder.AppendLine("  Argument: " + hitInfo.SeriesPoint.Argument);
        if(!hitInfo.SeriesPoint.IsEmpty)
            builder.AppendLine("  Value: " + hitInfo.SeriesPoint.Values[0]);
    }
    if(hitInfo.InAnnotation)
        if(hitInfo.Annotation is TextAnnotation)
            builder.AppendLine("In annotation: " + ((TextAnnotation)hitInfo.Annotation).Name);
        else if(hitInfo.Annotation is ImageAnnotation)
            builder.AppendLine("In annotation: " + ((ImageAnnotation)hitInfo.Annotation).Name);
    if(builder.Length > 0)
        toolTipController.ShowHint("Hit-testing results:\n" + builder.ToString(), chartControl.PointToScreen(e.Location));
    else
        toolTipController.HideHint();
}
void chart_MouseLeave(object sender, EventArgs e) {
    toolTipController.HideHint();
}
Member Description
ChartHitInfo Contains information about a specific point in a chart.
ChartControl.CalcHitInfo Returns information about the chart elements at the specified x and y coordinates.
See Also