Skip to content

Commit

Permalink
Preserve graph data when dragging
Browse files Browse the repository at this point in the history
  • Loading branch information
Gold872 committed Nov 25, 2023
1 parent 519c32f commit 2af3b12
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions lib/widgets/nt4_widgets/single_topic/graph.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class GraphWidget extends StatelessWidget with NT4Widget {
double? maxValue;
late Color mainColor;

late final List<double> _graphData;
List<double> _graphData = [];
_GraphWidgetGraph? _graphWidget;

GraphWidget({
super.key,
Expand Down Expand Up @@ -54,8 +55,6 @@ class GraphWidget extends StatelessWidget with NT4Widget {
void init() {
super.init();

_graphData = [];

resetGraphData();
}

Expand Down Expand Up @@ -174,7 +173,17 @@ class GraphWidget extends StatelessWidget with NT4Widget {
Widget build(BuildContext context) {
notifier = context.watch<NT4WidgetNotifier?>();

return GraphWidgetGraph(
resetGraphData();

List<double>? currentGraphData = _graphWidget?.getCurrentData();

if (currentGraphData != null &&
currentGraphData.length == _graphData.length) {
_graphData = currentGraphData;
}

return _graphWidget = _GraphWidgetGraph(
key: GlobalKey(),
initialData: _graphData,
subscription: subscription,
mainColor: mainColor,
Expand All @@ -184,15 +193,22 @@ class GraphWidget extends StatelessWidget with NT4Widget {
}
}

class GraphWidgetGraph extends StatefulWidget {
class _GraphWidgetGraph extends StatefulWidget {
final NT4Subscription? subscription;
final double? minValue;
final double? maxValue;
final Color mainColor;

final List<double> 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,
Expand All @@ -201,11 +217,15 @@ class GraphWidgetGraph extends StatefulWidget {
this.maxValue,
});

List<double> getCurrentData() {
return _currentData.map((e) => e.y).toList();
}

@override
State<GraphWidgetGraph> createState() => _GraphWidgetGraphState();
State<_GraphWidgetGraph> createState() => _GraphWidgetGraphState();
}

class _GraphWidgetGraphState extends State<GraphWidgetGraph> {
class _GraphWidgetGraphState extends State<_GraphWidgetGraph> {
ChartSeriesController? seriesController;
late List<_GraphPoint> graphData;
late StreamSubscription<Object?>? subscriptionListener;
Expand All @@ -223,12 +243,16 @@ class _GraphWidgetGraphState extends State<GraphWidgetGraph> {
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(
Expand Down

0 comments on commit 2af3b12

Please sign in to comment.