Skip to content

Commit

Permalink
Fix bar_chart_helper minY calculation bug, #1388
Browse files Browse the repository at this point in the history
  • Loading branch information
imaNNeo committed Aug 21, 2023
1 parent 5d81d61 commit 43b89e4
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 73 deletions.
2 changes: 1 addition & 1 deletion example/macos/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 353c8bcc5d5b0994e508d035b5431cfe18c1dea7

COCOAPODS: 1.12.0
COCOAPODS: 1.12.1
2 changes: 1 addition & 1 deletion example/macos/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0920;
LastUpgradeCheck = 1300;
LastUpgradeCheck = 1430;
ORGANIZATIONNAME = "";
TargetAttributes = {
33CC10EC2044A3C60003C045 = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1300"
LastUpgradeVersion = "1430"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
10 changes: 5 additions & 5 deletions lib/src/chart/bar_chart/bar_chart_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class BarChartHelper {
}

var maxY = barGroup.barRods[0].toY;
var minY = 0.0;
var minY = barGroup.barRods[0].fromY;

for (var i = 0; i < barGroups.length; i++) {
final barGroup = barGroups[i];
Expand All @@ -49,12 +49,12 @@ class BarChartHelper {
maxY = rod.backDrawRodData.toY;
}

if (rod.toY < minY) {
minY = rod.toY;
if (rod.fromY < minY) {
minY = rod.fromY;
}

if (rod.backDrawRodData.show && rod.backDrawRodData.toY < minY) {
minY = rod.backDrawRodData.toY;
if (rod.backDrawRodData.show && rod.backDrawRodData.fromY < minY) {
minY = rod.backDrawRodData.fromY;

Check warning on line 57 in lib/src/chart/bar_chart/bar_chart_helper.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/chart/bar_chart/bar_chart_helper.dart#L57

Added line #L57 was not covered by tests
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class AxisChartScaffoldWidget extends StatelessWidget {
border: data.borderData.isVisible() ? data.borderData.border : null,
),
child: chart,
)
),
];

int insertIndex(bool drawBelow) => drawBelow ? 0 : widgets.length;
Expand Down
48 changes: 46 additions & 2 deletions test/chart/bar_chart/bar_chart_helper_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ void main() {
BarChartRodData(toY: 10),
BarChartRodData(toY: 5),
],
)
),
];
final result = BarChartHelper.calculateMaxAxisValues(barGroups);
expect(result.minY, -40);
expect(result.minY, 0);
expect(result.maxY, 10);
});

Expand Down Expand Up @@ -78,5 +78,49 @@ void main() {
final result2 = BarChartMinMaxAxisValues(1, 11, readFromCache: true);
expect(result1, result2);
});

test('Test calculateMaxAxisValues with all positive values', () {
final barGroups = [
barChartGroupData1.copyWith(
barRods: barChartGroupData1.barRods
.map(
(rod) => rod.copyWith(
fromY: 5,
backDrawRodData: BackgroundBarChartRodData(show: false),
),
)
.toList(),
),
barChartGroupData2.copyWith(
barRods: barChartGroupData2.barRods
.map(
(rod) => rod.copyWith(
fromY: 8,
backDrawRodData: BackgroundBarChartRodData(show: false),
),
)
.toList(),
),
];
final result1 = BarChartHelper.calculateMaxAxisValues(barGroups);
expect(result1.minY, 5);
});

