I am trying to get the correct value of x values following a touch on the
SfCartesianChart. I used the code written on your documentation "Get the touch position in SfCartesianChart"
<chart:SfCartesianChart.InteractiveBehavior> <local:ChartInteractiveExt/> </chart:SfCartesianChart.InteractiveBehavior>
public class ChartInteractiveExt: ChartInteractiveBehavior
{
protected override void OnTouchDown(ChartBase chart, float pointX, float pointY)
{
base.OnTouchDown(chart, pointX, pointY);
}
protected override void OnTouchMove(ChartBase chart, float pointX, float pointY)
{
base.OnTouchMove(chart, pointX, pointY);
}
protected override void OnTouchUp(ChartBase chart, float pointX, float pointY)
{
base.OnTouchUp(chart, pointX, pointY);
}
}this code returns the absolute position with respect to the SfCartesianChart so for example pointx 0 pointy 0 are relative position to the total control not the position on the chart.
It is really hard to calculate start position because XAsex and YAsex widht and height, is not determinated and I cannot find any reference to check widh and height of XAsex and YAsex .
Hi Marco,
We looked into your question and found you can get the chart's boundaries using SeriesBounds to get the chart positions of the x and y-axis points. Here's a simplified code example:
|
public class CustomBehaviorClass : ChartInteractiveBehavior { public CustomBehaviorClass() { }
protected override void OnTouchDown(ChartBase chart, float pointX, float pointY) { if (chart is not SfCartesianChart cartesianChart) return;
// Adjust touch coordinates using SeriesBounds var xPosition = pointX - cartesianChart.SeriesBounds.Left; var yPosition = pointY + cartesianChart.SeriesBounds.Top;
var xAxis = cartesianChart.XAxes[0]; var yAxis = cartesianChart.YAxes[0];
double xdataPoint = xAxis.PointToValue(xPosition, yPosition); double ydataPoint = yAxis.PointToValue(xPosition, yPosition);
if (xdataPoint != null && ydataPoint != null) { // Display the nearest data point values Application.Current.MainPage.DisplayAlert( "Touch Data Point", $"X: {xdataPoint}\nY: {ydataPoint}", "OK" ); } } } |
This code subtracts the SeriesBounds.Left value from the touch x-coordinate and adds the SeriesBounds.Top value to the touch y-coordinate to get the correct data point values.
We've attached a sample for your reference. Please review it for more details.
Regards,
Arul Jenith B,