Visible range becomes null and chart is empty after setData on a series that was setData twice before another series was added
Lightweight Charts™ Version: 5.2.1 (also 5.0.9)
Steps/code to reproduce:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="https://unpkg.com/[email protected]/dist/lightweight-charts.standalone.production.js"></script>
</head>
<body>
<div id="chart" style="width: 800px; height: 400px"></div>
<pre id="out"></pre>
<script>
const chart = LightweightCharts.createChart(document.getElementById('chart'), { width: 800, height: 400 });
const data = Array.from({ length: 200 }, (_, i) => ({
time: 1600000000 + i * 86400, open: 100 + i, high: 102 + i, low: 99 + i, close: 101 + i,
}));
const log = (s) => { document.getElementById('out').textContent += s + '\n'; };
const range = () => JSON.stringify(chart.timeScale().getVisibleLogicalRange());
const candles = chart.addSeries(LightweightCharts.CandlestickSeries, {});
candles.setData(data.map(d => ({ ...d })));
candles.setData(data.map(d => ({ ...d }))); // same data a second time
log('after 2 candle setData: ' + range());
const line = chart.addSeries(LightweightCharts.LineSeries, {});
line.setData(data.map(d => ({ time: d.time, value: d.close })));
log('after line setData: ' + range());
candles.setData(data.map(d => ({ ...d }))); // e.g. new bar colours
log('after 3rd candle setData: ' + range());
setTimeout(() => log('500 ms later: ' + range() + ' (version ' + LightweightCharts.version() + ')'), 500);
</script>
</body>
</html>Actual behavior:
After the third candles.setData(...) the chart is empty: no candles, no line, no price scale labels, no time scale labels. chart.timeScale().getVisibleLogicalRange() returns null and stays null. No error is thrown.
Output (same on 5.0.9 and 5.2.1):
after 2 candle setData: {"from":66.66666666666666,"to":199}
after line setData: {"from":66.66666666666666,"to":199}
after 3rd candle setData: null
500 ms later: null (version 5.2.1)Expected behavior:
The visible range stays as it was (the data and its times are the same) and the chart is drawn.
Notes:
The trigger is the sequence below (C = candles.setData(data), L = add a line series with the same times and setData, W = wait 100 ms), checked on 5.2.1 with 200 and 13,570 daily bars:
| Sequence | getVisibleLogicalRange() after |
|---|---|
| C C L C | null (empty chart) |
| C W C W L W C | null (empty chart) |
| C L C | normal |
| C W L W C | normal |
| C W C W L W C W C | normal again (one more setData restores it) |
So it needs a second setData on the first series before the other series is added. A real use case: an app that resets the candle colours with setData on each redraw, then adds indicator lines, then applies per-bar candle colours with setData.
Source: tradingview/lightweight-charts