-
Notifications
You must be signed in to change notification settings - Fork 848
[syncfusion_charts] Cancel long-press zoom #2183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi @DrNiels , In the current SfCartesianChart implementation, there is no default feature to cancel the zooming events. However, you can implement this functionality by using a variable to cancel zoom in sample level. enableZooming: Added this field to control the pinch zooming, double-tap zooming, and panning properties. Demo: If you need further assistance or have additional questions, please let us know! Regards, |
Please reopen this ticket, if you need further assistance with this. |
It seems a bit tricky, but it's working for me now. Temporaryly disabling selection zoom will cancel the current zoom operation, see here: Code sampleimport 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:syncfusion_flutter_charts/charts.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Cancel long-press zoom',
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late SfCartesianChart chart;
late ZoomPanBehavior _zoomPanBehavior;
bool enableZooming = false;
bool enableSelectionZooming = false;
final FocusNode _focusNode = FocusNode();
@override
void initState() {
super.initState();
_updateZoomBehavior();
}
@override
Widget build(BuildContext context) {
final List<ChartData> chartData = [
ChartData(10, 17),
ChartData(20, 34),
ChartData(30, 50),
ChartData(40, 70),
];
chart = SfCartesianChart(
zoomPanBehavior: _zoomPanBehavior,
series: <CartesianSeries>[
ColumnSeries<ChartData, double>(
dataSource: chartData,
xValueMapper: (ChartData data, _) => data.x,
yValueMapper: (ChartData data, _) => data.y,
),
],
);
return Scaffold(
body: Center(
child: KeyboardListener(
focusNode: _focusNode,
autofocus: true,
onKeyEvent: (KeyEvent event) {
if ((event is KeyDownEvent) && (event.logicalKey == LogicalKeyboardKey.escape) && enableSelectionZooming) {
setState(() {
enableSelectionZooming = !enableSelectionZooming;
_updateZoomBehavior();
});
Future.delayed(const Duration(milliseconds: 100), () {
setState(() {
enableSelectionZooming = !enableSelectionZooming;
_updateZoomBehavior();
});
});
}
},
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
_buildZoomButton(
label: enableZooming ? 'Disable Zoom' : 'Enable Zoom',
onPressed: () {
setState(() {
enableZooming = !enableZooming;
_updateZoomBehavior();
});
},
),
_buildZoomButton(
label:
enableSelectionZooming
? 'Disable Selected Area Zoom'
: 'Enable Selected Area Zoom',
onPressed: () {
setState(() {
enableSelectionZooming = !enableSelectionZooming;
_updateZoomBehavior();
});
},
),
_buildZoomButton(label: 'Zoom Out', onPressed: zoom),
SizedBox(height: 300, child: chart),
],
),
),
),
);
}
void _updateZoomBehavior() {
_zoomPanBehavior = ZoomPanBehavior(
enableSelectionZooming: enableSelectionZooming,
enableDoubleTapZooming: enableZooming,
enablePinching: enableZooming,
enablePanning: enableZooming,
);
}
Widget _buildZoomButton({
required String label,
required VoidCallback onPressed,
}) {
return TextButton(onPressed: onPressed, child: Text(label));
}
void zoom() {
if (enableZooming) {
_zoomPanBehavior.zoomOut();
}
}
}
class ChartData {
ChartData(this.x, this.y);
final double x;
final double? y;
} Thank you for your support and sorry for the late reply |
Use case
Sometimes, you want to drag a chart but accidently remain in place too long and start a zoom operation by accident or just decide that you want to cancel the zoom operation while you're at it. However, you can't.
Proposal
Making the zoom operation via long-press cancelable would be nice, either by providing a hook, so a developer can introduce a cancel operation that fits their app, or by providing a default like pressing Escape.
The text was updated successfully, but these errors were encountered: