This example demonstrates how to add the multiple trackball to a single chart and drag them independently to view the information of different data points at the same time in Xamarin.iOS chart.
SFChart allows you add multiple trackballs to a single chart and drag them independently to view the information of different data points at the same time.
The following steps describe how to add multiple trackballs to SFChart.
Step 1: Create a custom ChartTrackballBehaviorExt class, which is inherited from SFChartTrackballBehavior.
public class ChartTrackballBehaviorExt : SFChartTrackballBehavior
{
}
Step 2: Create an instance of ChartTrackballBehaviorExt and add it to the Behaviors collection.
ChartTrackballBehaviorExt trackballBehavior1 = new ChartTrackballBehaviorExt();
ChartTrackballBehaviorExt trackballBehavior2 = new ChartTrackballBehaviorExt();
chart.Behaviors.Add(trackballBehavior1);
chart.Behaviors.Add(trackballBehavior2);
Step 3: Activate the multiple trackballs at load time using the Show method.
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
trackballBehavior1.Show(pointX, pointY);
trackballBehavior2.Show(pointX, pointY);
}
Step 4: Set ActivationMode to None to restrict the default movement of the trackball behavior.
trackballBehavior1.ActivationMode = SFChartTrackballActivationMode.None;
trackballBehavior2.ActivationMode = SFChartTrackballActivationMode.None;
Step 5: Interact with multiple trackballs by overriding the touch methods of SFChartTrackballBehavior class and the HitTest method. The HitTest method is used to find the trackball that is currently activated by user.
public class ChartTrackballBehaviorExt : SFChartTrackballBehavior
{
bool isActivated = false;
public override void TouchesBegan(NSSet touches, UIEvent uiEvent)
{
if (touches?.AnyObject is UITouch touch)
{
CGPoint location = touch.LocationInView(Chart);
if (HitTest((float)location.X, (float)location.Y))
{
isActivated = true;
base.TouchesBegan(touches, uiEvent);
}
}
}
public override void TouchesMoved(NSSet touches, UIEvent uiEvent)
{
if (isActivated)
{
if (touches?.AnyObject is UITouch touch)
{
CGPoint location = touch.LocationInView(Chart);
Show(location);
base.TouchesMoved(touches, uiEvent);
}
}
}
public override void TouchesEnded(NSSet touches, UIEvent uiEvent)
{
isActivated = false;
}
}
Step 6: Tap and drag each trackball separately to view the data points at different positions simultaneously.
KB article - How to add multiple trackball in Xamarin.iOS Chart?
If you are facing path too long exception when building this example project, close Visual Studio and rename the repository to short and build the project.