Vishal Arora
Vishal Arora

Reputation: 153

How to load two Google Line charts in one page?

I had a deep look at somewhat similar questions asked here and here. I tried their implementation. It didn't work !

Then I had a look at following link by Google : tow charts in one page. Followed the same procedure but still ended up showing only one chart at a time.

Below is my code :

 <script type = "text/javascript" src = "https://www.google.com/jsapi" ></script>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <!--Load the Ajax API-->

    <script type="text/javascript">

      google.load("visualization", "1", {packages:["line"]});         
        google.setOnLoadCallback(drawChart);    
        google.setOnLoadCallback(drawOutputChart);

        function drawChart() {
            var data = new google.visualization.DataTable(<?= $jsonAnalogTable ?>);
            var options = {
                chart: {
                    title: ' Data ',
                    is3D: 'true'
                },
                width: 550,
                height: 350,

            };       
            var chart = new google.charts.Line(document.getElementById('analogchart'));
            chart.draw(data, options);
        } 

        function drawOutputChart(){
           var data2 = new google.visualization.DataTable(<?= $jsonOutputTable ?>);
            var options2 = {
                chart: {
                    title: ' Data ',
                    is3D: 'true'
                },
                width: 550,
                height: 350,

            };

            var chart2 = new google.charts.Line(document.getElementById('outputChart'));
            chart2.draw(data2, options2); 
        }


    </script>

**HTML Code to call it : **

<table>
            <tr>
                <td>
                    <div id="analogchart" >

                    </div>  
                </td>
                <td>
                    <div id="outputChart" >

                    </div>
                </td>

            </tr>
        </table>

I seriously have no idea what is going wrong as it is able to show one chart at a time, but not both !

Upvotes: 1

Views: 62

Answers (1)

WhiteHat
WhiteHat

Reputation: 61275

it is recommended to use loader.js vs. the older library jsapi
and don't need both for a Line chart

also, setOnLoadCallback should only be called once per page
which can be included in the load statement, as in the following example

try something like this...

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">

  google.charts.load('current', {
    callback: function () {
      drawChart();
      drawOutputChart();
    },
    packages: ['line']
  });

  function drawChart() {
    var data = new google.visualization.DataTable(<?= $jsonAnalogTable ?>);
    var options = {
      chart: {
        title: ' Data ',
        is3D: 'true'
      },
      width: 550,
      height: 350,
    };       
    var chart = new google.charts.Line(document.getElementById('analogchart'));
    chart.draw(data, options);
  } 

  function drawOutputChart(){
    var data2 = new google.visualization.DataTable(<?= $jsonOutputTable ?>);
    var options2 = {
      chart: {
        title: ' Data ',
        is3D: 'true'
      },
      width: 550,
      height: 350,
    };

    var chart2 = new google.charts.Line(document.getElementById('outputChart'));
    chart2.draw(data2, options2); 
  }

</script>

Upvotes: 1

Related Questions