fix(kline): 稀疏数据时让最新K线居中而非整组居中

修正居中逻辑:当K线数量少时,让最新的那根K线(最右边)显示在屏幕中央,而不是让整个K线组居中。

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

View File

@ -583,9 +583,14 @@ class _KlineChartWidgetState extends State<KlineChartWidget> {
final availableWidth = chartWidth - rightPadding - leftPadding;
final totalWidth = widget.klines.length * _candleWidth;
// K线总宽度小于可用宽度时K线居中显示
// = ( - K线总宽度) / 2
final leftOffset = totalWidth < availableWidth ? (availableWidth - totalWidth) / 2 : 0.0;
// K线总宽度小于可用宽度时K线
// K线位置 = totalWidth - candleWidth/2K线的中心点
// = - K线位置 = availableWidth/2 - (totalWidth - candleWidth/2)
double leftOffset = 0.0;
if (totalWidth < availableWidth) {
// K线居中 = - K线中心位置
leftOffset = (availableWidth / 2) - (totalWidth - _candleWidth / 2);
}
// K线范围
final int startIndex = (_scrollX / _candleWidth).floor().clamp(0, widget.klines.length - 1);