Articles in this section
Category / Section

How to add the multiple trackball in Xamarin.Android charts?

4 mins read

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 ChartTrackballBehavior.

 

public class ChartTrackballBehaviorExt : ChartTrackballBehavior
{
}

 

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 OnAttachedToWindow()
{
    base.OnAttachedToWindow();
    
    Task.Run(async () =>
    {
        await ShowTrackball();
    });
}
 
async Task ShowTrackball()
{
    Task.Delay(1000).Wait();
 
    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 = ChartTrackballActivationMode.None;
trackballBehavior2.ActivationMode = ChartTrackballActivationMode.None;

 

Step 5: Interact with multiple trackballs by overriding the touch methods of ChartTrackballBehavior class and the HitTest method. The HitTest method is used to find the trackball that is currently activated by user.

 

public class ChartTrackballBehaviorExt : ChartTrackballBehavior
{
    bool isActivated= false;
 
    protected override void OnTouchDown(float pointX, float pointY)
    {
        if (HitTest(pointX, pointY))
        {
            isActivated= true;
            base.OnTouchDown(pointX, pointY);
        }
    }
 
    protected override void OnTouchMove(float pointX, float pointY)
    {
        if (isActivated)
        {
            Show(pointX, pointY);
            base.OnTouchMove(pointX, pointY);
        }
    }
 
    protected override void OnTouchUp(float pointX, float pointY)
    {
        isActivated= false;
    }
}  
 

 

Step 6: Tap and drag each trackball separately to view the data points at different positions simultaneously.

Output

Multiple trackball in Xamarin.Android Chart.

Conclusion

I hope you enjoyed learning about how to add the multiple trackball in Xamarin.Android charts.


You can refer to our Xamarin.Android Charts feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial
to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied