d3 js按字母顺序汇总

时间:2016-12-08 13:35:18

标签: javascript d3.js

我在json数据下方绘制了一个图形中的多个矩形

data.push(["Shanghai","Los Angeles","33333333.33"]);
data.push(["Los Angeles","Shanghai","222222.33"]);

data.push(["Los Angeles","Shanghai","22.33"]);
data.push(["chennai","Bombay","33333333.33"]);

data.push(["Canada","Japan","33333333.33"]);
data.push(["Japan","Canada","222222.33"]);

data.push(["Bombay","Canada","222222222.33"]);
data.push(["Bombay","Belgaum","222222.33"]);
data.push(["Bombay","Delhi","222222.33"]);

如何根据第一个字符数据(如B,C等分组)对上述数据进行分组

1 个答案:

答案 0 :(得分:3)

您可以使用d3.nest()

var d = d3.nest()
  .key(function(d) { 
      return d[0].charAt(0); // 1st char of the 1st item of the array
   })
  .entries(data);

请参阅http://jsfiddle.net/rc32kdxn/