Trackball and Crosshair in Flutter Cartesian Charts (SfCartesianChart)

7 Jun 202124 minutes to read

Trackball

Trackball feature displays the tooltip for the data points that are closer to the point where you touch on the chart area. This feature, especially, can be used instead of data label feature when you cannot show data labels for all data points due to space constraint. This feature can be enabled using enable property of trackballBehavior. Trackball will be activated once you long-press anywhere on the chart area. Once it is activated, it will appear in the UI and move based on your touch movement until you stop touching on the chart.

You can use the following properties to customize the appearance of trackball tooltip.

NOTE

The above mentioned properties are only applicable for SfCartesian types of charts.

  • dart
  • late TrackballBehavior _trackballBehavior;
    
        @override
        void initState(){
          _trackballBehavior = TrackballBehavior(
                      // Enables the trackball
                      enable: true,
                      tooltipSettings: InteractiveTooltip(
                        enable: true,
                        color: Colors.red
                      )
                    );
          super.initState();
        }
    
        @override
        Widget build(BuildContext context) {
          return Scaffold(
            body: SafeArea(
              child: Center(
                child: Container(
                  child: SfCartesianChart(
                    trackballBehavior: _trackballBehavior,
                  )
                )
              )
            )
          );
        }

    Trackball

    Label display mode

    The tooltipDisplayMode property is used to specify whether to display label for all the data points along the vertical line or display only single label. Following are the options you can set to this property,

    • floatAllPoints - displays label for all the data points along the tracker line.
    • nearestPoint - displays label for single data point that is nearer to the touch contact position.
    • groupAllPoints - displays label for all the data points grouped and positioned at the top of the chart area.
    • none - doesn’t display the label.
  • dart
  • late TrackballBehavior _trackballBehavior;
    
        @override
        void initState(){
          _trackballBehavior = TrackballBehavior(
                      enable: true,
                      // Display mode of trackball tooltip
                      tooltipDisplayMode: TrackballDisplayMode.floatAllPoints
                    );
          super.initState();
        }
    
        @override
        Widget build(BuildContext context) {
          return Scaffold(
            body: SafeArea(
              child: Center(
                child: Container(
                  child: SfCartesianChart(
                    trackballBehavior: _trackballBehavior, 
                  )
                )
              )
            )
          );
        }

    Label display mode

    Label alignment

    The position of trackball tooltip can be changed using the tooltipAlignment property of trackballBehavior. The following options are available in tooltipAlignment.

    • near - aligns the trackball tooltip to the top position of plot area.
    • far - aligns the trackball tooltip to the bottom position of plot area.
    • center - aligns the trackball tooltip to the center position of plot area.
  • dart
  • late TrackballBehavior _trackballBehavior;
    
        @override
        void initState(){
          _trackballBehavior = TrackballBehavior(
                      enable: true,
                      tooltipAlignment: ChartAlignment.near,
                      tooltipDisplayMode: TrackballDisplayMode.groupAllPoints
                    );
          super.initState();
        }
    
        @override
        Widget build(BuildContext context) {
          return Scaffold(
            body: SafeArea(
              child: Center(
                child: Container(
                  child: SfCartesianChart(
                    trackballBehavior: _trackballBehavior,
                  )
                )
              )
            )
          );
        }

    Label alignment

    NOTE

    This is applicable only when the tooltipDisplayMode is set to groupAllPoints.

    Label format

    By default, axis value will be displayed in the tooltip, and it can be customized using format property by adding desired text as prefix or suffix.

  • dart
  • late TrackballBehavior _trackballBehavior;
    
        @override
        void initState(){
          _trackballBehavior = TrackballBehavior(
                      enable: true,
                      tooltipSettings: InteractiveTooltip(
                        // Formatting trackball tooltip text
                        format: 'point.x : point.y%'
                      )
                    );
          super.initState();
        }
    
        @override
        Widget build(BuildContext context) {
          return Scaffold(
            body: SafeArea(
              child: Center(
                child: Container(
                  child: SfCartesianChart(
                    trackballBehavior: _trackballBehavior,
                  )
                )
              )
            )
          );
        }

    Label format

    Activation mode

    The activationMode property is used to restrict the visibility of trackball based on the touch actions. The default value of this property is ActivationMode.longPress.

    The ActivationMode enum contains the following values:

    • longPress - activates trackball only when performing the long press action.
    • singleTap - activates trackball only when performing single tap action.
    • doubleTap - activates trackball only when performing double tap action.
    • none - Hides the visibility of trackball when setting activation mode to none. It will be activated when calling the show method.
  • dart
  • late TrackballBehavior _trackballBehavior;
    
        @override
        void initState(){
          _trackballBehavior = TrackballBehavior(
                      enable: true,
                        // Displays the trackball on single tap
                        activationMode: ActivationMode.singleTap
                    );
          super.initState();
        }
    
        @override
        Widget build(BuildContext context) {
          return Scaffold(
            body: SafeArea(
              child: Center(
                child: Container(
                  child: SfCartesianChart(
                    trackballBehavior: _trackballBehavior,
                  )
                )
              )
            )
          );
        }

    Trackball tooltip overlap

    SfCartesianChart provides support to avoid the overlapping of two or more tooltips of the trackball and no API is required for this feature as it will be done by default. For example, If we have 2 or more series data points rendered close to each other then, the trackball tooltips of each data point will not be overlap with each other.

  • dart
  • late TrackballBehavior _trackballBehavior;
    
        @override
        void initState(){
          _trackballBehavior = TrackballBehavior(
                      enable: true);
          super.initState();
        }
    
        @override
        Widget build(BuildContext context) {
          return Scaffold(
              child: Center(
                child: Container(
                  child: SfCartesianChart(
                    primaryXAxis: DateTimeAxis(),
                    trackballBehavior: _trackballBehavior,
                    <LineSeries<SalesData, dateTime>>[
                          LineSeries<SalesData, dateTime>(
                              dataSource: ChartData,
                              markerSettings: MarkerSettings(enable: true),
                              xValueMapper: (SalesData sales, _) => sales.year,
                              yValueMapper: (SalesData sales, _) => sales.sales)
                          LineSeries<SalesData, dateTime>(
                              dataSource: ChartData1,
                              markerSettings: MarkerSettings(enable: true),
                              xValueMapper: (SalesData sales, _) => sales.year,
                              yValueMapper: (SalesData sales, _) => sales.sales),
                          LineSeries<SalesData, dateTime>(
                              dataSource: ChartDat2,
                              markerSettings: MarkerSettings(enable: true),
                              xValueMapper: (SalesData sales, _) => sales.year,
                              yValueMapper: (SalesData sales, _) => sales.sales),
                          LineSeries<SalesData, dateTime>(
                              dataSource: ChartData3,
                              markerSettings: MarkerSettings(enable: true),
                              xValueMapper: (SalesData sales, _) => sales.year,
                              yValueMapper: (SalesData sales, _) => sales.sales)
                    ]
                  )
                )
            )
          );
        }
    
        class SalesData {
            SalesData(this.year, this.sales);
            final DateTime year;
            final double? sales;
          }

    Trackball tooltip overlap

    Trackball marker settings

    Trackball markers are used to provide information about the exact point location. You can add a shape to adorn each data point when the trackball is visible. Trackball markers can be enabled by using the markerVisibility property of TrackballMarkerSettings. The below markerVisibility property determines whether the trackball marker should be visible or not when the trackball is enabled in the chart

    Also refer, marker customization for customizing the appearance of trackball marker.

  • dart
  • late TrackballBehavior _trackballBehavior;
    
        @override
        void initState(){
          _trackballBehavior = TrackballBehavior(
                      enable: true,
                      markerSettings: TrackballMarkerSettings(
                        markerVisibility: TrackballVisibilityMode.visible)
                    );
          super.initState();
        }
    
        @override
        Widget build(BuildContext context) {
          return Scaffold(
              child: Center(
                child: Container(
                  child: SfCartesianChart(
                    primaryXAxis: DateTimeAxis(),
                    trackballBehavior: _trackballBehavior,
                    <LineSeries<SalesData, dateTime>>[
                          LineSeries<SalesData, dateTime>(
                              dataSource: ChartData,
                              xValueMapper: (SalesData sales, _) => sales.year,
                              yValueMapper: (SalesData sales, _) => sales.sales)
                          LineSeries<SalesData, dateTime>(
                              dataSource: ChartData1,
                              xValueMapper: (SalesData sales, _) => sales.year,
                              yValueMapper: (SalesData sales, _) => sales.sales),
                          LineSeries<SalesData, dateTime>(
                              dataSource: ChartData2,
                              xValueMapper: (SalesData sales, _) => sales.year,
                              yValueMapper: (SalesData sales, _) => sales.sales),
                    ]
                  )
                )
              )
          );
        }
    
        class SalesData {
            SalesData(this.year, this.sales);
            final DateTime year;
            final double? sales;
          }

    Trackball marker

    See Also

    Trackball tooltip template

    You can customize the appearance of the trackball tooltip with your own widgets by using the builder property of trackballBehavior.

  • dart
  • late TrackballBehavior _trackballBehavior;
    
        @override
        void initState(){
          _trackballBehavior = TrackballBehavior(
                        enable: true,
                        builder: (BuildContext context,
                            TrackballDetails trackballDetails) {
                          return Container(
                              height: 50,
                              width: 150,
                              decoration: BoxDecoration(
                                color: Color.fromRGBO(0, 8, 22, 0.75),
                                borderRadius:
                                    BorderRadius.all(Radius.circular(6.0)),
                              ),
                              child: Row(children: [
                                Padding(
                                    padding: EdgeInsets.only(left: 5),
                                    child: SizedBox(
                                      child:
                                          Image.asset('images/People_Circle16.png'),
                                      height: 30,
                                      width: 30,
                                    )),
                                Center(
                                    child: Container(
                                        padding: EdgeInsets.only(top: 11, left: 7),
                                        height: 40,
                                        width: 100,
                                        child: Text(
                                            '${trackballDetails.point.x.toString()} : \$${trackballDetails.point.y.toString()}',
                                            style: TextStyle(
                                                fontSize: 13,
                                                color: Color.fromRGBO(
                                                    255, 255, 255, 1)))))
                              ]));
                        },
                      );
          super.initState();
        }
    
        @override
        Widget build(BuildContext context) {
         return Scaffold(
           child: Center(
             child: Container(
                child: SfCartesianChart(
                  primaryXAxis: CategoryAxis(),
                  trackballBehavior: _trackballBehavior,
                  series: <CartesianChart<SalesData, String>>[
                    SplineSeries<SalesData, String>(
                        dataSource: ChartData,
                        xValueMapper: (SalesData sales, _) => sales.year,
                        yValueMapper: (SalesData sales, _) => sales.sales)
                    ]
                  )
                )
              )
          );
        }
    
        class SalesData {
            SalesData(this.year, this.sales);
            final String year;
            final double? sales;
          }

    Trackball template

    Trackball grouping mode info

    TrackballGroupingModeInfo is store the group mode details of trackball template.

    The following properties are available in TrackballGroupingModeInfo:

    The canShowMarker is used to toggle the visibility of the marker in the trackball tooltip.

    Markers are rendered with the series color and placed near the value in trackball tooltip to convey which value belongs to which series.

    Trackball tooltip marker uses the same shape specified for the series marker. But trackball tooltip marker will render based on the value specified to this property irrespective of considering the series marker’s visibility.

    Defaults to true.

  • dart
  • late TrackballBehavior _trackballBehavior;
    
        @override
        void initState(){
          _trackballBehavior = TrackballBehavior(
                          enable: true,
                        );
          super.initState();
        }
        
        @override
        Widget build(BuildContext context) {
          return Scaffold(
            child: Center(
                child: Container(
                    child: SfCartesianChart(
                        primaryXAxis: CategoryAxis(),
                        trackballBehavior: _trackballBehavior,
                        series: <LineSeries<SalesData, String>>[
                          LineSeries<SalesData, String>(
                              dataSource: chartData,
                              xValueMapper: (SalesData sales, _) => sales.year,
                              yValueMapper: (SalesData sales, _) => sales.sales),
                          LineSeries<SalesData, String>(
                              dataSource: chartData,
                              xValueMapper: (SalesData sales, _) => sales.year,
                              yValueMapper: (SalesData sales, _) => sales.sales1),
                          LineSeries<SalesData, String>(
                              dataSource: chartData,
                              xValueMapper: (SalesData sales, _) => sales.year,
                              yValueMapper: (SalesData sales, _) => sales.sales2),
                          LineSeries<SalesData, String>(
                              dataSource: chartData,
                              xValueMapper: (SalesData sales, _) => sales.year,
                              yValueMapper: (SalesData sales, _) => sales.sales3),
                        ]
                    )
                )
            )
          );
        }
    
        class SalesData {
          SalesData(this.year, this.sales, this,sales1, this.sales2, this.sales3);
          final String year;
          final double? sales;
          final double? sales1;
          final double? sales2;
          final double? sales3;
        }

    Trackball tooltip marker

    See Also

    Crosshair

    Crosshair has a vertical and horizontal line to view the value of the axis.

    Crosshair lines can be enabled by using enable property in the crosshairBehavior. Likewise tooltip label for an axis can be enabled by using enable property of interactiveTooltip in the corresponding axis. The hideDelay property can be used to specify a disappear delay for the crosshair.

    NOTE

    The above mentioned properties are only applicable for SfCartesian types of charts.

  • dart
  • late CrosshairBehavior _crosshairBehavior;
    
        @override
        void initState(){
          _crosshairBehavior = CrosshairBehavior(
                      // Enables the crosshair
                      enable: true
                    );
          super.initState();
        }
    
        @override
        Widget build(BuildContext context) {
          return Scaffold(
            body: SafeArea(
              child: Center(
                child: Container(
                  child: SfCartesianChart(
                    primaryXAxis: NumericAxis(
                      crosshairTooltip: InteractiveTooltip(
                        // Enables the crosshair tooltip
                        enable: true
                      )
                    ),
                    crosshairBehavior: _crosshairBehavior
                  )
                )
              )
            )
          );
        }

    Crosshair

    Track line customization

    The appearance of the track line in crosshair can be customized using the following properties.

    • lineType - specifies the type of crosshair line.
    • lineColor - specifies the color of the crosshair line.
    • lineWidth - specifies the stroke width of the crosshair line.
    • lineDashArray - used to render crosshair line with dashes.
    • shouldAlwaysShow - enables or disables the crosshair. Defaults to false.
  • dart
  • late CrosshairBehavior _crosshairBehavior;
    
        @override
        void initState(){
          _crosshairBehavior = CrosshairBehavior(
                      enable: true,
                      lineColor: Colors.red,
                      lineDashArray: <double>[5,5],
                      lineWidth: 2,
                      lineType: CrosshairLineType.vertical
                    );
          super.initState();
        }
    
        @override
        Widget build(BuildContext context) {
          return Scaffold(
            body: SafeArea(
            child: Center(
              child: Container(
                child: SfCartesianChart(
                  crosshairBehavior: _crosshairBehavior
                  )
                )
              )
            )
          );
        }

    Customized trackline

    Show axis tooltip

    The axis tooltip can be enabled using enable property of interactiveTooltip. You can customize the appearance of axis tooltip using the following properties.

    • enable - used to enable the axis tooltip.
    • borderWidth - used to change the stroke width of the axis tooltip.
    • borderColor - used to change the stroke color of the axis tooltip.
    • format - by default, axis value will be displayed in the tooltip, and it can be customized by adding desired text as prefix or suffix.
    • textStyle - used to change the text color, size, font family, fontStyle, and font weight.
    • textStyle.color - used to change the color of the text.
    • textStyle.fontFamily - used to change the font family for chart title.
    • textStyle.fontStyle - used to change the font style for the chart title.
    • textStyle.fontSize - used to change the font size for the chart title.

    Activation mode

    The activationMode property is used to restrict the visibility of trackball based on the touch actions. The default value of this property is ActivationMode.longPress.

    The ActivationMode enum contains the following values:

    • longPress - activates crosshair only when performing the long press action.
    • singleTap - activates crosshair only when performing single tap action.
    • doubleTap - activates crosshair only when performing double tap action.
    • none - hides the visibility of crosshair when setting activation mode to none. It will be activated when calling the show method.
  • dart
  • late CrosshairBehavior _crosshairBehavior;
    
        @override
        void initState(){
          _crosshairBehavior = CrosshairBehavior(
                      enable: true,
                      // Displays the crosshair on single tap
                      activationMode: ActivationMode.singleTap
                    );
          super.initState();
        }
    
        @override
        Widget build(BuildContext context) {
          return Scaffold(
            body: SafeArea(
              child: Center(
                child: Container(
                  child: SfCartesianChart(
                    crosshairBehavior: _crosshairBehavior
                  )
                )
              )
            )
          );
        }

    Also refer crosshair and trackball events for customizing the crosshair and trackball further.

    See Also

    NOTE

    chartData in the above code snippets is a class type list and holds the data for binding to the chart series. Refer Bind data source topic for more details.