配置项如下
const points = [{
name: '西南',
coord: [88.11, 31.97],
}, {
name: '西北',
coord: [96.27, 37.47],
}, {
name: '华北',
coord: [113.65, 42.82],
}, {
name: '东北',
coord: [127.63, 46.75],
}, {
name: '华东',
coord: [118.08, 30.82],
}, {
name: '华中',
coord: [112.65, 32.06],
}, {
name: '华南',
coord: [110.52, 24.3],
}];
const areas = {
"华北": ["北京", "天津", "河北", "山西", "内蒙古"],
"东北": ["辽宁", "吉林", "黑龙江"],
"华东": ["上海", "江苏", "浙江", "安徽", "福建", "江西", "山东", "台湾"],
"华中": ["河南", "湖北", "湖南"],
"西南": ["重庆", "四川", "贵州", "云南", "西藏"],
"西北": ["陕西", "甘肃", "青海", "宁夏", "新疆"],
"华南": ["广东", "广西", "海南", "香港", "澳门"]
};
option = {
tooltip: {
trigger: 'item',
formatter: '{b}'
},
series: [{
name: '中国',
type: 'map',
mapType: 'china',
label: {
normal: {
show: false
},
emphasis: {
show: false
}
},
markPoint: {
label: {
normal: {
position: 'bottom',
formatter: '{b}大区'
}
},
data: points
}
}]
};
var status = 'cleared';
var timer;
myChart.on('mouseover', ({ name }) => {
if (status !== 'over') {
status = 'over';
const [key, value] = Object.entries(areas).find(([, value]) => value.includes(name)) || [null, null];
if (value == null) {
return;
}
const filteredPoints = points.filter(({ name }) => name === key);
myChart.setOption({
series: [{
markPoint: {
data: filteredPoints
}
}]
});
value.forEach(name => myChart.dispatchAction({ type: 'highlight', name }));
}
});
myChart.getZr().on('mousemove', () => {
if (status !== 'cleared') {
status = 'toBeCleared';
}
clearTimeout(timer);
timer = setTimeout(() => {
if (status === 'toBeCleared') {
status = 'cleared';
myChart.setOption({
series: [{
markPoint: {
data: points
}
}]
});
myChart.dispatchAction({ type: 'downplay' });
}
}, 0);
});
function update() {
if (overName) {
const [key, value] = Object.entries(areas).find(([, value]) => value.includes(overName));
const filteredPoints = points.filter(({ name }) => name === key);
myChart.setOption({
series: [{
markPoint: {
data: filteredPoints
}
}]
});
value.forEach(name => myChart.dispatchAction({ type: 'highlight', name }));
overName = null;
}
else {
myChart.setOption({
series: [{
markPoint: {
data: points
}
}]
});
console.log('adsf');
myChart.dispatchAction({ type: 'downplay' });
}
}