简体   繁体   English

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

[英]dynamic google chart, Can't add new data

I call updateChart(d) functions via ajax and pass 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>

When I try to add the next value for the chart, it throws an error:当我尝试为图表添加下一个值时,它会引发错误:

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

I did not find any solutions on the Internet (This is for a robot, it swears that there is not enough text).我在网上没有找到任何解决方案(这是一个机器人,它发誓没有足够的文字)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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