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

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

本文介绍了如何在AmCharts中访问嵌套的Json数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using AmCharts in my AngularJs Application and this is the Json Data which I get back:

{
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.

Could someone let me know how I could access nested Json data in Amcharts. I want to make one of the fields as my valueField in the chart.

推荐答案

AmCharts不支持嵌套的JSON集.您必须将数据重组为包含valueField和categoryField的对象的扁平数组.就您而言:

AmCharts doesn't support nested JSON sets. You have to restructure your data as a flattened array of objects containing your valueField(s) and categoryField. In your case:

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

请注意,图形只能支持数值轴的数值,因此"typeName"不是适当的valueField.

Note that graphs can only support numeric values for the value axis, so "typeName" isn't an appropriate valueField.

这篇关于如何在AmCharts中访问嵌套的Json数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 20:08