Unable to extract data to get google chart. Nothin

2019-08-30 12:52发布

Here are my 2 files. //getdata.php: extracts data and converts to json format using json_encode. Is it the right way of encoding the data? I am not changing the column names. Is that necessary?

<?php

mysql_connect('localhost','akshita','123456');
mysql_select_db('rcusers');

$sqlquery1="select userid,group_name,req_nodes,actualPE from jobs where userid='zhang' limit 200";

$sqlresult1=mysql_query($sqlquery1);

$rows=array();

while($r=mysql_fetch_assoc($sqlresult1)){
        $rows[]=$r;
}
print json_encode($rows);
?>

//chartDraw.php: uses api functions to print it. Nothing is being displayed.

<html>
<head>
<!--Load the AJAX API -->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></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(){
  var jsonData = $.ajax({
        url:"getData.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>

1条回答
Animai°情兽
2楼-- · 2019-08-30 13:18

You need to take some time to understand how ajax works. Its not synchronous in that it will not immediately return a response, instead the response is passed via callbacks once the request is completed.

var jsonData = $.ajax({
    url:"getData.php",
    dataType:"json",
    async:false,
    success : function(response) {
        // Init chart here
    }
    });

You will likely get chided for using mysql functions which will be deprecated in PHP 5.5 and removed in the future.

查看更多
登录 后发表回答