test('Test calculateMaxAxisValues with all negative values', () {
final barGroups = [
barChartGroupData1.copyWith(
barRods: barChartGroupData1.barRods
.map((rod) => rod.copyWith(fromY: -5))
.toList(),
),
barChartGroupData2.copyWith(
barRods: barChartGroupData2.barRods
.map((rod) => rod.copyWith(fromY: -8))
.toList(),
),
];
final result1 = BarChartHelper.calculateMaxAxisValues(barGroups);
expect(result1.minY, -8);
});
});
}
94 changes: 41 additions & 53 deletions test/chart/bar_chart/bar_chart_painter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ void main() {
28.5,
0,
38.5,
76.9,
100,
const Radius.circular(0.1),
),
),
Expand All @@ -402,9 +402,9 @@ void main() {
results[1]['rRect'] as RRect,
RRect.fromLTRBR(
43.5,
15.4,
20,
54.5,
76.9,
100,
const Radius.circular(0.2),
),
),
Expand All @@ -417,9 +417,9 @@ void main() {
results[2]['rRect'] as RRect,
RRect.fromLTRBR(
59.5,
15.4,
20,
71.5,
76.9,
100,
const Radius.circular(0.3),
),
),
Expand All @@ -434,7 +434,7 @@ void main() {
81.5,
0,
91.5,
76.9,
100,
const Radius.circular(0.4),
),
),
Expand All @@ -445,9 +445,9 @@ void main() {
results[4]['rRect'] as RRect,
RRect.fromLTRBR(
96.5,
15.4,
20,
106.5,
76.9,
100,
const Radius.circular(5),
),
),
Expand All @@ -459,9 +459,9 @@ void main() {
results[5]['rRect'] as RRect,
RRect.fromLTRBR(
116.5,
15.4,
20,
126.5,
76.9,
100,
const Radius.circular(5),
),
),
Expand All @@ -475,7 +475,7 @@ void main() {
116.5,
0,
126.5,
76.9,
100,
const Radius.circular(5),
),
),
Expand All @@ -486,9 +486,9 @@ void main() {
results[7]['rRect'] as RRect,
RRect.fromLTRBR(
131.5,
15.4,
20,
141.5,
76.9,
100,
const Radius.circular(5),
),
),
Expand All @@ -500,9 +500,9 @@ void main() {
results[8]['rRect'] as RRect,
RRect.fromLTRBR(
146.5,
76.9,
156.5,
100,
156.5,
130,
const Radius.circular(5),
),
),
Expand All @@ -514,9 +514,9 @@ void main() {
results[9]['rRect'] as RRect,
RRect.fromLTRBR(
146.5,
15.4,
20,
156.5,
76.9,
100,
const Radius.circular(5),
),
),
Expand All @@ -527,9 +527,9 @@ void main() {
results[10]['rRect'] as RRect,
RRect.fromLTRBR(
161.5,
15.4,
20,
171.5,
76.9,
100,
const Radius.circular(5),
),
),
Expand Down Expand Up @@ -647,9 +647,9 @@ void main() {
results[0]['rrect'] as RRect,
RRect.fromLTRBR(
84,
65,
76.5,
94,
66.7,
78.4,
const Radius.circular(0.1),
),
),
Expand All @@ -662,9 +662,9 @@ void main() {
results[1]['rrect'] as RRect,
RRect.fromLTRBR(
83.5,
68.3,
80.4,
94.5,
83.3,
98,
const Radius.circular(0.2),
),
),
Expand All @@ -677,9 +677,9 @@ void main() {
results[2]['rrect'] as RRect,
RRect.fromLTRBR(
83,
85,
95,
100,
95,
117.6,
const Radius.circular(0.3),
),
),
Expand All @@ -692,9 +692,9 @@ void main() {
results[3]['rrect'] as RRect,
RRect.fromLTRBR(
106,
33.3,
39.2,
116,
35,
41.2,
const Radius.circular(0.1),
),
),
Expand All @@ -705,9 +705,9 @@ void main() {
results[4]['rrect'] as RRect,
RRect.fromLTRBR(
105.5,
16.7,
19.6,
116.5,
31.7,
37.3,
const Radius.circular(0.2),
),
),
Expand All @@ -721,7 +721,7 @@ void main() {
105,
0,
117,
15,
17.6,
const Radius.circular(0.3),
),
),
Expand Down Expand Up @@ -1619,7 +1619,7 @@ void main() {
color: const Color(0x00000000),
borderRadius: const BorderRadius.all(Radius.circular(0.1)),
rodStackItems: [
BarChartRodStackItem(0, 5, const Color(0xFF0F0F0F))
BarChartRodStackItem(0, 5, const Color(0xFF0F0F0F)),
],
),
],
Expand Down Expand Up @@ -1666,35 +1666,30 @@ void main() {
null,
);
expect(
painter.handleTouch(const Offset(91.3, 55.3), viewSize, holder),
painter.handleTouch(const Offset(100.4, 21.2), viewSize, holder),
null,
);
expect(
painter.handleTouch(const Offset(100.4, 21.2), viewSize, holder),
painter.handleTouch(const Offset(80.1, 22), viewSize, holder),
null,
);

expect(
painter.handleTouch(const Offset(80.1, 22), viewSize, holder),
painter.handleTouch(const Offset(110.1, 70.2), viewSize, holder),
null,
);

final result1 =
painter.handleTouch(const Offset(110.1, 70.2), viewSize, holder);
expect(result1!.touchedBarGroupIndex, 1);
painter.handleTouch(const Offset(89, 38.5), viewSize, holder);
expect(result1!.touchedBarGroupIndex, 0);
expect(result1.touchedRodDataIndex, 0);
expect(result1.touchedStackItemIndex, -1);

final result2 =
painter.handleTouch(const Offset(89, 38.5), viewSize, holder);
painter.handleTouch(const Offset(88.8, 16.5), viewSize, holder);
expect(result2!.touchedBarGroupIndex, 0);
expect(result2.touchedRodDataIndex, 0);
expect(result2.touchedStackItemIndex, 0);

final result3 =
painter.handleTouch(const Offset(88.8, 16.5), viewSize, holder);
expect(result3!.touchedBarGroupIndex, 0);
expect(result3.touchedRodDataIndex, 0);
expect(result3.touchedStackItemIndex, -1);
expect(result2.touchedStackItemIndex, -1);
});

test('test 3', () {
Expand Down Expand Up @@ -2147,17 +2142,10 @@ void main() {
holder,
);

expect(results.length, 2);
expect(results.length, 1);

expect(results[0]['paint_color'], Colors.cyanAccent.value);
expect(results[0]['paint_stroke_width'], 90);
expect(results[0]['from'], const Offset(0, 300));
expect(results[0]['to'], const Offset(400, 300));

expect(results[1]['paint_color'], Colors.cyanAccent.value);
expect(results[1]['paint_stroke_width'], 100);
expect(results[1]['from'], const Offset(0, 392));
expect(results[1]['to'], const Offset(400, 392));

Utils.changeInstance(utilsMainInstance);
});
Expand All @@ -2174,7 +2162,7 @@ void main() {
BarChartRodData(fromY: 1, toY: 10),
BarChartRodData(fromY: 2, toY: 10),
],
)
),
],
titlesData: const FlTitlesData(show: false),
extraLinesData: ExtraLinesData(
Expand Down
Loading

0 comments on commit 43b89e4

Please sign in to comment.