Skip to content

This example demonstrates how to add multiple trackball in Xamarin.Forms Chart

Notifications You must be signed in to change notification settings

SyncfusionExamples/How-to-add-multiple-trackball-in-Xamarin.Forms-Chart

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 

Repository files navigation

How to add multiple trackballs in Xamarin.Forms Chart (SfChart)?

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 ChartBehaviors collection.

<chart:SfChart.ChartBehaviors>
     <local:ChartTrackballBehaviorExt />
     <local:ChartTrackballBehaviorExt />
</chart:SfChart.ChartBehaviors>

Step 3: Activate the multiple trackballs at load time using the Show method.

protected override void OnAppearing()
{
    base.OnAppearing();
 
    Task.Run(async () =>
    {
        await ShowTrackball();
    });
}
 
async Task ShowTrackball()
{
    Task.Delay(1000).Wait();
 
    Device.BeginInvokeOnMainThread(() =>
    {
        trackballBehavior1.Show(pointX, pointY);
        trackballBehavior2.Show(pointX, pointY);
    });
}

Step 4: Set ActivationMode to None to restrict the default movement of the trackball behavior.

<chart:SfChart.ChartBehaviors>
    <local:ChartTrackballBehaviorExt ActivationMode="None" x:Name="trackballBehavior1" />
    <local:ChartTrackballBehaviorExt ActivationMode="None" x:Name="trackballBehavior2" />
</chart:SfChart.ChartBehaviors>

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:

Xamarin.Forms multiple trackball behavior

KB article - How to add multiple trackballs in Xamarin.Forms Chart?

Troubleshooting

Path too long exception

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.

Releases

No releases published

Packages

No packages published

Languages