Skip to content

Commit

Permalink
Fix FlSpot.nullSpot at the first of list bug, #912.
Browse files Browse the repository at this point in the history
  • Loading branch information
imaNNeo committed Feb 23, 2022
1 parent cc3462a commit 94e186c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## newVersion
* **BUGFIX** Fix `FlSpot.nullSpot` at the first of list bug, #912.

## 0.45.0
* **BUGFIX** Fix `clipData` implementation in ScatterChart and LineChart, #897.
* **BUGFIX** Fix PieChart changing sections issue (we have disabled semantics for pieChart badgeWidgets), #861.
Expand Down
20 changes: 14 additions & 6 deletions lib/src/chart/line_chart/line_chart_helper.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import 'package:equatable/equatable.dart';
import 'package:fl_chart/fl_chart.dart';
import 'package:fl_chart/src/utils/list_wrapper.dart';

import 'line_chart_data.dart';

/// Contains anything that helps LineChart works
class LineChartHelper {
/// Contains List of cached results, base on [List<LineChartBarData>]
Expand Down Expand Up @@ -32,10 +31,19 @@ class LineChartHelper {
return LineChartMinMaxAxisValues(0, 0, 0, 0);
}

var minX = lineBarData.spots[0].x;
var maxX = lineBarData.spots[0].x;
var minY = lineBarData.spots[0].y;
var maxY = lineBarData.spots[0].y;
final FlSpot firstValidSpot;
try {
firstValidSpot =
lineBarData.spots.firstWhere((element) => element != FlSpot.nullSpot);
} catch (e) {
// There is no valid spot
return LineChartMinMaxAxisValues(0, 0, 0, 0);
}

var minX = firstValidSpot.x;
var maxX = firstValidSpot.x;
var minY = firstValidSpot.y;
var maxY = firstValidSpot.y;

for (var i = 0; i < lineBarsData.length; i++) {
final barData = lineBarsData[i];
Expand Down
26 changes: 26 additions & 0 deletions test/chart/line_chart/line_chart_helper_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,31 @@ void main() {
final result2 = LineChartHelper.calculateMaxAxisValues(lineBarsClone);
expect(result1, result2);
});

test('Test null spot 1', () {
final lineBars = [
LineChartBarData(spots: [
FlSpot.nullSpot,
FlSpot.nullSpot,
FlSpot.nullSpot,
FlSpot.nullSpot,
])
];
final result1 = LineChartHelper.calculateMaxAxisValues(lineBars);
expect(result1, LineChartMinMaxAxisValues(0, 0, 0, 0));
});

test('Test null spot 2', () {
final lineBars = [
LineChartBarData(spots: [
FlSpot.nullSpot,
const FlSpot(-1, 5),
FlSpot.nullSpot,
const FlSpot(4, -3),
])
];
final result1 = LineChartHelper.calculateMaxAxisValues(lineBars);
expect(result1, LineChartMinMaxAxisValues(-1, 4, -3, 5));
});
});
}

0 comments on commit 94e186c

Please sign in to comment.