echarts地图限制拖动范围demo
时间: 2023-07-04 12:30:31 浏览: 551
以下是一个简单的 echarts 地图限制拖动范围的 demo:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>ECharts Map Demo</title>
<!-- 引入 echarts.js -->
<script src="https://cdn.jsdelivr.net/npm/echarts/dist/echarts.min.js"></script>
</head>
<body>
<!-- 定义一个容器来渲染 echarts 地图 -->
<div id="map" style="width: 100%; height: 500px;"></div>
<script>
// 初始化 echarts 实例
var myChart = echarts.init(document.getElementById('map'));
// 配置 echarts 地图
var option = {
// 地图类型
series: [{
type: 'map',
mapType: 'china'
}]
};
// 设置限制拖动范围
myChart.setOption(option);
var zoom = myChart.getOption().geo[0].zoom;
var centerX = myChart.getOption().geo[0].center[0];
var centerY = myChart.getOption().geo[0].center[1];
var boundLeft = centerX - 20 / zoom;
var boundRight = centerX + 20 / zoom;
var boundTop = centerY - 10 / zoom;
var boundBottom = centerY + 10 / zoom;
myChart.setOption({
geo: {
center: [centerX, centerY],
zoom: zoom,
roam: true,
scaleLimit: {
min: 1,
max: 5
},
// 设置限制拖动范围
boundingCoords: [[boundLeft, boundTop], [boundRight, boundBottom]]
}
});
// 监听 echarts 地图缩放事件
myChart.on('geoRoam', function(params) {
var zoom = params.zoom;
var centerX = params.center[0];
var centerY = params.center[1];
var boundLeft = centerX - 20 / zoom;
var boundRight = centerX + 20 / zoom;
var boundTop = centerY - 10 / zoom;
var boundBottom = centerY + 10 / zoom;
myChart.setOption({
geo: {
center: [centerX, centerY],
zoom: zoom,
roam: true,
scaleLimit: {
min: 1,
max: 5
},
// 更新限制拖动范围
boundingCoords: [[boundLeft, boundTop], [boundRight, boundBottom]]
}
});
});
// 渲染 echarts 地图
myChart.setOption(option);
</script>
</body>
</html>
```
这个 demo 展示了如何使用 echarts 的 `boundingCoords` 属性来限制地图的拖动范围,在缩放地图时动态更新限制范围。具体实现方法可以参考代码注释。
阅读全文