动态的Google图表,可以添加新数据

发布于 2025-01-29 06:19:45 字数 1132 浏览 2 评论 0原文

我通过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]

我在Internet上找不到任何解决方案(这是针对机器人,它发誓没有足够的文本)。

I call updateChart(d) functions via ajax and pass new data.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

电影里的梦 2025-02-05 06:19:45
function updateChart(d) {
        i++;
        console.log(i+"======"+d);
        //data.setValue(i,1,d);
        data.addRow([String(i), Number(d)]);
        chart.draw(data, options);
    }
function updateChart(d) {
        i++;
        console.log(i+"======"+d);
        //data.setValue(i,1,d);
        data.addRow([String(i), Number(d)]);
        chart.draw(data, options);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文