本文介绍了循环使用Google Charts Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在arrayToDataTable中的Google Charts Javascript中执行循环".谁能建议该怎么做?同样可以在数据集中使用php变量吗?

I am looking to "perform a loop" in Google Charts Javascript in the arrayToDataTable. Can anyone suggest how to do this ? Also it it ok to use a php variable in the dataset like this ?

<script>

   google.setOnLoadCallback(drawChart);

   function drawChart() {

     var data = google.visualization.arrayToDataTable([
      ['Month', 'Movie'],
      ['1', <?php echo $Total[0]; ?>],
      ['2', <?php echo $Total[1]; ?>],
      ['3', <?php echo $Total[2]; ?>],
      ['4', <?php echo $Total[3]; ?>],
      ['5', <?php echo $Total[4]; ?>],
      ['6', <?php echo $Total[5]; ?>],
      ['7', <?php echo $Total[6]; ?>],
      ['8', <?php echo $Total[7]; ?>],
      ['9', <?php echo $Total[8]; ?>],
      ['10', <?php echo $Total[9]; ?>],
      ['11', <?php echo $Total[10]; ?>],
      ['12', <?php echo $Total[11]; ?>],
      ['13', <?php echo $Total[12]; ?>],
      ['14', <?php echo $Total[13]; ?>],
      ['15', <?php echo $Total[14]; ?>],
      ['16', <?php echo $Total[15]; ?>],
      ['17', <?php echo $Total[16]; ?>],
      ['18', <?php echo $Total[17]; ?>],
      ['19', <?php echo $Total[18]; ?>],
      ['20', <?php echo $Total[19]; ?>],
      ['21', <?php echo $Total[20]; ?>],
      ['22', <?php echo $Total[21]; ?>],
      ['23', <?php echo $Total[22]; ?>],
      ['24', <?php echo $Total[23]; ?>],
      ['25', <?php echo $Total[24]; ?>]
    ]);

... etc...

</script>

推荐答案

类似的东西适合您吗?

<?php
  $Total = array(0,1,2,3,4,5,6,7,8,9);
?>

<html lang="en">
<head>
  <script src="https://www.google.com/jsapi"></script>
  <script>
    google.load('visualization', '1', {packages: ['corechart'], callback: drawChart});

    function drawChart() {
      var data = google.visualization.arrayToDataTable([
        ['Month', 'Movie'],

<?php
    foreach($Total as $row){
      echo "['" . $row . "', " . $row . "],";
    }
?>

      ]);
      document.getElementById('chart').innerHTML = data.getNumberOfRows();
    }
  </script>
</head>
<body>
  <div id="chart"></div>
</body>
</html>

这篇关于循环使用Google Charts Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 05:34