Skip to content

Commit

Permalink
Fix getBestInitialIntervalValue() problem with values above or below …
Browse files Browse the repository at this point in the history
…zero, #893.
  • Loading branch information
imaNNeo committed Feb 4, 2022
1 parent 4917dc4 commit 9137f08
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 11 deletions.
9 changes: 8 additions & 1 deletion lib/src/chart/base/axis_chart/axis_chart_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class AxisChartHelper {
final initialValue =
Utils().getBestInitialIntervalValue(min, max, interval);
var axisSeek = initialValue;
if (!minIncluded && axisSeek == min) {
final firstPositionOverlapsWithMin = axisSeek == min;
if (!minIncluded && firstPositionOverlapsWithMin) {
axisSeek += interval;
}
final diff = max - min;
Expand All @@ -40,9 +41,15 @@ class AxisChartHelper {
!maxIncluded && lastPositionOverlapsWithMax ? max - interval : max;

final epsilon = interval / 100000;
if (minIncluded && !firstPositionOverlapsWithMin) {
action(min);
}
while (axisSeek <= end + epsilon) {
action(axisSeek);
axisSeek += interval;
}
if (maxIncluded && !lastPositionOverlapsWithMax) {
action(max);
}
}
}
22 changes: 19 additions & 3 deletions lib/src/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,28 @@ class Utils {
/// For example if we have -3 to +3, with interval 2. if we start from -3, we get something like this: -3, -1, +1, +3
/// But the most important point is zero in most cases. with this logic we get this: -2, 0, 2
double getBestInitialIntervalValue(double min, double max, double interval) {
if (min > 0 || max < 0) {
return min;
}
if (max - min <= interval) {
return min;
}

if (min > 0 || max < 0) {
var minIsNegative = false;
if (min < 0) {
minIsNegative = true;
}

final mod = min.abs() % interval;
if (mod == 0) {
return min;
}
final covered = (min.abs() ~/ interval) * interval;
final result = covered + mod + (interval - mod);
if (minIsNegative) {
return -result;
}
return result;
}

return interval * (min ~/ interval).toDouble();
}

Expand Down
2 changes: 1 addition & 1 deletion test/chart/bar_chart/bar_chart_painter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,7 @@ void main() {

barChartPainter.drawTitles(
_mockBuildContext, _mockCanvasWrapper, barGroupsPosition, holder);
expect(results.length, 14);
expect(results.length, 16);
});
});

Expand Down
22 changes: 21 additions & 1 deletion test/chart/base/axis_chart_helper_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ void main() {
results.add(axisValue);
},
);
expect(results.length, 4);
expect(results.length, 5);
expect(results[0], 0);
expect(results[1], 3);
expect(results[2], 6);
expect(results[3], 9);
expect(results[4], 10);
});

test('test 4', () {
Expand All @@ -87,5 +88,24 @@ void main() {
expect(results[1], 6);
expect(results[2], 9);
});

test('test 4', () {
List<double> results = [];
AxisChartHelper().iterateThroughAxis(
min: 35,
minIncluded: true,
max: 130,
maxIncluded: true,
interval: 50,
action: (axisValue) {
results.add(axisValue);
},
);
expect(results.length, 4);
expect(results[0], 35);
expect(results[1], 50);
expect(results[2], 100);
expect(results[3], 130);
});
});
}
6 changes: 3 additions & 3 deletions test/chart/scatter_chart/scatter_chart_painter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ void main() {
_mockCanvasWrapper,
holder,
);
verify(_mockCanvasWrapper.drawText(any, any, 0.0)).called(4);
verify(_mockCanvasWrapper.drawText(any, any, 0.0)).called(5);
});

test('test 3', () {
Expand Down Expand Up @@ -330,7 +330,7 @@ void main() {
_mockCanvasWrapper,
holder,
);
verify(_mockCanvasWrapper.drawText(any, any, 0.0)).called(7);
verify(_mockCanvasWrapper.drawText(any, any, 0.0)).called(8);
expect(leftTitlesCalledValues.contains(0.0), true);
expect(leftTitlesCalledValues.contains(5.0), true);
expect(leftTitlesCalledValues.contains(10.0), true);
Expand Down Expand Up @@ -396,7 +396,7 @@ void main() {
_mockCanvasWrapper,
holder,
);
verify(_mockCanvasWrapper.drawText(any, any, 0.0)).called(7);
verify(_mockCanvasWrapper.drawText(any, any, 0.0)).called(8);
expect(leftTitlesCalledValues.contains(0.0), true);
expect(leftTitlesCalledValues.contains(5.0), true);
expect(leftTitlesCalledValues.contains(10.0), true);
Expand Down
19 changes: 17 additions & 2 deletions test/utils/utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ void main() {
expect(Utils().getBestInitialIntervalValue(-3, 3, 1), -3);
expect(Utils().getBestInitialIntervalValue(-30, -20, 13), -30);
expect(Utils().getBestInitialIntervalValue(0, 13, 8), 0);
expect(Utils().getBestInitialIntervalValue(1, 13, 7), 1);
expect(Utils().getBestInitialIntervalValue(1, 13, 3), 1);
expect(Utils().getBestInitialIntervalValue(1, 13, 7), 7);
expect(Utils().getBestInitialIntervalValue(1, 13, 3), 3);
expect(Utils().getBestInitialIntervalValue(-1, 13, 3), 0);
expect(Utils().getBestInitialIntervalValue(-2, 13, 3), 0);
expect(Utils().getBestInitialIntervalValue(-3, 13, 3), -3);
Expand All @@ -183,6 +183,21 @@ void main() {
expect(Utils().getBestInitialIntervalValue(-3, 0, 2), -2);
expect(Utils().getBestInitialIntervalValue(-4, 0, 2), -4);
expect(Utils().getBestInitialIntervalValue(-0.5, 0.5, 2), -0.5);
expect(Utils().getBestInitialIntervalValue(35, 130, 50), 50);
expect(Utils().getBestInitialIntervalValue(49, 130, 50), 50);
expect(Utils().getBestInitialIntervalValue(50, 130, 50), 50);
expect(Utils().getBestInitialIntervalValue(60, 130, 50), 100);
expect(Utils().getBestInitialIntervalValue(110, 130, 50), 110);
expect(Utils().getBestInitialIntervalValue(90, 180, 50), 100);
expect(Utils().getBestInitialIntervalValue(100, 180, 50), 100);
expect(Utils().getBestInitialIntervalValue(110, 180, 50), 150);
expect(Utils().getBestInitialIntervalValue(170, 180, 50), 170);
expect(Utils().getBestInitialIntervalValue(-120, -10, 50), -150);
expect(Utils().getBestInitialIntervalValue(-110, -10, 50), -150);
expect(Utils().getBestInitialIntervalValue(-100, -10, 50), -100);
expect(Utils().getBestInitialIntervalValue(-90, -10, 50), -100);
expect(Utils().getBestInitialIntervalValue(-80, -10, 50), -100);
expect(Utils().getBestInitialIntervalValue(-150, -10, 50), -150);
});

test('test convertRadiusToSigma()', () {
Expand Down

0 comments on commit 9137f08

Please sign in to comment.