Skip to content

Commit 10d1b03

Browse files
committed
Fix a crash if volume numbers are the same
1 parent 27c7fc8 commit 10d1b03

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

example/lib/main.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class _MyAppState extends State<MyApp> {
7676
// overlayBackgroundColor: Colors.red[900]!.withOpacity(0.6),
7777
// overlayTextStyle: TextStyle(color: Colors.red[100]),
7878
// timeLabelHeight: 32,
79+
// volumeHeightFactor: 0.2, // volume area is 20% of total height
7980
// ),
8081
/** Customize axis labels */
8182
// timeLabel: (timestamp, visibleDataCount) => "📅",

lib/src/interactive_chart.dart

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,14 @@ class _InteractiveChartState extends State<InteractiveChart> {
150150
candlesInRange.map(highest).whereType<double>().reduce(max);
151151
final minPrice =
152152
candlesInRange.map(lowest).whereType<double>().reduce(min);
153-
final maxVol =
154-
candlesInRange.map((c) => c.volume).whereType<double>().reduce(max);
155-
final minVol =
156-
candlesInRange.map((c) => c.volume).whereType<double>().reduce(min);
153+
final maxVol = candlesInRange
154+
.map((c) => c.volume)
155+
.whereType<double>()
156+
.fold(double.negativeInfinity, max);
157+
final minVol = candlesInRange
158+
.map((c) => c.volume)
159+
.whereType<double>()
160+
.fold(double.infinity, min);
157161

158162
final child = TweenAnimationBuilder(
159163
tween: PainterParamsTween(

lib/src/painter_params.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,22 @@ class PainterParams {
5959
double fitVolume(double y) {
6060
final gap = 12; // the gap between price bars and volume bars
6161
final baseAmount = 2; // display at least "something" for the lowest volume
62+
63+
if (maxVol == minVol) {
64+
// Apparently max and min values (in the current visible range, at least)
65+
// are the same. It's likely they passed in a bunch of zeroes, because
66+
// they don't have real volume data or don't want to draw volumes.
67+
assert(() {
68+
if (style.volumeHeightFactor != 0) {
69+
print('If you do not want to show volumes, '
70+
'make sure to set `volumeHeightFactor` (ChartStyle) to zero.');
71+
}
72+
return true;
73+
}());
74+
// Since they are equal, we just draw all volume bars as half height.
75+
return priceHeight + volumeHeight / 2;
76+
}
77+
6278
final volGridSize = (volumeHeight - baseAmount - gap) / (maxVol - minVol);
6379
final vol = (y - minVol) * volGridSize;
6480
return volumeHeight - vol + priceHeight - baseAmount;

0 commit comments

Comments
 (0)