Hit Test feature enables users to determine the X and Y coordinates, as well as the index of any point on the FlexChart on tapping. This method is helpful in scenarios such as displaying tooltips that lie outside the series of the FlexChart.
The following code examples demonstrate how to define the MChart_Tapped event. This event invokes the HiTest method to retrieve the information of the tapped point in the FlexChart region.
The image below shows how the FlexChart appears, after defining the HitTest feature.
To initialize the FlexChart control and use HitTest() method, open the MainActivity.cs and replace its content with the code below. This overrides the OnCreate method of the activity.
CS |
Copy Code |
---|---|
public class MainActivity : Activity { int count = 1; private FlexChart mChart; private TextView mHitTestInfo; private string chartElement; private string chartElementNone; private string pointIndex; private string seriesName; protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); // initializing widgets mChart = this.FindViewById<FlexChart>(Resource.Id.flexchart); mHitTestInfo = (TextView)FindViewById(Resource.Id.hitTestInfo); // set the binding for X-axis of FlexChart mChart.BindingX = "Count"; mChart.AxisY.Format = "#.##"; chartElement = this.Resources.GetString(Resource.String.hitTestChartElement); chartElementNone = this.Resources.GetString(Resource.String.hitTestChartElementNone); pointIndex = this.Resources.GetString(Resource.String.hitTestPointIndex); seriesName = this.Resources.GetString(Resource.String.hitTestSeriesName); // initialize series elements and set the binding to variables of // ChartPoint ChartSeries seriesSine = new ChartSeries(); seriesSine.Chart = mChart; seriesSine.SeriesName = "sin(x)"; seriesSine.Binding = "Sine"; ChartSeries seriesCosine = new ChartSeries(); seriesCosine.Chart = mChart; seriesCosine.SeriesName = "cos(x)"; seriesCosine.Binding = "Cosine"; // setup individual series item source int len = 40; List<object> list = new List<object>(); for (int i = 0; i < len; i++) { list.Add(new ChartPoint(i, (float)Math.Sin(0.12 * i), (float)Math.Cos(0.12 * i))); } mChart.ItemsSource = list; // add series to list mChart.Series.Add(seriesSine); mChart.Series.Add(seriesCosine); mChart.Tapped += MChart_ChartTapped; } private void MChart_ChartTapped(object sender, C1TappedEventArgs e) { C1Point point = e.GetPosition(mChart); ChartHitTestInfo info = mChart.HitTest(point); // display hitTestInfo for each touch event string displayText = string.Empty; if (info != null) { displayText = chartElement + (info.ChartElement == null ? chartElementNone : info.ChartElement.ToString()); if (info.Item != null) { displayText += "\n" + seriesName + info.Series.Name + "\n" + pointIndex + info.PointIndex; ChartPoint data = (ChartPoint)info.Item; displayText += "\nX : " + data.Count; if (info.Series.Name.Equals("sin(x)")) { displayText += " sin(x) : " + data.Sine; } else { displayText += " cos(x) : " + data.Cosine; } } } else { displayText = "Well, this is not happening.."; } mHitTestInfo.Text = displayText; } } |