最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Dynamic data with Google Charts - Stack Overflow

programmeradmin1浏览0评论

I require my Google chart to be drawn with dynamic data, so I'm storing it in a variable:

var rowData = "[ [{ v: 'Mike', f: 'Mike' }, '', 'The President'], [{ v: 'Jim', f: 'Jim Vice President' }, 'Mike', 'VP'], ['Alice', 'Mike', ''], ['Bob', 'Jim', 'Bob Sponge'], ['Carol', 'Bob', ''] ]";

I'm then passing this variable into Google's function:

    google.load('visualization', '1', { packages: ['orgchart'] });
    //google.setOnLoadCallback(drawChart);
    function drawChart(json) {
        data = new google.visualization.DataTable();
        data.addColumn('string', 'Name');
        data.addColumn('string', 'Manager');
        data.addColumn('string', 'ToolTip');
        data.addRows([
            rowData
        ]);
        chart = new google.visualization.OrgChart(document.getElementById('orgChart'));
        chart.draw(data, { allowHtml: true });
        google.visualization.events.addListener(chart, 'select', selectHandler);
    }

However, this gives me an 'Every row given must be either null or an array.' error. If I remove the variable and pass the data in directly, it works fine:

    ...
    data.addRows(
        [ [{ v: 'Mike', f: 'Mike' }, '', 'The President'], [{ v: 'Jim', f: 'Jim Vice President' }, 'Mike', 'VP'], ['Alice', 'Mike', ''], ['Bob', 'Jim', 'Bob Sponge'], ['Carol', 'Bob', ''] ]
    );
    ...

Can anyone help tell me what's going on here? Thanks in advance.

I require my Google chart to be drawn with dynamic data, so I'm storing it in a variable:

var rowData = "[ [{ v: 'Mike', f: 'Mike' }, '', 'The President'], [{ v: 'Jim', f: 'Jim Vice President' }, 'Mike', 'VP'], ['Alice', 'Mike', ''], ['Bob', 'Jim', 'Bob Sponge'], ['Carol', 'Bob', ''] ]";

I'm then passing this variable into Google's function:

    google.load('visualization', '1', { packages: ['orgchart'] });
    //google.setOnLoadCallback(drawChart);
    function drawChart(json) {
        data = new google.visualization.DataTable();
        data.addColumn('string', 'Name');
        data.addColumn('string', 'Manager');
        data.addColumn('string', 'ToolTip');
        data.addRows([
            rowData
        ]);
        chart = new google.visualization.OrgChart(document.getElementById('orgChart'));
        chart.draw(data, { allowHtml: true });
        google.visualization.events.addListener(chart, 'select', selectHandler);
    }

However, this gives me an 'Every row given must be either null or an array.' error. If I remove the variable and pass the data in directly, it works fine:

    ...
    data.addRows(
        [ [{ v: 'Mike', f: 'Mike' }, '', 'The President'], [{ v: 'Jim', f: 'Jim Vice President' }, 'Mike', 'VP'], ['Alice', 'Mike', ''], ['Bob', 'Jim', 'Bob Sponge'], ['Carol', 'Bob', ''] ]
    );
    ...

Can anyone help tell me what's going on here? Thanks in advance.

Share Improve this question edited Feb 1, 2012 at 22:53 Jonathan asked Feb 1, 2012 at 21:56 JonathanJonathan 15.5k17 gold badges100 silver badges131 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

In your first example the data is a single string, in the second example your data is a JavaScript structure of arrays and individual strings / values.

Hard to remend a best-practice without knowing where you're dynamic data is ing from, but you should be able to remove the outermost quotes like this and have it work:

var rowData = [ [{ v: 'Mike', f: 'Mike' }, '', 'The President'], [{ v: 'Jim', f: 'Jim Vice President' }, 'Mike', 'VP'], ['Alice', 'Mike', ''], ['Bob', 'Jim', 'Bob Sponge'], ['Carol', 'Bob', ''] ];

By enclosing in double-quotes rowData bees string while the API needs Array. Use it like this

var rowData = [ [{ v: 'Mike', f: 'Mike' }, '', 'The President'], [{ v: 'Jim', f: 'Jim Vice President' }, 'Mike', 'VP'], ['Alice', 'Mike', ''], ['Bob', 'Jim', 'Bob Sponge'], ['Carol', 'Bob', ''] ];
发布评论

评论列表(0)

  1. 暂无评论