For advanced control, such as implementing custom zoom/pan buttons or resetting the chart state, provide a TransformationController within your FlTransformationConfig.
Warning: When using a custom TransformationController, the library does not prevent the chart from moving out of the visible screen area. You are responsible for implementing logic to keep the chart within bounds and within transformation limits.
class ChartWithControls extends StatefulWidget {
@override
State<ChartWithControls> createState() => _ChartWithControlsState();
}
class _ChartWithControlsState extends State<ChartWithControls> {
late TransformationController _controller;
@override
void initState() {
super.initState();
_controller = TransformationController();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Column(
children: [
AspectRatio(
aspectRatio: 1.4,
child: LineChart(
LineChartData(...),
transformationConfig: FlTransformationConfig(
scaleAxis: FlScaleAxis.horizontal,
minScale: 1.0,
maxScale: 25.0,
transformationController: _controller,
),
),
),
Row(
children: [
IconButton(
icon: Icon(Icons.zoom_in),
onPressed: () {
_controller.value *= Matrix4.diagonal3Values(1.1, 1.1, 1);
},
),
IconButton(
icon: Icon(Icons.zoom_out),
onPressed: () {
_controller.value *= Matrix4.diagonal3Values(0.9, 0.9, 1);
},
),
IconButton(
icon: Icon(Icons.refresh),
onPressed: () {
_controller.value = Matrix4.identity();
},
),
],
),
],
);
}
}