带有详细JSON的Amcharts

时间:2017-10-12 15:44:40

标签: javascript json amcharts

[{ “SUM_PTS”:{ “数据类型”: “INTEGER”, “长度”: “8”, “值”: “29903727”, “模糊”: “假”}, “SUM_TOTAL”:{“数据类型“:” INTEGER “ ”长度“: ”10“, ”值“: ”1644704985“, ”模糊“: ”假“}, ”MID“:{ ”数据类型“: ”字母数字“, ”长度“:” 27 “,”value“:”供应商1“,”混淆“:”false“}},{”SUM_PTS“:{”datatype“:”INTEGER“,”length“:”7“,”value“:”3283570“ “模糊”: “假”}, “SUM_TOTAL”:{ “数据类型”: “INTEGER”, “长度”: “9”, “值”: “180596350”, “模糊”: “假”},“MID “:{”datatype“:”ALPHANUMERIC“,”length“:”23“,”value“:”供应商2“,”混淆“:”false“}}]

考虑到上面详细的JSON,我该如何分配valueField和titleField?

SUM_PTS和MID似乎不起作用。我认为这是因为我的JSON比示例中使用的更详细。我无法更改JSON,但我需要在Amcharts Javascript中解决。

1 个答案:

答案 0 :(得分:1)

由于SUM_PTS包含您的值,MID包含您的标题,因此将其映射为简化的dataProvider格式非常简单:

//assuming rawJson contains the above data:

var dataProvider = rawJson.map(function(jsonObj) {
  return {
    "title": jsonObj.MID.value,
    "value": jsonObj.SUM_PTS.value
  }
});

以下是您的重新映射数据,以饼图表示,用于演示目的:

var rawJson = [{
	"SUM_PTS": {
		"datatype": "INTEGER",
		"length": "8",
		"value": "29903727",
		"obfuscated": "false"
	},
	"SUM_TOTAL": {
		"datatype": "INTEGER",
		"length": "10",
		"value": "1644704985",
		"obfuscated": "false"
	},
	"MID": {
		"datatype": "ALPHANUMERIC",
		"length": "27",
		"value": "Vendor 1",
		"obfuscated": "false"
	}
}, {
	"SUM_PTS": {
		"datatype": "INTEGER",
		"length": "7",
		"value": "3283570",
		"obfuscated": "false"
	},
	"SUM_TOTAL": {
		"datatype": "INTEGER",
		"length": "9",
		"value": "180596350",
		"obfuscated": "false"
	},
	"MID": {
		"datatype": "ALPHANUMERIC",
		"length": "23",
		"value": "Vendor 2",
		"obfuscated": "false"
	}
}]

var dataProvider = rawJson.map(function(jsonObj) {
  return {
    "title": jsonObj.MID.value,
    "value": jsonObj.SUM_PTS.value
  }
});

AmCharts.makeChart("chartdiv", {
  "type": "pie",
  "titleField": "title",
  "valueField": "value",
  "dataProvider": dataProvider
})
<script src="//www.amcharts.com/lib/3/amcharts.js"></script>
<script src="//www.amcharts.com/lib/3/pie.js"></script>

<div id="chartdiv" style="width: 100%; height: 300px"></div>