Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
143
rated 0 times [  148] [ 5]  / answers: 1 / hits: 19382  / 12 Years ago, tue, march 6, 2012, 12:00:00

I have a very simple D3 example that first reads data into an associative array, then displays it in a bar graph.



I can't seem to get anything to display using this method though. Instead, I have to insert a task in between: Read the data into an associative array, copy that data into a simple array, then display the bar graph using the simple array.



chart.selectAll(div)
.data(genreAssociative)
.enter().append(div)
.style(width, function(d) { return d * 10 + px; })
.text(function(d) { return d; });


The above does not work.



genreSimple = [];
for (var genre in genreAssociative) genreSimple.push(genreAssociative[genre]);
chart.selectAll(div)
.data(genreSimple)
.enter().append(div)
.style(width, function(d) { return d * 10 + px; })
.text(function(d) { return d; });


The above does; using a simple array as an intermediary. Is there a special way to iterate over an associative array instead of a standard array?


More From » d3.js

 Answers
20

You can use the functions d3.values or d3.entries to work directly with associative arrays. You simply need to take it into account in the functions that set attributes (e.g. function(d) { return d.value; }).


[#87022] Monday, March 5, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
valentinam

Total Points: 166
Total Questions: 117
Total Answers: 81

Location: Puerto Rico
Member since Sun, Jun 27, 2021
3 Years ago
;