The ability to determine chart objects by given window coordinates is an essential functionality built into Xceed Chart for WinForms. This feature is called "hit testing" and allows the developer to build advanced interactive applications.
You perform hit testing by calling the HitTest method of the ChartControl class. It accepts two parameters specifying a point in window coordinates. The function returns an object of type HitTestResult. The following code for example intercepts the mouse click event and obtains a HitTestResult object for the chart object under the mouse:
VB.NET | |
---|---|
|
C# | |
---|---|
|
The most important property of the HitTestResult object is the ChartElement property. It describes the type of the chart element at the given window coordinates. Depending on the chart element type, some of the properties of the HitTestResult referencing objects may be null (Nothing in VB .NET). Similarly, properties specifying indexes in some of the chart collections may be -1. The following table shows how the properties depend on the ChartElement property:
Property | Description | Valid for chart elements |
---|---|---|
Object | A refence to a chart element. | Valid for all chart elements except ChartElement.Nothing. |
Background | The chart background | The Object property contains a reference to the Background object of the control. |
Chart | Returns a reference to the Chart object containing the located object. |
Valid when the ChartElement is DataPoint, SurfaceDataPoint, Axis,AxisStripe and ChartWall. |
ChartIndex | Returns the index of the Chart object containing the located object in the Charts collection of the ChartControl. | Valid when the ChartElement is DataPoint, SurfaceDataPoint, Axis,AxisStripe and ChartWall. |
ChartWall | Returns a reference to a ChartWall object. | Valid only when ChartElement is set to ChartWall. |
ChartWallIndex | Returns the index of the ChartWall object in the Chart Walls collection. | Valid only when ChartElement is set to ChartWall. |
Series | Returns a reference to the SeriesBase object containing the located data item. | Valid when ChartElement is ChartElement.SurfaceDataPoint or ChartElement.DataPoint. |
SeriesIndex | Returns the index of the SeriesBase object in the Chart Series collection. | Valid when ChartElement is SurfaceDataPoint or DataPoint. |
DataPointIndex | Returns the index of the data point in the Series Values collection. | Valid only when ChartElement is set to DataPoint. |
SurfaceDataPointX | Returns the index of the data point in the SurfaceSeriesBase Values collection. | Valid only when ChartElement is set to SurfaceDataPoint. |
SurfaceDataPointY | Returns the index of the data point in the SurfaceSeriesBase Values collection. | Valid only when ChartElement is set to SurfaceDataPoint. |
Axis | Returns a reference to a Axis object. | Valid when ChartElement is Axis or AxisStripe. |
AxisIndex | Returns the index of the Axis object in the Chart Axes collection. | Valid when ChartElement is Axis or AxisStripe. |
AxisStripe | Returns a reference to a AxisStripe object. | Valid only when ChartElement is set to AxisStripe. |
AxisStripeIndex | Returns the index of the AxisStripe object in the Axis Stripes collection. | Valid only when ChartElement is set to AxisStripe. |
Label | Returns a reference to a Label object. | Valid only when ChartElement is set to Label. |
LabelIndex | Returns the index of the Label object in the control Labels collection. | Valid only when ChartElement is set to Label. |
Watermark | Returns a reference to a Watermark object. | Valid only when ChartElement is set to Watermark. |
WatermarkIndex | Returns the index of the Watermark object in the control Watermarks collection. | Valid only when ChartElement is set to Watermark. |
Now let's see how this works in practice. The following example creates an interactive pie chart that allows the user to click on the pie slices. When a pie is clicked, it will be detached from the pie center:
VB.NET | |
---|---|
|
C# | |
---|---|
|
Now let's take a closer look at the chartControl1_MouseDown function. The code first checks whether the user clicked on a data point object. If not, it does nothing. If yes, it obtains a reference to the PieSeries collection by using the properties in the HitTestResult object:
VB.NET | |
---|---|
|
C# | |
---|---|
|
This ensures that we'll access the proper chart in case the control displays multiple charts. Finally, it uses the DataPointIndex property to modify the detachment for the clicked pie.