我想從我的Rails應用中呈現以下樹狀圖渲染D3圖: http://bl.ocks.org/mbostock/4063570從JSON的字符串,而不是一個JSON文件
我有許多屬性的模型,但我想手動巢這些屬性並簡單地使用字符串插值來構建我自己的JSON字符串,然後直接將其傳遞給d3。
這裏是我的代碼:
<%= javascript_tag do %>
var width = 960,
height = 2200;
var cluster = d3.layout.cluster()
.size([height, width - 160]);
var diagonal = d3.svg.diagonal()
.projection(function(d) { return [d.y, d.x]; });
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(40,0)");
**d3.json("/assets/flare.json", function(root) {**
var nodes = cluster.nodes(root),
links = cluster.links(nodes);
var link = svg.selectAll(".link")
.data(links)
.enter().append("path")
.attr("class", "link")
.attr("d", diagonal);
var node = svg.selectAll(".node")
.data(nodes)
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; })
node.append("circle")
.attr("r", 4.5);
node.append("text")
.attr("dx", function(d) { return d.children ? -8 : 8; })
.attr("dy", 3)
.style("text-anchor", function(d) { return d.children ? "end" : "start"; })
.text(function(d) { return d.name; });
});
d3.select(self.frameElement).style("height", height + "px");
<% end %>
這裏是我的(unminified)JSON字符串:
var mystring = '{
"name": "Product",
"properties": {
"id": {
"type": "number",
"description": "Product identifier",
"required": true
},
"name": {
"type": "string",
"description": "Name of the product",
"required": true
},
"price": {
"type": "number",
"minimum": 0,
"required": true
},
"tags": {
"type": "array",
"items": {
"type": "string"
}
},
"stock": {
"type": "object",
"properties": {
"warehouse": {
"type": "number"
},
"retail": {
"type": "number"
}
}
}
}
}';
事情我已經嘗試:
1)涅槃的JSON所以它的輸入作爲只有一行(沒有效果)
2)在字符串上運行JSON.parse(mystring)
3)通過D3文檔查找和搜索以下方法來修改以下函數以接受字符串而不是文件路徑: d3.json(「/ assets/flare.json」,function(root) var nodes = cluster.nodes(root), links = cluster.links(nodes);
knolleary,謝謝你爲解釋灰。這是有幫助的,但我特別要找的是:如何更改d3.json(「/ assets/flare.json」,函數(root){line允許本地字符串?d3.json的語法是: d3.json(url [,callback])。我想在我的字符串上運行匿名函數,而不是URL。 –
Michael,您通過刪除它來更改d3.json(..)行... if你已經有了這個字符串,你不需要使用函數從url加載內容,這就是d3.json所做的一切。當前在d3.json(..)調用中指定的匿名函數可以被明確地調用這是我在前半部分的答案中描述的內容 – knolleary
我怎樣才能加載一個json文件呢?它總是給我錯誤 –