AmCharts 4 - 钻取地图 - 获取国家iso代码

问题描述 投票:1回答:1

我在AmCharts 4地图上开始了一个新项目。首先,我选择一个大陆,然后选择一个国家。

我怎样才能获得iso_code点击的国家/地区?

是否可以隐藏其他国家/地区并仅在地图上显示?

我的代码:

  // Countries
  var countriesSeries = chart.series.push(new am4maps.MapPolygonSeries());
  var countries = countriesSeries.mapPolygons;
  countriesSeries.visible = false; // start off as hidden
  countriesSeries.exclude = ["AQ"];
  countriesSeries.geodata = am4geodata_worldLow;
  countriesSeries.useGeodata = true;
  // Hide each country so we can fade them in
  countriesSeries.events.once("inited", function(){
    hideCountries(); 
  });

  var countryTemplate = countries.template;
  countryTemplate.applyOnClones = true;
  countryTemplate.fill = am4core.color("#a791b4");
  countryTemplate.fillOpacity = 0.3; 
  countryTemplate.strokeOpacity = 0.3;
  countryTemplate.tooltipText = "{name}";

  countryTemplate.events.on("hit", function(event){
    chart.zoomToMapObject(event.target);

// how can i get iso_code clicked country ?
// and hide other countries 

        showPoints(); // function show point on selected country
      });
javascript amcharts
1个回答
3
投票

您可以使用event.target.dataItem.dataContext.id处理国家/地区代码。

countryTemplate.events.on("hit", function(event){
    chart.zoomToMapObject(event.target);
    var countryCode = event.target.dataItem.dataContext.id;
    // ...
});

要隐藏某些国家/地区,您可以像在示例代码中一样使用countriesSeries.exclude。或者你可以使用countriesSeries.include,这显然是相反的。 (docs)。图表应该识别变化并重新渲染。但请记住,阵列上的更改并不容易检测,因此您应该重新分配excludeinclude属性。 (docs

polygonSeries.data = JSON.parse(JSON.stringify(dataArray));

或者您使用以下语法:

countriesSeries.include.push("DE");
countriesSeries.include = [...countriesSeries.include];
© www.soinside.com 2019 - 2024. All rights reserved.