How get price from previous point Lightweight Charts?
I have a chart with tooltip There I display the date and price of the current point.
var toolTipWidth = 125;
var width = 600;
var toolTip = document.createElement('div');
toolTip.className = 'tooltip';
chartContainer.appendChild(toolTip);
chart.subscribeCrosshairMove(function(param) {
if (!param.time || param.point.x < 0 || param.point.x > width || param.point.y < 0 || param.point.y > height) {
toolTip.style.display = 'none';
return;
}
let price = param.seriesPrices.get(areaSeries);
let date = LightweightCharts.isBusinessDay(param.time) ? businessDayToString(param.time) : new Date(param.time * 1000).toLocaleDateString();
toolTip.innerHTML = `<div class="tooltip__price">${price}</div><div class="tooltip__time">${date}</div>`;
let left = param.point.x - toolTipWidth / 2;
left = Math.max(0, Math.min(width - toolTipWidth, left));
toolTip.style.left = left + 'px';
});
On the chart, I need to show the dynamics of growth (how much the amount has changed, whether there has been an increase or decrease, how much it is as a percentage). To do this, I need to get the previous point and compare it with the current one. But only one chart point is passed to the tooltip function. How to get the previous value?
This is not possible as for v3.7 and upcoming v3.8 versions so you need to store your data somewhere and then use that data to find a previous data point.
In the next major v4.0 update the library will include a new method ISeriesApi.dataByIndex which you could use to get a data for the logical index (see PR with changes).