从网址获取d3.js数组 [英] Fetch d3.js array from a url

查看:145
本文介绍了从网址获取d3.js数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我拥有的数据

{ "_id" : ObjectId("kdahfa"), "minTime" : ISODate("2016-04-02T00:00:00.000+0000"), "maxTime" : ISODate("2016-04-02T00:11:00.000+0000"), "Time" : 660.0, "Name" : "Sam" }   
{ "_id" : ObjectId("aabhk"), "minTime" : ISODate("2016-04-02T01:00:00.000+0000"), "maxTime" : ISODate("2016-04-02T02:14:25.000+0000"), "Time" : 4465.0, "Name" : "Bob" }  
{ "_id" : ObjectId("bak"), "minTime" : ISODate("2016-04-02T19:00:00.000+0000"),     "maxTime" : ISODate("2016-04-02T19:52:22.000+0000"), "Time" : 3142.0, "Name" :     "Sam" }  

我写了一个python(bottle)代码来从mongoDB服务器获取数据并在本地主机上运行它.网址为 http://localhost:8080/result .

I wrote a python(bottle) code to fetch the data from mongoDB server and run it on local host. The url is http://localhost:8080/result.

  @bottle.route('/result')
     def grab_record():
     bottle.response.headers['Access-Control-Allow-Origin'] = '*'
     a = dbcoll.find_one({},{'_id':False})
     a['minTime'] = str(a['minTime'])
     a['maxTime'] = str(a['maxTime'])
     response.content_type = 'application/json'  
     return dumps(a)

现在,我希望以此为基础,从 http://bl.ocks的帮助下创建d3.js图.org/dk8996/5449641 .我希望example.js中的任务数组从url/mongodb中获取数据.我应该对d3.js代码进行哪些更改以使其能够根据我的要求运行?

Now I wish to create a d3.js graph from this taking help from http://bl.ocks.org/dk8996/5449641. I want the task array in the example.js to take data from the url/mongodb. What changes should I make to my d3.js code to make it function according to my requirements?

d3.json("http://localhost:8080/result", function(error, data) {
data.forEach(function(d) {
    d.maxTime = parseDate(d.maxTime);
    d.minTime = parseDate(d.minTime);
    d.Time = +d.Time;
});

var taskNames = ["Bob", "Sam"];

taskStatus = {
"Bob": "bar",
"Sam": "bar-failed",
};

var gantt = d3.gantt().taskTypes(taskNames).taskStatus(taskStatus);
gantt(tasks);
});

这些是我到目前为止在example.js文件中所做的更改.我遇到错误
未捕获的TypeError:data.forEach不是函数
我希望创建一个返回的d3.js对象的数组,并像在 http://上使用的那样使用它bl.ocks.org/dk8996/5449641 .

These are the changes in the example.js file that I've made so far. I am getting an error
Uncaught TypeError: data.forEach is not a function
I wish to create an array of the returned d3.js objects and use it like it is used on http://bl.ocks.org/dk8996/5449641.

推荐答案

 a = dbcoll.find_one({},{'_id':False})

在这一行中,您仅查询数据库中的一个条目,这就是为什么返回的结果是单个对象而不是数组的原因(可能还需要将第一个参数{}更改为[]或表示数组的东西,但我对dbcoll不熟悉.

In this line, you are only querying for one entry in your database, that is why the result returned is a single object instead of an array (probably you also need to change the first parameter {} to [] or something to denote array but I am not familiar with dbcoll.

您需要将find_one更改为find之类的东西,您将获得一个数组.

You need to change find_one into find or something, and you will get an array.

然后进行后期处理,您需要遍历a的数组:

Then the post-processing, you needs to iterate through the array of a:

a = a.map(function(b){
     b['minTime'] = str(b['minTime'])
     b['maxTime'] = str(b['maxTime'])
     return b;
}

在MongoDB中,有一种方法find: https://docs.mongodb.com/manual/reference/method/db.collection.find/

In MongoDB there is a method find: https://docs.mongodb.com/manual/reference/method/db.collection.find/

这篇关于从网址获取d3.js数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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