【问题标题】:AmCharts 4 - drill map - get country iso codeAmCharts 4 - 钻孔图 - 获取国家 iso 代码
【发布时间】:2019-02-19 19:51:24
【问题描述】:

我在 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】:

    您可以使用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];
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-22
    • 2014-08-29
    • 1970-01-01
    相关资源
    最近更新 更多