2017-01-18 80 views
1

我試圖d.bubbledata.values.valued.bubbledata.values[0].value,但沒有得到結果。獲取從JSON數據D3.js

我怎樣才能得到的值的所有屬性?

我的代碼是:

var circles = svg.selectAll(".bubble") 
     .data(json) 
     .enter().append("circle") 
     .attr("class","bubble") 
     .attr("r", function(d) { 
      return d.bubbledata.values[0].value; 
     }) 

和我的JSON是:

{ 
      "categories": [ 
      "2003", 
      "2004" 
      ], 
"bubbledata": { 
    "values": [ 
     { 
     "id": "346462", 
     "name": "blabla1", 
     "color": "#3a3790", 
     "bordercolor": "#3a3790", 
     "value": 0.82908, 
     "label": 9.2942 
     }, 
     { 
     "id": "346131", 
     "name": "blabla2", 
     "color": "#ea772a", 
     "bordercolor": "#ea772a", 
     "value": 0.26954, 
     "label": 3.0216 
     } 
] 
} 
} 

回答

1

這裏的問題是傳遞給data()功能的數據。

在D3,data()接受三件事情:

  1. 數組
  2. 函數
  3. 沒有

因此,你不能傳遞一個對象,你現在在做什麼。你必須通過一個陣列data

var circles = svg.selectAll(".bubble") 
    .data(json.bubbledata.values)//this is an array 
    .enter().append("circle") 
    .attr("class","bubble") 
    .attr("r", function(d) { 
     return d.value;//the "value" in each object 
    }) 
+0

感謝。我想添加到上面。如果有人用 「forceCollide」,你必須添加'JSON.parse()來'。例如'return JSON.parse(d.somevalue)'。 –