Back to Devexpress

Hit Information

windowsforms-120444-controls-and-libraries-chart-control-end-user-features-hit-information.md

latest11.6 KB
Original Source

Hit Information

  • Jan 24, 2025
  • 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.

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.

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.

csharp
private void Form_Load(object sender, EventArgs e) {
    chartControl.RuntimeHitTesting = true;
    //...
}
vb
Private Sub Form_Load(ByVal sender As Object, ByVal e As EventArgs)
    chartControl.RuntimeHitTesting = true
    '...
End Sub

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.)

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.

csharp
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();
}
vb
Dim toolTipController As ToolTipController = New ToolTipController()
'...
Private Sub chartControl_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
    Dim hitInfo As ChartHitInfo = chartControl.CalcHitInfo(e.Location)
    Dim builder As StringBuilder = New StringBuilder
    If hitInfo.InDiagram Then
        builder.AppendLine("In diagram")
    End If
    If hitInfo.InNonDefaultPane Then
        builder.AppendLine(("In non-default pane: " + hitInfo.NonDefaultPane.Name))
    End If
    If hitInfo.InAxis Then
        builder.AppendLine(("In axis: " + hitInfo.Axis.Name))
        If (Not (hitInfo.AxisLabelItem) Is Nothing) Then
            builder.AppendLine((" Label item: " + hitInfo.AxisLabelItem.Text))
        End If
        If (Not (hitInfo.AxisTitle) Is Nothing) Then
            builder.AppendLine((" Axis title: " + hitInfo.AxisTitle.Text))
        End If 
    End If   
    If hitInfo.InChartTitle Then
        builder.AppendLine(("In chart title: " + hitInfo.ChartTitle.Text))
    End If
    If hitInfo.InLegend Then
        builder.AppendLine("In legend")
        If ((Not (hitInfo.Series) Is Nothing) _
                    AndAlso Not hitInfo.InSeries) Then
            builder.AppendLine((" Series: " + CType(hitInfo.Series,Series).Name))
        End If
    End If
    If hitInfo.InSeries Then
        builder.AppendLine(("In series: " + CType(hitInfo.Series,Series).Name))
    End If

    If hitInfo.InSeriesLabel Then
        builder.AppendLine("In series label")
        builder.AppendLine((" Series: " + CType(hitInfo.Series,Series).Name))
    End If
    If hitInfo.InSeriesPoint Then
        builder.AppendLine((" Argument: " + hitInfo.SeriesPoint.Argument))
        If Not hitInfo.SeriesPoint.IsEmpty Then
            builder.AppendLine((" Value: " + hitInfo.SeriesPoint.Values(0)))
        End If
    End If
    If hitInfo.InAnnotation Then
        If (TypeOf hitInfo.Annotation Is TextAnnotation) Then
            builder.AppendLine(("In annotation: " + CType(hitInfo.Annotation,TextAnnotation).Name))
        ElseIf (TypeOf hitInfo.Annotation Is ImageAnnotation) Then
            builder.AppendLine(("In annotation: " + CType(hitInfo.Annotation,ImageAnnotation).Name))
        End If
    End If
    If (builder.Length > 0) Then
        toolTipController.ShowHint(("Hit-testing results:"& vbLf + builder.ToString), chartControl.PointToScreen(e.Location))
    Else
        toolTipController.HideHint
    End If       
End Sub
Private Sub chart_MouseLeave(ByVal sender As Object, ByVal e As EventArgs)
    toolTipController.HideHint
End Sub
MemberDescription
ChartHitInfoContains information about a specific point in a chart.
ChartControl.CalcHitInfoReturns information about the chart elements at the specified x and y coordinates.

See Also

How to: Determine which Series Point Is Under the Test Point

How to: Determine a Chart Element the Mouse Pointer Hovers Over

Drill Down