fix(kline): 修正最新K线居中逻辑
K线从屏幕左边开始画,通过leftOffset偏移使最新K线显示在展示区中央。 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
ccbea74f87
commit
5dfef3172e
|
|
@ -584,12 +584,16 @@ class _KlineChartWidgetState extends State<KlineChartWidget> {
|
|||
final totalWidth = widget.klines.length * _candleWidth;
|
||||
|
||||
// 计算左侧偏移量:当K线总宽度小于可用宽度时,让最新K线(最右边)居中显示
|
||||
// 最新K线位置 = totalWidth - candleWidth/2(最后一根K线的中心点)
|
||||
// 居中偏移 = 屏幕中心 - 最新K线位置 = availableWidth/2 - (totalWidth - candleWidth/2)
|
||||
// K线从左边开始画,第一根K线在 x=0,最后一根K线中心在 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线范围
|
||||
|
|
|
|||
Loading…
Reference in New Issue