简体   繁体   English

json mysql数据到d3图

[英]json mysql data to d3 plot

I am trying to plot mysql data with d3. 我试图用d3绘制mysql数据。 I am using php to convert to json format but it does not work yet! 我正在使用php将其转换为json格式,但尚无法使用! For testing purposes I followed the steps indicated here I used simple-graph.html with my own php file on XAMPP. 为了进行测试,我按照此处指示的步骤操作,在XAMPP上使用了simple-graph.html和我自己的php文件。

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"}] 它以json格式回显,如下所示: [{"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. 当我提供json文件但不提供php文件时,将显示该图。

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? 有人可以指出我的错误吗?

You haven't provided your full php source on how your sending the json output to your d3 script. 您尚未提供有关如何将json输出发送到d3脚本的完整php源。 One issue would be if your not sending the proper content type header. 一个问题是您是否未发送正确的内容类型标头。 Below is some content from the API reference of d3. 以下是d3的API参考中的一些内容。

Creates a request for the JSON file at the specified url with the mime type "application/json". 在MIME类型为“ application / json”的指定URL处创建对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; 如果指定了回调,则立即使用GET方法发出请求,并且在加载文件或请求失败时将异步调用该回调; the callback is invoked with two arguments: the error, if any, and the parsed JSON. 回调使用两个参数调用:错误(如果有)和已解析的JSON。 The parsed JSON is undefined if an error occurs. 如果发生错误,则解析的JSON是未定义的。 If no callback is specified, the returned request can be issued using xhr.get or similar, and handled using xhr.on. 如果未指定回调,则可以使用xhr.get或类似方法发出返回的请求,并使用xhr.on处理。

make sure you have issued the following statement before you echo the json output. 在回显json输出之前,请确保已发出以下语句。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM