0

I've trying to use google charts to make chart from an array that I need to choose from a bunch of arrays. One of my problems is to make 2D array that get chosen from 10 another 2D arrays. This is the parts of my code:

Starting the script and defining the arrays:

    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
      google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(drawVisualization);

    var arrays = new Array();
    arrays[0] = [['Day', 'research', 'Weight'], ['32', 1.4, null], ['33', 2.42, null], ['34', 2.5, null], ['35', 2.2, null],
                ['36', 2.6, null], ['37', 3.0, null], ['38', 3.2, null], ['39', 3.4, null],
                ['40', 3.561, null], ['41', 3.706, null], ['42', 3.576 , null], ['43', 3.465, null]];

    arrays[1] = [['Day', 'research', 'Weight'], ['32', 1.405, null], ['33', 1.779, null], ['34', 2.158, null], ['35', 2.272, null],
                ['36', 2.493, null], ['37', 2.783, null], ['38', 2.984, null], ['39', 3.185, null],
                ['40', 3.310, null], ['41', 3.426, null], ['42', 3.531, null], ['43', 3.784, null]];
    ...                          

    arrays[10] = [['Day', 'research', 'Weight'], ['32', 2.605 ,null], ['33', 3.402, null], ['34', 3.500, null], ['35', 3.705, null],
                 ['36', 3.810, null], ['37', 3.856, null], ['38', 3.955, null], ['39', 4.120, null],
                 ['40', 4.260, null], ['41', 4.100, null], ['42', 4.000 ,null], ['43', 3.900, null]];

By using the variables below (taken from the user), fixing the right array:

      function fixArray(){
        var number = document.getElementById("number");
        var day = document.getElementById("day");
        var w = document.getElementById("weight");
        var fixed = new Array();
        for (i=1; i<12;i++){
            if(day== arrays[number][i][0]){
                arrays[number][i][2] = w;
                }
            }
            fixed = arrays[number];
        drawVisualization(fixed);
        }

The Google Charts drawVisalization that take the right array after we enter the weight instead of null at the right place

      function drawVisualization(fix) {
        var data = google.visualization.arrayToDataTable(fix);

          var options = {
          title :'results using the research',
          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>

The table that get all the information from the user:

<table cellpadding="10" style="width: 506px; height: 498px">
    <tr>
        <td>number</td>
        <td><input id="number" name="number" type="text"></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" onclick="fixArray()" style="width: 80px; height: 35px"></td>
        <td>
        <input name="Reset1" type="reset" value="Reset" style="width: 80px; height: 35px"></td>
    </tr>

</table>
 <div id="chart_div" style="width: 900px; height: 500px"></div>

When I try to check the web, the console show me an error at the if statement (line 77):

main.html:77 Uncaught TypeError: Cannot read property '1' of undefined
    at fixArray (main.html:77)

what is the problem? did I define the arrays well? or how do I get all the inforamtion from the user, "fix" the table and show it up with Google Charts?

  • Looks to me like your loop check is off one. Your array indices are 1..10. Your loop checks for <12. Maybe it should be <11? I'm looking in function fixArray(). If that's not it, then what code is at line 77 in main.html? –  Apr 23 '20 at 21:24
  • I put 10 as an example of the data. The for at fixArray should run over the 12 elements inside each array[number]. The error is the line code:" if(day== arrays[number][i][0])" – Ram Rahamim Apr 23 '20 at 21:28
  • I edit my comment. By mistakley I posted it first without it – Ram Rahamim Apr 23 '20 at 21:31
  • One more thing. You do `var number = document.getElementById("number");` and then use 'number' (I think) as a numeric value. number is an element, not a number. Maybe you want its value? Right, so you need the value of the element, not the element itself. number.value maybe? –  Apr 23 '20 at 21:32
  • Yes I need it as value – Ram Rahamim Apr 23 '20 at 21:32

0 Answers0