I am trying to render the following Dendrogram from my Rails app: http://bl.ocks.org/mbostock/4063570
我试图从我的Rails应用程序渲染以下树形图:http://bl.ocks.org/mbostock/4063570
I have a model with many attributes, but I would like to manually nest those attributes and simply use string interpolation to build up my own JSON string, then pass that to d3 directly.
我有一个具有许多属性的模型,但我想手动嵌套这些属性,只需使用字符串插值来构建我自己的JSON字符串,然后直接将其传递给d3。
Here is my code:
这是我的代码:
<%= 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 %>
Here is my (unminified) JSON string:
这是我的(未经通知的)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"
}
}
}
}
}';
Things I've tried:
我尝试过的事情:
-
minifying the JSON so it's inputted as just one line (no effect)
缩小JSON,使其只输入一行(无效果)
-
running JSON.parse(mystring) on the string
在字符串上运行JSON.parse(mystring)
-
looking through the D3 documentation and and googling for a way to modify the following function to accept a string instead of a file path:
查看D3文档和谷歌搜索一种方法来修改以下函数来接受字符串而不是文件路径:
d3.json("/assets/flare.json", function(root) { var nodes = cluster.nodes(root), links = cluster.links(nodes);
2 个解决方案
#1
26
First, lets look at what d3.json
does.
首先,让我们看看d3.json做了什么。
d3.json("/assets/flare.json", function(root) {
// code that uses the object 'root'
});
This loads the file /assets/flare.json
from the server, interprets the contents as JSON and passes the resulting object as the root
argument to the anonymous function.
这将从服务器加载文件/assets/flare.json,将内容解释为JSON,并将结果对象作为根参数传递给匿名函数。
Where you already have a JSON object, you don't need to use the d3.json
function - you can just use the object directly.
在已有JSON对象的地方,您不需要使用d3.json函数 - 您可以直接使用该对象。
var root = {
"name": "flare",
"children": [
...
]
};
// code that uses the object 'root'
If the object is represented as a string, then you can use JSON.parse
to get the object:
如果对象表示为字符串,则可以使用JSON.parse获取对象:
var myString = '{"name": "flare","children": [ ... ] }';
var root = JSON.parse(mystring);
// code that uses the object 'root'
Second, lets look at what d3.layout.cluster
expects of your data. As per the docs:
其次,让我们看一下d3.layout.cluster对数据的期望。根据文档:
... the default children accessor assumes each input data is an object with a children array ...
...默认的子访问器假设每个输入数据是一个带有子数组的对象...
In other words, you data needs to be of the form:
换句话说,您的数据需要具有以下形式:
var mystring = '{
"name": "Product",
"children": [
{
"name": "id",
"type": "number",
"description": "Product identifier",
"required": true
},
...
{
"name": "stock",
"type": "object",
"children": [
{
"name: "warehouse",
"type": "number"
},
{
"name": "retail",
"type": "number"
}
]
}
]
}
#2
3
d3.json actually takes URL as an argument, so instead of giving it the path to the file, I would suggest to delegate data management to the controller (especially, if in future you would need to load it from DB), so to simplify things:
d3.json实际上将URL作为参数,因此我建议将数据管理委托给控制器(特别是如果以后需要从DB加载它),而不是给它提供文件的路径,所以为了简化事情:
- Create a method in your controller, which would actually open the file and return its content:
- 在控制器中创建一个方法,该方法实际上会打开文件并返回其内容:
class YourFlareController < ApplicationController def load @data = File.read("app/assets/json/flare.json") render :json => @data end end
- Make sure you have a route in your routes.rb
- 确保您的routes.rb中有路由
get "yourflare/load"
得到“你的火焰/负荷”
- And now in your javascript you can simply call
- 现在在你的javascript中,你可以简单地打电话
d3.json("http://host/yourflare/load", function(root) {
d3.json(“http:// host / yourflare / load”,function(root){
#1
26
First, lets look at what d3.json
does.
首先,让我们看看d3.json做了什么。
d3.json("/assets/flare.json", function(root) {
// code that uses the object 'root'
});
This loads the file /assets/flare.json
from the server, interprets the contents as JSON and passes the resulting object as the root
argument to the anonymous function.
这将从服务器加载文件/assets/flare.json,将内容解释为JSON,并将结果对象作为根参数传递给匿名函数。
Where you already have a JSON object, you don't need to use the d3.json
function - you can just use the object directly.
在已有JSON对象的地方,您不需要使用d3.json函数 - 您可以直接使用该对象。
var root = {
"name": "flare",
"children": [
...
]
};
// code that uses the object 'root'
If the object is represented as a string, then you can use JSON.parse
to get the object:
如果对象表示为字符串,则可以使用JSON.parse获取对象:
var myString = '{"name": "flare","children": [ ... ] }';
var root = JSON.parse(mystring);
// code that uses the object 'root'
Second, lets look at what d3.layout.cluster
expects of your data. As per the docs:
其次,让我们看一下d3.layout.cluster对数据的期望。根据文档:
... the default children accessor assumes each input data is an object with a children array ...
...默认的子访问器假设每个输入数据是一个带有子数组的对象...
In other words, you data needs to be of the form:
换句话说,您的数据需要具有以下形式:
var mystring = '{
"name": "Product",
"children": [
{
"name": "id",
"type": "number",
"description": "Product identifier",
"required": true
},
...
{
"name": "stock",
"type": "object",
"children": [
{
"name: "warehouse",
"type": "number"
},
{
"name": "retail",
"type": "number"
}
]
}
]
}
#2
3
d3.json actually takes URL as an argument, so instead of giving it the path to the file, I would suggest to delegate data management to the controller (especially, if in future you would need to load it from DB), so to simplify things:
d3.json实际上将URL作为参数,因此我建议将数据管理委托给控制器(特别是如果以后需要从DB加载它),而不是给它提供文件的路径,所以为了简化事情:
- Create a method in your controller, which would actually open the file and return its content:
- 在控制器中创建一个方法,该方法实际上会打开文件并返回其内容:
class YourFlareController < ApplicationController def load @data = File.read("app/assets/json/flare.json") render :json => @data end end
- Make sure you have a route in your routes.rb
- 确保您的routes.rb中有路由
get "yourflare/load"
得到“你的火焰/负荷”
- And now in your javascript you can simply call
- 现在在你的javascript中,你可以简单地打电话
d3.json("http://host/yourflare/load", function(root) {
d3.json(“http:// host / yourflare / load”,function(root){