如何在Highmaps中缩放到特定点

Gid*_*eon 3 javascript highcharts highmaps

Highmaps/highcharts是一个javascript/jquery适配器,可以在浏览器中呈现地图等.

我有一个突出显示单个国家/地区的地图,但是,(世界)地图的比例是这样的,我想在地图加载到相关国家后放大.

看看API我觉得这是可能的.

有一个事件监听器,这样我就可以在加载时执行自定义函数.如此示例所示,在负载上添加一个系列(Js小提琴)

此外,还有一种方法mapZoom允许您使用以下参数指定要缩放的点:

mapZoom(数字howMuch,[Number centerX],[Number centerY],[Number mouseX],[Number mouseY])

但是当我尝试调用这个方法onload(我的代码尝试如下,JS 在这里捏)时,没有任何反应.

$(function () {

    $.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=world-population-density.json&callback=?', function (data) {

        // Assign id's
        $.each(data, function () {
            this.id = this.code;
        });

        // Initiate the chart
        $('#container').highcharts('Map', {
            chart: {
                events: {
                    load: function () {
                    mapZoom(0.5, 100, 100);
                    }
                }
            },
            title: {
                text: 'Zoom on load attempt'
            },


            legend: {
                title: {
                    text: 'Population density per km²'
                }
            },

            colorAxis: {
                min: 1,
                max: 1000,
                type: 'logarithmic'
            },

            mapNavigation: {
                enabled: true,
                buttonOptions: {
                    verticalAlign: 'bottom'
                }
            },

            series : [{
                data : data,
                mapData: Highcharts.maps['custom/world-highres'],
                joinBy: ['iso-a2', 'code'],
                name: 'Population density',
                allowPointSelect: true,
                cursor: 'pointer',
                states: {
                    hover: {
                        color: '#BADA55'
                    }
                },
                tooltip: {
                    pointFormat: '{point.id} {point.name}',
                    valueSuffix: '/km²'
                }
            }]
        });

    });
});
Run Code Online (Sandbox Code Playgroud)

Roc*_*CTZ 9

mapZoom是一个属于chart对象的方法,因此,为了将其称为en event(load),您必须使用this关键字来调用它.

您可以像这样编辑代码(JSFiddle):

...
events: {
    load: function () {
        this.mapZoom(0.5, 100, 100);
        }
    }
}
...
Run Code Online (Sandbox Code Playgroud)

或者,您可以随时使用jQuery引用(JSFiddle)调用它:

$('#container').highcharts().mapZoom(0.5, 100, 100);
Run Code Online (Sandbox Code Playgroud)


小智 5

在地图上缩放到特定国家/地区非常简单

series : [{
            ...
            data: [{code: 'HU', id: 'HU', value: 7.5, name: 'Hungary'}],
            ...
            }
Run Code Online (Sandbox Code Playgroud)

...然后...

var mapChart=$('#MapContainer').highcharts(); //get map chart object from DOM
mapChart.get('HU').zoomTo(); //zoom to the country using "id" from data serie
mapChart.mapZoom(5); //elevate viewpoint a little to see surrounding countries as well
Run Code Online (Sandbox Code Playgroud)