使用关联数组作为D3的数据 [英] Using an associative array as data for D3
问题描述
我有一个非常简单的D3示例,首先将数据读入关联数组,然后将其显示在条形图中。
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; });
上述不起作用。
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?
推荐答案
=https://github.com/mbostock/d3/wiki/Arrays#wiki-d3_values> d3.values 或 d3.entries 直接使用关联数组。你只需要在设置属性的函数中考虑它(例如 function(d){return d.value;}
)。
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; }
).
这篇关于使用关联数组作为D3的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!