fix(kline): 修正最新K线居中逻辑

K线从屏幕左边开始画,通过leftOffset偏移使最新K线显示在展示区中央。

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-01-19 19:57:32 -08:00
parent ccbea74f87
commit 5dfef3172e
1 changed files with 8 additions and 4 deletions

View File

@ -584,12 +584,16 @@ class _KlineChartWidgetState extends State<KlineChartWidget> {
final totalWidth = widget.klines.length * _candleWidth;
// K线总宽度小于可用宽度时K线
// K线位置 = totalWidth - candleWidth/2K线的中心点
// = - K线位置 = availableWidth/2 - (totalWidth - candleWidth/2)
// K线从左边开始画K线在 x=0K线中心在 x=(n-1)*candleWidth + candleWidth/2
// K线的中心位于屏幕中央 (availableWidth/2)
// leftOffset + (n-1)*candleWidth + candleWidth/2 = availableWidth/2
// leftOffset = availableWidth/2 - (n-1)*candleWidth - candleWidth/2
// leftOffset = availableWidth/2 - n*candleWidth + candleWidth/2
// leftOffset = (availableWidth - totalWidth + candleWidth) / 2
double leftOffset = 0.0;
if (totalWidth < availableWidth) {
// K线居中 = - K线中心位置
leftOffset = (availableWidth / 2) - (totalWidth - _candleWidth / 2);
final lastKlineCenter = (widget.klines.length - 1) * _candleWidth + _candleWidth / 2;
leftOffset = (availableWidth / 2) - lastKlineCenter;
}
// K线范围