dongyong8071 2018-10-02 17:08
浏览 57
已采纳

使用PHP生成Google功能图表来自MySQL的数据,但功能无法可视化

I want to create a function chart from Google Charts with my data coming from a MySQL database (localhost). The program shows no error, that is conflicting with this topic, when compiling with a online markup language validator, though it still doesn't create the function chart when viewing in the browser. I checked my code and online sources, including multiple posts on this topic from this forum, but to no avail. The PHP code works for sure. The only thing i can think off straight away would be, that the JavaScript code is not loaded right.

The JavaScript part is a slightly modified version from sgoogle linechart (type function) sourcecode (https://developers.google.com/chart/interactive/docs/gallery/linechart).

Code:

<!Doctype html>
<html>
<body>
<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $database = "daylight";
    $conn = new mysqli($servername, $username, $password, $database);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
        $startdate=time()-(time()%(24*3600))-7200;
        $enddate=$startdate+365*24*3600;
        //echo $startdate."<br>".$enddate;
        $s=strftime("%Y-%m-%d", $startdate);
        $e=strftime("%Y-%m-%d", $enddate);

        $Off = 12;  //Offset
        $A = 4;     //Amplidute
        $light=0;

        for($i=$startdate; $i<=$enddate; $i+=$day)  //genData (works)
        {...}

        $sql_request = "SELECT * FROM `db` WHERE `Datum` >= '$s' And `Datum` <= '$e'";
        $daylight_array=array();
        $date_array=array();
        foreach($conn->query($sql_request) as $row)
        {
            $daylight_array[]=$row["Tageslicht"];
            $date_array[]=$row["Datum"];
            //print $row["Tageslicht"];
        }
        // echo json_encode($date_array);
        // echo json_encode($daylight_array);

?>
<div id="chart" style="width: 900px; height: 500px"></div>

<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);

    var date = <?php echo json_encode($date_array); ?>;
    var daylight = <?php echo json_encode($daylight_array); ?>;

    function drawChart() {
        var data = google.visualization.arrayToDataTable();
        data.addColumn('string', 'Jahr-Monat-Tag');
        data.addColumn('number', 'Sonnenstunden');

        for(i = 0; i < date.length; i++)
            data.addRow([date[i], daylight[i]]);

        var options = {

          title: 'Sonnenstunden über das Jahr',
          curveType: 'function',
          legend: { position: 'bottom' }
        };

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

        chart.draw(data, options);
  }
</script>
</body>
</html>
  • 写回答

2条回答 默认 最新

  • dr9379 2018-10-02 17:21
    关注

    I believe your method of initializing the DataTable is incorrect. As the docs say, arrayToDatatable creates and populates a DataTable using a single call. So instead, you should initialize the DataTable with something like this:

    var data = new google.visualization.DataTable();
    

    Didn't have the time to try it out, but seems to be this way....

    If you insist on using the method you used, then provide the method with array of data as per documentation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 CSS实现渐隐虚线边框
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥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添加列问题