我正在处理谷歌图表,我已经有一个基本的设置。
它目前所做的是连接到数据库并根据1个查询返回数据集。我想知道的是,如果我想用不同的查询绘制更多图表到数据库我该怎么做?或者最佳做法是什么?
例如,一个查询已经有一个连接,如何添加另一个查询,然后根据返回的内容绘制图表?
我理解这可能是一个广泛的问题,但也许有人可以告诉我如何从数据库中返回不同的查询/数据集?
这是我的代码:
$(document).ready(function(){
console.log("hello world")
//alert("result")
$.ajax({
url:"data.php",
dataType : "json",
success : function(result) {
google.charts.load('current', {
'packages':['corechart','bar']
});
google.charts.setOnLoadCallback(function() {
console.log(result[0]["name"])
drawChart(result);
});
}
});
//add a 2nd call - will need a 2nd draw charts to draw the different dataset assuming it will be different
// - will need a 2nd data.php as the query will be different on the dataset
$.ajax({
url:"data.php",
dataType : "json",
success : function(result2) {
google.charts.load('current', {
'packages':['corechart','bar']
});
google.charts.setOnLoadCallback(function() {
console.log(result2[0]["name"])
drawChart(result2);
});
}
});
function drawChart(result) {
var data = new google.visualization.DataTable();
data.addColumn('string','Name');
data.addColumn('number','Quantity');
var dataArray=[];
$.each(result, function(i, obj) {
dataArray.push([ obj.name, parseInt(obj.quantity) ]);
});
data.addRows(dataArray);
var piechart_options = {
title : 'Pie Chart: How Much Products Sold By Last Night',
width : 400,
height : 300
}
var piechart = new google.visualization.PieChart(document
.getElementById('piechart_div'));
piechart.draw(data, piechart_options)
var columnchart_options = {
title : 'Bar Chart: How Much Products Sold By Last Night',
width : 400,
height : 300,
legend : 'none'
}
//var barchart = new google.visualization.BarChart(document
// .getElementById('barchart_div'));
//barchart.draw(data, barchart_options)
var chart = new google.charts.Bar(document.getElementById('columnchart_material'));
chart.draw(data, google.charts.Bar.convertOptions(columnchart_options));
} //have added this column chart but need to wrok out if it is best practice????
});
我从数据库查询中获取了一个对象,但我想知道如何从同一个数据库连接返回更多/不同的数据集?例如,如果我想使用此查询select * from product where name="Product1" OR name="Product2";
0: Object { id: "1", name: "Product1", quantity: "2" }
1: Object { id: "2", name: "Product2", quantity: "3" }
2: Object { id: "3", name: "Product3", quantity: "4" }
3: Object { id: "4", name: "Product4", quantity: "2" }
4: Object { id: "5", name: "Product5", quantity: "6" }
5: Object { id: "6", name: "Product6", quantity: "11" }
值得我的PHP代码如下:
data.php
<?php
require_once 'database.php';
$stmt = $conn->prepare('select * from product');
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_OBJ);
echo json_encode($results);
?>
database.php中
<?php
$conn = new PDO('mysql:host=192.168.99.100;dbname=demo','root', 'root');
?>
注意:this可能会引起您的兴趣
答案 0 :(得分:1)
google图表每页加载只需要加载一次,
并非每次都需要绘制图表
另外,google.charts.load
可用于代替 - &gt; $(document).ready
它会在执行回调/承诺
建议设置类似于以下代码段...
google.charts.load('current', {
packages: ['corechart', 'bar']
}).then(function () {
$.ajax({
url: 'data.php',
dataType: 'json'
}).done(drawChart1);
$.ajax({
url: 'data.php',
dataType: 'json'
}).done(drawChart2);
});
function drawChart1(result) {
...
}
function drawChart2(result) {
...
}