2016-11-30 64 views
0

我想用nvd3數據創建條形圖。分組選項工作正常,但是當我選擇堆疊時,它會產生以下錯誤。NVD3堆疊條形圖選項不起作用

Uncaught TypeError: Cannot read property '1' of undefined(…)

JSON格式如下。

var test = [ 

    { 
     "key":"A", 
     "values": 
      [ 
       {"x":"2016-11-24","y":34}, 
       {"x":"2016-11-25","y":10} 
       ] 
    }, 
    { 
     "key":"B", 
     "values": 
      [ 
       {"x":"2016-11-25","y":15} 
      ] 
    }, 
    { 
     "key":"C", 
     "values": 
      [ 
       {"x":"2016-11-28","y":11} 
       ] 
    }, 
] 

javascript代碼:

var chart; 
nv.addGraph(function() { 
    chart = nv.models.multiBarChart() 
    .color(d3.scale.category10().range()) 
     .rotateLabels(0)  //Angle to rotate x-axis labels. 
     .transitionDuration(300) 
     .showControls(true) //Allow user to switch between 'Grouped' and 'Stacked' mode. 
     .groupSpacing(0.24) //Distance between each group of bars. 

     ; 

chart.reduceXTicks(false).staggerLabels(true).groupSpacing(0.3); 

chart.x(function(d) { return d.x; }); 
chart.y(function(d) { return d.y; }); 

    d3.select('#chart1 svg') 
     .datum(test) 
     .call(chart); 

    nv.utils.windowResize(chart.update); 



    return chart; 
}); 

我都嘗試過,但無法找到答案。任何幫助?

回答

0

答案在JSON數據中。基本上,values陣列在所有數據序列中應該具有相同的長度。在您的示例中,當nvd3將數據轉換爲堆棧視圖時,它期望具有values數組的第二個元素。

{ 
"key":"B", 
"values": 
    [ 
     {"x":"2016-11-25","y":15} 
    ] 
}