0

我正在使用谷歌圖表,我試圖在表格上顯示趨勢線。只有表格的第一行實際顯示趨勢線。我也得到一個Uncaught(在promise中)TypeError:不能讀取未定義錯誤信息的屬性'X'。任何幫助,將不勝感激。 下面是錯誤的截圖,我得到:Google charts error messageGoogle Charts - Uncaught(承諾中)TypeError:無法讀取未定義的屬性'X'

這裏是我的代碼:

 var dataFororange = []; 
    var dataForred=[]; 
    var dataForblue=[]; 

    function calling(plot_data, color, row_num){ 
    if (color==1) 
     dataFororange[row_num]=plot_data;   
    if (color==2) 
     dataForred[row_num]=plot_data; 
    if (color==3) 
     dataForblue[row_num]=plot_data; 
    } 

    window.onload = function(e){ 
     google.charts.load('current', {packages: ['corechart', 'line']}); 
     google.charts.setOnLoadCallback(onChartLoad); 
    } 

    function onChartLoad() { 
    for(var i=0; i<20; i++){ 
     drawOrangeColor(dataFororange[i], i); 
     drawRedColor(dataForred[i], i); 
     drawBlueColor(dataForblue[i], i); 
    } 
    } 

    function drawOrangeColor(amount, counter) { 

    var dataA = new google.visualization.DataTable(); 
    dataA.addColumn('new Date(Year, Month, Day, Hours, Minutes, Seconds, 
    Milliseconds)', 'X'); 
    dataA.addColumn('number', 'price'); 

    for(var i= 0; i<amount.X.length; i++){ 
     var date_string = amount.X[i]; 
     var year=date_string.slice(6,10); 
     var month=date_string.slice(3,5); 
     var day=date_string.slice(0,2); 
     var dataY = amount.Y[i]; 
     dataA.addRows([[new Date(parseInt(year), parseInt(month)-1, 
    parseInt(day)), dataY]]); 
    } 
    var optionA = { 
     legend: 'none', 
     vAxis: { baselineColor: 'none', 
       gridlineColor: 'none', 
       textPosition: 'none' }, 
     hAxis: { baselineColor: 'none', 
       gridlineColor: 'none', 
       textPosition: 'none' }, 
       format: 'dd/MM/yyyy', 
     backgroundColor: 'none', 
     colors:['#ff8800'] 
    }; 
    var pos = "A_chart"+counter; 
    var selected = document.getElementById(pos); 
    var chart = new google.visualization.LineChart(selected); 
    chart.draw(dataA, optionA); 
    } 

    //////////////////////////////////// 
    function drawRedColor(amount, counter) { 
    var dataB = new google.visualization.DataTable(); 
    dataB.addColumn('date', 'X'); 
    dataB.addColumn('number', 'price'); 
    for(var i= 0; i<amount.X.length; i++){ 
     var date_string = amount.X[i]; 
     var year=date_string.slice(6,10); 
     var month=date_string.slice(3,5); 
     var day=date_string.slice(0,2); 
     var dataY = amount.Y[i]; 
     dataB.addRows([[new Date(parseInt(year), parseInt(month)-1, 
    parseInt(day)), dataY]]); 
    } 
    var optionB = { 
     legend: 'none', 
     vAxis: { baselineColor: 'none', 
       gridlineColor: 'none', 
       textPosition: 'none' }, 
     hAxis: { baselineColor: 'none', 
       gridlineColor: 'none', 
       format: 'dd/MM/yyyy', 
       textPosition: 'none' }, 
     backgroundColor: 'none', 
     colors:['red'] 
    }; 
    var pos = "B_chart"+counter; 
    var selected = document.getElementById(pos); 
    var chart = new google.visualization.LineChart(selected); 
    chart.draw(dataB, optionB); 
    } 
    /////////// 
    function drawBlueColor(amount, counter) { 
    var dataC = new google.visualization.DataTable(); 
    dataC.addColumn('date', 'X'); 
    dataC.addColumn('number', 'price'); 
    for(var i= 0; i<amount.X.length; i++){ 
     var date_string = amount.X[i]; 
     var year=date_string.slice(6,10); 
     var month=date_string.slice(3,5); 
     var day=date_string.slice(0,2); 
     var dataY = amount.Y[i]; 
     dataC.addRows([[new Date(parseInt(year), parseInt(month)-1, 
    parseInt(day)), dataY]]); 
    } 
    var optionC = { 
     legend: 'none', 
     vAxis: { baselineColor: 'none', 
       gridlineColor: 'none', 
       textPosition: 'none' }, 
     hAxis: { baselineColor: 'none', 
       gridlineColor: 'none', 
       format: 'dd/MM/yyyy', 
       textPosition: 'none' }, 
     backgroundColor: 'none', 
     colors:['blue'] 
    }; 
    var pos = "C_chart"+counter; 
    var selected = document.getElementById(pos); 
    var chart = new google.visualization.LineChart(selected); 
    chart.draw(dataC, optionC); 
    } 

    function onUKChartLoad() {  
    drawOrangeColor(test_data); 
    drawRedColor(test_data); 
    drawBlueColor(test_data); 
    } 

`

回答

0

在你的代碼的33行,你想獲得屬性「X」的目的。我不知道什麼X應該是在這種情況下,但你可以嘗試改變

amount.X.length 

amount.length 

通過您的數組進行迭代。請記住,金額作爲函數中的參數傳入,因此如果在調用函數時沒有屬性「X」,則它將不確定。

相關問題