C3.js“未捕获的错误:源数据缺less(1,844)处的组件!”

我正在使用C3.js和Electron(Atom Shell)为数据可视化制作桌面应用程序。 我无法将我的数据input到C3中。 我有一个DataArray包含所有的坐标:DataAray = [[x1,y1],[x2,y2],[x3,y3],…]。 我使用下面的代码将其分解成一个xData数组和一个yData数组:

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' } }); 

但是当我运行应用程序,我得到这个错误: “未捕获的错误:源数据缺less(1,844)!”组件,源:path/到/ c3-0.4.10 / c3.min.js(2)

并没有绘制图表。 如果我改变for循环

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

但是,它确实绘制了图表。 之前我曾经使用Plotly,而且我曾经运行完全相同的代码来为Plotly准备数据,而且工作得很好。 这里有什么问题? 另外,有没有办法让C3忽略数据中的错误? 例如,如果在其中一个点上有一个空值,那么C3有没有办法绘制graphics?

我不知道是否有一种方法来configurationC3忽略空值或未定义的值。 尝试忽略空值/未定义值,以便C3可以绘制graphics。

 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); } ... } 

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]); } ... } 

C3有一个内部的function,可以帮助做到这一点

 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; };