gpt4 book ai didi

php - 在 php mysql 中使用 Google Chart API 显示条形图

转载 作者:IT王子 更新时间:2023-10-28 23:44:50 26 4
gpt4 key购买 nike

我一直在寻找这个。我得到了解决方案。就在这里。它是在 php 中使用 AJAX 完成的。我有 2 个页面是 googleapi.php 和其他 getData.php,它们被从 googleapi.php 发送的 AJAX 请求使用。

googleapi.php

 <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(object) {

// Create our data table out of JSON data loaded from server.
var data = new google.visualization.DataTable(object);
var options = {
title: 'Test API',
is3D: 'true',
width: 200,
height: 100
};
// Instantiate and draw our chart, passing in some options.
// Do not forget to check your div ID
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, options);
}

function show()
{

xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","getdata.php?name=jaspreet",true);
xmlhttp.send();
xmlhttp.onreadystatechange = getResponse;
}
function getResponse()
{
if(xmlhttp.readyState==4){
alert(xmlhttp.responseText);
var obj = JSON.parse(xmlhttp.responseText);
drawChart(obj);
}
}
</script>
</head>

<body>
<!--this is the div that will hold the pie chart-->
<div id="chart_div"></div>
<select id="name" onchange="show();">
<option value="test">test</option>
<option value="test1">test1</option>
<option value="test2">test2</option>
</select>
</body>
</html>

getData.php

<?php $con=mysql_connect("localhost","root","") or die("Failed to connect with database!!!!");
mysql_select_db("student", $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 * FROM marks where name='jas'");

/*
---------------------------
example data: Table (Chart)
--------------------------
marks percentage
English 30
Maths 40
Science 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' => 'subject', 'type' => 'string'),
array('label' => 'marks', '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['subject']);

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

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

就是这样,您需要创建一个数据库。它就像一个魅力。

最佳答案

我知道您的帖子是一个“答案”,但我想我会插话。如果您希望将数据动态输入到 Google 图表中,您可以使用 PHP 简单地回显到 javascript 中。 php 可用于从您的服务器或 mysql 表中获取信息。

这是一个例子:

    <?php   $i = 0; $grab = mysql_query("SELECT * FROM `productList` WHERE 1");
$pricelist = mysql_fetch_array($grab);
$numberofproducts = mysql_num_rows($grab);

?>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['parts', 'Prices'],
['Number of Products', <?php echo $numberofproducts;?>],

]);

var options = {
title: 'Number of Products',
hAxis: {title: '', titleTextStyle: {color: 'red'}},
width: 980,
height: 200
};

var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, options);
}

关于php - 在 php mysql 中使用 Google Chart API 显示条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22380968/

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