首页 > 解决方案 > 方法 zoomToMapObject(ev.target) 不会在 amCharts 中缩放到美国

问题描述

我正在使用 amCharts 显示世界地图,并且当用户单击一个国家/地区时,我有以下代码:

    var groupData = [
        {
            "name": "America",
            "color": "#7DB378",
            "data": [{
                "title": "USA",
                "id": "US",
                "customData": "Data 1"
            }, {
                "title": "Mexico",
                "id": "MX",
                "customData": "Data 2"
            }]
        },
        {
            "name": "Europe",
            "color": "#1AE5B9",
            "data": [{
                "title": "Spain",
                "id": "ES",
                "customData": "Data 3"
            }]
        },
        {
            "name": "Asia",
            "color": "#193387",
            "data": [{
                "title": "China",
                "id": "CN",
                "customData": "Data 4"
            }]
        }
    ];
    var excludedCountries = ["AQ"]; // Exclude Antarctica
    // Create a series for each group, and populate the above array
    groupData.forEach(function (group) {
        var series = chart.series.push(new am4maps.MapPolygonSeries());
        series.name = group.name;
        series.useGeodata = true;
        var includedCountries = [];
        group.data.forEach(function (country) {
            includedCountries.push(country.id);
            excludedCountries.push(country.id);
        });
        series.include = includedCountries;
        series.fill = am4core.color(group.color);
        series.setStateOnChildren = true;
        var seriesHoverState = series.states.create("hover");
        // Country shape properties & behaviors
        var mapPolygonTemplate = series.mapPolygons.template;
        // Events
        mapPolygonTemplate.events.on("hit", function(ev) {
            // zoom to an object
            ev.target.series.chart.zoomToMapObject(ev.target);
        });
    });

此代码适用于除美国以外的所有国家/地区。单击它时,它不会缩放,只是将世界地图居中一点,仅此而已。为什么这只是未能放大美国?我究竟做错了什么?

标签: javascriptamcharts

解决方案


推荐阅读