嗨,我有一个代码,成功地将数据从我的SQLSVR拉入JSON记录
<?php
$db = new PDO("sqlsrv:Server=DK-MatthewClay\SQLExpress2012;Database=FFP_WebServices", "LocalAdmin", "");
$row=$db->prepare("FFP_WebServices.dbo.WEBSERV_DAILY_PERIOD_FIGURES_SELECT");
$row->execute();//execute the query
$json_data=array();//create the array
foreach($row as $rec)//foreach loop
{
$json_array['Dates']=$rec['Dates'];
$json_array['OrdersTD']=$rec['OrdersTD'];
$json_array['OrdersAD']=$rec['OrdersAD'];
//here pushing the values in to an array
array_push($json_data,$json_array);
}
//built in PHP function to encode the data in to JSON format
echo json_encode($json_data, JSON_NUMERIC_CHECK);
?>
这样就构建了我的JSON
[{ “日期”: “2018年3月26日”, “OrdersTD”:86043 “OrdersAD”:85900.55},{ “日期”: “2018年3月27日”, “OrdersTD”:86043 “OrdersAD”: 46288.81}]
但没有列标题。所以,当我尝试使用此代码时,我无法创建谷歌图表任何帮助都会很棒
这就是我试图创建图表的原因
<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></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.charts.load('current', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var jsonData = $.ajax({
url: "includes/buildJson.php",
dataType: "json",
async: false
}).responseText;
// Create our data table out of JSON data loaded from server.
var data = new google.visualization.DataTable(jsonData);
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, {width: 400, height: 240});
}
</script>
</head>
<body>
<!--Div that will hold the pie chart-->
<div id="chart_div"></div>
</body>
</html>
当直接从json创建DataTable时,json必须是某种格式, 在这里找到 - > Format of the Constructor's JavaScript Literal data Parameter
看下面的php生成所需的json ...
<?php
$db = new PDO("sqlsrv:Server=DK-MatthewClay\SQLExpress2012;Database=FFP_WebServices", "LocalAdmin", "");
$row=$db->prepare("FFP_WebServices.dbo.WEBSERV_DAILY_PERIOD_FIGURES_SELECT");
$row->execute();//execute the query
//create the array
$json_data['cols'] = array(
array('label' => 'Dates', 'type' => 'string'),
array('label' => 'OrdersTD', 'type' => 'number'),
array('label' => 'OrdersAD', 'type' => 'number')
);
$rows = array();
foreach($row as $rec)//foreach loop
{
$temp = array();
$temp[] = array('v' => (string) $rec['Dates']);
$temp[] = array('v' => (float) $rec['OrdersTD']);
$temp[] = array('v' => (float) $rec['OrdersAD']);
$rows[] = array('c' => $temp);
}
$json_data['rows'] = $rows;
//built in PHP function to encode the data in to JSON format
echo json_encode($json_data, JSON_NUMERIC_CHECK);
?>
另外,关于ajax的async: false
已被弃用,请改用done
回调...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
$.ajax({
url: 'includes/buildJson.php',
dataType: 'json'
}).done(function (jsonData) {
var data = new google.visualization.DataTable(jsonData);
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, {width: 400, height: 240});
});
});