Skip to content

Commit

Permalink
Added touchedStackItem and touchedStackItemIndex properties in th…
Browse files Browse the repository at this point in the history
…e BarTouchedSpot to determine in which BarChartRodStackItem click happened, imaNNeo#393.
  • Loading branch information
imaNNeo authored and ezmegy committed Nov 16, 2020
1 parent ee9e479 commit b1a2469
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## {newVersion}
* [Improvement] allowed to have topTitles in the [BarChart](https://github.com/imaNNeoFighT/fl_chart/blob/master/repo_files/documentations/bar_chart.md), see [BarChartSample5](https://github.com/imaNNeoFighT/fl_chart/blob/master/repo_files/documentations/bar_chart.md#sample-5-source-code), #394.
* [Improvement] [BREAKING] renamed `rodStackItem` to `rodStackItems` in [BarChartRodData](https://github.com/imaNNeoFighT/fl_chart/blob/master/repo_files/documentations/bar_chart.md#barchartroddata).
* [Improvement] Allowed to have topTitles in the [BarChart](https://github.com/imaNNeoFighT/fl_chart/blob/master/repo_files/documentations/bar_chart.md), see [BarChartSample5](https://github.com/imaNNeoFighT/fl_chart/blob/master/repo_files/documentations/bar_chart.md#sample-5-source-code), #394.
* [Improvement] Added `touchedStackItem` and `touchedStackItemIndex` properties in the [BarTouchedSpot](https://github.com/imaNNeoFighT/fl_chart/blob/master/repo_files/documentations/bar_chart.md#bartouchedspot) to determine in which [BarChartRodStackItem](https://github.com/imaNNeoFighT/fl_chart/blob/master/repo_files/documentations/bar_chart.md#barchartrodstackitem) click happened, #393.
* [Improvement] [BREAKING] Renamed `rodStackItem` to `rodStackItems` in [BarChartRodData](https://github.com/imaNNeoFighT/fl_chart/blob/master/repo_files/documentations/bar_chart.md#barchartroddata).

## 0.10.1
* [Improvement] Show barGroups `x` value instead of `index` in bottom titles, #342.
Expand Down
18 changes: 16 additions & 2 deletions lib/src/chart/bar_chart/bar_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -752,23 +752,35 @@ class BarTouchedSpot extends TouchedSpot with EquatableMixin {
final BarChartRodData touchedRodData;
final int touchedRodDataIndex;

/// It can be null, if nothing found
final BarChartRodStackItem touchedStackItem;

/// It can be -1, if nothing found
final int touchedStackItemIndex;

/// When touch happens, a [BarTouchedSpot] returns as a output,
/// it tells you where the touch happened.
/// [touchedBarGroup], and [touchedBarGroupIndex] tells you in which group touch happened,
/// [touchedRodData], and [touchedRodDataIndex] tells you in which rod touch happened.
/// [touchedBarGroup], and [touchedBarGroupIndex] tell you in which group touch happened,
/// [touchedRodData], and [touchedRodDataIndex] tell you in which rod touch happened,
/// [touchedStackItem], and [touchedStackItemIndex] tell you in which rod stack touch happened
/// ([touchedStackItemIndex] means nothing found).
/// You can also have the touched x and y in the chart as a [FlSpot] using [spot] value,
/// and you can have the local touch coordinates on the screen as a [Offset] using [offset] value.
BarTouchedSpot(
BarChartGroupData touchedBarGroup,
int touchedBarGroupIndex,
BarChartRodData touchedRodData,
int touchedRodDataIndex,
BarChartRodStackItem touchedStackItem,
int touchedStackItemIndex,
FlSpot spot,
Offset offset,
) : touchedBarGroup = touchedBarGroup,
touchedBarGroupIndex = touchedBarGroupIndex,
touchedRodData = touchedRodData,
touchedRodDataIndex = touchedRodDataIndex,
touchedStackItem = touchedStackItem,
touchedStackItemIndex = touchedStackItemIndex,
super(spot, offset);

/// Used for equality check, see [EquatableMixin].
Expand All @@ -778,6 +790,8 @@ class BarTouchedSpot extends TouchedSpot with EquatableMixin {
touchedBarGroupIndex,
touchedRodData,
touchedRodDataIndex,
touchedStackItem,
touchedStackItemIndex,
spot,
offset,
];
Expand Down
15 changes: 14 additions & 1 deletion lib/src/chart/bar_chart/bar_chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,20 @@ class BarChartPainter extends AxisChartPainter<BarChartData> with TouchHandler<B
final nearestSpot = FlSpot(nearestGroup.x.toDouble(), nearestBarRod.y);
final nearestSpotPos = Offset(barX, getPixelY(nearestSpot.y, chartViewSize));

return BarTouchedSpot(nearestGroup, i, nearestBarRod, j, nearestSpot, nearestSpotPos);
int touchedStackIndex = -1;
BarChartRodStackItem touchedStack;
for (int stackIndex = 0; stackIndex < nearestBarRod.rodStackItems.length; stackIndex++) {
final BarChartRodStackItem stackItem = nearestBarRod.rodStackItems[stackIndex];
final fromPixel = getPixelY(stackItem.fromY, chartViewSize);
final toPixel = getPixelY(stackItem.toY, chartViewSize);
if (touchedPoint.dy <= fromPixel && touchedPoint.dy >= toPixel) {
touchedStackIndex = stackIndex;
touchedStack = stackItem;
break;
}
}

return BarTouchedSpot(nearestGroup, i, nearestBarRod, j, touchedStack, touchedStackIndex, nearestSpot, nearestSpotPos);
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions test/chart/data_pool.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2427,6 +2427,8 @@ final BarTouchedSpot barTouchedSpot1 = BarTouchedSpot(
1,
barChartRodData1,
2,
barChartRodStackItem1,
1,
flSpot1,
Offset.zero,
);
Expand All @@ -2435,6 +2437,8 @@ final BarTouchedSpot barTouchedSpot1Clone = BarTouchedSpot(
1,
barChartRodData1Clone,
2,
barChartRodStackItem1Clone,
1,
flSpot1Clone,
Offset.zero,
);
Expand All @@ -2443,6 +2447,8 @@ final BarTouchedSpot barTouchedSpot2 = BarTouchedSpot(
1,
barChartRodData1,
2,
barChartRodStackItem2,
2,
flSpot1,
Offset.zero,
);
Expand All @@ -2451,6 +2457,8 @@ final BarTouchedSpot barTouchedSpot3 = BarTouchedSpot(
1,
barChartRodData2,
2,
barChartRodStackItem2,
2,
flSpot1,
Offset.zero,
);
Expand All @@ -2459,6 +2467,8 @@ final BarTouchedSpot barTouchedSpot4 = BarTouchedSpot(
2,
barChartRodData1,
2,
barChartRodStackItem2,
2,
flSpot1,
Offset.zero,
);
Expand All @@ -2467,6 +2477,8 @@ final BarTouchedSpot barTouchedSpot5 = BarTouchedSpot(
1,
barChartRodData1,
3,
barChartRodStackItem2,
2,
flSpot1,
Offset.zero,
);
Expand All @@ -2475,6 +2487,8 @@ final BarTouchedSpot barTouchedSpot6 = BarTouchedSpot(
1,
barChartRodData1,
2,
barChartRodStackItem2,
2,
flSpot2,
Offset.zero,
);
Expand All @@ -2483,6 +2497,8 @@ final BarTouchedSpot barTouchedSpot7 = BarTouchedSpot(
1,
barChartRodData1,
2,
barChartRodStackItem2,
2,
flSpot1,
const Offset(1, 10),
);
Expand Down

0 comments on commit b1a2469

Please sign in to comment.