简体   繁体   中英

Google Charts data array made from input values javascript

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?

the point of setOnLoadCallback is to pass a function,
so that you know when google charts has finished loading.
setOnLoadCallback will call the function you pass to it.

eg -- when finished loading, fixArray will be called.

google.charts.setOnLoadCallback(fixArray);

you can also use the promise the load statement returns...

google.charts.load('current', {
  packages: ['corechart']
}).then(fixArray);

next, when using jquery to get an element by id,
you need to add the pound sign ( #number ) to the front of the id...

$('#number').val()

and you can use the val method to get the value.
also, here, you're getting the element,
not the element's value...

var day = document.getElementById("day");

just use the same as the rest...

var day = Number($('#day').val());

next, I've given the input elements a default value,
so the chart will draw on first run.

not really sure how you want the page to "flow".

see following working snippet...

 google.charts.load('current', { packages: ['corechart'] }).then(fixArray); 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').val()); var day = Number($('#day').val()); var weight = Number($('#weight').val()); 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 src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://www.gstatic.com/charts/loader.js"></script> <form onsubmit="fixArray()"> <table> <tr> <td>Number:</td> <td><input id="number" name="number" type="number" value="0"></td> </tr> <tr> <td>Day:</td> <td><input id="day" name="day" type="text" value="32"></td> </tr> <tr> <td>Weight:</td> <td><input id="weight" name="weight" type="text" value="100"></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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM