0

I am trying to make use of the display:none and display:block on my website. The style as follows:

<style>
  .div_class {
    display: none;
  }
</style>

The body content is:

<div class="div_class">
   <div id="graph_id"></div>
</div>

The script is:

    <script>
      // some event occurs and sets the display property of div_class to block as follows
      $(".div_class").css('display','block'); 

      var BarGraph = echarts.init(document.getElementById('graph_id'), theme);
      BarGraph.setOption({  
       // properties that are set for the echart 'graph_id'
      });
    </script>

The display: block; makes the div visible but the echart properties that are set do not render. The div appears blank. What may be the reason? I am unable to figure out. Kindly help. Thanks :)

*************************** EDIT *************************************
Also, I think the title may be misleading, since I am able to hide the div onload and display it on click. But the later rendering is where I am facing certain issue.

d33a
  • 690
  • 1
  • 14
  • 39

4 Answers4

0

Rerender your BarGraph after showing the div

madalinivascu
  • 32,064
  • 4
  • 39
  • 55
0
 BarGraph .setOption({

works the same as

 BarGraph.setOption({

? (get rid of space)

also, try to wrap all your code in

$(document).ready(function() {
    ***code here***
});

Edit:

https://ecomfe.github.io/echarts-doc/public/tutorial-en.html#Introduction%20of%20ECharts%20features%0D

^ took code from there, added jquery to head, wrapped chart block in div.container with display:none, created click event

$(document).on('click', function() { $(".container").show(); myChart.setOption(option); });

working snippet:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>ECharts</title>
    <!-- including ECharts file -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script src="http://echarts.baidu.com/dist/echarts.common.min.js"></script>

</head>
<body>
 <style>
     .container {
      display: none;
     }
    </style>
    <!-- prepare a DOM container with width and height -->
  click anywhere!
    <div class="container">
     <div id="main" style="width: 600px;height:400px;"></div>
    </div>
    <script type="text/javascript">
        // based on prepared DOM, initialize echarts instance
        var myChart = echarts.init(document.getElementById('main'));

        // specify chart configuration item and data
        var option = {
            title: {
                text: 'ECharts entry example'
            },
            tooltip: {},
            legend: {
                data:['Sales']
            },
            xAxis: {
                data: ["shirt","cardign","chiffon shirt","pants","heels","socks"]
            },
            yAxis: {},
            series: [{
                name: 'Sales',
                type: 'bar',
                data: [5, 20, 36, 10, 10, 20]
            }]
        };

        // use configuration item and data specified to show chart
     $(document).on('click', function() {
      $(".container").show();
      myChart.setOption(option);
     });
    </script>
</body>
</html>
Bimbonkens
  • 319
  • 3
  • 11
  • Sorry. Typing error with the space here. Thanks for pointing it out. And yes, I will try your suggestion. – d33a Oct 25 '16 at 06:56
  • This seems to be a typo otherwise the above code block would also have been not functional which set the div to block. – Faisal Ashfaq Oct 25 '16 at 06:57
-1

I have tried with chart.js The chart can't be drawn if display property is set to none. The thing is visibility:hidden preserve the space, whereas display:none doesn't preserve the space

<style>
  .div_class{
      visibility:hidden;
  }
</style>

Try replacing the css with the above code. I hope this will help you :)

You can refer following post for more information: Difference between visibility:hidden and display:none?

Community
  • 1
  • 1
-1
    <script>
      // some event occurs and sets the display property of div_class to block as follows

      $(".div_class").css('display','block').promise().done(function(){

          //draw graph here

          var BarGraph = echarts.init(document.getElementById('graph_id'), theme);
          BarGraph .setOption({  
           // properties that are set for the echart 'graph_id'
          });

      }); 


    </script>

Try above code. I have added .promise() What it does is only after the css "display:block;" is applied then the graph drawing process will be started