Skip to content

Commit 8bce5d6

Browse files
committed
Add initialVisibleCandleCount parameter for default zoom level
1 parent d1bd294 commit 8bce5d6

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

lib/src/interactive_chart.dart

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@ class InteractiveChart extends StatefulWidget {
1414
///
1515
/// It needs to have at least 3 data points. If data is sufficiently large,
1616
/// the chart will default to display the most recent 90 data points when
17-
/// first opened, and allow user to freely zoom and pan however they like.
17+
/// first opened (configurable with [initialVisibleCandleCount] parameter),
18+
/// and allow users to freely zoom and pan however they like.
1819
final List<CandleData> candles;
1920

21+
/// The default number of data points to be displayed when the chart is first
22+
/// opened. The default value is 90. If [CandleData] does not have enough data
23+
/// points, the chart will display all of them.
24+
final int initialVisibleCandleCount;
25+
2026
/// If non-null, the style to use for this chart.
2127
final ChartStyle style;
2228

@@ -57,6 +63,7 @@ class InteractiveChart extends StatefulWidget {
5763
const InteractiveChart({
5864
Key? key,
5965
required this.candles,
66+
this.initialVisibleCandleCount = 90,
6067
ChartStyle? style,
6168
this.timeLabel,
6269
this.priceLabel,
@@ -66,6 +73,8 @@ class InteractiveChart extends StatefulWidget {
6673
}) : this.style = style ?? const ChartStyle(),
6774
assert(candles.length >= 3,
6875
"InteractiveChart requires 3 or more CandleData"),
76+
assert(initialVisibleCandleCount >= 3,
77+
"initialVisibleCandleCount must be more 3 or more"),
6978
super(key: key);
7079

7180
@override
@@ -264,8 +273,12 @@ class _InteractiveChartState extends State<InteractiveChart> {
264273
_getMaxStartOffset(w, _candleWidth),
265274
);
266275
} else {
267-
// Default 90 day chart. If data is shorter, we use the whole range.
268-
final count = min(widget.candles.length, 90);
276+
// Default zoom level. Defaults to a 90 day chart, but configurable.
277+
// If data is shorter, we use the whole range.
278+
final count = min(
279+
widget.candles.length,
280+
widget.initialVisibleCandleCount,
281+
);
269282
_candleWidth = w / count;
270283
// Default show the latest available data, e.g. the most recent 90 days.
271284
_startOffset = (widget.candles.length - count) * _candleWidth;

0 commit comments

Comments
 (0)