repo_files/documentations/handle_transformations.md
The transformation feature in fl_chart allows users to interact with charts through scaling and panning, similar to Flutter's InteractiveViewer widget.
To enable transformations, provide a FlTransformationConfig to your chart:
LineChart(
LineChartData(...),
transformationConfig: FlTransformationConfig(
scaleAxis: FlScaleAxis.horizontal,
minScale: 1.0,
maxScale: 2.5,
),
)
See FlTransformationConfig for more information.
BarChartAlignment.center, end, or start, horizontal scaling is not supportedFor more control over transformations, you can provide a TransformationController. This allows you to:
At this moment, transformations made with a custom TransformationController are not prevented from moving the chart out of the screen. Developers are responsible for ensuring that the chart remains within the visible area and within the transformation limits.
See the implementation of AxisChartScaffoldWidget for how to prevent the chart from moving out of the screen when using a custom TransformationController.
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();
},
),
],
),
],
);
}
}
See Matrix4 for more information on how to manipulate the matrix.
TransformationController when you're done with itminScale and maxScale values to prevent excessive zoomingscaleAxisRemember that transformations are purely visual and don't affect the underlying data. They're particularly useful for exploring detailed data sets or allowing users to focus on specific regions of interest in your charts.