Google组织结构图。调整JSON数据的行数和列数

时间:2019-02-17 23:37:53

标签: javascript html google-visualization organizational-chart

我需要我的Google组织结构图来接受任何长度的数组。这是我的HTML文件中的相关代码:

google.load('visualization', '1', {packages: ['orgchart']});
google.setOnLoadCallback(drawChart);
var type = 'string'
var name = 'Name'
var orgChartData
i = orgChartData.length
function drawChart() {


var data = new google.visualization.DataTable();
data.addColumn(type[i],name[i])
    data.addRow(
      [
        {
          v: orgChartData[i].Name,
          f: orgChartData[i].Name},
        orgChartData[i].Manager,
        orgChartData[i].Name
      ]
    );

var chart = new google.visualization.OrgChart(document.getElementById('chart_div'));
chart.draw(data, {
    allowHtml: true})}

所以我的问题是data.addColumn和data.addRow。基本上,不管我数组的长度如何,它都可以工作:

JSON数据:

var orgChartData = [
{
    "Name": "Alpha",
    "Manager": ""
},
{
    "Name": "Beta",
    "Manager": "Alpha"
},
{
    "Name": "Gamma",
    "Manager": "Beta"
}]

我似乎无法使其正常工作。通过此代码,我收到以下错误消息:

Error: Invalid type, i, for column "e".

0 个答案:

没有答案