fix(kline): 数据量大时只取最近数据,让最新K线在屏幕中心
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
a15ab7600f
commit
f149c2a06a
|
|
@ -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,
|
||||
);
|
||||
|
|
|
|||
Loading…
Reference in New Issue