2015-08-03 59 views
0

我試圖從動態表中獲取值,之後我想用Google圖表繪製它,但代碼不起作用,我無法理解有什麼問題。我認爲問題與我用來從表中獲得價值的方法有關,但我不確定。從動態表中獲取值並使用谷歌圖表進行繪製

當我的價值觀,我嘗試畫它,谷歌返回以下錯誤:

數據列(個),軸#0不能是字符串類型的

誰能幫我解決我的代碼好嗎?

鏈接的jsfiddle:https://jsfiddle.net/macco_cl/qaogs3sx/

var theTable, theTableBody 
 
var myForm = document.forms.myForm; \t 
 
var png = document.getElementById("png"); 
 

 
function init() { 
 
    theTable = (document.all) ? document.all.myTABLE : 
 
     document.getElementById("myTABLE") 
 
    theTableBody = theTable.tBodies[0] 
 
} 
 
function appendRow(form) { 
 
    insertTableRow(form, -1) 
 
} 
 
function addRow(form) { 
 
    insertTableRow(form, form.insertIndex.value) 
 
} 
 
function insertTableRow(form, where) { 
 
    var now = new Date() 
 
    var nowData = [now.getHours(), now.getMinutes(), now.getSeconds(), 
 
     now.getMilliseconds()] 
 
    clearBGColors() 
 
    var newCell 
 
    var newRow = theTableBody.insertRow(where) 
 
    for (var i = 0; i < nowData.length; i++) { 
 
     newCell = newRow.insertCell(i) 
 
     newCell.innerHTML = nowData[i] 
 
     newCell.style.backgroundColor = "salmon" 
 
    } 
 
    updateRowCounters(form) 
 
} 
 
function removeRow(form) { 
 
    theTableBody.deleteRow(form.deleteIndex.value) 
 
    updateRowCounters(form) 
 
} 
 
function insertTHEAD(form) { 
 
    var THEADData = ["Hours","Minutes","Seconds","Milliseconds"] 
 
    var newCell 
 
    var newTHEAD = theTable.createTHead() 
 
    newTHEAD.id = "myTHEAD" 
 
    var newRow = newTHEAD.insertRow(-1) 
 
    for (var i = 0; i < THEADData.length; i++) { 
 
     newCell = newRow.insertCell(i) 
 
     newCell.innerHTML = THEADData[i] 
 
    } 
 
    updateRowCounters(form) 
 
    form.addTHEAD.disabled = true 
 
    form.deleteTHEAD.disabled = false 
 
} 
 
function removeTHEAD(form) { 
 
    theTable.deleteTHead()  
 
    updateRowCounters(form) 
 
    form.addTHEAD.disabled = false 
 
    form.deleteTHEAD.disabled = true 
 
} 
 
function insertTFOOT(form) { 
 
    var TFOOTData = ["Hours","Minutes","Seconds","Milliseconds"] 
 
    var newCell 
 
    var newTFOOT = theTable.createTFoot() 
 
    newTFOOT.id = "myTFOOT" 
 
    var newRow = newTFOOT.insertRow(-1) 
 
    for (var i = 0; i < TFOOTData.length; i++) { 
 
     newCell = newRow.insertCell(i) 
 
     newCell.innerHTML = TFOOTData[i] 
 
    } 
 
    updateRowCounters(form) 
 
    form.addTFOOT.disabled = true 
 
    form.deleteTFOOT.disabled = false 
 
} 
 

 
function removeTFOOT(form) { 
 
    theTable.deleteTFoot()  
 
    updateRowCounters(form) 
 
    form.addTFOOT.disabled = false 
 
    form.deleteTFOOT.disabled = true 
 
} 
 
function insertCaption(form) { 
 
    var captionData = form.captionText.value 
 
    var newCaption = theTable.createCaption() 
 
    newCaption.innerHTML = captionData 
 
    form.addCaption.disabled = true 
 
    form.deleteCaption.disabled = false 
 
} 
 
function removeCaption(form) { 
 
    theTable.deleteCaption()  
 
    form.addCaption.disabled = false 
 
    form.deleteCaption.disabled = true 
 
} 
 
// housekeeping functions 
 
function updateRowCounters(form) { 
 
    var sel1 = form.insertIndex 
 
    var sel2 = form.deleteIndex 
 
    sel1.options.length = 0 
 
    sel2.options.length = 0 
 
    for (var i = 0; i < theTableBody.rows.length; i++) { 
 
     sel1.options[i] = new Option(i, i) 
 
     sel2.options[i] = new Option(i, i) 
 
    } 
 
    form.removeRowBtn.disabled = (i==0) 
 
} 
 
