简体   繁体   English

如何创建响应式 Tradingview 轻量级图表?

[英]How to create responsive Tradingview Lightweight Chart?

I am new to Tradingview charting library and I want to create like responsive chart.我是 Tradingview 图表库的新手,我想创建类似响应式图表。

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)',
      },
    });

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 })您需要检测包含图表的容器元素上的调整大小事件,然后使用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.有一些库可以检测元素调整大小,我能找到的最好的非库方式是使用ResizeObserver API,但看起来它可能还没有集成到所有现代浏览器中。 You would set an update callback function using the elements new height/width and use that in the ResizeObserver :您将使用元素 new height/width 设置更新回调函数,并在ResizeObserver使用它:

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

new ResizeObserver(outputsize).observe($('#chartDiv'))

This is the code from timocov's comment, which just work.这是 timocov 评论中的代码,它可以正常工作。 Put it after createChart and setData .把它放在createChartsetData and rename chartContainer to your chart container HTML element.并将 chartContainer 重命名为您的图表容器 HTML 元素。

  // 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);

Here is a pretty simple solution.这是一个非常简单的解决方案。 Let's assume you create a LightweightChart inside of a div container:假设您在div容器内创建了一个 LightweightChart:

    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 
        });
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM