Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an associative array as data for D3

Tags:

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?

like image 447
Malcolm Crum Avatar asked Mar 06 '12 18:03

Malcolm Crum


People also ask

How do you find the data from an associative array?

Answer: Use the PHP array_values() function You can use the PHP array_values() function to get all the values of an associative array.

How do you use associative array?

Associative array will have their index as string so that you can establish a strong association between key and values. The associative arrays have names keys that is assigned to them. $arr = array( "p"=>"150", "q"=>"100", "r"=>"120", "s"=>"110", "t"=>"115"); Above, we can see key and value pairs in the array.

Is an associative array a data structure?

An Associative Array Data Structure is a collection data structure that facilitates the storage, update, and retrieval of key-value pairs with the same key. AKA: Key-Value Table, Associative Lookup Container, Map Structure, Dictionary Structure.

Can associative array be represented using linked list?

They can be implemented using an association list, or by overlaying a doubly linked list on top of a normal dictionary.


1 Answers

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; }).

like image 132
Lars Kotthoff Avatar answered Sep 28 '22 04:09

Lars Kotthoff