【问题标题】:AmCharts 4 - How to use custom ajax response in MapPolygonSeries()AmCharts 4 - 如何在 MapPolygonSeries() 中使用自定义 ajax 响应
【发布时间】:2018-10-30 13:57:44
【问题描述】:

我正在测试 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/

【问题讨论】:

    标签: javascript arrays amcharts


    【解决方案1】:

    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";
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-10
      • 1970-01-01
      相关资源
      最近更新 更多