我尝试使用D3创建固定数量的元素,从json文件中获取此值。这意味着,如果给定的json数据是 n ,我想在我的svg中打印 n 元素。
这是我的代码:
// 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
}
]
我的预期结果应该是那样的([ - ] - &gt; svg矩形)
拉里佩奇: [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ]
谢尔盖·宾: [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ] [ - ]
依旧......
你能帮助我理解我做错了吗?
谢谢!
答案 0 :(得分:2)
纯粹的D3方法根本不需要for-loops(就像Gerardo Furtado在他的comment中提到的那样)并且将是以下几行:
d3.select("body").selectAll("svg")
.data(dataset) // Bind the entire dataset
.enter().append("svg") // Append one svg per object in dataset
.selectAll("rect")
.data(function(d) { // Inherit data from svg by mapping of objects
// Create an array of number of required rects
return d3.range(d.age).map(function(d) { return d*15; });
})
.enter().append("rect") // Append rects per svg
.attr("x", function(d) {
return d; // Position was calculated in above mapping in data()
});
这就是绘制你所追求的图形所需的全部内容(属性留待)。对于完整的示例设置,所有值都要看下面的示例:
var dataset = [{
"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
}
];
// Defining the size of the svg element
var w = 1000;
var h = 50;
// Defining the dataset
//d3.json("people.json", function(dataset) {
d3.select("body")
.selectAll("svg")
.data(dataset) // Bind the entire dataset
.enter().append("svg") // Append one svg per object in dataset
.attr("width", w)
.attr("height", h)
.selectAll("rect")
.data(function(d) { // Inherit data from svg by mapping of objects
// Create an array of number of required rects
return d3.range(d.age).map(function(d) { return d*15; });
})
.enter().append("rect") // Append rects per svg
.attr("width", 10)
.attr("height",10)
.attr("x", function(d) {
return d; // Position was calculated in above mapping in data()
});
//});
&#13;
<script src="https://d3js.org/d3.v4.js"></script>
&#13;
答案 1 :(得分:1)
你可以这样做:
var dataset = [{
"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
}];
// Defining the size of the svg element
var w = 1000;
var h = 500;
var itemH = 10;
var itemW = 3;
var padding = 2;
// Append svg element
var svg = d3.select("body").append("svg")
.attr("width", w)
.attr("height", h);
// Append groups for each in dataset
var group = svg.selectAll("g")
.data(dataset)
.enter()
.append('g');
// Each group
group.each(function(d, i) {
//console.log(d);
var age = d.age;
var self = d3.select(this);
// Transform the group in Y
self.attr('transform', function() {
return 'translate(0,' + (i * (itemH + padding)) + ')';
});
// Append rect forEach from 0 - age
d3.range(age).forEach(function(i) {
//console.log(i);
self.append('rect')
.attr('width', itemW)
.attr('height', itemH)
.attr('x', function() {
return i * (itemW + padding);
});
});
});
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<body></body>
&#13;