function clearBGColors() { 
 
    for (var i = 0; i < theTableBody.rows.length; i++) { 
 
     for (var j = 0; j < theTableBody.rows[i].cells.length; j++) { 
 
      theTableBody.rows[i].cells[j].style.backgroundColor = ""   
 
     } 
 
    } 
 
} 
 

 
function GetCellValues() { 
 
\t \t 
 
     var table = document.getElementById("myTABLE"); 
 
     
 
     for (var r = 0, n = table.rows.length; r < n; r++) { 
 
      for (var c = 0, m = table.rows[r].cells.length; c < m; c++) { 
 
       alert(table.rows[r].cells[c].innerHTML); 
 
      } 
 
     } 
 
     
 
     return table; 
 
} 
 

 
function mostra() { 
 
\t document.getElementById("chart_div").style.display="block"; 
 
} 
 

 
function nascondi() { 
 
\t document.getElementById("chart_div").style.display="none"; 
 
} \t 
 
\t 
 
function finestra(){ 
 

 
\t \t if(tf == true){ 
 
\t \t \t window.open("pngbar.html"); 
 
\t \t \t //document.getElementById("chart_div").style.display="none"; 
 
\t \t } 
 
} 
 

 
function setDati(){ 
 
\t \t 
 
\t \t \t \t 
 
\t \t //if(png.checked) 
 
\t \t // \t tf = true; 
 
\t \t //else 
 
\t \t // \t tf = false; 
 
\t \t \t 
 
\t \t drawRightY(); 
 
\t \t 
 
} 
 
\t 
 
function drawRightY() { 
 
\t \t tf = false; 
 
\t \t var valori = GetCellValues(); \t 
 
\t \t 
 
\t \t 
 
\t \t var f = new Array(); 
 
\t \t 
 
\t \t \t \t 
 
\t \t for (var i=0;i<valori.rows.length;i++) { 
 
\t \t \t f[i]=new Array(); 
 
\t \t \t \t for (var j=0;j<valori.rows[i].cells.length;j++) { 
 
\t \t \t \t \t f[i][j]= valori.rows[i].cells[j].innerHTML; 
 
\t \t \t \t \t 
 
    \t \t \t } \t \t \t 
 
\t \t } 
 
\t \t 
 
\t \t var data = new google.visualization.arrayToDataTable(f,true); 
 

 
     var options = { 
 
     chart: { 
 
     // title: z, 
 
     }, 
 
     hAxis: { 
 
    
 
      minValue: 0, 
 
     }, 
 
     vAxis: { 
 

 
     }, 
 
    
 
     axes: { 
 
      y: { 
 
      0: {side: 'right'} 
 
      } 
 
     } 
 
     }; 
 
\t if (tf == false){ 
 
\t \t var material = new google.visualization.BarChart(document.getElementById('chart_div')); 
 
\t \t material.draw(data, options); 
 
\t } 
 
\t if (tf == true) { 
 
\t \t var chart_div = document.getElementById('chart_div'); 
 
\t \t var chart = new google.visualization.BarChart(chart_div); 
 

 
\t \t // Wait for the chart to finish drawing before calling the getImageURI() method. 
 
\t \t  google.visualization.events.addListener(chart, 'ready', function() { 
 
\t \t \t chart_div.innerHTML = '<img src="' + chart.getImageURI() + '">'; 
 
\t \t \t console.log(chart_div.innerHTML); 
 
\t \t \t 
 
\t \t }); 
 

 
\t \t options = {width: 1920, height: 1080} 
 
\t \t chart.draw(data, options); 
 
\t \t document.getElementById("chart_div").style.display="none"; 
 
\t \t window.open(chart.getImageURI()); 
 
\t \t 
 
\t \t } 
 
\t 
 
}
<HTML> 
 
<HEAD> 
 
<TITLE>Modifying Table Cell Content</TITLE> 
 
<STYLE TYPE="text/css"> 
 
THEAD {background-color:lightyellow; font-weight:bold} 
 
TFOOT {background-color:lightgreen; font-weight:bold} 
 
#myTABLE {background-color:bisque} 
 
</STYLE> 
 
\t <SCRIPT src="funzioni.js"></SCRIPT> 
 
    <SCRIPT type="text/javascript" src="https://www.google.com/jsapi"></SCRIPT> 
 
    <SCRIPT type="text/javascript"> 
 

 
     // Load the Visualization API and the piechart package. 
 
     google.load('visualization', '1.0', {'packages':['corechart']}); 
 

 
     // Set a callback to run when the Google Visualization API is loaded. 
 
     google.setOnLoadCallback(drawRightY()); 
 

 
</SCRIPT> 
 

 
</HEAD> 
 
<BODY onLoad="init(),nascondi()"> 
 
