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.
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.
private void Form_Load(object sender, EventArgs e) {
chartControl.RuntimeHitTesting = true;
//...
}
Important
When Runtime
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:
The properties that indicate whether the test point is a specific chart element. For example, the ChartHitInfo.InSeries property shows whether the test point is in a series.
Member Description Chart Hit Info. In Diagram Gets a value indicating whether the test point is within the diagram. Chart Hit Info. In Non Default Pane Returns a value that indicates whether the test point is in a Chart Hit .Info. Non Default Pane Chart Element Named. Name Gets or sets a chart element‘s name. Chart Hit Info Base. In Axis Returns a value that indicates whether the test point is on an axis. Chart Hit Info Base. Axis Label Item Gets an axis label item under the test point. Chart Hit Info Base. Axis Title Returns an axis title under the test point. Chart Hit Info. In Chart Title Returns a value that indicates whether the test point is in a chart title. Chart Hit Info. Chart Title Gets a chart title under the test point. Chart Hit Info Base. In Legend Returns a value that indicates whether the test point is in a legend. Chart Hit Info. In Series Returns a value that indicates whether the test point is in a series. Chart Hit Info. Series Gets a series under the test point. Chart Hit Info. In Series Label Gets a value that indicates whether the test point is in a series label. Chart Hit Info. In Series Point Returns a value that indicates whether the test point is in a series point. Chart Hit Info. Series Point Returns a series point under the test point. Chart Hit Info. In Annotation Gets a value that indicates whether the test point is in an annotation. Chart Hit Info. Annotation Gets an annotation under the test point. Chart Hit Info. In Constant Line Returns a value that indicates whether the test point is in a constant line. Chart Hit Info. Constant Line Returns a constant line. - The ChartHitInfo.HitTest property returns the type of the chart element at the test point. The ChartHitTest enumeration lists types the HitTest property can return.
- The ChartHitInfoBase.HitObject property specifies the topmost chart element under the test point. The ChartHitInfoBase.HitObjects property allows you to receive all chart elements in the test point.
- The ChartHitInfoBase.HitPoint property returns the test point.
#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 |
---|---|
Chart |
Contains information about a specific point in a chart. |
Chart |
Returns information about the chart elements at the specified x and y coordinates. |