ここから多くの例を読みましたが、何らかの理由でそれらを機能させることができません。
私はここから例を取りました:
PHP MySQL Google Chart JSON - 完全な例
PHP-MySQLi-JSON-Google Chart Example を使用しています
何らかの理由で、データは foreach メソッドを使用して mysql データベースから取得されませんでした。これを fetch_assoc を使用して while ループに変更し、データを取得して正しい json 形式を作成しました。
ページを読み込むと、空白のページが表示されます。
なぜ機能しないのか、今ではわかりません。
役立つ情報を次に示します。php 5.3.17 OpenSuse 12.3
Apache ログを確認しましたが、エラーはありません。これを理解するために他に何ができるか考えていますか?
jsonTable:
{
"cols":[
{"label":"Weekly Task","type":"string"},
{"label":"Percentage","type":"number"}
],
"rows":[
{"c":[{"v":"running"},{"v":30}]},
{"c":[{"v":"jorunning"},{"v":30}]},
{"c":[{"v":"job"},{"v":20}]},
{"c":[{"v":"sleeping"},{"v":40}]},
{"c":[{"v":"exercise"},{"v":50}]},
{"c":[{"v":"resting"},{"v":30}]}
]
}
これが私のコードです:
<?php
$DB_NAME = 'chart';
$DB_HOST = 'localhost';
$DB_USER = 'test';
$DB_PASS = '123456';
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "select * from googlechart";
if ($result = $mysqli->query($query)) {
{
$rows = array();
$table = array();
$table['cols'] = array(
array('label' => 'Weekly Task', 'type' => 'string'),
array('label' => 'Percentage', 'type' => 'number')
);
while ($row = $result->fetch_assoc()) {
$temp = array();
$temp[] = array('v' => (string) $row['weekly_activity']);
$temp[] = array('v' => (int) $row['percentage']);
$rows[] = array('c' => $temp);
}
}
}
$table['rows'] = $rows;
$jsonTable = json_encode($table);
?>
<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>
<!--this is the div that will hold the pie chart-->
<div id="chart_div"></div>
<?php echo $jsonTable; ?>
</body>
</html>
読み込まれたページのソースは次のとおりです。
<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.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.LineChart(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>