doushai4890 2016-09-23 10:13
浏览 75
已采纳

如何使用<?php echo $ data; ?>为了填充Google图表

I am currently working on a project at university which involves sampling values (CO2, temperature, humidity ...) to a SQLite3 database with a Raspberry Pi and using a webinterface to show the data. I'm fairly new to php/html, but I have no problems understanding what the code does.

I already found a german tutorial describing how to create a php/html script which imports the data from the SQLite database and uses Google Graphs in order to display it. I used the provided code as the base for my script.

This is what I have so far:

<?php

require_once("config.php");
require_once("functions.php");
//------------------------ PHP Settings ----------------------------------------
ini_set('track_errors', 1);
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set("memory_limit","64M");
ini_set("max_execution_time","30");
@ob_implicit_flush(true);
@ob_end_flush();
$_SELF=$_SERVER['PHP_SELF'];


$SQL="SELECT CO2, Temperature FROM value";

$db = db_con($DBfile);
$q = db_query($SQL);

 $data = "var data = new google.visualization.DataTable();<br>"
 ."data.addColumn('number', 'CO2');<br>"
 ."data.addColumn('number', 'Temperature');<br><br>"
 ."data.addRows([<br>";

 while ($res = $q->fetch(PDO::FETCH_ASSOC)) {

        $temp = (int)$res['Temperature'];
        $co2 = (int)$res['CO2'];
        $data = $data."  [".$temp.", ".$co2."],<br>";
        }
        $data = $data."]);<br>";

//Print data to check if data from database is loaded
echo $data;

?>

 <html>
  <head>
    <script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></sc$
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
      google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(drawChart);

      function drawChart() {
        <?php echo $data; ?>

        var options = {
          title: 'Air Quality',
          curveType: 'function',
          legend: { position: 'bottom' }
        };

        var chart = new google.visualization.LineChart(document.getElementById('temp_curve_chart'));

        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <div id="temp_curve_chart" style="width: 900px; height: 500px"></div>
  </body>
</html>

Now the problem is, that the graph does not show up.

If I add echo $data;the content of the variable shows up in the browser, which means that the import part is working, but I just can't get it to display the graph.

var data = new google.visualization.DataTable();
data.addColumn('number', 'CO2');
data.addColumn('number', 'Temperature');

data.addRows([
[23, 500],
[23, 500],
[25, 600],
]);

I suspect it has something to do with the following line of code or with the way the $data variable is set up:

function drawChart() {
        <?php echo $data; ?>

If I replace it with code from the example provided by Google, it shows the graph just fine.

  function drawChart() {
    var data = google.visualization.arrayToDataTable([
      ['Year', 'Sales', 'Expenses'],
      ['2004',  1000,      400],
      ['2005',  1170,      460],
      ['2006',  660,       1120],
      ['2007',  1030,      540]
    ]);

    var options = {
      title: 'Company Performance',
      curveType: 'function',
      legend: { position: 'bottom' }
    };

I already spent hours trying to get it work, but I just can't get it right.

Any help/tips would be greatly appreciated!

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dongtao9095 2016-09-23 10:36
    关注

    I found two things:

    first is this line

    <script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></sc$
    

    must be

    <script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
    

    secondly remove all

    <br>

    in your JavaScript and replace (not necessary, because it is just an visual effect in the source code) with

    like

     $data = "var data = new google.visualization.DataTable();
    "
     ."data.addColumn('number', 'CO2');
    "
     ."data.addColumn('number', 'Temperature');
    
    "
     ."data.addRows([
    ";
    
     while ($res = $q->fetch(PDO::FETCH_ASSOC)) {
    
            $temp = (int)$res['Temperature'];
            $co2 = (int)$res['CO2'];
            $data = $data."  [".$temp.", ".$co2."],
    ";
            }
            $data = $data."]);
    ";
    

    And you will get a nice looking graph!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog