I am attempting to generate a specific number of elements using D3, extracting this information from a json file. Essentially, if the json data provided is n, I aim to display n elements within my svg.
This snippet showcases my code:
// Setting up the dimensions of the svg element
var w = 1000;
var h = 50;
// Establishing the dataset
d3.json("people.json", function(dataset) {
// Cycling through the json data
for(var i=0; i<dataset.length; i++) {
var svg = d3.select("body").append("svg")
.attr("width", w)
.attr("height", h);
// Looping 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 })
}
}
});
This represents an example of my json file (featuring arbitrary age values):
[{
"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
}
]
The anticipated outcome should resemble the following ( [-] --> svg rectangle)
Larry Page: [-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-]
Sergey Bean: [-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-][-]
and so forth...
Could you assist me in identifying any errors in my approach?
Appreciate it!