Skip to content
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

Feature: Add draggable spots functionality for LineChart #1428

Open
wants to merge 152 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
152 commits
Select commit Hold shift + click to select a range
10673fa
Add draggable logic for LineChart
G33kFreak Aug 26, 2023
b0d0ced
Remake logic of updating spot's position and adjust tests
G33kFreak Aug 26, 2023
773cabf
Add dragSpotUpdatedCallback functionality
G33kFreak Aug 26, 2023
8430ec3
Update changelog
G33kFreak Aug 26, 2023
99d2484
Add sample of draggable line chart
G33kFreak Aug 26, 2023
c96551a
minor fixes
G33kFreak Aug 26, 2023
4eeeb77
Change the comparing logic in didUpdateWidget
G33kFreak Aug 26, 2023
ffe2237
Removed unnecessary logic
G33kFreak Aug 26, 2023
4c6d2b3
chore: bump actions/checkout from 2 to 4
dependabot[bot] Sep 4, 2023
ce49b92
Fix donation link in README.md file
imaNNeo Oct 1, 2023
e074877
Add draggable callbacks
G33kFreak Oct 1, 2023
f883446
Add draggable logic for LineChart
G33kFreak Aug 26, 2023
0bfef57
Remake logic of updating spot's position and adjust tests
G33kFreak Aug 26, 2023
5e9a68e
Add dragSpotUpdatedCallback functionality
G33kFreak Aug 26, 2023
1e9a832
Update changelog
G33kFreak Aug 26, 2023
2a47fc4
Add sample of draggable line chart
G33kFreak Aug 26, 2023
b1703f7
minor fixes
G33kFreak Aug 26, 2023
32b7162
Change the comparing logic in didUpdateWidget
G33kFreak Aug 26, 2023
60e9e4a
Removed unnecessary logic
G33kFreak Aug 26, 2023
f42ba99
Add draggable callbacks
G33kFreak Oct 1, 2023
e9b9046
Merge branch 'master' of https://github.com/G33kFreak/fl_chart
G33kFreak Oct 1, 2023
77646e4
Add initializations for drag feature callbacks
G33kFreak Oct 1, 2023
8f8a851
Add draggable logic for LineChart
G33kFreak Aug 26, 2023
76348e3
Remake logic of updating spot's position and adjust tests
G33kFreak Aug 26, 2023
539cca8
Add initializations for drag feature callbacks
G33kFreak Oct 1, 2023
7875962
Merge branch 'master' of https://github.com/G33kFreak/fl_chart
G33kFreak Oct 1, 2023
cb51189
Consider fraction digits when formatting chart side titles, #1267
imaNNeo Oct 7, 2023
621b79e
Update CHANGELOG.md
imaNNeo Oct 7, 2023
e3910de
Bump version to 0.64.0
imaNNeo Oct 7, 2023
7918942
Fix macOS debug network issue
imaNNeo Oct 7, 2023
b99169b
Fix radar chart sample 1 overflow
imaNNeo Oct 7, 2023
396b2e1
Upgrade example/ dependencies
imaNNeo Oct 7, 2023
79e6ec7
Open flchart.dev when user clicks on the fl_chart logo
imaNNeo Oct 7, 2023
ab88658
Added gradient to FlLine
Dartek12 Sep 24, 2023
696bb2d
Updated docs
Dartek12 Oct 1, 2023
c8dc3c0
Mention the issue id in the CHANGELOG.md file
imaNNeo Nov 5, 2023
07c3d85
Fix iOS, macOS build issues
imaNNeo Nov 11, 2023
ba68c76
Fix bar line shadow crash when we have only one (or zero) spot, #1466
imaNNeo Nov 11, 2023
3c30cc3
Fix having negative `toY` (or positive `fromY`) in BarChart's `minY` …
imaNNeo Nov 11, 2023
b3529d3
Remove the changelog section from CONTRIBUTING. Because it is a file …
imaNNeo Nov 12, 2023
f0b387d
Fix bargraph overflow
Oct 26, 2023
356b722
Fix documentation
bobatsar Oct 26, 2023
1334eb7
Add golden widget tests for bargraph overflow
Nov 13, 2023
24d0fc5
Update CHANGELOG.md
Nov 13, 2023
cd14e6d
Open fl_chart web page only when we're on the web, otherwise open the…
imaNNeo Nov 12, 2023
fb672b5
Add appVersion and fl_chart version on the bottom of app menu
imaNNeo Nov 13, 2023
dcb1188
Add flutter version veriable in the gh-pages.yml
imaNNeo Nov 13, 2023
0aaf7ee
Fix format
imaNNeo Nov 13, 2023
87f0444
Add tryToLaunchUrl function in the app_utils to have a single place t…
imaNNeo Nov 13, 2023
0ae88cf
Remove flaky golden tests, I need to learn more about them later
imaNNeo Nov 21, 2023
4a2924a
Use TextScaler textScaler insted of double textScale to get rid of th…
imaNNeo Nov 21, 2023
d173cc0
Add dashArray parameter to BarChartRodData
k0psutin Oct 16, 2023
98d10e5
Make border draw last and use toDashedPath to draw the border
k0psutin Oct 16, 2023
1f1834a
Add new sample to show the dashed border
k0psutin Oct 16, 2023
d9a0efb
Update BarChartRodData documentation
k0psutin Oct 16, 2023
5151fc2
Create two tests to test borders
k0psutin Oct 16, 2023
4424452
Fix @kopsutin -> @k0psutin
k0psutin Oct 16, 2023
2955cef
Rename dashArray to borderDashArray
k0psutin Nov 16, 2023
0d4faea
Fix tests
k0psutin Nov 21, 2023
c911024
chore(handle_animations.md):replace = with :
polonski Nov 22, 2023
2b7be70
Allow to show single point line in LineChart, #1438
imaNNeo Nov 24, 2023
4a9bd3f
Fix flutter version issue in gh-pages.yml
imaNNeo Nov 24, 2023
40caf90
Change the drawer header logic to always open the landing page
imaNNeo Nov 24, 2023
df87e97
Open flutter release page when user clicks on the fl_chart version on…
imaNNeo Nov 24, 2023
9877450
Remove buy me a coffe button in the app
imaNNeo Nov 24, 2023
803409c
Bump version to 0.65.0
imaNNeo Nov 24, 2023
6b52a4e
Add pubspec constraints for newer version of flutter/dart, #1509
imaNNeo Dec 1, 2023
22b02c3
Use FlDotPainter to draw scatter spots
imaNNeo Dec 3, 2023
bb7b885
Update CHANGELOG.md
imaNNeo Dec 3, 2023
a99a3dd
Fix some test issues
imaNNeo Dec 3, 2023
256e2cc
Implement lerp method for FlDotPainter
imaNNeo Dec 3, 2023
ae7432f
Define customized hitTest for different FlDotPainters
imaNNeo Dec 3, 2023
9e133f1
Move FlDotPainter from line_chart_data to axis_chart_data
imaNNeo Dec 3, 2023
c1a5189
Generate new mock files
imaNNeo Dec 3, 2023
2f6a0e1
Update CHANGELOG.md to add migration guide
imaNNeo Dec 3, 2023
2bb9c81
Add a feature to allow user to select a painterType in ScatterChartSa…
imaNNeo Dec 3, 2023
e9ab493
Fix barChart tooltip for values below or above the 0 point, #1462
imaNNeo Dec 3, 2023
68cf161
Fix pieChart drawing single section on iPhone, #1515
imaNNeo Dec 15, 2023
08107bf
Fix test fail issue
imaNNeo Dec 15, 2023
bcd4aef
Add gradient property to the HorizontalLine and VerticalLine, #1525
imaNNeo Dec 25, 2023
bee1cf3
Update CHANGELOG.md
imaNNeo Dec 25, 2023
6f9c859
Fix format issue
imaNNeo Dec 25, 2023
950ac7f
Update base_chart.md docs
imaNNeo Dec 25, 2023
2482719
Rename all /master to /main
imaNNeo Dec 25, 2023
4c1c27a
Update handle_animations.md
LIZA1608 Dec 24, 2023
944bef4
Add gradient for PieChart
RAldhafeeri Dec 18, 2023
d3d06d1
update docs for pie chart
RAldhafeeri Dec 25, 2023
494a51f
Update pie_chart.md with gradient usage clarification
RAldhafeeri Dec 25, 2023
00c1cd6
Update CHANGELOG.md
imaNNeo Dec 25, 2023
001fd68
Update CHANGELOG.md
imaNNeo Dec 25, 2023
5f92d72
Update flutter version to remove the upper bound (to fix the flutter …
imaNNeo Dec 25, 2023
c0ca436
Bump version to 0.66.0
imaNNeo Dec 25, 2023
d116601
Upgrade Kotlin version from 1.6.10 to 1.7.10
Dec 26, 2023
4838f8b
Fix PieChart blackout issue, #1538
imaNNeo Jan 10, 2024
ba2f03f
Add some new brandings in README.md
imaNNeo Jan 13, 2024
5f31d52
Fix readme issue
imaNNeo Jan 13, 2024
3858b0b
Decrease thumbnail video size
imaNNeo Jan 13, 2024
50def3d
Fix memory leak in LineChart and BarChart, #1106
imaNNeo Jan 22, 2024
122a9e3
Update CHANGELOG.md
imaNNeo Jan 22, 2024
294f250
Minor change on .codecov.yml
imaNNeo Jan 22, 2024
90b615e
Bump version to 0.66.1
imaNNeo Jan 22, 2024
c75c6ea
Minor fix on README.md
imaNNeo Jan 22, 2024
0c75173
Fix README [ issue
imaNNeo Jan 22, 2024
4b144a0
Use <a> tag for link in README
imaNNeo Jan 22, 2024
2ff658b
Update README.md
r3za13 Jan 27, 2024
46dd5ad
Fix unsupported environment access on web
julianscheel Jan 25, 2024
75a3b98
Update bar_chart.md
adamsocrat Feb 7, 2024
2d21444
Update pie_chart.md
adamsocrat Feb 7, 2024
c2e634d
Update radar_chart.md
adamsocrat Feb 7, 2024
eb82f1c
- removed dependency to dart:io
stwarwas Feb 6, 2024
09d1944
Update README.md
imaNNeo Feb 10, 2024
5a01893
Bump version to 0.66.2
imaNNeo Feb 10, 2024
fb7cf1b
Add sponsors section
imaNNeo Mar 3, 2024
16d92d7
add the option to display the text vertically for a FLineLabel
julien4215 Feb 4, 2024
9c28a8a
edit changelog
julien4215 Feb 10, 2024
dbaa832
edit test for coverage
julien4215 Feb 4, 2024
288852a
WIP test
julien4215 Feb 9, 2024
1d1e137
end unit tests
julien4215 Feb 9, 2024
2c7295b
make code more understandable
julien4215 Feb 9, 2024
bf36544
fix : label always inside chart
julien4215 Feb 10, 2024
a540582
add direction in lerp method
julien4215 Feb 29, 2024
34a6af0
Update CHANGELOG.md
imaNNeo Mar 28, 2024
cdfdb66
Dynamically changing tooltipBgColor
xvrh Feb 16, 2024
0d486c8
Adrresses review comments
xvrh Mar 28, 2024
bba7394
Typo in changelog
xvrh Mar 28, 2024
bfdd91b
Add migration guide
xvrh Mar 28, 2024
e170af8
Fix scatter_chart_sample2 color contrast
imaNNeo Mar 28, 2024
2d0ce64
Update CHANGELOG.md
imaNNeo Mar 28, 2024
f82c496
Bump version to 0.67.0
imaNNeo Mar 28, 2024
930ac12
Update CHANGELOG.md
imaNNeo Mar 28, 2024
033e367
Add draggable logic for LineChart
G33kFreak Aug 26, 2023
dd241fb
Remake logic of updating spot's position and adjust tests
G33kFreak Aug 26, 2023
b060874
Add dragSpotUpdatedCallback functionality
G33kFreak Aug 26, 2023
b481923
Update changelog
G33kFreak Aug 26, 2023
0cd3590
Add sample of draggable line chart
G33kFreak Aug 26, 2023
cd3e422
minor fixes
G33kFreak Aug 26, 2023
fc35f04
Change the comparing logic in didUpdateWidget
G33kFreak Aug 26, 2023
74f7ed5
Removed unnecessary logic
G33kFreak Aug 26, 2023
c298aac
Add draggable callbacks
G33kFreak Oct 1, 2023
ba18217
Add draggable logic for LineChart
G33kFreak Aug 26, 2023
b476743
Remake logic of updating spot's position and adjust tests
G33kFreak Aug 26, 2023
0d5026c
Add initializations for drag feature callbacks
G33kFreak Oct 1, 2023
e4509e0
Add draggable logic for LineChart
G33kFreak Aug 26, 2023
a325175
Remake logic of updating spot's position and adjust tests
G33kFreak Aug 26, 2023
916c1e1
Change the comparing logic in didUpdateWidget
G33kFreak Aug 26, 2023
9d283be
Removed unnecessary logic
G33kFreak Aug 26, 2023
f664065
Merge branch 'master' of https://github.com/G33kFreak/fl_chart
G33kFreak Mar 29, 2024
6870cd4
fix example
G33kFreak Mar 29, 2024
04f18f2
Fix after rebase logic
G33kFreak Mar 29, 2024
9357bf2
Fix tests
G33kFreak Mar 29, 2024
b60e4fe
Adjust data tests
G33kFreak Mar 29, 2024
2ae1234
Update CHANGELOG
G33kFreak Mar 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* **BUGFIX** (by @Anas35) Fix Tooltip not displaying when value from BackgroundBarChartRodData is less than zero. #1345.
* **BUGFIX** (by @imaNNeo) Fix Negative BarChartRodStackItem are not drawn correctly bug, #1347
* **BUGFIX** (by @imaNNeo) Fix bar_chart_helper minY calculation bug, #1388
* **FEATURE** (by @G33kFreak) Add drag spots feature for LineChart, [#1420](https://github.com/imaNNeo/fl_chart/issues/1420)

## 0.63.0
* **BUGFIX** (by @imaNNeo) Fix PieChart crash on web-renderer html by ignoring `sectionsSpace` when `Path.combine()` does not work (it's flutter engine [issue](https://github.com/flutter/flutter/issues/44572)), #955
Expand Down
2 changes: 2 additions & 0 deletions example/lib/presentation/samples/chart_samples.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'chart_sample.dart';
import 'line/line_chart_sample1.dart';
import 'line/line_chart_sample10.dart';
import 'line/line_chart_sample11.dart';
import 'line/line_chart_sample12.dart';
import 'line/line_chart_sample2.dart';
import 'line/line_chart_sample3.dart';
import 'line/line_chart_sample4.dart';
Expand Down Expand Up @@ -40,6 +41,7 @@ class ChartSamples {
LineChartSample(9, (context) => LineChartSample9()),
LineChartSample(10, (context) => const LineChartSample10()),
LineChartSample(11, (context) => const LineChartSample11()),
LineChartSample(12, (context) => const LineChartSample12()),
],
ChartType.bar: [
BarChartSample(1, (context) => BarChartSample1()),
Expand Down
132 changes: 132 additions & 0 deletions example/lib/presentation/samples/line/line_chart_sample12.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import 'package:fl_chart/fl_chart.dart';
import 'package:fl_chart_app/presentation/resources/app_colors.dart';
import 'package:flutter/material.dart';

class LineChartSample12 extends StatefulWidget {
const LineChartSample12({super.key});

@override
State<LineChartSample12> createState() => _LineChartSample12State();
}

class _LineChartSample12State extends State<LineChartSample12> {
List<FlSpot> get spots1 => [
const FlSpot(1, 3),
const FlSpot(2, 2),
const FlSpot(4, 5),
const FlSpot(6, 4),
];
List<FlSpot> get spots2 => [
const FlSpot(1, 1),
const FlSpot(2, 4),
const FlSpot(3, 6),
const FlSpot(5, 3),
];

UpdatedDragSpotsData? _lastUpdate;

void _onUpdated(UpdatedDragSpotsData newUpdate) {
setState(() {
_lastUpdate = newUpdate;
});
}

String get _formattedLastSpotValue {
if (_lastUpdate == null) {
return 'Updated spot: None';
}

final flSpot = _lastUpdate!.newSpots[_lastUpdate!.updatedSpotIndex];

return 'new X: ${flSpot.x.toStringAsFixed(2)}, new Y: ${flSpot.y.toStringAsFixed(2)}';
}

@override
Widget build(BuildContext context) {
return Column(
children: [
const SizedBox(height: 12),
Text(
'Last updated bar index: ${_lastUpdate?.updatedBarIndex ?? 'none'}',
style: const TextStyle(
color: AppColors.mainTextColor2,
fontSize: 12,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 12),
Text(
'Last updated spot index: ${_lastUpdate?.updatedSpotIndex ?? 'none'}',
style: const TextStyle(
color: AppColors.mainTextColor2,
fontSize: 12,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 12),
Text(
_formattedLastSpotValue,
style: const TextStyle(
color: AppColors.mainTextColor2,
fontSize: 12,
fontWeight: FontWeight.bold,
),
),
AspectRatio(
aspectRatio: 1,
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 24.0,
vertical: 10,
),
child: LayoutBuilder(builder: (context, constraints) {
return LineChart(
LineChartData(
minY: 0,
minX: 0,
maxX: 8,
maxY: 8,
lineTouchData: LineTouchData(
enabled: true,
handleBuiltInTouches: true,
dragSpotUpdateFinishedCallback: _onUpdated,
getTouchedSpotIndicator:
(LineChartBarData barData, List<int> spotIndexes) {
return spotIndexes.map((index) {}).toList();
},
touchTooltipData: LineTouchTooltipData(
tooltipBgColor: Colors.transparent,
getTooltipItems: (List<LineBarSpot> lineBarsSpot) {
return lineBarsSpot.map((lineBarSpot) {}).toList();
},
),
),
lineBarsData: [
LineChartBarData(
spots: spots1,
isCurved: true,
isDraggable: true,
),
LineChartBarData(
spots: spots2,
isCurved: true,
isDraggable: false,
color: Colors.red,
),
],
gridData: const FlGridData(show: false),
borderData: FlBorderData(
show: true,
border: Border.all(
color: AppColors.borderColor,
),
),
),
);
}),
),
),
],
);
}
}
19 changes: 19 additions & 0 deletions lib/src/chart/base/axis_chart/axis_chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,25 @@ abstract class AxisChartPainter<D extends AxisChartData>
return viewSize.height - (((spotY - data.minY) / deltaY) * viewSize.height);
}

/// Function converts pixelX and pixelY to the [FlSpot],
/// reversed logic of [getPixelY] and [getPixelX]
FlSpot getTouchedCoordinates(
double pixelX,
double pixelY,
Size viewSize,
PaintHolder<D> holder,
) {
final data = holder.data;
final deltaY = data.maxY - data.minY;
final deltaX = data.maxX - data.minX;

final x = ((pixelX * deltaX) / viewSize.width) + data.minX;
final y =
((viewSize.height - pixelY) * deltaY) / viewSize.height + data.minY;

return FlSpot(x, y);
}

/// With this function we can get horizontal
/// position for the tooltip.
double getTooltipLeft(
Expand Down
87 changes: 84 additions & 3 deletions lib/src/chart/line_chart/line_chart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:fl_chart/fl_chart.dart';
import 'package:fl_chart/src/chart/base/axis_chart/axis_chart_scaffold_widget.dart';
import 'package:fl_chart/src/chart/line_chart/line_chart_renderer.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

/// Renders a line chart as a widget, using provided [LineChartData].
Expand Down Expand Up @@ -40,10 +41,42 @@ class _LineChartState extends AnimatedWidgetBaseState<LineChart> {
/// but we need to keep the provided callback to notify it too.
BaseTouchCallback<LineTouchResponse>? _providedTouchCallback;

DragSpotUpdateFinishedCallback? _dragSpotUpdatedCallback;

final List<ShowingTooltipIndicators> _showingTouchedTooltips = [];

final Map<int, List<int>> _showingTouchedIndicators = {};

List<LineChartBarData> _lineBarsData = [];

/// Keeps index of bar and spot that currently is being dragging
(int barIndex, int spotIndex)? _draggingSpotIndexes;

bool get _isAnyDraggable =>
_lineBarsData.indexWhere((lineBarData) => lineBarData.isDraggable) != -1;

@override
void initState() {
_lineBarsData = List.from(
widget.data.lineBarsData
.map((lineBarData) => lineBarData.copyWith())
.toList(),
);
super.initState();
}

@override
void didUpdateWidget(covariant LineChart oldWidget) {
if (!listEquals(oldWidget.data.lineBarsData, widget.data.lineBarsData)) {
_lineBarsData = List.from(
widget.data.lineBarsData
.map((lineBarData) => lineBarData.copyWith())
.toList(),
);
}
super.didUpdateWidget(oldWidget);
}

@override
Widget build(BuildContext context) {
final showingData = _getData();
Expand Down Expand Up @@ -79,11 +112,16 @@ class _LineChartState extends AnimatedWidgetBaseState<LineChart> {
final lineTouchData = widget.data.lineTouchData;
if (lineTouchData.enabled && lineTouchData.handleBuiltInTouches) {
_providedTouchCallback = lineTouchData.touchCallback;
_dragSpotUpdatedCallback = lineTouchData.dragSpotUpdateFinishedCallback;
return widget.data.copyWith(
lineTouchData: widget.data.lineTouchData
.copyWith(touchCallback: _handleBuiltInTouch),
lineBarsData: _lineBarsData,
lineTouchData: widget.data.lineTouchData.copyWith(
touchCallback: _handleBuiltInTouch,
distanceCalculator: _isAnyDraggable ? vectorDistanceCalculator : null,
),
);
}

return widget.data;
}

Expand All @@ -94,8 +132,34 @@ class _LineChartState extends AnimatedWidgetBaseState<LineChart> {
if (!mounted) {
return;
}
_providedTouchCallback?.call(event, touchResponse);

// Cancel dragging
if (event is FlPanEndEvent || event is FlLongPressEnd) {
if (_draggingSpotIndexes != null) {
final (barIndex, spotIndex) = _draggingSpotIndexes!;
_dragSpotUpdatedCallback?.call(
UpdatedDragSpotsData(
barIndex,
spotIndex,
_lineBarsData[barIndex].spots,
),
);
}
setState(() {
_draggingSpotIndexes = null;
});
}

// if indexes of dragging spot exist, changes it's position
if (_draggingSpotIndexes != null) {
final (barIndex, spotIndex) = _draggingSpotIndexes!;
setState(() {
_lineBarsData[barIndex].spots[spotIndex] =
touchResponse!.touchedAxesPoint!;
});
}

_providedTouchCallback?.call(event, touchResponse);
if (!event.isInterestedForInteractions ||
touchResponse?.lineBarSpots == null ||
touchResponse!.lineBarSpots!.isEmpty) {
Expand All @@ -121,6 +185,23 @@ class _LineChartState extends AnimatedWidgetBaseState<LineChart> {
..clear()
..add(ShowingTooltipIndicators(sortedLineSpots));
});

// If there is needed event and any lineBar with .isDraggable flag exists,
// sets indexes of needed spot and starts dragging process.
if (event is FlPanStartEvent || event is FlLongPressStart) {
final barIndex = touchResponse.lineBarSpots?.first.barIndex;
final spotIndex = touchResponse.lineBarSpots?.first.spotIndex;

if (spotIndex != null && barIndex != null) {
final isDraggable = widget.data.lineBarsData[barIndex].isDraggable;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused about the design, we have an isDraggable flag in our LineChartBarData class, also we have DragSpotUpdateFinishedCallback? dragSpotUpdateFinishedCallback; in our LineTouchData.
(I know we might have something similar in other places, and I admit that is a bad design)

But for now, what if we keep these two variables beside each other in the LineTouchData class?
What do you think?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, we can add other kinds of callbacks for example dragSpotStartedCallback, dragSpotMovedCallback, and dragSpodFinishedCallback.

The other (perfect) solution is to touch the RenderBaseChart to add a DragGestureRecognizer to detect the gesture events using this predefined, then we can pass down the gesture events to the touchCallback event as a FlTouchEvent. This way, we can use the drag-drop feature in all kinds of charts with the same logic.

For example in LineChart, we can have a flag to do the spot drag-drop (that you already implemented) as an under-the-hood magic. Also, users can implement any other kind of interaction with the drag-drop feature.

What do you think?

Copy link
Author

@G33kFreak G33kFreak Oct 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comments! I like the 2nd idea and will research and play a little bit with it.

Copy link
Author

@G33kFreak G33kFreak Oct 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other (perfect) solution is to touch the RenderBaseChart to add a DragGestureRecognizer to detect the gesture events using this predefined, then we can pass down the gesture events to the touchCallback event as a FlTouchEvent. This way, we can use the drag-drop feature in all kinds of charts with the same logic.

For example in LineChart, we can have a flag to do the spot drag-drop (that you already implemented) as an under-the-hood magic. Also, users can implement any other kind of interaction with the drag-drop feature.

What do you think?

So, after some checks about the additional gesture recognizer, I realized, that it's already done :D

See, the DragGestureRecognizer is an abstract class.

abstract class DragGestureRecognizer extends OneSequenceGestureRecognizer {
    // ...Its code here
}

However, the package already implements PanGestureRecognizer in the RenderBaseChart which extends DragGestureRecognizer and has everything we need to implement some kind of similar features in other charts as well.

Based on that, we already have the needed gesture recognizer and this PR just creates the wanted under-the-hood magic for the LineChart, if I got you right.

I'm a bit confused about the design, we have an isDraggable flag in our LineChartBarData class, also we have DragSpotUpdateFinishedCallback? dragSpotUpdateFinishedCallback; in our LineTouchData. (I know we might have something similar in other places, and I admit that is a bad design)

But for now, what if we keep these two variables beside each other in the LineTouchData class? What do you think?

To be honest, I don't really know what would be better here. My intention was to allow users to implement a draggable feature for particular LineChartBarData. For example, we can have a few bars, but only one of them is going to be draggable. It's similar to isCurved flag to me.

lineBarsData: [
    LineChartBarData(
        spots: spots1,
        isCurved: true,
        isDraggable: true,
    ),
    LineChartBarData(
        spots: spots2,
        isCurved: true,
        isDraggable: false,
        color: Colors.red,
    ),
],

So that's why I put this flag there.

Also, we can add other kinds of callbacks for example dragSpotStartedCallback, dragSpotMovedCallback, and dragSpodFinishedCallback.

Definitely! Added it in the commit f42ba99.
Anyway, we have to pay users' attention to be careful with using setState in the dragSpotUpdateCallback. Would be good to have some notes in the docs :)


if (isDraggable) {
setState(() {
_draggingSpotIndexes = (barIndex, spotIndex);
});
}
}
}
}

@override
Expand Down