【发布时间】:2016-08-23 10:26:36
【问题描述】:
我正在尝试使用 D3 创建固定数量的元素,并从 json 文件中获取该值。这意味着,如果给定的 json 数据是 n,我想将 n 元素打印到我的 svg 中。
这是我的代码:
// Defining the size of the svg element
var w = 1000;
var h = 50;
// Defining the dataset
d3.json("people.json", function(dataset) {
// Iterating through the json
for(var i=0; i<dataset.length; i++) {
var svg = d3.select("body").append("svg")
.attr("width", w)
.attr("height", h);
// Iterating through the value
for(var j=0; j<dataset[i].age; j++) {
svg.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("width", 20)
.attr("height", 20)
.attr("x", function(d, j) { return j * 55 })
}
}
});
这是我的 json 示例文件(年龄的完全随机值):
[{
"name": "Larry",
"last": "Page",
"country": "USA",
"city": "Mountain View",
"age": 32
}, {
"name": "Sergey",
"last": "Bean",
"country": "USA",
"city": "Mountain View",
"age": 37
}, {
"name": "Bill",
"last": "Gates",
"country": "USA",
"city": "Seattle",
"age": 60
}, {
"name": "Mark",
"last": "Zuckemberg",
"country": "USA",
"city": "Palo Alto",
"age": 35
}, {
"name": "Sergio",
"last": "Marchionne",
"country": "Italy",
"city": "Milan",
"age": 65
}
]
我的预期结果应该是这样的([-] --> svg rectangle)
-
拉里·佩奇: [-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][- ][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-]
谢尔盖·比恩: [-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][- ][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][ -][-]
等等……
你能帮我理解我做错了什么吗?
谢谢!
【问题讨论】:
-
只需从矩形绘图中删除
.data(dataset)。那是放错地方了。 -
感谢您的回答。但是删除它会导致此错误:get_data_from_json.html?_ijt=4057q09d7k7p7tej2g0mf5objt:56 Uncaught TypeError: svg.selectAll(...).enter is not a function(anonymous function) @ get_data_from_json.html?
-
让我免费给您一个建议:每次您考虑在 D3 可视化中使用 fior 循环时,问问自己:“这有必要吗?”。在 98.7℅ 的情况下,没有必要(来源:FakeData Inc.)
-
selectAll也放错了位置,例如enter()。你只需要append("rect")并设置它的属性。 -
这个名字是“象形图”。这是 d3 中的一项非常基本的任务,无需单个 for 循环即可创建您想要的内容。现在我在我的手机上,写起来很糟糕,但我相信很快就会有人向你展示如何做到这一点。
标签: javascript json d3.js svg