json mysql data to d3 plot

前提是你 提交于 2020-01-16 12:08:27

问题


I am trying to plot mysql data with d3. I am using php to convert to json format but it does not work yet! For testing purposes I followed the steps indicated here I used simple-graph.html with my own php file on XAMPP.

include('connect.php');
$result=$con->query("SELECT date, close FROM testable");    
echo json_encode($result->fetchAll(PDO::FETCH_ASSOC)); 

It echoes in json format like so: [{"date":"1-May-12","close":"43.32"},{"date":"30-Apr-12","close":"22.54"},{"date":"27-Apr-12","close":"21.54"},{"date":"25-Apr-12","close":"21.42"}]

The graph gets displayed when I provide a json file but not with the php file.

This works:

d3.json("json.json", function(error, data) {        
data.forEach(function(d) {                              
d.date =parseDate(d.date);                          
d.close = +d.close;                                 
});

This does not work:

d3.json("tablecreate.php", function(error, data) {      
data.forEach(function(d) {                              
d.date =parseDate(d.date);                          
d.close = +d.close;                                 
});

All files are in the same folder.

Could someone please indicate my mistake?


回答1:


You haven't provided your full php source on how your sending the json output to your d3 script. One issue would be if your not sending the proper content type header. Below is some content from the API reference of d3.

Creates a request for the JSON file at the specified url with the mime type "application/json". If a callback is specified, the request is immediately issued with the GET method, and the callback will be invoked asynchronously when the file is loaded or the request fails; the callback is invoked with two arguments: the error, if any, and the parsed JSON. The parsed JSON is undefined if an error occurs. If no callback is specified, the returned request can be issued using xhr.get or similar, and handled using xhr.on.

make sure you have issued the following statement before you echo the json output.

header("Content-Type: application/json");


来源:https://stackoverflow.com/questions/17470571/json-mysql-data-to-d3-plot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!