Screenshot
Data
A button that saves the chart as a PNG image.
Interactive example — scroll, zoom, and hover the chart.
Code
// Lightweight Charts™ Example: Screenshot
// https://tradingview.github.io/lightweight-charts/examples/data/screenshot
const container = document.getElementById('container');
// The chart takes the space above the button, which is added further down.
container.style.display = 'flex';
container.style.flexDirection = 'column';
const chartElement = document.createElement('div');
chartElement.style.flex = '1';
chartElement.style.minHeight = '0';
container.appendChild(chartElement);
/** @type {import('lightweight-charts').IChartApi} */
const chart = createChart(chartElement, chartOptions);
const series = chart.addSeries(CandlestickSeries, {
upColor: '#26a69a',
downColor: '#ef5350',
borderVisible: false,
wickUpColor: '#26a69a',
wickDownColor: '#ef5350',
});
series.setData(generateBars());
chart.timeScale().fitContent();
function saveScreenshot() {
// A canvas with the chart drawn on it, scales included.
const canvas = chart.takeScreenshot();
canvas.toBlob(blob => {
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'chart.png';
link.click();
// Released a second later: some browsers still read the file after `click` returns.
setTimeout(() => URL.revokeObjectURL(link.href), 1000);
}, 'image/png');
}
const button = document.createElement('button');
button.innerText = 'Save as PNG';
button.addEventListener('click', saveScreenshot);
buttonsContainer.appendChild(button);
container.appendChild(buttonsContainer);
// Sample data: daily bars, a seeded random walk, so the chart looks the same
// on every load.
function generateBars(count = 100) {
let seed = 31;
const random = () => (seed = (seed * 16807) % 2147483647) / 2147483647;
const bars = [];
let time = Date.UTC(2024, 0, 1) / 1000;
let close = 100;
for (let i = 0; i < count; i++) {
const open = close;
close = open + (random() - 0.48) * 4;
const high = Math.max(open, close) + random() * 2;
const low = Math.min(open, close) - random() * 2;
bars.push({ time, open, high, low, close });
time += 24 * 60 * 60;
}
return bars;
}
The code expects the library’s exports, such as createChart, to be imported and a div with the id container on the page. Copy as HTML gives a page that runs on its own.
How it works
The chart can draw itself on a new canvas, which the page can then save as an image.
takeScreenshotreturns a canvas with the chart on it, including the price and time scales.toBlobturns the canvas into a PNG file.- A temporary link with the
downloadattribute saves the file aschart.png.
These lines are highlighted in the code.
Tips
- By default, the screenshot leaves out the chart's top layer, where the crosshair and some plugin drawings are drawn.
takeScreenshot(true)adds the top layer without the crosshair, andtakeScreenshot(true, true)adds the crosshair as well. - To show the image on the page instead of saving it, set an
imgelement'ssrctocanvas.toDataURL(). - Elements you add over the chart, such as a legend or a tooltip, are HTML, not part of the chart canvas, so they are not in the screenshot.
Resources
IChartApi.takeScreenshot- Related examples: Legend, Text watermark