fix(kline): 数据量大时只取最近数据,让最新K线在屏幕中心

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-01-19 20:39:33 -08:00
parent a15ab7600f
commit f149c2a06a
1 changed files with 16 additions and 6 deletions

View File

@ -568,19 +568,29 @@ class _KlineChartWidgetState extends State<KlineChartWidget> {
});
}
// -
// - K线数据
// K线在屏幕中心
_VisibleData _getVisibleData(double chartWidth) {
if (widget.klines.isEmpty || chartWidth == 0) {
return _VisibleData(klines: [], startIndex: 0, candleWidth: _candleWidth);
}
// K线范围
final int startIndex = (_scrollX / _candleWidth).floor().clamp(0, widget.klines.length - 1);
final int visibleCount = (chartWidth / _candleWidth).ceil() + 1; // +1 K线可见
final int endIndex = math.min(startIndex + visibleCount, widget.klines.length);
// K线
final int halfScreenCount = (chartWidth / 2 / _candleWidth).ceil();
//
if (widget.klines.length <= halfScreenCount) {
return _VisibleData(
klines: widget.klines,
startIndex: 0,
candleWidth: _candleWidth,
);
}
// halfScreenCount
final int startIndex = widget.klines.length - halfScreenCount;
return _VisibleData(
klines: widget.klines.sublist(startIndex, endIndex),
klines: widget.klines.sublist(startIndex),
startIndex: startIndex,
candleWidth: _candleWidth,
);