fix(kline): 修正K线显示逻辑,从左开始排列,最新K线居中

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

View File

@ -70,39 +70,43 @@ class _KlineChartWidgetState extends State<KlineChartWidget> {
// LayoutBuilder // LayoutBuilder
} }
/// K 线 K 线 /// K 线
///
///
/// - K线从左边开始排列
/// -
/// - K线在屏幕中心
void _initializeCandleWidth(double chartWidth) { void _initializeCandleWidth(double chartWidth) {
if (_initialized || widget.klines.isEmpty || chartWidth == 0) return; if (_initialized || widget.klines.isEmpty || chartWidth == 0) return;
_initialized = true; _initialized = true;
_chartWidth = chartWidth; _chartWidth = chartWidth;
// K 线 // K线宽度
final idealWidth = chartWidth / widget.klines.length; // _candleWidth 8.0
//
_candleWidth = idealWidth.clamp(_minCandleWidth, _maxCandleWidth);
_prevCandleWidth = _candleWidth; _prevCandleWidth = _candleWidth;
// K 线 // K线在屏幕中心
_scrollToCenter(); _scrollToCenter();
} }
/// 使 K 线 /// 使 K 线
/// ///
/// ///
/// - K 线 /// - K线从左边开始排列
/// - K 线 /// - <=
/// - K线在屏幕中心
void _scrollToCenter() { void _scrollToCenter() {
if (widget.klines.isEmpty || _chartWidth == 0) return; if (widget.klines.isEmpty || _chartWidth == 0) return;
final totalWidth = widget.klines.length * _candleWidth; final totalWidth = widget.klines.length * _candleWidth;
if (totalWidth <= _chartWidth) { if (totalWidth <= _chartWidth) {
// K 线 //
_scrollX = 0; _scrollX = 0;
} else { } else {
// K 线 // K线在屏幕中心的滚动位
// K 线= (K线数量 - 0.5) * K线宽度 // K线的中心位置 = (K线数量 - 0.5) * K线宽度
final lastKlineCenter = (widget.klines.length - 0.5) * _candleWidth; final lastKlineCenter = (widget.klines.length - 0.5) * _candleWidth;
// = K线中心 - // = K线中心 -
final targetScroll = lastKlineCenter - _chartWidth / 2; final targetScroll = lastKlineCenter - _chartWidth / 2;