2016-01-15 142 views
0

有人可以告诉我如何添加获取的json数据和Ajax到D3吗? 我将这个例子https://github.com/samehelhakim/D3-saChart---Bilevel-Partition实现到我的项目中,只是想用我的数据填充辐射。下面是一个屏幕截图与实际二层径向布局: enter image description here使用Ajax添加json数据到D3

的问题是,我必须从dinamically网址,以获取JSON,下面的屏幕截图来自URL我的JSON数据:

enter image description here

这里是卷曲的反应,如果这能帮助: curl 'http://127.0.0.1:5000/api/files/3/domains' -H 'Host: 127.0.0.1:5000' -H 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:43.0) Gecko/20100101 Firefox/43.0' -H 'Accept: application/json, text/javascript, */*; q=0.01' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'X-Requested-With: XMLHttpRequest' -H 'Referer: http://127.0.0.1:5000/file/3' -H 'Cookie: session=.eJw9kM2KwkAQhF9lmbOHdZJcAh6ESUKE7mCYGLovwq5RMz-7EBXjiO--g4c9VhV8VNVT7I_TcDmL_DrdhoXYjweRP8XHl8gFVEWgYJNG2ZTNNo1asqIM-tI1-mxIl4ZVa8GjJ1PM3LcewsaR75JGnxIKhxHNKcNqN0LfBdbt2Oj6jsFKroro04yylmzYYXCOTRezdQK-9NSXBtXGouyWGLoUjHOk6wfrLmPPvtHOki5iL2dB0kq8FuL7Mh331187_PxPoB4-IdADKgpc7UxEzaDqOU65o4YE-50D3To0bUQWS1ZbievVG3e7DNP7DrEUrz_1nGGI.CXkyKA.wxR75Mly8-Ohvld90-z4CuIenzg' -H 'Connection: keep-alive' -H 'Cache-Control: max-age=0' {"data": [{"count": 1, "disabled": 0, "file_id": 3, "name": "11.client-channel.google.com"}, {"count": 1, "disabled": 0, "file_id": 3, "name": "clients4.google.com"}, {"count": 2, "disabled": 0, "file_id": 3, "name": "likeabosh.hipchat.com"}, {"count": 1, "disabled": 0, "file_id": 3, "name": "609.talkgadget.google.com"}, {"count": 1, "disabled": 0, "file_id": 3, "name": "12.client-channel.google.com"}, {"count": 9, "disabled": 0, "file_id": 3, "name": "smetrics.allstate.com"}, {"count": 1, "disabled": 0, "file_id": 3, "name": "calendar.google.com"}, {"count": 1, "disabled": 0, "file_id": 3, "name": "ps499ee8d0.pubnub.com"}, {"count": 1, "disabled": 0, "file_i

这是我如何设法获取使用jQuery的Ajax从URL数据:

$(document).ready(function(){ 
    $.ajax({ 
     type: "GET", 
     contentType: "application/json; charset=utf-8", 
     url: '{{ url_for("diffs.api_domains", file_id=file.id) }}', 
     dataType: 'json' 
    }); 
}) 

您可以在此处看到d3布局的代码:https://github.com/samehelhakim/D3-saChart---Bilevel-Partition/blob/master/index.html。有一部分是d3.json("expanses.json", function(error, root) {,我在想我必须在那里插入我提取的数据。

请让我知道如果您需要关于此问题的其他信息,我希望我解释正确。

+0

只需在$ .ajax'done'回调函数中将数据添加到图表 – reptilicus

+0

@reptilicus您能更清楚地了解这一点吗?一些代码会真的帮我很多 – Valip

回答

0

是的,你可以使用d3.json去获取数据,然后用它来填充图表,无论你需要什么方式。例如:

$.ajax({ 
    type: "GET", 
    contentType: "application/json; charset=utf-8", 
    url: '{{ url_for("diffs.api_domains", file_id=file.id) }}', 
    dataType: 'json' 
}).done(function (resp) { 
    // Transform data as you need it and do all the d3 layout in here 
    d3.selectAll('.wedge') 
     .data(resp) 
     .enter().append(YADA) 
}); 
+0

不,我不能使用它,因为url地址是动态的,这就是为什么我用jquery ajax得到它。我在帖子中指出了这一点。 – Valip

+0

这有帮助吗? – reptilicus

+0

'.append(YADA)'做了什么? – Valip