0

我想显示一个从数据库中的表中获取信息的折线图。我遇到了似乎最适合我需要的 Google Graphs。我可以让图表显示我在代码中放置的数据,但是当我尝试使用 echo 从数据库中添加数据时,它不起作用。请在下面找到我的代码。我不确定我在这里做错了什么。

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">

      // Load the Visualization API and the piechart package.
      google.load('visualization', '1.0', {'packages':['corechart']});

      // Set a callback to run when the Google Visualization API is loaded.
      google.setOnLoadCallback(drawChart);

      // Callback that creates and populates a data table,
      // instantiates the pie chart, passes in the data and
      // draws it.
      function drawChart() {

        // Create the data table.
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Date');
        data.addColumn('number', 'Number of Products');
        data.addRows([
<!--CHANGE FOR PHP TO PULL INFORMATION FRO MYSQL DBA-->

<?php
// Create connection
$con=mysqli_connect("host","username","password","database");

// Check connection
if (mysqli_connect_errno($con))
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

  $result = mysqli_query($con,"SELECT * FROM table");
$start = 0;
while($row = mysqli_fetch_array($result)){

    echo "['".$row["dateadded"]."', ".$row["products"]."],";

mysqli_close($con);
?>
        ]);

        // Set chart options
        var options = {'title':'Product Count History',
                       'width':600  ,
                       'height':550};

        // Instantiate and draw our chart, passing in some options.
        var chart = new google.visualization.LineChart(document.getElementById('chart_product_history'));
        chart.draw(data, options);
      }
    </script>


                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                <td width="50%">
                <div id="chart_product_history"></div>
                </td>
                <td width="50%">

                </td>
                </tr>
                <tr>
                <td width="50%">

                </td>
                <td width="50%">

                </td>
                </tr>
                </table>

谢谢瑞恩

4

2 回答 2

0

我用一些固定的随机数据尝试了你的代码,它确实产生了一个图表(尽管你在数组中有一个额外的逗号,有时可能会导致问题)。

您似乎缺少围绕查询结果的循环中的右大括号。

尝试以下

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">

      // Load the Visualization API and the piechart package.
      google.load('visualization', '1.0', {'packages':['corechart']});

      // Set a callback to run when the Google Visualization API is loaded.
      google.setOnLoadCallback(drawChart);

      // Callback that creates and populates a data table,
      // instantiates the pie chart, passes in the data and
      // draws it.
      function drawChart() {

        // Create the data table.
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Date');
        data.addColumn('number', 'Number of Products');
        data.addRows([
<!--CHANGE FOR PHP TO PULL INFORMATION FRO MYSQL DBA-->

<?php
// Create connection
$con=mysqli_connect("host","username","password","database");

// Check connection
if (mysqli_connect_errno($con))
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM table");
$start = 0;
$OutArray = array();
while($row = mysqli_fetch_array($result))
{
    $OutArray[] = "['".$row["dateadded"]."', ".$row["products"]."],";
}

echo explode(',', $OutArray);

mysqli_close($con);
?>
        ]);

        // Set chart options
        var options = {'title':'Product Count History',
                       'width':600  ,
                       'height':550};

        // Instantiate and draw our chart, passing in some options.
        var chart = new google.visualization.LineChart(document.getElementById('chart_product_history'));
        chart.draw(data, options);
      }
    </script>


                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                <td width="50%">
                <div id="chart_product_history"></div>
                </td>
                <td width="50%">

                </td>
                </tr>
                <tr>
                <td width="50%">

                </td>
                <td width="50%">

                </td>
                </tr>
                </table>
于 2013-08-15T12:47:50.773 回答
0

尝试改变这个:

 echo "['".$row["dateadded"]."', ".$row["products"]."],";

对此:

 echo "['".$row['dateadded']."', '".$row['products']."'],";
于 2013-08-15T11:53:23.357 回答