安装echarts
npm install echarts --save
import { useEffect } from "react"
import * as echarts from 'echarts';
import china from "../utils/china.json"
import "./map.css";
const Map=()=>{
var myChart ;
useEffect(()=>{
loadingChina()
},[])
const loadingChina = () => {
mapOption("china", china)
}
const convertData = () => {
let res = [{
name: "南海诸岛",
value: 0,
itemStyle: {
normal: {
opacity: 0,
label: {
show: false
}
}
}
}]
return res
}
const mapOption=(mapName,data)=>{
if (myChart !== null && myChart !== "" && myChart !== undefined) {
myChart.dispose();
}
myChart = echarts.init(document.querySelector('.map'));
echarts.registerMap(mapName, data)
var option = {
geo: {
map: mapName,
geoIndex: 0,
label: {
emphasis: {
show: true,
color: '#fff'
}
},
roam: true,
zoom: 1.2,
regions: convertData(),
itemStyle: {
normal: {
areaColor: 'rgb(30,55,106)',
borderColor: 'rgba(128,208,255,.8)',
shadowColor: 'rgb(20,50,97)',
shadowBlur: 20,
borderWidth: 1.8,
},
emphasis: {
areaColor: {
type: 'linear',
x: 0,
y: 0,
x2: 0,
y2: 1,
colorStops: [{
offset: 0, color: '#0AFBFF'
}, {
offset: 1, color: '#0157C9'
}],
global: false
},
}
}
}
};
myChart.setOption(option);
myChart.getZr().on('click', params => {
if (params.target) {
myChart.on('click', echartsMapClick);
}
})
}
const echartsMapClick=()=>{
}
return (
<div className="map">
</div>
)
}
export default Map
//地区json文件下载地址
http://datav.aliyun.com/tools/atlas/#&lat=33.521903996156105&lng=104.29849999999999&zoom=4
http://datav.aliyun.com/tools/atlas/#&lat=30.332329214580188&lng=106.72278672066881&zoom=3.5
在此基础上要实现点击地区- 某地区放大单独显示功能
1.引入某地区的json文件 2.在点击事件中,再次绘图 mapOption(“sichuan”, json文件) PS:一般地图下面都有几段线,在绘制时都要求去掉,引入没有下面几段线的json文件即可
|