2014-04-25 33 views
0

當試圖使用Google圖表代碼繪製折線圖時,我收到此錯誤Google圖表錯誤

錯誤:類型不匹配。值0.8與列索引中的類型編號不匹配0

'0.8'是指代碼中的值p1。

function drawChart() { 

// Create the data table. 
var data = new google.visualization.DataTable(); 
data.addColumn('number', 'Topping'); 
data.addColumn('number', 'Slices'); 
data.addRows([ 
    [p1,1.89], 
[ch_period[17],5], 
[3,2], 
[5,2], 
[5,2], 
[6,7] 
]); 

// Set chart options 
var options = {'title':'How Much Pizza I Ate Last Night', 
      'width':400, 
      'height':300}; 

// Instantiate and draw our chart, passing in some options. 
var chart = new google.visualization.LineChart(document.getElementById('chart_div')); 
chart.draw(data, options); 
+0

你確定'p1'是一個數字嗎?嘗試parseInt(p1) – Kai

回答

1

我做你的代碼的jsfiddle的作品:http://jsfiddle.net/kychan/Dfx4V/1/

var p1  = parseInt('4'), 
    ch_period = {'17':4}; 

function drawChart() { 

    // Create the data table. 
    var data = new google.visualization.DataTable(); 
    data.addColumn('number', 'Topping'); 
    data.addColumn('number', 'Slices'); 
    data.addRows([ 
     [p1, 1.89], 
     [ch_period[17], 5], 
     [3, 2], 
     [5, 2], 
     [5, 2], 
     [6, 7] 
    ]); 

    // Set chart options 
    var options = { 
     'title': 'How Much Pizza I Ate Last Night', 
      'width': 400, 
      'height': 300 
    }; 

    // Instantiate and draw our chart, passing in some options. 
    var chart = new google.visualization.LineChart(document.getElementById('chart_div')); 
    chart.draw(data, options); 
} 


drawChart(); 

問題是p1(也許ch_period)不是類型number。因此,您必須使用parseInt(p1)/ parseInt(ch_period)將其作爲數字,或手動將其分配給一個數字。

+0

p1和ch_period都是數字 – user3471759

+0

確定嗎? Javascript與其值類型非常不一致。只要需要,它可以從Number跳轉到String。你有沒有試過添加'parseInt(p1)'?我給的代碼作品。當我刪除parseInt時,它會返回與你一樣的錯誤。 – Kai

+0

好吧,我試圖parseInt和圖被繪製,但我想繪製[0,Ch [0] [11]],但是當我添加parseInt Ch [0] [11]在圖上出現1或0。有任何想法嗎? parseInt四捨五入任何機會?如果是這樣,我如何制止小數的數字。 – user3471759