I have a problem with making charts with google chart. My page need to choose right array, fix it with input value and then show it on a line chart. The script code:
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type = "text/javascript" src = "prototype.js"></script>
<script type="text/javascript">
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback();
function fixArray(){
var array_0 = [['Day', 'research', 'Weight'], ['32', 1898, null], ['33', 2432, null], ['34', 2533, null], ['35', 2267, null],
['36', 2649, null], ['37', 3018, null], ['38', 3264, null], ['39', 3460, null],
['40', 3561, null], ['41', 3706, null], ['42', 3576 , null], ['43', 3465, null]];
var array_1 = [['Day', 'research', 'Weight'], ['32', 1405, null], ['33', 1779, null], ['34', 2158, null], ['35', 2272, null],
['36', 2493, null], ['37', 2783, null], ['38', 2984, null], ['39', 3185, null],
['40', 3310, null], ['41', 3426, null], ['42', 3531, null], ['43', 3784, null]];
.
.
.
var array_10 = [['Day', 'research', 'Weight'], ['32', 2605 ,null], ['33', 3402, null], ['34', 3500, null], ['35', 3705, null],
['36', 3810, null], ['37', 3856, null], ['38', 3955, null], ['39', 4120, null],
['40', 4260, null], ['41', 4100, null], ['42', 4000 ,null], ['43', 3900, null]];
var fixed = new Array(12); // defining empty 2D array to the fixed array that will be show
for (var i = 0; i < fixed.length; i++)
fixed[i] = [];
var number = Number($('number').getValue());
var day = document.getElementById("day");
var weight = Number($('weight').getValue());
switch (number) {
case 0:
for(i=1; i<=12; i++){
if(array_0[i][0] == day)
array_0[i][2] = weight;
}
fixed = array_0;
break;
case 1:
for(i=1; i<=12; i++){
if(array_1[i][0] == day)
array_1[i][2] = weight;
}
fixed = array_1;
break;
.
.
.
case 10:
for(i=1; i<=12; i++){
if(array_10[i][0] == day)
array_10[i][2] = weight;
}
fixed = array_10;
break;
default:
alert("Not Enough Data");
break;
}
drawVisualization(fixed);
}
function drawVisualization(fixed) {
var data = google.visualization.arrayToDataTable(Array.from(fixed));
var options = {
title : 'Results',
vAxis: {title: 'Weight'},
hAxis: {title: 'Day'},
seriesType: 'line',
series: {1: {type: 'scatter'}}
};
var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
$..getValue() taken from Prototype - Form.Element getValue() Method
My html form part:
<form onsubmit="fixArray()">
<table>
<tr>
<td>Number:</td>
<td><input id="number" name="number" type="number"></td>
</tr>
<tr>
<td>Day:</td>
<td><input id="day" name="day" type="text"></td>
</tr>
<tr>
<td>Weight:</td>
<td><input id="weight" name="weight" type="text"></td>
</tr>
<tr>
<td>
<input name="Submit1" type="submit" value="Send" style="width: 80px; height: 35px"></td>
<td>
<input name="Reset1" type="reset" value="Reset" style="width: 80px; height: 35px"></td>
</tr>
</table>
</form>
<div id="chart_div" style="width: 900px; height: 500px"></div>
When I try to run the page and enter values it show me nothing but red error where is the chart div.
I can see what is error when I change google.charts.setOnLoadCallback(); to google.charts.setOnLoadCallback(fixArray); and the error is "All series on a given axis must be of the same data type×"
I guess it's not a problem of fixing the array because when I try drawVisualization(array_0) at the code instead drawVisualization(fixed) for check, it show the same error.
What is the problem here?