无法使用MySQL表数据作为数据源来生成Google图表 [英] Not able to generate a Google Chart using MySQL table data as the data source

查看:63
本文介绍了无法使用MySQL表数据作为数据源来生成Google图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从MySQL数据库生成Google图表。我引用了这篇文章: PHP MySQL Google Chart JSON-完整示例

I am trying to generate google chart from MySQL database. I refereed to this post : PHP MySQL Google Chart JSON - Complete Example

我做了帖子中提到的所有内容,但在网页上得到了:

I did everything mentioned in the post but getting this on the webpage:


'每周任务','类型'=>'字符串'),array('标签'=>'百分比','类型'=>'数字')); $ rows = array(); while($ r = mysql_fetch_assoc($ sth)){$ temp = array(); //以下行将用于切片饼图$ temp [] = array(’v’=>(string)$ r [’Weekly_task’]); //每个切片的值$ temp [] = array(’v’=>(int)$ r [’percentage’]); $ rows [] = array(’c’=> $ temp); } $ table [’rows]] = $ rows; $ jsonTable = json_encode($ table); //回显$ jsonTable; ?>

'Weekly Task', 'type' => 'string'), array('label' => 'Percentage', 'type' => 'number') ); $rows = array(); while($r = mysql_fetch_assoc($sth)) { $temp = array(); // the following line will be used to slice the Pie chart $temp[] = array('v' => (string) $r['Weekly_task']); // Values of each slice $temp[] = array('v' => (int) $r['percentage']); $rows[] = array('c' => $temp); } $table['rows'] = $rows; $jsonTable = json_encode($table); //echo $jsonTable; ?>

我尝试过检查数据库,似乎一切都很好。

I tried and checked my database at everything seems to be fine.

代码:

 <html>
  <head>
    <!--Load the Ajax API-->
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script type="text/javascript">

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

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

    function drawChart() {

      // Create our data table out of JSON data loaded from server.
      var data = new google.visualization.DataTable(<?=$jsonTable?>);
      var options = {
           title: 'My Weekly Plan',
          is3D: 'true',
          width: 800,
          height: 600
        };
      // Instantiate and draw our chart, passing in some options.
      // Do not forget to check your div ID
      var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
      chart.draw(data, options);
    }
    </script>
  </head>

  <body>
    <?php
$con=mysql_connect("localhost","root","password") or die("Failed to connect with database!!!!");
mysql_select_db("googlecharts", $con); 
// The Chart table contains two fields: weekly_task and percentage
// This example will display a pie chart. If you need other charts such as a Bar chart, you will need to modify the code a little to make it work with bar chart and other charts
$sth = mysql_query("SELECT weekly_task, percentage FROM chart");

/*
---------------------------
example data: Table (Chart)
--------------------------
weekly_task     percentage
Sleep           30
Watching Movie  40
work            44
*/

$rows = array();
//flag is not needed
$flag = true;
$table = array();
$table['cols'] = array(

    // Labels for your chart, these represent the column titles
    // Note that one column is in "string" format and another one is in "number" format as pie chart only required "numbers" for calculating percentage and string will be used for column title
    array('label' => 'Weekly Task', 'type' => 'string'),
    array('label' => 'Percentage', 'type' => 'number')

);

$rows = array();
while($r = mysql_fetch_assoc($sth)) {
    $temp = array();
    // the following line will be used to slice the Pie chart
    $temp[] = array('v' => (string) $r['Weekly_task']); 

    // Values of each slice
    $temp[] = array('v' => (int) $r['percentage']); 
    $rows[] = array('c' => $temp);
}

$table['rows'] = $rows;
$jsonTable = json_encode($table);
//echo $jsonTable;
?>
    <!--this is the div that will hold the pie chart-->
    <div id="chart_div"></div>
  </body>
</html>

更新
我将HTML和PHP代码分为不同的文件。
现在我的网页空白。
代码:
PHP:

UPDATE I separated HTML and PHP codes into different files. Now I am getting blank web page. CODE: PHP:

<?php
$con=mysql_connect("localhost","root","pass") or die("Failed to connect with database!!!!");
mysql_select_db("googlechart", $con); 
// The Chart table contains two fields: weekly_task and percentage
// This example will display a pie chart. If you need other charts such as a Bar chart, you will need to modify the code a little to make it work with bar chart and other charts
$sth = mysql_query("SELECT weekly_task, percentage FROM chart");

/*
---------------------------
example data: Table (Chart)
--------------------------
weekly_task     percentage
Sleep           30
Watching Movie  40
work            44
*/

$rows = array();
//flag is not needed
$flag = true;
$table = array();
$table['cols'] = array(

    // Labels for your chart, these represent the column titles
    // Note that one column is in "string" format and another one is in "number" format as pie chart only required "numbers" for calculating percentage and string will be used for column title
    array('label' => 'Weekly Task', 'type' => 'string'),
    array('label' => 'Percentage', 'type' => 'number')

);

$rows = array();
while($r = mysql_fetch_assoc($sth)) {
    $temp = array();
    // the following line will be used to slice the Pie chart
    $temp[] = array('v' => (string) $r['Weekly_task']); 

    // Values of each slice
    $temp[] = array('v' => (int) $r['percentage']); 
    $rows[] = array('c' => $temp);
}

$table['rows'] = $rows;
$jsonTable = json_encode($table);
//echo $jsonTable;
?> 

HTML:

<?php
    include('new.php');
    ini_set('display_errors', 1); 
    error_reporting(E_ALL);
?>

 <html>
  <head>
    <!--Load the Ajax API-->
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script type="text/javascript">

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

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

    function drawChart() {

      // Create our data table out of JSON data loaded from server.
      var data = new google.visualization.DataTable(<?=$jsonTable?>);
      var options = {
           title: 'No. of Kills',
          is3D: 'true',
          width: 800,
          height: 600
        };
      // Instantiate and draw our chart, passing in some options.
      // Do not forget to check your div ID
      var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
      chart.draw(data, options);
    }
    </script>
  </head>

  <body>

    <!--this is the div that will hold the pie chart-->
    <div id="chart_div"></div>
  </body>
</html>


推荐答案

有几项需要调整。我会尝试逐项列出它们。

There are several things to adjust. I'll try to itemize them.


  1. 将所有内容都放在一个文件中,这样可以减少混乱,并且分割文件并没有真正的好处。代码。

  2. 交换您的 mysql _ 函数,至少获取 mysqli _ 函数。

  3. 将您的google可视化javascript代码块移到将接收它的DOM元素之后的行。 (如果您尝试将内容放入不存在的元素中,那么您将看不到任何内容。)

  4. 您的问题中未提供示例数据,所以我不知道如果 $ jsonTable 提供正确的数据-您需要自己评估。

  1. Put everything on the one file, this will reduce confusion and there is no real benefit to splitting the codes.
  2. Swap out your mysql_ functions for at least mysqli_ functions.
  3. Move your google visualization javascript code block to a line that follows the DOM element which will receive it. (if you try to put content into a non-existent element, you aren't going to see anything.)
  4. No sample data is provided in your question, so I don't know if $jsonTable is providing the correct data -- you will need to assess this yourself.

在执行上述更正时,请确保检查代码中是否存在服务器端错误。

As you implement the above correction, be sure to check your code for server-side errors.

如果您没有服务器端错误,但图形未显示,则:

If you have no server-side errors, but the graphic is not being displayed, then:


  1. 检查页面的源代码并确认 DateTable()函数包含 $ jsonTable 变量中的正确/实际数据字符串。

  2. 访问浏览器的开发人员工具界面并查看对于任何错误消息。这些将是客户端错误。

  1. Check your page's source code and confirm that the DateTable() function contains the correct/actual data string from your $jsonTable variable.
  2. Access your browser's Developer Tools interface and look for any error messages. These will be client-side errors.

如果您仍然遇到问题,需要更多信息或希望看到类似的工作示例,请转到此处

If you are still stuck, desire more information, or want to see similar working examples, go here.

这是 Google图表参考页

这篇关于无法使用MySQL表数据作为数据源来生成Google图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