Thanks for mbelton. My code works good now, and I also modified the following code I post.
I'm trying to use Google Pie chart to perform the year of sales. First I have HTML file which to let user choose the year they wish to review; Second, based on the $year be selected, I code PHP to connect to mysql to grab related sales data; Third I create a pieData table to store the String Quarter and double sales number; and then JavaScript to get pie char. I run the code doesn't show any errors but I don't see pie chart showing up. Can please someone tell me where to modify to make it work? Thanks.
$year = $_POST['year'];
$mysqli = mysqli_connect("root", "account", "passwd", "testDB")
or die(mysqli_error());
$query = "SELECT Q1,Q2,Q3,Q4 from sales where year LIKE '$year'";
$result = mysqli_query($mysqli, $query) or die(mysqli_error($mysqli));
/*get the values of each quarter, and store in new table:pieData.
* if user choose year of 2012, then the new table should look like:
* Quarter Number
* ------------------
* Q1 127.24
* Q2 106.54
* Q3 88.04
* Q4 120.89
*/
while ($info = mysqli_fetch_array($result)) {
$Q1 = $info['Q1'];
$Q2 = $info['Q2'];
$Q3 = $info['Q3'];
$Q4 = $info['Q4'];
}
$pieData = array(
array('Quarter', 'Number'),
array('Q1', (double)$Q1),
array('Q2', (double)$Q2),
array('Q3', (double)$Q3),
array('Q4', (double)$Q4)
);
$jsonTable = json_encode($pieData);
<script type="text/javascript" src="http://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 package
google.load("visualization", "1", {packages: ["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
// Create and populate the data table.
var data = google.visualization.arrayToDataTable(
<?php echo $jsonTable; ?>
);
var options = {
title:"Sales Pie"
};
// Create and draw the visualization.
var chart = new google.visualization.PieChart(document.getElementById("piechart"));
chart.draw(data, options);
}
</script>
I think error in your code is in opening/closing <script
tag and in .arrayToDataTable
you need to remove square brackets [ ]
Change code to:
<?php
$Q1 = 127.24;
$Q2 = 106.54;
$Q3 = 88.04;
$Q4 = 120.89;
$pieData = array(
array('Quarter', 'Number'),
array('Q1', (double)$Q1),
array('Q2', (double)$Q2),
array('Q3', (double)$Q3),
array('Q4', (double)$Q4)
);
$jsonTable = json_encode($pieData);
?>
<html>
<head>
<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">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable(
<?php echo $jsonTable; ?>
);
var options = {
title: "My Daily Activities"
};
var chart = new google.visualization.PieChart(document.getElementById("piechart"));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="piechart" style="width: 900px; height: 500px;"></div>
</body>
</html>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.