先看效果样式
一、整体设置
1.建议使用series是map的设置
分别设置柱状图与地图的内容与样式
series: [
{
type: "bar",
emphasis: {
focus: "self",
},
data: wuhanqx.value.number,
barWidth: 20,
itemStyle: {
//柱形图圆角,鼠标移上去效果,如果只是一个数字则说明四个参数全部设置为那么多
normal: {
//柱形图圆角,初始化效果
barBorderRadius: [0, 15, 15, 0],
},
},
color: {
type: "linear",
x: 0,
y: 0,
x2: 1,
y2: 0,
colorStops: [
{
offset: 0,
color: "rgb(55,66,219)", // 100% 处的颜色
},
{
offset: 1,
color: "rgb(76,160,202)", // 0% 处的颜色
},
],
global: false,
},
label: {
show: true,
position: "right",
color: "#fff",
},
},
{
type: "map",
// emphasis: {
// focus: "self",
// },
map: "wuhanmap",
zoom: 1.2,
emphasis: {
itemStyle: {
areaColor: "#4da9e1",
},
label: {
color: "#020f4a",
},
},
left: "30%",
data: wuhanqx.value.number,
itemStyle: {
//柱形图圆角,鼠标移上去效果,如果只是一个数字则说明四个参数全部设置为那么多
normal: {
areaColor: "#020f4a",
borderColor: "#2256dd",
},
},
label: {
show: true,
position: "right",
color: "#bbdaff",
},
tooltip: {
show: true,
formatter: (params) => {
return `${params.value}<br />工会数`;
},
position: function (point, params, dom, rect, size) {
return point;
},
extraCssText: `
width: 50px;
height: 50px;
border-radius: 50%;
background-color: #1471d6;
border: 2px solid #59caef;
color: #fff;
text-align: center;
color:#a8f6ff;
font-size:12px;`,
},
},
],
2.分别设置其他配置项
grid: {
left: "8%",
top: "12%",
bottom: "5%",
right: "75%",
},
tooltip: {},
xAxis: {
splitLine: {
show: false,
},
axisLabel: {
show: false, //隐藏x轴显示内容
},
},
yAxis: [
{
data: wuhanqx.value.name,
name: "{icon|}工会分布",
nameTextStyle: {
color: "#c4d7fb",
fontWeight: "bold",
fontSize: 16,
rich: {
icon: {
width: 16,
height: 16,
color: "#c4d7fb",
backgroundColor: {
image: iconTitle,
},
},
},
},
// inverse: true,
axisLine: {
show: false,
},
axisTick: {
show: false, //是否显示刻度
},
axisLabel: {
show: false,
},
offset: -24,
},
{
//名称
type: "category",
offset: -10,
position: "left",
axisLine: {
show: false,
},
inverse: false,
axisTick: {
show: false,
},
axisLabel: {
interval: 0,
color: "#bbdaff",
align: "left",
verticalAlign: "bottom",
lineHeight: 40,
fontSize: 14,
},
data: wuhanqx.value.name,
},
],
3.设置定时器让地图与柱状图一起轮播显示
let loopTimer = null;
let loopIndex = 0;
let oldIndex = 0;
let overIndex = 0;
loopTimer = setInterval(() => {
myChart.dispatchAction({
type: "hideTip",
seriesIndex: [0, 1],
dataIndex: loopIndex - 1,
});
myChart.dispatchAction({
type: "downplay",
seriesIndex: [0, 1],
dataIndex: loopIndex - 1,
});
if (loopIndex >= wuhanqx.value.number.length) {
loopIndex = 0;
}
myChart.dispatchAction({
type: "showTip",
seriesIndex: 1,
dataIndex: loopIndex,
});
myChart.dispatchAction({
type: "highlight",
seriesIndex: [0, 1],
dataIndex: loopIndex,
});
oldIndex = loopIndex;
loopIndex += 1;
}, 2600);
myChart.on("mouseover", function (params) {
clearInterval(loopTimer);
if (params.seriesType == "map") {
myChart.dispatchAction({
type: "downplay",
seriesIndex: [0, 1],
});
myChart.dispatchAction({
type: "highlight",
seriesIndex: [0, 1],
dataIndex: params.dataIndex,
});
myChart.dispatchAction({
type: "showTip",
seriesIndex: [0, 1],
dataIndex: params.dataIndex,
});
} else {
myChart.dispatchAction({
type: "downplay",
seriesIndex: 1,
});
// myChart.dispatchAction({
// type: "hideTip",
// seriesIndex: [0, 1],
// dataIndex: [0, 14],
// });
myChart.dispatchAction({
type: "highlight",
seriesIndex: 1,
dataIndex: params.dataIndex,
});
myChart.dispatchAction({
type: "showTip",
seriesIndex: 1,
dataIndex: params.dataIndex,
});
}
overIndex = loopIndex;
loopIndex = params.dataIndex;
});
myChart.on("mouseout", function (params) {
clearInterval(loopTimer);
loopTimer = setInterval(() => {
myChart.dispatchAction({
type: "hideTip",
seriesIndex: [0, 1],
dataIndex: oldIndex,
});
myChart.dispatchAction({
type: "downplay",
seriesIndex: [0, 1],
dataIndex: oldIndex,
});
myChart.dispatchAction({
type: "downplay",
seriesIndex: [0, 1],
dataIndex: overIndex,
});
if (loopIndex >= wuhanqx.value.number.length) {
loopIndex = 0;
}
myChart.dispatchAction({
type: "showTip",
seriesIndex: 1,
dataIndex: loopIndex,
});
myChart.dispatchAction({
type: "highlight",
seriesIndex: [0, 1],
dataIndex: loopIndex,
});
oldIndex = loopIndex;
loopIndex += 1;
}, 2600);
});
4.鼠标移入或者移出时,去掉活加上定时器
myChart.on("mouseover", function (params) {
clearInterval(loopTimer);
myChart.dispatchAction({
type: "downplay",
seriesIndex: [0, 1],
});
myChart.dispatchAction({
type: "highlight",
seriesIndex: [0, 1],
dataIndex: params.dataIndex,
});
myChart.dispatchAction({
type: "showTip",
seriesIndex: [0, 1],
dataIndex: params.dataIndex,
});
loopIndex = params.dataIndex;
});
myChart.on("mouseout", function (params) {
clearInterval(loopTimer);
loopTimer = setInterval(() => {
myChart.dispatchAction({
type: "hideTip",
seriesIndex: [0, 1],
dataIndex: loopIndex - 1,
});
myChart.dispatchAction({
type: "downplay",
seriesIndex: [0, 1],
dataIndex: loopIndex - 1,
});
if (loopIndex >= wuhanqx.value.number.length) {
loopIndex = 0;
}
myChart.dispatchAction({
type: "showTip",
seriesIndex: 1,
dataIndex: loopIndex,
});
myChart.dispatchAction({
type: "highlight",
seriesIndex: [0, 1],
dataIndex: loopIndex,
});
loopIndex += 1;
}, 2600);
});
二、细节样式改变
1.改变tooltip的样式
formatter:对显示框内容的设置
可以对函数里面进行渲染调整
position:显示框显示的位置
默认是根据鼠标显示的,设置成数组的话分表表示x,y两轴对应的值,可以是数字,也可以是百分比都是对应地图的,例如[10,23]或[10%,20%]
extraCssText:对显示框样式进行更改
与css写法一致
tooltip: {
show: true,
formatter: (params) => {
return `${params.value}<br />工会数`;
},
position: function (point, params, dom, rect, size) {
return point;
},
extraCssText: `
width: 50px;
height: 50px;
border-radius: 50%;
background-color: #1471d6;
border: 2px solid #59caef;
color: #fff;
text-align: center;
color:#a8f6ff;
font-size:12px;`,
},
2.富文本样式,添加小图标图片
{icon| }:富文本标签
对样式进行跳转,引入图片。iconTitle是我引入的图片地址
yAxis: [
{
data: wuhanqx.value.name,
name: "{icon|}分布",
nameTextStyle: {
color: "#c4d7fb",
fontWeight: "bold",
fontSize: 16,
rich: {
icon: {
width: 16,
height: 16,
color: "#c4d7fb",
backgroundColor: {
image: iconTitle,
},
},
},
},
// inverse: true,
axisLine: {
show: false,
},
axisTick: {
show: false, //是否显示刻度
},
axisLabel: {
show: false,
},
offset: -24,
},]
三、完整代码
<template>
<div class="map">
<div class="tbia">
<div class="allbiao">
<p>截止今日工会总数为:</p>
<div>2</div>
<div>0</div>
<div>5</div>
<div>3</div>
<div>5</div>
</div>
</div>
<div id="wuhanmap"></div>
</div>
</template>
<script setup>
import whmap from "./json/wuh.js";
import * as echarts from "echarts";
import { onMounted, ref } from "vue";
import iconTitle from "../../assets/dsj.png";
let top = {
label: { color: "#fff" },
itemStyle: {
color: {
type: "linear",
x: 0,
y: 0,
x2: 1,
y2: 0,
colorStops: [
{
offset: 0,
color: "rgb(236,111,58)", // 100% 处的颜色
},
{
offset: 1,
color: "rgb(246,201,148)", // 0% 处的颜色
},
],
global: false,
},
},
};
let wuhanqx = ref({
name: [
"汉南区",
"硚口区",
"黄陂区",
"东西湖区",
"汉阳区",
"江汉区",
"江夏区",
"蔡甸区",
"武昌区",
"青山区",
"新洲区",
"江岸区",
"洪山区",
],
number: [
{ name: "汉南区", value: "12" },
{ name: "硚口区", value: "42" },
{ name: "黄陂区", value: "49" },
{ name: "东西湖区", value: "79" },
{ name: "汉阳区", value: "85" },
{ name: "江汉区", value: "93" },
{ name: "江夏区", value: "97" },
{ name: "蔡甸区", value: "109" },
{ name: "武昌区", value: "109" },
{ name: "青山区", value: "113" },
{ name: "新洲区", value: "155", ...top },
{ name: "江岸区", value: "260", ...top },
{ name: "洪山区", value: "756", ...top },
],
});
let option = {
grid: {
left: "8%",
top: "12%",
bottom: "5%",
right: "75%",
},
tooltip: {},
xAxis: {
splitLine: {
show: false,
},
axisLabel: {
show: false, //隐藏x轴显示内容
},
},
yAxis: [
{
data: wuhanqx.value.name,
name: "{icon|}分布",
nameTextStyle: {
color: "#c4d7fb",
fontWeight: "bold",
fontSize: 16,
rich: {
icon: {
width: 16,
height: 16,
color: "#c4d7fb",
backgroundColor: {
image: iconTitle,
},
},
},
},
// inverse: true,
axisLine: {
show: false,
},
axisTick: {
show: false, //是否显示刻度
},
axisLabel: {
show: false,
},
offset: -24,
},
{
//名称
type: "category",
offset: -10,
position: "left",
axisLine: {
show: false,
},
inverse: false,
axisTick: {
show: false,
},
axisLabel: {
interval: 0,
color: "#bbdaff",
align: "left",
verticalAlign: "bottom",
lineHeight: 40,
fontSize: 14,
},
data: wuhanqx.value.name,
},
],
series: [
{
type: "bar",
emphasis: {
focus: "self",
},
data: wuhanqx.value.number,
barWidth: 20,
itemStyle: {
//柱形图圆角,鼠标移上去效果,如果只是一个数字则说明四个参数全部设置为那么多
normal: {
//柱形图圆角,初始化效果
barBorderRadius: [0, 15, 15, 0],
},
},
color: {
type: "linear",
x: 0,
y: 0,
x2: 1,
y2: 0,
colorStops: [
{
offset: 0,
color: "rgb(55,66,219)", // 100% 处的颜色
},
{
offset: 1,
color: "rgb(76,160,202)", // 0% 处的颜色
},
],
global: false,
},
label: {
show: true,
position: "right",
color: "#fff",
},
},
{
type: "map",
// emphasis: {
// focus: "self",
// },
map: "wuhanmap",
zoom: 1.2,
emphasis: {
itemStyle: {
areaColor: "#4da9e1",
},
label: {
color: "#020f4a",
},
},
left: "30%",
data: wuhanqx.value.number,
itemStyle: {
//柱形图圆角,鼠标移上去效果,如果只是一个数字则说明四个参数全部设置为那么多
normal: {
areaColor: "#020f4a",
borderColor: "#2256dd",
},
},
label: {
show: true,
position: "right",
color: "#bbdaff",
},
tooltip: {
show: true,
formatter: (params) => {
return `${params.value}<br />数`;
},
position: function (point, params, dom, rect, size) {
return point;
},
extraCssText: `
width: 50px;
height: 50px;
border-radius: 50%;
background-color: #1471d6;
border: 2px solid #59caef;
color: #fff;
text-align: center;
color:#a8f6ff;
font-size:12px;`,
},
},
],
};
onMounted(() => {
var myChart = echarts.init(document.getElementById("wuhanmap"));
echarts.registerMap("wuhanmap", whmap);
myChart.setOption(option);
let loopTimer = null;
let loopIndex = 0;
let oldIndex = 0;
let overIndex = 0;
loopTimer = setInterval(() => {
myChart.dispatchAction({
type: "hideTip",
seriesIndex: [0, 1],
dataIndex: loopIndex - 1,
});
myChart.dispatchAction({
type: "downplay",
seriesIndex: [0, 1],
dataIndex: loopIndex - 1,
});
if (loopIndex >= wuhanqx.value.number.length) {
loopIndex = 0;
}
myChart.dispatchAction({
type: "showTip",
seriesIndex: 1,
dataIndex: loopIndex,
});
myChart.dispatchAction({
type: "highlight",
seriesIndex: [0, 1],
dataIndex: loopIndex,
});
oldIndex = loopIndex;
loopIndex += 1;
}, 2600);
myChart.on("mouseover", function (params) {
clearInterval(loopTimer);
if (params.seriesType == "map") {
myChart.dispatchAction({
type: "downplay",
seriesIndex: [0, 1],
});
myChart.dispatchAction({
type: "highlight",
seriesIndex: [0, 1],
dataIndex: params.dataIndex,
});
myChart.dispatchAction({
type: "showTip",
seriesIndex: [0, 1],
dataIndex: params.dataIndex,
});
} else {
myChart.dispatchAction({
type: "downplay",
seriesIndex: 1,
});
// myChart.dispatchAction({
// type: "hideTip",
// seriesIndex: [0, 1],
// dataIndex: [0, 14],
// });
myChart.dispatchAction({
type: "highlight",
seriesIndex: 1,
dataIndex: params.dataIndex,
});
myChart.dispatchAction({
type: "showTip",
seriesIndex: 1,
dataIndex: params.dataIndex,
});
}
overIndex = loopIndex;
loopIndex = params.dataIndex;
});
myChart.on("mouseout", function (params) {
clearInterval(loopTimer);
loopTimer = setInterval(() => {
myChart.dispatchAction({
type: "hideTip",
seriesIndex: [0, 1],
dataIndex: oldIndex,
});
myChart.dispatchAction({
type: "downplay",
seriesIndex: [0, 1],
dataIndex: oldIndex,
});
myChart.dispatchAction({
type: "downplay",
seriesIndex: [0, 1],
dataIndex: overIndex,
});
if (loopIndex >= wuhanqx.value.number.length) {
loopIndex = 0;
}
myChart.dispatchAction({
type: "showTip",
seriesIndex: 1,
dataIndex: loopIndex,
});
myChart.dispatchAction({
type: "highlight",
seriesIndex: [0, 1],
dataIndex: loopIndex,
});
oldIndex = loopIndex;
loopIndex += 1;
}, 2600);
});
});
</script>
<style lang="scss" scoped>
.map {
color: #fff;
position: relative;
.tbia {
width: 100%;
height: 10%;
margin-top: 12px;
// border: 1px solid springgreen;
position: relative;
.allbiao {
position: absolute;
left: 36%;
p {
color: #00a1fe;
text-align: center;
margin-bottom: 12px;
}
div {
float: left;
border: 2px solid #3b77dd;
font-weight: bolder;
font-size: 34px;
color: #66efff;
padding: 10px;
margin-right: 10px;
background: linear-gradient(
to bottom,
rgba(44, 91, 213, 0) 50%,
rgba(44, 91, 213, 0.5) 50%
);
border-radius: 4px;
}
}
}
#wuhanmap {
width: calc(100%);
height: calc(90%);
// border: 1px solid springgreen;
}
}
.custom-tooltip {
position: absolute;
pointer-events: none;
/* 防止tooltip遮挡数据点 */
}
.tooltip-content {
padding: 4px;
}
</style>
四、引入的地图json数据
可以在阿里云下载地图json数据,我下载的数武汉的数据,地址在下方
https://datav.aliyun.com/portal/school/atlas/area_selector