FlexChart supports hit testing, which enables you to fetch information about a specific point in the control at run-time. The information obtained about the pointed coordinate can then be reused to drill down the chart data, to set alerts, or to enable other user interaction functionalities.
FlexChart supports hit testing by utilizing HitTest() method. This method takes two overloads, one accepts pointer location and the other accepts pointer location, MeasureOption, and seriesIndex value in the control as parameters; and returns an object of HitTestInfo class, which provides the following information about the pointer location:
- Chart element comprising the pointer
- Distance of the pointer location from the closest data point in chart, if the pointer is within plot area. Distance is returned as Double.Nan, if the pointer is outside the plot area.
This distance value varies depending upon the MeasureOption being used. For example, with MeasureOption.X, the Distance is calculated along the X-axis only.
- Data object corresponding to the closest data point
- Index of the nearest data point
- Series name that the nearest data point belongs to
- X value of the nearest data point
- Y value of the nearest data point
Note that, the mouse coordinates that are passed to HitTest() method are in pixels and are relative to the upper left corner of the form.
In this example, HitTest() method is called on MouseMove event of the FlexChart control. Here, the point coordinates of pointer location are passed as parameter to HitTest() method.
To enable hit testing in FlexChart, follow these steps:
- Add a data bound FlexChart control
- Subscribe to a Mouse event
- Invoke chart’s HitTest method in mouse event handler
- Use the information returned by HitTestInfo object
Back to Top
-
Add a data bound FlexChart control
Add an instance of FlexChart control to your application, and bind it to an appropriate data source, as shown in the below code snippet.
const int SERIES_NUMBER = 2;
void SetupChart()
{
flexChart2.BeginUpdate();
flexChart2.Series.Clear();
// Create sample data and add it to the chart
var offset = 0;
for (int i = 0; i < SERIES_NUMBER; i++)
{
Series tbs = new Series()
{
Name = "Series " + i.ToString(),
DataSource = DataCreator.Create(x => Math.Sin(x + offset), 0, 60, 7),
Binding = "YVals",
BindingX = "XVals"
};
flexChart2.ChartType = C1.Chart.ChartType.SplineSymbols;
flexChart2.Series.Add(tbs);
offset += 5;
}
flexChart2.EndUpdate();
}
Back to Top
-
Subscribe to a Mouse event
Subscribe to a mouse event to capture the pointer coordinates, as shown in the below code snippet.
flexChart2.MouseDoubleClick += flexChart2_MouseDoubleClick;
Back to Top
-
Invoke chart’s HitTest method in mouse event handler
In the respective event handler, invoke the HitTest() method and pass the captured mouse pointer coordinates, as shown in the below code snippet.
private void flexChart2_MouseDoubleClick(object sender, MouseEventArgs e)
{
// Show information about chart element under mouse cursor
var ht = flexChart2.HitTest(e.Location);
Back to Top
-
Use the information returned by HitTestInfo object
The information regarding mouse pointer location, as returned by the HitTestInfo object, can then be reused. For example in the below code snippet, the values returned by HitTestInfo object are converted to string and displayed in a TextBlock.
...
var result = new StringBuilder();
result.AppendLine(string.Format("Chart element:{0}", ht.ChartElement));
if (ht.Series != null)
result.AppendLine(string.Format("Serie name:{0}", ht.Series.Name));
//result.AppendLine(String.Format("Item", ht.Item));
if (ht.PointIndex > 0)
result.AppendLine(string.Format("Point index={0:0}", ht.PointIndex));
if (ht.Distance > 0)
result.AppendLine(string.Format("Distance={0:0}", ht.Distance));
if (ht.X != null)
result.AppendLine(string.Format("X={0:0.00}", ht.X));
if (ht.Y != null)
result.AppendLine(string.Format("Y={0:0.00}", ht.Y));
lblPosition.Text = result.ToString();
}
Back to Top