2013-02-11 105 views
1

我有以下結構的JSON列表:D3:無效屬性[對象的對象]

[ 
    {'uid': 4, 'x': -1.2354657, 'y': -0.992893} 
    {'uid': 26, 'x': 0.2783682, 'y': -0.1931} 
    ... 
    {'uid': 26, 'x': -0.278356, 'y': 0.082983} 
] 

我試圖svg:circle元件附加到svg在通過在xy值指定的點這個清單。 AJAX請求正常工作,但我一直收到Error: Invalid value for <circle> attribute cy="[object Object]"

相關javascript代碼:

var svg = d3.select('#d3') 
     .append('svg') 
     .append('g') 
      .attr('class', 'main') 

    ... 

var x = d3.scale.linear() 
     .domain(d3.extent(data, function (d) {return d.x;})) 
     .range([0, width]); 

var y = d3.scale.linear() 
     .domain(d3.extent(data, function (d) {return d.y;})) 
     .range([height, 0]); 
    ... 

svg.selectAll('.bloom') 
     .data(data) 
    .enter() 
    .append('g') 
     .attr('class', 'bloom') 
    .append('circle') 
     .attr('class', 'bloom-circle') 
     .attr('cx', function (d) {return x(d.x);}) 
     .attr('cy', function (d) {return y(d.y);}) 
     .attr('r', 10) 
     .style('fill', 'purple') 
     .style('stroke', 'white') 
     .style('stroke-width', '5px'); 

我真的不知道發生了什麼事情,因爲d3.scale.linear應始終在其range返回一個值。有人知道爲什麼會發生這種情況嗎?

+0

x和y字符串或數字? – Wex 2013-02-11 19:36:36

回答

0

當範圍沒有很好地指定時,秤會返回一個空對象。

d3.scale.linear.domain([0, 1]).range([0, undefined]) 
d3.scale.linear.domain([0, 1]).range([0, NaN]) 

因此,被heightwidth在執行時良好定義:例如,當調用的任何值滿足以下兩個秤會返回{}