Xamarin.iOS Documentation | ComponentOne
Controls / FlexChart / Features / Hit Test
In This Topic
    Hit Test
    In This Topic

    The HitTest() method is used to determine X and Y coordinates, as well as the index of a point on the FlexChart where the user taps. This method is helpful in scenarios such as displaying tooltips that lie outside the series of the FlexChart.

    The following code example demonstrates how to define the chart_Tapped event. This event invokes the HitTest() method to retrieve the information of the tapped point in the FlexChart region and displays it in the chart footer.

    The image below shows how the FlexChart appears, after defining the HitTest() method.

    To initialize the FlexChart control and use HitTest() method, open the ViewController.cs and replace its content with the code below. This overrides the viewDidLoad method of the view controller.

    CS
    Copy Code
    public partial class ViewController : UIViewController
    {
        public ViewController(IntPtr handle) : base(handle)
        {
    
        }
    
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            // Perform any additional setup after loading the view, typically from a nib.
    
            int len = 20;
            List<CGPoint> listCosTuple = new List<CGPoint>();
            List<CGPoint> listSinTuple = new List<CGPoint>();
    
            for (int i = 0; i < len; i++)
            {
                listCosTuple.Add(new CGPoint(i, Math.Cos(0.12 * i)));
                listSinTuple.Add(new CGPoint(i, Math.Sin(0.12 * i)));
            }
    
            //chart = new FlexChart();
            chart.BindingX = "X";
            chart.ChartType = ChartType.LineSymbols;
            chart.AxisY.Format = "F";
            chart.Header = "Trigonometric Functions";
            chart.Footer = "Cartesian coordinates";
    
            chart.Series.Add(new ChartSeries() { SeriesName = "cos(x)", Binding = "Y,Y", ItemsSource = listCosTuple });
            chart.Series.Add(new ChartSeries() { SeriesName = "sin(x)", Binding = "Y,Y", ItemsSource = listSinTuple });
    
            chart.Tapped += OnChartTapped;
        }
    
        private void OnChartTapped(object sender, C1TappedEventArgs e)
        {
            var pt = e.GetPosition(chart);
            var info = chart.HitTest(pt);
    
            if (info != null)
            {
                pointIndexLabel.Text = string.Format(NSBundle.MainBundle.LocalizedString("Point Index", "Point Index") + " {0}", info.PointIndex);
                xyValuesLabel.Text = string.Format(NSBundle.MainBundle.LocalizedString("X Y Values", "X Y Values") + " X: {0}, Y: {1:F2}", info.X, info.Y);
    
                if (info.Series != null)
                {
                    seriesLabel.Text = string.Format(NSBundle.MainBundle.LocalizedString("SeriesName", "SeriesName") + " {0}", info.Series.Name);
                }
                else
                {
                    seriesLabel.Text = NSBundle.MainBundle.LocalizedString("SeriesName", "SeriesName");
                }
                chartElementLabel.Text = string.Format(NSBundle.MainBundle.LocalizedString("Chart element", " Chart element") + " {0}", info.ChartElement.ToString());
            }
        }
    
        public override void DidReceiveMemoryWarning()
        {
            base.DidReceiveMemoryWarning();
            // Release any cached data, images, etc that aren't in use.
        }
    }