How to create responsive Tradingview Lightweight Chart?
Asked Answered
Y

4

7

I am new to Tradingview charting library and I want to create like responsive chart.

The problem is, trading view charting library requires to specify width and height. Here is the code.

const chart = LightweightCharts.createChart(e, {
      width: e.offsetWidth,
      height: e.offsetHeight,
      layout: {
        backgroundColor: 'rgb(17, 17, 39)',
        textColor: 'rgba(255, 255, 255, 0.9)',
      },
      grid: {
        vertLines: {
          color: 'rgb(41, 44, 58)',
        },
        horzLines: {
          color: 'rgb(41, 44, 58)',
        },
      },
      crosshair: {
        mode: LightweightCharts.CrosshairMode.Normal,
      },
      priceScale: {
        borderColor: 'rgba(197, 203, 206, 0.8)',
      },
      timeScale: {
        borderColor: 'rgba(197, 203, 206, 0.8)',
      },
    });
Yoshi answered 12/9, 2019 at 1:41 Comment(1)
Follow this solution: https://mcmap.net/q/1475684/-how-to-make-embedded-chart-responsiveEonian
W
13

This is the code from timocov's comment, which just work. Put it after createChart and setData. and rename chartContainer to your chart container HTML element.

  // Make Chart Responsive with screen resize
  new ResizeObserver(entries => {
      if (entries.length === 0 || entries[0].target !== chartContainer) { return; }
      const newRect = entries[0].contentRect;
      chart.applyOptions({ height: newRect.height, width: newRect.width });
    }).observe(chartContainer);
Want answered 17/12, 2021 at 6:15 Comment(0)
L
7

Lightweight charts has the option for autoSize.

Setting this flag to true will make the chart watch the chart container's size and automatically resize the chart to fit its container whenever the size changes.

This feature requires ResizeObserver class to be available in the global scope. Note that calling code is responsible for providing a polyfill if required. If the global scope does not have ResizeObserver, a warning will appear and the flag will be ignored.

Lavonda answered 11/3, 2023 at 11:26 Comment(2)
this should be the right answer, because not only this works, but also this usese the tools provided by the lybrary instead of using hacks/external code.Subtotal
This is only if you have v4.0Radon
C
1

You need to detect a resize event on the container element containing the chart, then update the charts dimensions with chart.applyOptions({ width: newWidth, height: newHeight })

There are some libraries to detect element resizes, the best non-lib way that I can find is with the ResizeObserver API, but it looks like it might not be integrated into all modern browsers yet. You would set an update callback function using the elements new height/width and use that in the ResizeObserver:

function resize() {
    chart.applyOptions({ width: $('#chartDiv').width(), height: $('#chartDiv').height() })
}

new ResizeObserver(outputsize).observe($('#chartDiv'))
Cormophyte answered 12/9, 2019 at 17:55 Comment(1)
You don't even need to measure container's height if you're using ResizeObserver: ``` new ResizeObserver(entries => { if (entries.length === 0 || entries[0].target !== container) { return; } const newRect = entries[0].contentRect; chart.applyOptions({ height: newRect.height, width: newRect.width }); }).observe(container); ```Earthaearthborn
F
1

Here is a pretty simple solution. Let's assume you create a LightweightChart inside of a div container:

    const chartDiv = document.getElementById('chart');
    const chart = LightweightCharts.createChart(chartDiv, {
        // ...
    }

Then this will resize the chart on the fly:

    window.onresize = function() {
        chart.applyOptions({ 
            width: chartDiv.offsetWidth, 
            height: chartDiv.offsetHeight 
        });
    }
Frawley answered 22/12, 2022 at 21:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.