<H1>Modifying Tables</H1> 
 
<HR> 
 
<FORM NAME="controls"> 
 
<FIELDSET> 
 
<LEGEND>Add/Remove Rows</LEGEND> 
 
<TABLE WIDTH="100%" CELLSPACING=20><TR> 
 
<TD><INPUT TYPE="button" VALUE="Append 1 Row" 
 
    onClick="appendRow(this.form)"></TD> 
 
<TD><INPUT TYPE="button" VALUE="Insert 1 Row" onClick="addRow(this.form)"> at index: 
 
    <SELECT NAME="insertIndex"> 
 
     <OPTION VALUE="0">0 
 
    </SELECT></TD> 
 
<TD><INPUT TYPE="button" NAME="removeRowBtn" VALUE="Delete 1 Row" DISABLED 
 
    onClick="removeRow(this.form)"> at index: 
 
    <SELECT NAME="deleteIndex"> 
 
     <OPTION VALUE="0">0 
 
    </SELECT></TD> 
 
</TR> 
 
</TABLE> 
 
</FIELDSET> 
 
<FIELDSET> 
 
<LEGEND>Add/Remove THEAD and TFOOT</LEGEND> 
 
<TABLE WIDTH="100%" CELLSPACING=20><TR> 
 
<TD><INPUT TYPE="button" NAME="addTHEAD" VALUE="Insert THEAD" 
 
    onClick="insertTHEAD(this.form)"><BR> 
 
    <INPUT TYPE="button" NAME="deleteTHEAD" VALUE="Remove THEAD" DISABLED 
 
     onClick="removeTHEAD(this.form)"> 
 
</TD> 
 
<TD><INPUT TYPE="button" NAME="addTFOOT" VALUE="Insert TFOOT" 
 
    onClick="insertTFOOT(this.form)"><BR> 
 
    <INPUT TYPE="button" NAME="deleteTFOOT" VALUE="Remove TFOOT" DISABLED 
 
     onClick="removeTFOOT(this.form)"> 
 
</TD> 
 
</TR> 
 
</TABLE> 
 
</FIELDSET> 
 
<FIELDSET> 
 
<LEGEND>Add/Remove Caption</LEGEND> 
 
<TABLE WIDTH="100%" CELLSPACING=20><TR> 
 
<TD><INPUT TYPE="button" NAME="addCaption" VALUE="Add Caption" 
 
    onClick="insertCaption(this.form)"></TD> 
 
<TD>Text: <INPUT TYPE="text" NAME="captionText" SIZE=40 VALUE="Sample Caption"> 
 
<TD><INPUT TYPE="button" NAME="deleteCaption" VALUE="Delete Caption" DISABLED 
 
    onClick="removeCaption(this.form)"></TD> 
 
    
 
<TD><INPUT TYPE="button" NAME="Prendi valori" VALUE="get" 
 
    onClick="GetCellValues()"></TD> 
 

 
</TR> 
 
</TABLE> 
 
</FIELDSET> 
 
<!-- PNG<input type="checkbox" id="png" value="false" />--> 
 
\t <input type="button" value="Draw Chart" onclick="mostra(),setDati()" /> 
 
    
 
    <!--Div that will hold the pie chart--> 
 
\t 
 
\t \t <div id="chart_div"></div> 
 

 
</FORM> 
 
<HR> 
 
<TABLE ID="myTABLE" CELLPADDING=10 BORDER=1> 
 
<TBODY> 
 
</TABLE> 
 

 

 
\t </BODY> 
 
</HTML>

+0

你應該在JSFiddle中設置它,以便我們可以測試和使用它。 –

+0

我將鏈接添加到JSFiddle。 – macco

回答

0

你應該儘量縮小你的問題,並張貼只有相關的代碼。

您遇到的問題是,當您將表中的值添加到「dataArray」時,您不會將字符串的HTML表值轉換爲數字,這會導致整個輸入嘗試用數字表示字符串。

我修改了您的數組構建循環,並添加了if(i > 0)(如果它不再是第一行(標題))。整個循環現在看起來像:

for (var i=0;i<valori.rows.length;i++) { 
     f[i]=new Array(); 
      for (var j=0;j<valori.rows[i].cells.length;j++) { 
       if(i > 0){ 
        f[i][j]= Number(valori.rows[i].cells[j].innerHTML); 
       }else{ 
        f[i][j]= valori.rows[i].cells[j].innerHTML; 
       } 
     }   
    } 

,它現在就像一個魅力。

鏈接到jsfiddle

+0

感謝您的幫助,現在我繪製了圖表,但列並未出現小時,此外,當我添加兩行或多行時,圖表不會將這些行添加爲條形圖。 – macco