json mysql数据到d3图 [英] json mysql data to d3 plot

查看:113
本文介绍了json mysql数据到d3图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用d3绘制mysql数据.我正在使用php将其转换为json格式,但尚无法正常工作!为了进行测试,我按照此处指示的步骤进行操作 我在XAMPP上使用了simple-graph.html和我自己的php文件.

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)); 

它以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"}]

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文件但不提供php文件时,将显示该图.

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

这有效:

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

这不起作用:

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?

推荐答案

您尚未提供有关如何将json输出发送到d3脚本的完整php源.一个问题是您是否未发送正确的内容类型标头.以下是d3的API参考中的一些内容.

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.

使用mime在指定的url处创建对JSON文件的请求 输入"application/json".如果指定了回调,则请求为 立即使用GET方法发出,回调将是 当文件加载或请求失败时异步调用; 回调使用两个参数调用:错误(如果有)和 解析的JSON.如果发生错误,则解析的JSON是未定义的.如果不 指定了回调,可以使用以下命令发出返回的请求 xhr.get或类似内容,并使用xhr.on进行处理.

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.

在回显json输出之前,请确保已发出以下语句.

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

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

这篇关于json mysql数据到d3图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