Get additional metadata from lineTouchData
- Dominant language
- Dart
- Stars
- 7.6k
- Forks
- 2k
- Avg merge
- 9d 1h
- Merged PRs (30d)
- 2
Description
### Discussed in https://github.com/imaNNeoFighT/fl_chart/discussions/1133
Originally posted by **KapitanPL** September 3, 2022
Hi Guys,
I generate my LineChartBarData based on model.
I then want to let user interact with the model via LineTouchData.
`LineTouchData.lineBarSpots!.first.bar` seemed to reference the LineChartBarData.
So I tried to extend LineChartBarData to contain my metadata and set this extended object as LineChartBarData. It works, data is displayed.
```dart
class LineBarDataMeta extends LineChartBarData {
LineBarDataMeta(
{
...
required this.displayed,
required this.dataKey});
bool displayed;
String dataKey;
}
```
However when I try:
```dart
lineTouchData: LineTouchData(
touchCallback: (p0, p1) {
if (p0 is FlTapDownEvent &&
p1 is LineTouchResponse &&
p1.lineBarSpots != null) {
var data = p1.lineBarSpots!.first;
if (p1.lineBarSpots!.first.bar is LineBarDataMeta) {
var spots = (p1.lineBarSpots!.first.bar as LineBarDataMeta);
print("key: ${spots.dataKey}");
}
print("touch!: ${p1.lineBarSpots}");
}
},
)
```
the `if (p1.lineBarSpots!.first.bar is LineBarDataMeta) ` part is never true.
How else can I pass some metadata to the lineTouchData.touchCallback in order to get it when processing the callback?
Edit:
Okay, I get it. There is `p1.lineBarSpots![n].barIndex`
The problem is that if you have two or more spots with the same X-coordinate you do not know which one you tap.
They are ordered by y-value.
And moreover the FlTapDownEvent has coordinates in "screen" coordinates, however the spot is in the chart coordinates. (x/y values).
This is going to be a nightmare!
How to know which one of the spots I clicked?
Contributor guide
Research direction
Start with the linked discussion and the LineTouchData touchCallback, LineTouchResponse, and lineBarSpots entry points described in the issue. Determine how a callback can identify the exact tapped spot when X-coordinates overlap and expose associated metadata; done means the selection is unambiguous and covered by relevant tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- dart, flutter
- Domain
- frontend
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100