Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fda019c

Browse files
committed
Added touchedStackItem and touchedStackItemIndex properties in the BarTouchedSpot to determine in which BarChartRodStackItem click happened, imaNNeo#393.
1 parent 95fd3d1 commit fda019c

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## {newVersion}
2-
* [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.
3-
* [Improvement] [BREAKING] renamed `rodStackItem` to `rodStackItems` in [BarChartRodData](https://github.com/imaNNeoFighT/fl_chart/blob/master/repo_files/documentations/bar_chart.md#barchartroddata).
2+
* [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.
3+
* [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.
4+
* [Improvement] [BREAKING] Renamed `rodStackItem` to `rodStackItems` in [BarChartRodData](https://github.com/imaNNeoFighT/fl_chart/blob/master/repo_files/documentations/bar_chart.md#barchartroddata).
45

56
## 0.10.1
67
* [Improvement] Show barGroups `x` value instead of `index` in bottom titles, #342.

lib/src/chart/bar_chart/bar_chart_data.dart

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -752,23 +752,35 @@ class BarTouchedSpot extends TouchedSpot with EquatableMixin {
752752
final BarChartRodData touchedRodData;
753753
final int touchedRodDataIndex;
754754

755+
/// It can be null, if nothing found
756+
final BarChartRodStackItem touchedStackItem;
757+
758+
/// It can be -1, if nothing found
759+
final int touchedStackItemIndex;
760+
755761
/// When touch happens, a [BarTouchedSpot] returns as a output,
756762
/// it tells you where the touch happened.
757-
/// [touchedBarGroup], and [touchedBarGroupIndex] tells you in which group touch happened,
758-
/// [touchedRodData], and [touchedRodDataIndex] tells you in which rod touch happened.
763+
/// [touchedBarGroup], and [touchedBarGroupIndex] tell you in which group touch happened,
764+
/// [touchedRodData], and [touchedRodDataIndex] tell you in which rod touch happened,
765+
/// [touchedStackItem], and [touchedStackItemIndex] tell you in which rod stack touch happened
766+
/// ([touchedStackItemIndex] means nothing found).
759767
/// You can also have the touched x and y in the chart as a [FlSpot] using [spot] value,
760768
/// and you can have the local touch coordinates on the screen as a [Offset] using [offset] value.
761769
BarTouchedSpot(
762770
BarChartGroupData touchedBarGroup,
763771
int touchedBarGroupIndex,
764772
BarChartRodData touchedRodData,
765773
int touchedRodDataIndex,
774+
BarChartRodStackItem touchedStackItem,
775+
int touchedStackItemIndex,
766776
FlSpot spot,
767777
Offset offset,
768778
) : touchedBarGroup = touchedBarGroup,
769779
touchedBarGroupIndex = touchedBarGroupIndex,
770780
touchedRodData = touchedRodData,
771781
touchedRodDataIndex = touchedRodDataIndex,
782+
touchedStackItem = touchedStackItem,
783+
touchedStackItemIndex = touchedStackItemIndex,
772784
super(spot, offset);
773785

774786
/// Used for equality check, see [EquatableMixin].
@@ -778,6 +790,8 @@ class BarTouchedSpot extends TouchedSpot with EquatableMixin {
778790
touchedBarGroupIndex,
779791
touchedRodData,
780792
touchedRodDataIndex,
793+
touchedStackItem,
794+
touchedStackItemIndex,
781795
spot,
782796
offset,
783797
];

lib/src/chart/bar_chart/bar_chart_painter.dart

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,20 @@ class BarChartPainter extends AxisChartPainter<BarChartData> with TouchHandler<B
689689
final nearestSpot = FlSpot(nearestGroup.x.toDouble(), nearestBarRod.y);
690690
final nearestSpotPos = Offset(barX, getPixelY(nearestSpot.y, chartViewSize));
691691

692-
return BarTouchedSpot(nearestGroup, i, nearestBarRod, j, nearestSpot, nearestSpotPos);
692+
int touchedStackIndex = -1;
693+
BarChartRodStackItem touchedStack;
694+
for (int stackIndex = 0; stackIndex < nearestBarRod.rodStackItems.length; stackIndex++) {
695+
final BarChartRodStackItem stackItem = nearestBarRod.rodStackItems[stackIndex];
696+
final fromPixel = getPixelY(stackItem.fromY, chartViewSize);
697+
final toPixel = getPixelY(stackItem.toY, chartViewSize);
698+
if (touchedPoint.dy <= fromPixel && touchedPoint.dy >= toPixel) {
699+
touchedStackIndex = stackIndex;
700+
touchedStack = stackItem;
701+
break;
702+
}
703+
}
704+
705+
return BarTouchedSpot(nearestGroup, i, nearestBarRod, j, touchedStack, touchedStackIndex, nearestSpot, nearestSpotPos);
693706
}
694707
}
695708
}

test/chart/data_pool.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2427,6 +2427,8 @@ final BarTouchedSpot barTouchedSpot1 = BarTouchedSpot(
24272427
1,
24282428
barChartRodData1,
24292429
2,
2430+
barChartRodStackItem1,
2431+
1,
24302432
flSpot1,
24312433
Offset.zero,
24322434
);
@@ -2435,6 +2437,8 @@ final BarTouchedSpot barTouchedSpot1Clone = BarTouchedSpot(
24352437
1,
24362438
barChartRodData1Clone,
24372439
2,
2440+
barChartRodStackItem1Clone,
2441+
1,
24382442
flSpot1Clone,
24392443
Offset.zero,
24402444
);
@@ -2443,6 +2447,8 @@ final BarTouchedSpot barTouchedSpot2 = BarTouchedSpot(
24432447
1,
24442448
barChartRodData1,
24452449
2,
2450+
barChartRodStackItem2,
2451+
2,
24462452
flSpot1,
24472453
Offset.zero,
24482454
);
@@ -2451,6 +2457,8 @@ final BarTouchedSpot barTouchedSpot3 = BarTouchedSpot(
24512457
1,
24522458
barChartRodData2,
24532459
2,
2460+
barChartRodStackItem2,
2461+
2,
24542462
flSpot1,
24552463
Offset.zero,
24562464
);
@@ -2459,6 +2467,8 @@ final BarTouchedSpot barTouchedSpot4 = BarTouchedSpot(
24592467
2,
24602468
barChartRodData1,
24612469
2,
2470+
barChartRodStackItem2,
2471+
2,
24622472
flSpot1,
24632473
Offset.zero,
24642474
);
@@ -2467,6 +2477,8 @@ final BarTouchedSpot barTouchedSpot5 = BarTouchedSpot(
24672477
1,
24682478
barChartRodData1,
24692479
3,
2480+
barChartRodStackItem2,
2481+
2,
24702482
flSpot1,
24712483
Offset.zero,
24722484
);
@@ -2475,6 +2487,8 @@ final BarTouchedSpot barTouchedSpot6 = BarTouchedSpot(
24752487
1,
24762488
barChartRodData1,
24772489
2,
2490+
barChartRodStackItem2,
2491+
2,
24782492
flSpot2,
24792493
Offset.zero,
24802494
);
@@ -2483,6 +2497,8 @@ final BarTouchedSpot barTouchedSpot7 = BarTouchedSpot(
24832497
1,
24842498
barChartRodData1,
24852499
2,
2500+
barChartRodStackItem2,
2501+
2,
24862502
flSpot1,
24872503
const Offset(1, 10),
24882504
);

0 commit comments

Comments
 (0)