Javascript Google图表API-使用JS动态添加数据

Javascript Google图表API-使用JS动态添加数据,javascript,api,google-visualization,Javascript,Api,Google Visualization,早上好,我在页面中隐藏了数据,但我不知道如何将其添加到addRows函数中 这就是我所拥有的: google.load("visualization", "1", {packages:["corechart"]}); $(document).ready(function(){ var rowArray = []; $('input[name=device_name]').each(function(i){ var name = $(this).val();

早上好,我在页面中隐藏了数据,但我不知道如何将其添加到addRows函数中

这就是我所拥有的:

    google.load("visualization", "1", {packages:["corechart"]});
$(document).ready(function(){
    var rowArray = [];
    $('input[name=device_name]').each(function(i){
        var name = $(this).val();
        var amount = $(this).next().val();
        rowArray.push(["'" + name + "'", amount]);
    });
    google.setOnLoadCallback(drawChart);
    function drawChart() {
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Device');
        data.addColumn('number', 'Amount');
        data.addRows( rowArray );
        var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
        chart.draw(data, {
            width: 600, 
            height: 340, 
            title: 'Handheld Device Usage',
            hAxis: {
                title: 'Mobile Device Name', 
                titleTextStyle: {
                    color: '#404040'
                }
            }
        });
    }
});
有人能看出我哪里做错了吗

问候,

菲尔

也许这会奏效:

$('input[name=device_name]').each(function(i){
        var name = $(this).val();
        var amount = ($(this).next().val() * 1);
        rowArray.push([name, amount]);
});
也许这会奏效:

$('input[name=device_name]').each(function(i){
        var name = $(this).val();
        var amount = ($(this).next().val() * 1);
        rowArray.push([name, amount]);
});

问题是金额是一个字符串。。。我已经看到您正在使用一个js框架,因此您可能可以创建一个
console.log(rowArray)以进行调试

纠正这种情况的一个好方法是,如果您更改以下内容:

var amount = $(this).next().val().toInt();
我已经测试了它和它的工作。虽然我不得不改变一些事情,因为我在使用Mootools。。我没有html代码:P


祝你好运

问题是金额是一个字符串。。。我已经看到您正在使用一个js框架,因此您可能可以创建一个
console.log(rowArray)以进行调试

纠正这种情况的一个好方法是,如果您更改以下内容:

var amount = $(this).next().val().toInt();
我已经测试了它和它的工作。虽然我不得不改变一些事情,因为我在使用Mootools。。我没有html代码:P


祝你好运

控制台上是否有错误?是:类型不匹配。值1与列索引1第251行中的类型编号不匹配控制台上是否有错误?是:类型不匹配。值1与列索引1第251行中的类型编号不匹配getting.toInt()不是一个函数我理解提出的要点这就是为什么下面的答案有效。getting.toInt()不是一个函数我理解提出的要点这就是为什么下面的答案有效。