动态 google 图表,无法添加新数据

dynamic google chart, Can't add new data

我通过 ajax 调用 updateChart(d) 函数并传递新数据。

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
    var chart;
    var dataTable;
    var options;
    var i = 1;

    google.charts.load('current', {'packages':['corechart']});
    google.charts.setOnLoadCallback(drawChart);
    function drawChart() {
        dataTable = google.visualization.arrayToDataTable([
            ['Amplitude', 'time'],
            ['0', 0]
        ]);
        options = {
            title: '',
            curveType: 'function',
            legend: { position: 'bottom' }
        };

        chart = new google.visualization.LineChart(document.getElementById('curve_chart'));
        chart.draw(dataTable, options);
    }
    
    function updateChart(d) {
        i++;
        dataTable.setValue(i, d); // Вот тут
        chart.draw(dataTable, options);

    }
</script>

当我尝试为图表添加下一个值时,出现错误:

Uncaught Error: Invalid row index 2. Should be in the range [0-0]

网上没找到解决方法(这个是给机器人的,发誓文字不够)

function updateChart(d) {
        i++;
        console.log(i+"======"+d);
        //data.setValue(i,1,d);
        data.addRow([String(i), Number(d)]);
        chart.draw(data, options);
    }