2015-10-04 42 views
1

我使用C3.js和Electron(Atom Shell)爲數據可視化創建桌面應用程序。我無法將數據提供給C3。我有一個包含所有座標的DataArray:DataAray = [[x1,y1],[x2,y2],[x3,y3],...]。 我使用下面的代碼把它分解成一個擴展數據陣列和YDATA陣列:C3.js「未捕獲錯誤:源數據缺少(1,844)處的組件!」

xData = []; 
yData=[]; 
xData.push('data1_x'); 
yData.push('data1'); 
for (var i = 0; i < DataArray.length ; i++){ 
     xData.push (DataArray[i][0]); 
     yData.push (DataArray[i][1]); 
       } 
var chart = c3.generate({ 
      bindto: '#chart', 
      data: { 
       xs: { 
        data1: 'data1_x', 

       }, 
       columns: [ 
         x, 
         y 
          ], 
       type: 'scatter' 
         } 
        }); 

但是當我運行應用程序,我得到這個錯誤: 「未捕獲錯誤:源數據丟失的成分在(1,844)!「,來源:PATH/To/c3-0.4.10/c3.min.js(2)

並且根本沒有繪製圖表。如果我將for循環更改爲

for (var i = 0; i < 843 ; i++) 

但是,它確實繪製了圖形。 我之前使用Plotly,而且我曾經運行完全相同的代碼來爲Plotly準備數據,並且它工作得很好。這裏有什麼問題?另外,有沒有辦法讓C3忽略數據中的錯誤?例如,如果其中一個點有空,那麼C3有沒有辦法繪製圖形?

回答

1

我不知道是否有辦法配置C3忽略空值或未定義的值。嘗試忽略空值/未定義值,以便C3可以繪製圖。

for (var i = 0; i < DataArray.length ; i++) { 
    // if the value is not undefined or null, push to array 
    if (DataArray[i][0] !== undefined && DataArray[i][0] !== null) { 
     xData.push (DataArray[i][0]); 
    } else { 
     // push 0 to signify no data 
     xData.push(0); 
    } 
    ... 
} 
0

C3將忽略空值。但是,如果某個值未定義,則會出現此錯誤。 TonalLynx的答案將起作用。或者您可以將未定義的值更改爲null。

for (var i = 0; i < DataArray.length ; i++) { 
// if the value is undefined, push null to array 
if (DataArray[i][0] === undefined) { 
    xData.push (null); 
} else { 
    xData.push (DataArray[i][0]); 
} 
... 
} 
0

C3有一個內部的功能,可以幫助做到這一點

c3_chart_internal_fn.convertColumnsToData = function (columns) { 
    var new_rows = [], i, j, key; 
    for (i = 0; i < columns.length; i++) { 
     key = columns[i][0]; 
     for (j = 1; j < columns[i].length; j++) { 
      if (isUndefined(new_rows[j - 1])) { 
       new_rows[j - 1] = {}; 
      } 
      if (isUndefined(columns[i][j])) { 
       throw new Error("Source data is missing a component at (" + i + "," + j + ")!"); 
      } 
      new_rows[j - 1][key] = columns[i][j]; 
     } 
    } 
    return new_rows; 
}; 
相關問題