d3 json nest data mapping, grouped bar chart

时间:2017-04-10 01:43:39

标签: javascript json d3.js

I've got a situation my d3.nest code don't understand. and this is my first question in Stackoverflow, so plz help his poor guy!!

here is my code

    var nested = d3.nest()
  .key(function(d) { return d.AREA})
  .key(function(d) { return d.LINE})
  .rollup(function(leaves) {
  return [{
    key: leaves[0]['LINE_LENGTH'],
    value: leaves[0]['avr']
  }, {
    key: " ",
    value: leaves[0]['max']
  }];
    })
  .entries(data);

}

and the problem is this.

var lines = nested[0].values.map(function(d, i) {
return console.log(JSON.stringify(d.key));});

this code I overwrite to see in the console and it should be A1,A2,B1,B2,B3,C1,C2,C3,C4,C5 like this but only I can see A1,A2 here is my nested array

[{"key":"America","values":[{"key":"A1","values":[{"key":"3M","value":"0.0002"},{"key":" ","value":"0.0002"}]},{"key":"A2","values":[{"key":"4M","value":"0.00030000000000000003"},{"key":" ","value":"0.00030000000000000003"}]}]},{"key":"EU","values":[{"key":"B1","values":[{"key":"2M","value":"0.0004"},{"key":" ","value":"0.0004"}]},{"key":"B2","values":[{"key":"2M","value":"0.0005"},{"key":" ","value":"0.0005"}]},{"key":"B3","values":[{"key":"2M","value":"0.0006000000000000001"},{"key":" ","value":"0.0006000000000000001"}]}]},{"key":"ASIA","values":[{"key":"C1","values":[{"key":"2M","value":"0.0007"},{"key":" ","value":"0.0007"}]},{"key":"C2","values":[{"key":"2M","value":"0.0008"},{"key":" ","value":"0.0008"}]},{"key":"C3","values":[{"key":"2M","value":"0.0009000000000000001"},{"key":" ","value":"0.0009000000000000001"}]},{"key":"C4","values":[{"key":"2M","value":"0.001"},{"key":" ","value":"0.001"}]},{"key":"C5","values":[{"key":"2M","value":"0.0011"},{"key":" ","value":"0.0011"}]}]}]

1 个答案:

答案 0 :(得分:0)

最后我自己得到了线索,

线索在这里

var lines = [];for(var i=0; i<3; i++){nested[i].values.map(function(d, i) {return lines.push(d.key); });

}

我是系统工程师,但公司要我制作一个程序。

,这不是很糟糕但很好