AmCharts 4 - 如何在 MapPolygonSeries() 中使用自定义 ajax 响应

AmCharts 4 - How to use custom ajax response in MapPolygonSeries()

我正在测试 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 MapPolygons, 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";

你希望它做什么?您想要通过地图系列实现什么目的?