无法使用Highchart API构建多级钻取图表

时间:2017-01-02 21:31:02

标签: javascript jquery json go highcharts

我无法使用Highcharts API构建2级深入分析图表。我正在使用Jquery的$.getJSON()方法,该方法从我的一个Go方法中检索数据。出于某种原因,我无法将数据保存到javascript变量中,因此我必须在$.getJSON方法中构建图表。我可以看到导航到该URL时正确显示了JSON。我在操纵数据以显示正确的图表方面遇到了很多麻烦。如果有人可以请解释/帮助,我会非常感激,因为我已经在这方面工作了一段时间,我需要为我的工作完成它。我将尝试尽可能简单地理解/阅读。以下是我到目前为止的情况:

转到:

type Office struct {
    Austin struct {
        Balance string
        RM struct {
            Matt struct {
                Balance string
            }
            John struct {
                Balance string
            }
            Blake struct {
                Balance string
            }
            Jamie struct {
                Balance string
            }
        }
    }
    ElPaso struct {
        Balance string
        RM struct {
            Brenda struct {
                Balance string
            }
            Ericka struct {
                Balance string
            }
            Nicole struct {
                Balance string
            }
            Stephanie struct {
                Balance string
            }
            Tricia struct {
                Balance string
            }
            Viri struct {
                Balance string
            }
        }
    }
    ABL struct {
        Balance string
        RM struct {
            BrianABL struct {
                Balance string
            }
            JamieABL struct {
                Balance string
            }
            JohnABL struct {
                Balance string
            }
            MattABL struct {
                Balance string
            }
            TimABL struct {
                Balance string
            }
        }
    }
}

func getData(res http.ResponseWriter, req *http.Request) {
    office := Office{}
    conn, err := sql.Open("mssql", "my db credentials")
    if err != nil {
        log.Fatal("Open connection failed:", err.Error())
    }
    defer conn.Close()

    rows, err := conn.Query("my select query")
    if err != nil {
        panic(err.Error())
    }
    for rows.Next() {
        var Austin, ElPaso, ABL, Matt, John, Blake, Jamie, Brenda, Ericka, Nicole, Stephanie, Tricia, Viri, BrianABL, JamieABL, JohnABL, MattABL, TimABL string
        rows.Scan(&Austin, &ElPaso, &ABL, &Matt, &John, &Blake, &Jamie, &Brenda, &Ericka, &Nicole, &Stephanie, &Tricia, &Viri, &BrianABL, &JamieABL, &JohnABL, &MattABL, &TimABL)
        office.Austin.Balance = Austin
            office.Austin.RM.Matt.Balance = Matt
            office.Austin.RM.John.Balance = John
            office.Austin.RM.Blake.Balance = Blake
            office.Austin.RM.Jamie.Balance = Jamie
        office.ElPaso.Balance = ElPaso
            office.ElPaso.RM.Brenda.Balance = Brenda
            office.ElPaso.RM.Ericka.Balance = Ericka
            office.ElPaso.RM.Nicole.Balance = Nicole
            office.ElPaso.RM.Stephanie.Balance = Stephanie
            office.ElPaso.RM.Tricia.Balance = Tricia
            office.ElPaso.RM.Viri.Balance = Viri
        office.ABL.Balance = ABL
            office.ABL.RM.BrianABL.Balance = BrianABL
            office.ABL.RM.JamieABL.Balance = JamieABL
            office.ABL.RM.JohnABL.Balance = JohnABL
            office.ABL.RM.MattABL.Balance = MattABL
            office.ABL.RM.TimABL.Balance = TimABL
    }
    js, err := json.Marshal(office)
    if err != nil {
        http.Error(res, err.Error(), http.StatusInternalServerError)
        return
    }
    res.Header().Set("Content-Type", "text/json")
    res.Header().Set("Access-Control-Allow-Origin", "*")
    res.Write(js)
}

JavaScript的:

