Articles in this section
Category / Section

How to synchronize trackball in multiple Flutter SfCartesianChart ?

3 mins read

In this article, we described how to synchronize the trackball in multiple charts. 

In Flutter Cartesian Chart widget, you can synchronize the trackball of multiple charts using the available callback events and public methods. A callback event is a callback function or method, which you can pass as an argument to another function or method. It can perform an action when you require it, and public methods are methods that can be called by using the class object where they are defined. The callback event used for synchronizing trackball is the onChartTouchInteractionMove and the show public method of the trackball is used for activating the trackball in multiple charts in a synchronized fashion on user interaction.

The following steps explains how to synchronize the trackball in multiple charts

 

Step 1: Define two TrackballBehavior variables with the required properties globally for the first and second charts respectively to use the public methods of trackball.

// Trackball behavior for first chart
late TrackballBehavior _trackballBehavior1; 
// Trackball behavior for second chart
late TrackballBehavior _trackballBehavior2;
 
@override
void initState(){
  _trackballBehavior1 =
    TrackballBehavior(enable: true, activationMode: ActivationMode.singleTap);
_trackballBehavior2 =
    TrackballBehavior(enable: true, activationMode: ActivationMode.singleTap);
super.initState();
}

 

Step 2: Initialize a Cartesian chart (First chart) as an individual StatefulWidget with all the necessary properties and also set the globally defined trackball behavior variable (trackBall1) of the first chart in the trackballBehavior property available in the chart.

class FirstChart extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return FirstChartState();
  }
}
 
class FirstChartState extends State<FirstChart> {
  @override
  Widget build(BuildContext context) {
    return SfCartesianChart(
        primaryXAxis: CategoryAxis(),
        title: ChartTitle(text: 'Chart 1'),
        trackballBehavior: _trackballBehavior1, // Trackball behavior variable of the first chart.
        series: <LineSeries<SalesData, String>>[
          LineSeries<SalesData, String>(
              dataSource: chartData,
              xValueMapper: (SalesData sales, _) => sales.year,
              yValueMapper: (SalesData sales, _) => sales.sales)
        ]);
  }
}

 

Step 3: Initialize another Cartesian chart (Second chart) as an individual StatefulWidget with all the necessary properties and also set the globally defined trackball behavior variable (trackBall2) of the second chart in the trackballBehavior property available in the chart.

class SecondChart extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return SecondChartState();
  }
}
 
class SecondChartState extends State<SecondChart> {
  @override
  Widget build(BuildContext context) {
    return SfCartesianChart(
        primaryXAxis: CategoryAxis(),
        title: ChartTitle(text: 'Chart 2'),
        trackballBehavior: _trackballBehavior2, // Trackball behavior of second chart.
        series: <LineSeries<SalesData, String>>[
          LineSeries<SalesData, String>(
              dataSource: chartData,
              xValueMapper: (SalesData sales, _) => sales.year,
              yValueMapper: (SalesData sales, _) => sales.sales)
        ]);
  }
}

 

Step 4: The synchronization of trackball can be done with the help of the onChartTouchInteractionMove callback event and the trackball’s show public method is available in the chart.

In the first chart, with the help of the pixel position arguments are obtained from the onChartTouchInteractionMove event on user interaction, call the trackball show method for the second chart with the obtained pixel position arguments (dx and dy) as the parameters.

class SecondChartState extends State<SecondChart> {
  @override
  Widget build(BuildContext context) {
    return SfCartesianChart(
        trackballBehavior: _trackballBehavior1, // Trackball behavior variable of the first chart.
        onChartTouchInteractionMove: (ChartTouchInteractionArgs args) {
           // Called the second chart’s trackball show method with pixel position arguments as parameters
           _trackballBehavior2.show(args.position.dx, args.position.dy, 'pixel');
        },
        // Other configurations
    )
  }
} 

 

Similarly, in the second chart, call the trackball show method for the first chart with the obtained pixel position arguments (dx and dy) from the onChartTouchInteractionMove as the parameters.

class SecondChartState extends State<SecondChart> {
  @override
  Widget build(BuildContext context) {
    return SfCartesianChart(
        trackballBehavior: _trackballBehavior2, // Trackball behavior of second chart.
        onChartTouchInteractionMove: (ChartTouchInteractionArgs args) {
           // Called the First chart’s trackball show method with pixel positional arguments as parameters
          _trackballBehavior1.show(args.position.dx, args.position.dy, 'pixel');
        },
        // Other configurations
    )
  }
}

 

Thus, the trackballs can be synchronized in the multiple charts.

 

Screenshot

 

Synchronized trackball

 

View the sample in GitHub.

 

Conclusion

I hope you enjoyed learning about how to synchronize trackball in multiple charts (SfCartesianChart).

You can refer to our  Flutter CartesianChart feature tour page to know about its other groundbreaking feature representations. You can also explore our Flutter CartesianChart documentation to understand how to create and manipulate data.

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
Please sign in to leave a comment
Access denied
Access denied