From 2af3b12a92859804899037edbd82e64549d69bec Mon Sep 17 00:00:00 2001 From: Gold872 Date: Fri, 24 Nov 2023 21:48:50 -0500 Subject: [PATCH] Preserve graph data when dragging --- .../nt4_widgets/single_topic/graph.dart | 40 +++++++++++++++---- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/lib/widgets/nt4_widgets/single_topic/graph.dart b/lib/widgets/nt4_widgets/single_topic/graph.dart index 93d062c8..a603b6f8 100644 --- a/lib/widgets/nt4_widgets/single_topic/graph.dart +++ b/lib/widgets/nt4_widgets/single_topic/graph.dart @@ -20,7 +20,8 @@ class GraphWidget extends StatelessWidget with NT4Widget { double? maxValue; late Color mainColor; - late final List _graphData; + List _graphData = []; + _GraphWidgetGraph? _graphWidget; GraphWidget({ super.key, @@ -54,8 +55,6 @@ class GraphWidget extends StatelessWidget with NT4Widget { void init() { super.init(); - _graphData = []; - resetGraphData(); } @@ -174,7 +173,17 @@ class GraphWidget extends StatelessWidget with NT4Widget { Widget build(BuildContext context) { notifier = context.watch(); - return GraphWidgetGraph( + resetGraphData(); + + List? currentGraphData = _graphWidget?.getCurrentData(); + + if (currentGraphData != null && + currentGraphData.length == _graphData.length) { + _graphData = currentGraphData; + } + + return _graphWidget = _GraphWidgetGraph( + key: GlobalKey(), initialData: _graphData, subscription: subscription, mainColor: mainColor, @@ -184,7 +193,7 @@ class GraphWidget extends StatelessWidget with NT4Widget { } } -class GraphWidgetGraph extends StatefulWidget { +class _GraphWidgetGraph extends StatefulWidget { final NT4Subscription? subscription; final double? minValue; final double? maxValue; @@ -192,7 +201,14 @@ class GraphWidgetGraph extends StatefulWidget { final List initialData; - const GraphWidgetGraph({ + final List<_GraphPoint> _currentData = []; + + set currentData(List<_GraphPoint> data) { + _currentData.clear(); + _currentData.addAll(data); + } + + _GraphWidgetGraph({ super.key, required this.initialData, required this.subscription, @@ -201,11 +217,15 @@ class GraphWidgetGraph extends StatefulWidget { this.maxValue, }); + List getCurrentData() { + return _currentData.map((e) => e.y).toList(); + } + @override - State createState() => _GraphWidgetGraphState(); + State<_GraphWidgetGraph> createState() => _GraphWidgetGraphState(); } -class _GraphWidgetGraphState extends State { +class _GraphWidgetGraphState extends State<_GraphWidgetGraph> { ChartSeriesController? seriesController; late List<_GraphPoint> graphData; late StreamSubscription? subscriptionListener; @@ -223,12 +243,16 @@ class _GraphWidgetGraphState extends State { fakeXIndex++; } + widget.currentData = graphData; + subscriptionListener = widget.subscription?.periodicStream().listen((data) { if (data != null) { graphData.add( _GraphPoint(x: fakeXIndex.toDouble(), y: tryCast(data) ?? 0.0)); graphData.removeAt(0); + widget.currentData = graphData; + fakeXIndex++; seriesController?.updateDataSource(