由输入值javascript构成的Google Charts数据数组

问题描述 投票:1回答:1

我在用Google图表制作图表时遇到问题。我的页面需要选择正确的数组,用输入值修复它,然后在折线图上显示它。脚本代码:

<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()取自Prototype - Form.Element getValue() Method] >>

我的html表单部分:

<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>

[当我尝试运行该页面并输入值时,除了红色错误以外,什么都没有显示。div图表在哪里。我将google.charts.setOnLoadCallback();更改为google.charts.setOnLoadCallback(fixArray);时看到错误是错误,错误是“给定轴上的所有序列必须具有相同的数据类型×”

我想这不是修复数组的问题,因为当我在代码中尝试使用drawVisualization(array_0)而不是drawVisualization(fixed)进行检查时,它显示了相同的错误。

这里是什么问题?

我在用Google图表制作图表时遇到问题。我的页面需要选择正确的数组,用输入值修复它,然后在折线图上显示它。脚本代码:

javascript html charts google-visualization
1个回答
0
投票

setOnLoadCallback的要点是传递函数,让您知道Google图表何时加载完毕。setOnLoadCallback将调用您传递给它的函数。

© www.soinside.com 2019 - 2024. All rights reserved.