Articles in this section

How to update chart efficiently based on selected value from Flutter Range Selector?.

Using Flutter Range Selectoryou can dynamically change the visible range of a chart based on the selected range in the SfRangeSelector widget.

 

Step 1: Add the Syncfusion® Flutter Sliders package to your dependencies in the pubspec.yaml file.



Step 2: Initialize the SfRangeSelector widget as a child of any widget. Create a RangeController instance in the initState() method and set it to the SfRangeSelector.controller property along with the SfRangeSelector.min and SfRangeSelector.max properties. The value of SfRangeSelector.controller property should be given within the min and max values.

 

final double _min = 2.0;
final double _max = 19.0;
SfRangeValues _values = SfRangeValues(8.0, 16.0);
late RangeController _rangeController;
 
@override
void initState() {
  super.initState();
  _rangeController = RangeController(start: _values.start, end: _values.end);
}

 

Step 3: Set the SfCartesianChart instance to the SfRangeSelector.child property. Then set the same range controller value to the primaryXAxis.rangeController property of the SfCartesianChart, to change the visible range of the chart

 

Step 4: The range selector updates values to the controller property when users interact with the thumbs. This automatically updates the chart's visible range.

final double _min = 2.0;
final double _max = 19.0;
SfRangeValues _values = SfRangeValues(8.0, 16.0);
late RangeController _rangeController;
late SfCartesianChart _splineChart;
 
@override
void initState() {
  super.initState();
  _rangeController = RangeController(start: _values.start, end: _values.end);
}
 
@override
void dispose() {
  _rangeController.dispose();
  super.dispose();
}
 
final List<Data> chartData = <Data>[
  Data(x: 2.0, y: 2.2),
  Data(x: 3.0, y: 3.4),
  Data(x: 4.0, y: 2.8),
  Data(x: 5.0, y: 1.6),
  Data(x: 6.0, y: 2.3),
  Data(x: 7.0, y: 2.5),
  Data(x: 8.0, y: 2.9),
  Data(x: 9.0, y: 3.8),
  Data(x: 10.0, y: 3.7),
  Data(x: 11.0, y: 2.2),
  Data(x: 12.0, y: 3.4),
  Data(x: 13.0, y: 2.8),
  Data(x: 14.0, y: 1.6),
  Data(x: 15.0, y: 2.3),
  Data(x: 16.0, y: 2.5),
  Data(x: 17.0, y: 2.9),
  Data(x: 18.0, y: 3.8),
  Data(x: 19.0, y: 3.7),
];
 
@override
Widget build(BuildContext context) {
  _splineChart = SfCartesianChart(
    margin: const EdgeInsets.only(left: 10, right: 10, bottom: 20),
    primaryXAxis: NumericAxis(
      minimum: _min,
      maximum: _max,
      isVisible: true,
     rangeController: _rangeController),
   primaryYAxis: NumericAxis(isVisible: true),
   plotAreaBorderWidth: 0,
   series: <SplineSeries<Data, double>>[
     SplineSeries<Data, double>(
       color: Color.fromARGB(255, 126, 184, 253),
       dataSource: chartData,
       animationDuration: 0,
       xValueMapper: (Data sales, int index) => sales.x,
       yValueMapper: (Data sales, int index) => sales.y)
    ],
 );
 
 return Scaffold(
   body: Center(
     child: Padding(
       padding: EdgeInsets.only(left: 10, right: 10, top: 80),
       child: Column(
         children: <Widget>[
            Container(
              child: _splineChart,
            ),
           SfRangeSelector(
             min: _min,
             max: _max,
             interval: 2,
             showTicks: true,
             showLabels: true,
             controller: _rangeController,
             child: Container(
               height: 130,
               child: SfCartesianChart(
                 margin: const EdgeInsets.all(0),
                 primaryXAxis: NumericAxis(
                     minimum: _min, maximum: _max, isVisible: false),
                 primaryYAxis: NumericAxis(isVisible: false),
                 plotAreaBorderWidth: 0,
                 series: <SplineSeries<Data, double>>[
                   SplineSeries<Data, double>(
                     color: Color.fromARGB(255, 126, 184, 253),
                     dataSource: chartData,
                     xValueMapper: (Data sales, int index) => sales.x,
                     yValueMapper: (Data sales, int index) => sales.y)
                 ],
               ),
             ),
           ),
         ],
       ),
     ),
  ));
}
 
class Data {
  Data({required this.x, required this.y});
  final double x;
  final double y;
}

 

Output: 

Chart, line chart

Description automatically generated

 

 

Check the below links for more features in Syncfusion® Flutter Sliders,

 

Live samples,

 

Conclusion

I hope you enjoyed learning about how to update chart efficiently based on selected value from Flutter Range Selector.

You can refer to our  Flutter Range Selector feature tour page to know about its other groundbreaking feature representations. You can also explore our Flutter Range Selector 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 (0)
Access denied
Access denied