handler for subscribeVisibleLogicalRangeChange recall himself when adding klines even when shiftVisibleRangeOnNewBar is false
Lightweight Charts™ Version:
Steps/code to reproduce:
this.#chart.applyOptions({
// Option to prevent shift visible range on new bar when last bar is visible
// Works fine outside from subscribeVisibleLogicalRangeChange
shiftVisibleRangeOnNewBar: false,
});
// Handler for visible logical range changed
const handler = (logicalRange) => {
// Get a copy of the last two klines in chart
const lastTwo = [ {
time: this.#klines.at(-1).time + 24 * 3600,
open: this.#klines.at(-1).open,
high: this.#klines.at(-1).high,
low: this.#klines.at(-1).low,
close: this.#klines.at(-1).close
}, {
time: this.#klines.at(-2).time + 24 * 3600 * 3,
open: this.#klines.at(-2).open,
high: this.#klines.at(-2).high,
low: this.#klines.at(-2).low,
close: this.#klines.at(-2).close
} ];
// Add copy of last two klines
this.#klines = [ ... this.#klines, ... lastTwo ];
// Unsubscribe ( handler will be called in infinite loop without it )
this.#chart.timeScale().unsubscribeVisibleLogicalRangeChange(handler);
// Set klines // Also moves logical range to the right <-- How to prevent this ?
this.#seriesKline.setData(this.#klines);
// Using update() instead of setData() does the same
// this.#seriesKline.update(lastTwo[1]);
// Resubscribe
this.#chart.timeScale().subscribeVisibleLogicalRangeChange(handler);
};
// Subscribe
this.#chart.timeScale().subscribeVisibleLogicalRangeChange(handler);Actual behavior:
When the chart is scrolled manually to the left : the visible logical range moves to the right with a little jump because of the added klines.
Expected behavior:
When the chart is scrolled manually to the left : the visible logical range does not jump to the right because of the added klines.
Screenshots:
In this video the chart is always scrolled to the LEFT :: video 1
In this video the klines are added with the exact same configuration by clicking a button :: video 2
Klines cannot be added to the right in the same way they are added to the LEFT.
Their are multiple valid reason to do so, ie : showing a very old area on the chart without loading 30k to 100k klines and still allowing to load more kline while scrolling to the RIGHT.
Adding old klines to the LEFT is smooth. Adding kline to the RIGHT with this method makes the visible logical range jump to the right while scrolling.
Source: tradingview/lightweight-charts