d3.js Tag Cloud size from a Json/array?

筅森魡賤 提交于 2019-12-04 07:31:13

Try d3.zip: d3.zip(jWord, jCount) returns a merged array where the first element is the text and size of the first word [jWord[0], jCount[0]], the second element is the second word, and so on. For example:

.words(d3.zip(jWord, jCount).map(function(d) {
  return {text: d[0], size: d[1]};
}))

In effect, d3.zip turns column-oriented data into row-oriented data. You could also just represent your data in row-oriented form to begin with:

var words = [
  {text: "abc", size: 2},
  {text: "def", size: 5},
  {text: "ghi", size: 3},
  {text: "jkl", size: 8}
];

Lastly, watch out with types. Your counts are represented as strings ("2") rather than numbers (2). So you might want to use + to coerce them to numbers.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!