$.getJSON('/getdata',  function(data) {
    for (office in data) {
        if (data.hasOwnProperty(office)) {
            officeVal = 0;
            officeP = {
                id: 'id_' + officeI,
                name: office,
                color: Highcharts.getOptions().colors[officeI]
            };
            officeBalI = 0;
            for (officeBalance in data[office]) {
                if (data[office].hasOwnProperty(officeBalance)) {
                    officeBalanceP = {
                        id: officeP.id + '_' + officeBalI,
                        name: officeBalance,
                        parent: officeP.id
                    };
                    points.push(officeBalanceP);
                    causeI = 0;
                    for (cause in data[office][country]) {
                        if (data[office][country].hasOwnProperty(cause)) {
                            causeP = {
                                id: countryP.id + '_' + causeI,
                                name: causeName[cause],
                                parent: countryP.id,
                                value: Math.round(+data[office][country][cause])
                            };
                            officeVal += causeP.value;
                            points.push(causeP);
                            causeI = causeI + 1;
                        }
                    }
                    countryI = countryI + 1;
                }
            }
            for (RM in data[office]) {
                if (data[office].hasOwnProperty(RM)) {
                    RMP = {
                        id: officeP.id + '_' + RMI,
                        name: RM,
                        parent: officeP.id
                    };
                    points.push(RMP);
              }
            }
            officeP.value = Math.round(officeVal / countryI);
            points.push(officeP);
            officeI = officeI + 1;
        }
    }
    Highcharts.chart('container', {
        series: [{
            type: 'treemap',
            layoutAlgorithm: 'squarified',
            allowDrillToNode: true,
            animationLimit: 1000,
            dataLabels: {
                enabled: false
            },
            levelIsConstant: false,
            levels: [{
                level: 1,
                dataLabels: {
                    enabled: true
                },
                borderWidth: 3
            }],
            data: points
        }],
        subtitle: {
            text: 'Subtitle test'
        },
        title: {
            text: 'Title test'
        }
    });
});

以下是来自/ getdata的JSON响应:

{
    "Austin" : {
        "Balance" : "12345.12",
        "RM" : {
            "Matt" : {"Balance" : "12345.12"},
            "John" : {"Balance" : "12345.12"},
            "Blake" : {"Balance" : "12345.12"},
            "Jamie" : {"Balance" : "12345.12"}
        }
    },
    "ElPaso" : {
        "Balance" : "12345.12",
        "RM" : {
            "Brenda" : {"Balance" : "12345.12"},
            "Ericka" : {"Balance" : "12345.12"},
            "Nicole" : {"Balance" : "12345.12"},
            "Stephanie" : {"Balance" : "12345.12"},
            "Tricia" : {"Balance" : "12345.12"},
            "Viri" : {"Balance" : "12345.12"}
        }
    },
    "ABL" : {
        "Balance" : "12345.12",
        "RM" : {
            "BrianABL" : {"Balance" : "12345.12"},
            "JamieABL" : {"Balance" : "12345.12"},
            "JohnABL" : {"Balance" : "12345.12"},
            "MattABL" : {"Balance" : "12345.12"},
            "TimABL" : {"Balance" : "12345.12"}
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您的JS代码不起作用,它有语法错误。

树的数据结构如下:

data: [{
    name: 'I have children',
    id: 'id-1'
}, {
    name: 'I am a child',
    parent: 'id-1',
    value: 2
}, {
    name: 'I am a smaller child',
    parent: 'id-1',
    value: 1
}]

所以,你的父母是办公室,他们有价值观,他们的孩子是人,他们也有价值观 - 所以你需要把办公室的人与正确的身份联系起来。例如。像这样:

var points = [];

Object.keys(data).forEach((office, i) => {
  var people = data[office]['RM'],
      color = Highcharts.getOptions().colors[i],
      id = 'id_' + i;

  points.push({
    name: office,
    value: Number(data[office]['Balance']),
    id: id,
    color: color
  });

  Object.keys(people).forEach((person, j) => {
    points.push({
      name: person,
      value: Number(people[person]['Balance']),
      parent: id,
      color: color
    });
  });
});

示例:https://jsfiddle.net/s4LLLo4z/