gpt4 book ai didi

php - 无法使用 MySQL 表数据作为数据源生成 Google Chart

转载 作者:行者123 更新时间:2023-11-28 23:44:59 25 4
gpt4 key购买 nike

我正在尝试从 MySQL 数据库生成谷歌图表。我引用了这篇文章:PHP MySQL Google Chart JSON - Complete Example

我做了帖子中提到的所有事情,但在网页上得到了这个:

'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>
<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:

<?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>

最佳答案

有几件事需要调整。我将尝试逐条列出它们。

  1. 将所有内容放在一个文件中,这将减少混淆,并且拆分代码并没有真正的好处。
  2. 将您的 mysql_ 函数换成至少 mysqli_ 函数。
  3. 将您的 google visualization javascript 代码块移动到将接收它的 DOM 元素后面的一行。 (如果您尝试将内容放入不存在的元素中,您将看不到任何内容。)
  4. 您的问题中没有提供示例数据,所以我不知道 $jsonTable 是否提供了正确的数据——您需要自己评估。

在实现上述更正时,请务必检查您的代码是否存在服务器端错误。

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

  1. 检查页面的源代码并确认 DateTable() 函数包含来自 $jsonTable 变量的正确/实际数据字符串。
  2. 访问浏览器的开发人员工具界面并查找任何错误消息。这些将是客户端错误。

如果您仍然卡住,想了解更多信息,或想查看类似的工作示例,请转至 here .

这里是 Google Charts Reference Page

关于php - 无法使用 MySQL 表数据作为数据源生成 Google Chart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33752165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com