3

I have embedded a stock chart from Tradingview into my site, but I cannot make it responsive (especially the width). When the page shrinks the chart begin to move outside the screen. Here is the code:

<div class="tradingview-widget-container">
  <div id="tradingview_8e656"></div>
  <div class="tradingview-widget-copyright"><a href="https://www.tradingview.com/symbols/SP-SPX/" rel="noopener" target="_blank"><span class="blue-text">SPX chart</span></a> by TradingView</div>
  <script type="text/javascript" src="https://s3.tradingview.com/tv.js"></script>
  <script type="text/javascript">
  new TradingView.widget({
    "height": 610,
    "symbol": "SP:SPX",
    "interval": "D",
    "timezone": "Etc/UTC",
    "theme": "Light",
    "style": "1",
    "locale": "en",
    "toolbar_bg": "#f1f3f6",
    "enable_publishing": false,
    "hide_side_toolbar": false,
    "allow_symbol_change": true,
    "container_id": "tradingview_8e656"
  });
  </script>
</div>

I tried changing the units in width to rem and % (because I suppose they use px) but it didn't work. I also tried erasing height and width completly and adding "autosize": true instead. Then I thought that maybe I'm not editing it correctly, so I started learning about JavaScript objects in W3Schools. Couldn't find the information there either.

The best solution that I could come with is to set the overflow to hidden, but you can understand it's not right. What I need is something like "shrink to fit" property or simply edit the javascript somehow.

Other links I read (which also didn't help):

Here is my entire code- what I wanted to achieve is to open the chart from the navbar (I am using bootstrap):

<div class="container-fluid sticky-top">
  <nav class="navbar navbar-expand-lg" style="background-color:#1B2034">
    <img src="media/a1.jpg" style="height:4rem;width:8rem">
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon mb-2"><i class="fa fa-bars fa-2x" style="color:#0040ff"></i></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarNav">
      <ul class="navbar-nav">
        <li class="nav-item active">
          <a class="nav-link" href="#">home page<span class="sr-only">את/ה כאן</span></a>
        </li>
        <li class="nav-item">
          **<a class="nav-link" data-toggle="collapse" href="#graphs">chart page</a>**
        </li>
        <!-- other nav items -->
        <li class="nav-item mt-2">
          <input class="form-control" type="search" placeholder="חיפוש" aria-label="Search">
                      </li>
        <li class="nav-item mt-2">
          <button class="btn btn-outline-info mr-2" type="submit">חיפוש</button>
        </li>
      </ul>
    </div>
  </nav>
  <!-- Other pages -->
  <div class="container-fluid">
    <div class="collapse sticky-top" style="top:5rem;overflow:hidden" id="graphs">
      <div class="card card-body height:100%">
        <!-- TradingView Widget BEGIN -->
        THE CHART CODE
        <!-- TradingView Widget END -->
      </div>
    </div>

<div class="collapse sticky-top" style="top:5rem;overflow:hidden" id="graphs">
          <div class="card card-body height:100%">

... Repeats ...

C. Arnold
  • 89
  • 1
  • 13
  • 1
    The widget itself is definitely responsive; when using `autosize:true`, I put one in a container down to `240px` wide and it resized as expected. The size of the chart itself is dependent on the size of the container it's in when using the `autosize` option; what your describing is likely the cause of other CSS being applied. You need to share your code that's producing the behavior you describe. – Stevangelista Oct 28 '18 at 00:25
  • Okay I will do that. What I wanted to achieve is to open the charts from the navbar @Stevangelista – C. Arnold Oct 28 '18 at 13:33

3 Answers3

1

The way I solved it was to go to Chrome Dev tools and play with the chart's design over there. I found what classes I should edit that effect the chart size, and used @media rule to change those classes for certain devices.

Use Dev tools next time @me ;)

C. Arnold
  • 89
  • 1
  • 13
0

Using vue js:

HTML Body:

<div class="chart" id="chart" ref="chart"></div>

Vue Methods:

 mounted() {
                this.chart = LightweightCharts.createChart(this.$refs.chart, {
                    width: this.$refs.chart.offsetWidth,
                    height: this.$refs.chart.offsetHeight
                });

                // var lineSeries = this.chart.addLineSeries();
                // lineSeries.setData(arr);

                // resize observer (native JS)
                const ro = new ResizeObserver((entries) => {
                    const cr = entries[0].contentRect;
                    this.resize(cr.width, cr.height);
                });

                ro.observe(this.$refs.chart);

                window.addEventListener("resize", () => {
                    this.resize(this.$refs.chart.offsetWidth, this.$refs.chart.offsetHeight );
                });
            },

And then add this:

methods: {
resize(width, height)
      { 
         this.chart.resize(width, height);
      }
}
Maizied Hasan Majumder
  • 1,197
  • 1
  • 12
  • 25
-1

Here is my solution how to make it in CSS.

Using the ID of this container - <div id="tradingview_8e656"></div>

Then just add in your .css file this lines of code Main parameter is !important

    #tradingview_8e656 {
width: 1000px !important;        
height: 750px !important;
    }

This works for me perfectly. Also it remains responsive on a devices. Hope this helps.

Hexycode
  • 428
  • 8
  • 26