PHP中的Google Chart API问题

时间:2016-03-23 17:24:12

标签: javascript php html

我需要使用循环显示动态谷歌图表...就像所有图表具有不同的值但在同一页面上...我有以下代码:

 <table>
    <?php 
      $i=0;
      while($i<4){
      echo"
        <script >
        google.charts.load('current', {'packages':['gantt']});
        google.charts.setOnLoadCallback(drawChart);

function drawChart() {

  var data = new google.visualization.DataTable();
  data.addColumn('string', 'Task ID');
  data.addColumn('string', 'Task Name');
  data.addColumn('string', 'Resource');
  data.addColumn('date', 'Start Date');
  data.addColumn('date', 'End Date');
  data.addColumn('number', 'Duration');
  data.addColumn('number', 'Percent Complete');
  data.addColumn('string', 'Dependencies');

  data.addRows([
    ['2014Spring', 'Spring 2014', 'spring',
     new Date(2014, 2, 22), new Date(2014, 5, 20), null, 100, null],
    ['2014Summer', 'Summer 2014', 'summer',
     new Date(2014, 1, 12), new Date(2014, 2, 20), null, 100, null],
     ['2015Winter', 'Winter 2015', 'winter',
     new Date(2015, 11, 21), new Date(2016, 2, 21), null, 0, null],

  ]);

  var options = {
    height: 200,
    gantt: {
      trackHeight: 25
    }
  };

          var chart = new       google.visualization.Gantt(document.getElementById('chart_div$i'));

      chart.draw(data, options);
    }
  </script>

  <tr>


     <td>    Hello </td>
      <td id='chart_div$i'></td>

      </tr>

      ";
      $i++;
      }
    ?>

</table>

我做错了什么以及如何实现目标?

1 个答案:

答案 0 :(得分:0)

您输出完整的<script>标签4次。您应该只输出一次,最好是在页面的页脚中输出。看看我的例子:

<table>
    <tr>
        <td>Hello</td>
        <td id='chart_div0'></td>
        <td id='chart_div1'></td>
        <td id='chart_div2'></td>
        <td id='chart_div3'></td>
    </tr>
</table>

然后在页脚中:

<script type="text/javscript">
    google.charts.load('current', {'packages':['gantt']});
    google.charts.setOnLoadCallback(drawChart);

    function drawChart() {
<?php for ( $counter = 0; $counter < 4; $counter++ ) : ?>
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Task ID');
        data.addColumn('string', 'Task Name');
        data.addColumn('string', 'Resource');
        data.addColumn('date', 'Start Date');
        data.addColumn('date', 'End Date');
        data.addColumn('number', 'Duration');
        data.addColumn('number', 'Percent Complete');
        data.addColumn('string', 'Dependencies');

        data.addRows([
            ['2014Spring', 'Spring 2014', 'spring',
            new Date(2014, 2, 22), new Date(2014, 5, 20), null, 100, null],
            ['2014Summer', 'Summer 2014', 'summer',
            new Date(2014, 1, 12), new Date(2014, 2, 20), null, 100, null],
            ['2015Winter', 'Winter 2015', 'winter',
            new Date(2015, 11, 21), new Date(2016, 2, 21), null, 0, null],
        ]);

        var options = {
            height: 200,
            gantt: {
                trackHeight: 25
            }
        };

        var chart = new google.visualization.Gantt(document.getElementById('chart_div<?php echo $counter;?>'));
        chart.draw(data, options);

<?php endfor; ?>
    }
  </script>