我正在测试amcharts 4地图(PieChart()和MapPolygonSeries()),并且我有一个response.data像这样:
{
"status": 200,
"legal_country": [
{
"legal_country": "US",
"count(*)": 171576
},
{
"legal_country": "GB",
"count(*)": 130246
},
{
"legal_country": "DE",
"count(*)": 112459
},
{
"legal_country": "NL",
"count(*)": 96554
}
]
}
我将legal_country数组存储在两个变量中:
var getAjaxRecords1 = data.legal_country;
var getAjaxRecords2 = data.legal_country;
现在,对于am4charts.PieSeries(),我可以访问数据并定义值和类别,如下所示:
var chart = am4core.create("lei_pie", am4charts.PieChart);
chart.data = getAjaxRecords2;
var series = chart.series.push(new am4charts.PieSeries());
series.dataFields.value = "count(*)";
series.dataFields.category = "legal_country";
一切正常。
但是如何使用MapPolygonSeries()中的数据?这不起作用:
// Create polygon series
var polygonSeries = chart.series.push(new am4maps.MapPolygonSeries());
polygonSeries.useGeodata = true;
...
polygonSeries.dataFields.value = "count(*)";
polygonSeries.dataFields.category = "legal_country";
polygonSeries.data = getAjaxRecords1;
...而我在文档中找不到它:https://www.amcharts.com/docs/v4/reference/mappolygonseries/
最佳答案
To tie data to particular MapPolygon
s, the data item itself needs to have an id
field that matches the polygon's id
.
另外,please note the array assigned to a MapPolygonSeries.data
gets mutated。因此,您可能需要提供该数组的副本,以便原始版本在整个应用程序中都能正常工作。
您可以先尝试格式化数据,然后再将其传递给MapPolygonSeries
,例如:
var mapData = [];
getAjaxRecords1.forEach(function(countryData) {
// Push a new object to our new array with an appropriate id
mapData.push(Object.assign( { "id": countryData.legal_country }, countryData ));
});
// Test mutation:
// mapData[0]["count(*)"] = 5;
// getAjaxRecords1[0]["count(*)"]; // 171576, unchanged
polygonSeries.data = mapData;
但是,我不清楚您要使用此代码做什么:
polygonSeries.dataFields.value = "count(*)";
polygonSeries.dataFields.category = "legal_country";
您期望该怎么做?您要通过地图系列实现什么?
关于javascript - AmCharts 4-如何在MapPolygonSeries()中使用自定义ajax响应,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53065976/