如何在 AmCharts 中访问嵌套的 Json 数据

How to access nested Json Data in AmCharts

我在我的 AngularJs 应用程序中使用 AmCharts,这是我得到的 Json 数据:

{
total: 2,
tools: [
{
id: "57c5a75d57d92f742dc96bf2",
empId: "1",
empName: "ABC",
empType: {
typeId: 3,
typeName: "Contract",
hours: 45,
}
},
{
id: "57c5a75d57d92f742dc96bf2",
empId: "2",
empName: "DEF",
empType: {
typeId: 2,
typeName: "Full-Time",
hours: 40,
}
},


]
}

我控制器中的代码:

const writeemp = data => {
        const {
            total,
            employees,
        } = data;

        empChart.dataProvider = employees;
        empChart.write('emp');
        empChart.validateData();
    };

    AmCharts.handleLoad();

    var configChart = function () {


        empChart = new AmCharts.AmSerialChart();
        empChart.categoryField = "empId";
        empChart.labelRotation = 90;

        var yAxis = new AmCharts.ValueAxis();
        yAxis.position = "left";
        empChart.addValueAxis(yAxis);


        empBarGraph = new AmCharts.AmGraph();
        empBarGraph.valueField = "";//This to be the value "TypeName" in "empType" from the Json Data
        empBarGraph.type = "column";
        empBarGraph.fillAlphas = 1;
        empBarGraph.lineColor = "#f0ab00";
        empBarGraph.valueAxis = yAxis;
        empChart.addGraph(empBarGraph);
        empChart.write('empChart');


        $http.get(hostNameService.getHostName()+"/dashboard/employees")
            .then(response => writeemp(response.data));
    }

谁能告诉我如何访问 Amcharts 中的嵌套 Json 数据。我想将其中一个字段作为我在图表中的 valueField。

AmCharts 不支持嵌套 JSON 集。您必须将数据重组为包含 valueField(s) 和 categoryField 的扁平对象数组。你的情况:

[
  {
    "empId": 1,
    "typeName": "Contract"
  },
// ...
]

请注意,图表只能支持值轴的数值,因此 "typeName" 不是合适的值字段。