如何使用循环数据填充PHP Google Chart?

时间:2015-04-08 15:22:39

标签: javascript php arrays charts

我使用Google API在php中执行了以下图表,其中包含来自json服务的数据。


 <html>
<head>
<style>
    #chart_div{
 height:200px;
 width:300px;    
}
</style>

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script>
    google.load("visualization", "1", {packages:["LineChart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
     var data = new google.visualization.DataTable();
        data.addColumn('string', 'Date');
        data.addColumn('number', 'Values');
        data.addRows([

    ["9H00" ,JSON.parse( <?php echo $json2['values']['0']['avg']; ?>)],
    ["10H00",JSON.parse( <?php echo $json2['values']['1']['avg']; ?>)],
    ["11H00",JSON.parse( <?php echo $json2['values']['2']['avg']; ?>)],
    ["12H00",JSON.parse( <?php echo $json2['values']['3']['avg']; ?>)],
    ["13H00",JSON.parse( <?php echo $json2['values']['4']['avg']; ?>)],
    ["14H00",JSON.parse( <?php echo $json2['values']['5']['avg']; ?>)],
    ["15H00",JSON.parse( <?php echo $json2['values']['6']['avg']; ?>)],
    ["16H00",JSON.parse( <?php echo $json2['values']['7']['avg']; ?>)],
    ["17H00",JSON.parse( <?php echo $json2['values']['8']['avg']; ?>)],
    ["18H00",JSON.parse( <?php echo $json2['values']['9']['avg']; ?>)],
    ["19H00",JSON.parse( <?php echo $json2['values']['10']['avg']; ?>)]
]);

        var options = {
          title: 'PowerActive',
          chart_div: 'function',
          legend: { position: 'bottom' }
        };

      var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(data, options);


    //   var chart = new google.visualization.AnnotatedTimeLine(document.getElementById('chart_div'));
    //  chart.draw(data, {displayAnnotations: true});
      }

    </script>
    </head>
<body>
    <div id="chart_div" style="width: 800px; height: 240px;" ></div>


        </body>
    </html>  

enter image description here

但是,手动输入正在显示的数据。

我喜欢数据是动态的,这意味着 Y 的值来自此循环:

for ($x = 0; $x <= $result2; $x++) {
    print_r($json2['values'][$x]['avg']);
}

X 值来自:

for ($x = 0; $x <= $result2; $x++) {
$milliseconds =  $json2['values'][$x]['startTime'];
$timestamp = $milliseconds/1000000000;
echo date("d-m-Y h:i:s", $timestamp);
}

可以做我想做的事吗?

有人可以帮我吗?

意识到我的怀疑? 谢谢大家的帮助,抱歉我的英语不好。

0 个答案:

没有答案