行政区可视化地图
1. 页面布局
<template>
<div class="mapbox" v-if="MapDisplay">
<div class="Mymaps" id="Mymap"></div>
<div class="timelinebox1" v-if="ButDisplay">
<button
title="时间点"
type="reset"
v-for="year in years"
:key="year.value"
:value="year.index"
@click="ChageColor(year.index)"
:style="{ backgroundColor: activeColor[year.index] }"
></button>
</div>
<div class="timelinebox2">
<span class="buttile" v-for="year in years" :key="year.value">{{
year.value
}}</span>
</div>
</div>
</template>
2. 方法实现
<script>
import "echarts/map/js/china";
export default {
name: "Mymap",
data() {
return {
PollutedDaysData: [
{ name: "天津", value: 196 },{ name: "河南", value: 193 },{ name: "山东", value: 170 },
{ name: "河北", value: 154 },{ name: "北京", value: 134 },{ name: "安徽", value: 116 },
{ name: "江苏", value: 115 },{ name: "湖北", value: 108 },{ name: "湖南", value: 105 },
{ name: "上海", value: 83 },{ name: "山西", value: 76 },{ name: "重庆", value: 62 },
{ name: "浙江", value: 57 },{ name: "陕西", value: 54 },{ name: "广西", value: 49 },
{ name: "贵州", value: 49 },{ name: "江西", value: 48 },{ name: "辽宁", value: 46 },
{ name: "广东", value: 34 },{ name: "吉林", value: 30 },{ name: "香港", value: 25 },
{ name: "澳门", value: 23 },{ name: "宁夏", value: 15 },{ name: "海南", value: 9 },
{ name: "福建", value: 7 },{ name: "新疆", value: 6 },{ name: "黑龙江", value: 5 },
{ name: "甘肃", value: 3 },{ name: "四川", value: 3 },{ name: "内蒙古", value: 2 },
{ name: "云南", value: 0 },{ name: "青海", value: 0 },{ name: "西藏", value: 0 },
],
ButDisplay: true,
MapDisplay: true,
years: [
{ index: 0, value: 2013 },
{ index: 1, value: 2014 },
{ index: 2, value: 2015 },
{ index: 3, value: 2016 },
{ index: 4, value: 2017 },
{ index: 5, value: 2018 },
],
activeColor: ["orange", "gray", "gray", "gray", "gray", "gray"],
selectyear: 0,
};
},
methods: {
MapShow() {
let that = this;
let echarts = require("echarts");
this.myecharts = echarts.init(document.getElementById("Mymap"));
var option = {
title: {
text: "中国污染天数统计图",
left: "center",
},
tooltip: {
trigger: "item",
},
legend: {
orient: "vertical",
left: "left",
data: ["中国污染天数统计图"],
},
visualMap: {
type: "piecewise",
pieces: [
{ min: 180, max: 365, label: "大于180天", color: "#372a28" },
{ min: 120, max: 179, label: "120-180天", color: "#4e160f" },
{ min: 60, max: 119, label: "60-120天", color: "#974236" },
{ min: 10, max: 59, label: "10-60天", color: "#ee7263" },
{ min: 0, max: 9, label: "小于10天", color: "#f5bba7" },
],
color: ["#E0022B", "#E09107", "#A3E00B"],
},
toolbox: {
show: true,
orient: "vertical",
left: "right",
top: "center",
feature: {
mark: { show: true },
dataView: {
title: "数据统计",
show: true,
readOnly: false,
//修改数据视图的格式,以表格的形式
optionToContent: function (opt) {
console.log(opt);
var data = opt.series[0].data;
var table =
'<table style="width:100%;text-align:center;left:center"><tbody><tr>' +
"<td>地区</td>" +
"<td>累计天数</td>" +
"</tr>";
for (var i = 0, l = data.length; i < l; i++) {
table +=
"<tr>" +
"<td>" +
data[i].name +
"</td>" +
"<td>" +
data[i].value +
"</td>" +
"</tr>";
}
table += "</tbody></table>";
return table;
},
},
restore: { show: true },
saveAsImage: { show: true },
},
},
roamController: {
show: true,
left: "right",
mapTypeControl: {
china: true,
},
},
series: [
{
name: "地区 累计天数",
type: "map",
mapType: "china",
roam: false,
label: {
show: true,
color: "rgb(249, 249, 249)",
},
data: that.PollutedDaysData,
},
],
};
this.myecharts.setOption(option);
window.addEventListener("resize", () => {
this.myecharts.resize();
});
},
//点击按钮显示高亮
ChageColor(e) {
this.ButDisplay = false;
this.MapShow();
this.activeColor[e] = "orange";
this.selectyear = e;
switch (e) {
case 0:
this.activeColor[1] =
this.activeColor[2] =
this.activeColor[3] =
this.activeColor[4] =
this.activeColor[5] =
"gray";
break;
case 1:
this.activeColor[0] =
this.activeColor[2] =
this.activeColor[3] =
this.activeColor[4] =
this.activeColor[5] =
"gray";
break;
case 2:
this.activeColor[1] =
this.activeColor[0] =
this.activeColor[3] =
this.activeColor[4] =
this.activeColor[5] =
"gray";
break;
case 3:this.activeColor[1] =
this.activeColor[2] =
this.activeColor[0] =
this.activeColor[4] =
this.activeColor[5] =
"gray";
break;
case 4:
this.activeColor[1] =
this.activeColor[2] =
this.activeColor[3] =
this.activeColor[0] =
this.activeColor[5] =
"gray";
break;
case 5:
this.activeColor[1] =
this.activeColor[2] =
this.activeColor[3] =
this.activeColor[4] =
this.activeColor[0] =
"gray";
break;
default:
break;
}
console.log("success");
this.ButDisplay = true;
},
},
mounted() {
let that = this;
that.MapShow();
},
};
</script>
如果需要点击按钮能够切换不同年份的数据,需要创建一个数据列表,然后修改如下代码就行
series: [
{
name: "地区 累计天数",
type: "map",
mapType: "china",
roam: false,
label: {
show: true,
color: "rgb(249, 249, 249)",
},
data: that.PollutedDaysData[that.selectyear],
},
创建数据列表如图所示
3. 实现效果
4. 出现的问题
- 按钮点击变色问题
:style="{ backgroundColor: activeColor[year.index] }"
这里设置了一个样式变量,但是样式设置中的背景颜色属性带有“-”,只需要将横杠去掉,横杆后面第一个字母改成大写
- 如何设置仅一个按钮变色,其他按钮恢复原色问题
ChageColor(e) {
this.ButDisplay = false;
this.MapShow();
this.activeColor[e] = "orange";
this.selectyear = e;
switch (e) {
case 0:
this.activeColor[1] =
this.activeColor[2] =
this.activeColor[3] =
this.activeColor[4] =
this.activeColor[5] =
"gray";
break;
case 1:
this.activeColor[0] =
this.activeColor[2] =
this.activeColor[3] =
this.activeColor[4] =
this.activeColor[5] =
"gray";
break;
case 2:
this.activeColor[1] =
this.activeColor[0] =
this.activeColor[3] =
this.activeColor[4] =
this.activeColor[5] =
"gray";
break;
case 3:
this.activeColor[1] =
this.activeColor[2] =
this.activeColor[0] =
this.activeColor[4] =
this.activeColor[5] =
"gray";
break;
case 4:
this.activeColor[1] =
this.activeColor[2] =
this.activeColor[3] =
this.activeColor[0] =
this.activeColor[5] =
"gray";
break;
case 5:
this.activeColor[1] =
this.activeColor[2] =
this.activeColor[3] =
this.activeColor[4] =
this.activeColor[0] =
"gray";
break;
default:
break;
}
console.log("success");
this.ButDisplay = true;
},
这里我使用了一个switch检查函数,遍历其他按钮,恢复原样
- 点击按钮后,仅仅后台数据修改,前端没有刷新
ChageColor(e) {
this.ButDisplay = false;
this.MapShow();
this.activeColor[e] = "orange";
this.selectyear = e;
switch (e) {
case 0:
this.activeColor[1] =
this.activeColor[2] =
this.activeColor[3] =
this.activeColor[4] =
this.activeColor[5] =
"gray";
break;
case 1:
this.activeColor[0] =
this.activeColor[2] =
this.activeColor[3] =
this.activeColor[4] =
this.activeColor[5] =
"gray";
break;
case 2:
this.activeColor[1] =
this.activeColor[0] =
this.activeColor[3] =
this.activeColor[4] =
this.activeColor[5] =
"gray";
break;
case 3:
this.activeColor[1] =
this.activeColor[2] =
this.activeColor[0] =
this.activeColor[4] =
this.activeColor[5] =
"gray";
break;
case 4:
this.activeColor[1] =
this.activeColor[2] =
this.activeColor[3] =
this.activeColor[0] =
this.activeColor[5] =
"gray";
break;
case 5:
this.activeColor[1] =
this.activeColor[2] =
this.activeColor[3] =
this.activeColor[4] =
this.activeColor[0] =
"gray";
break;
default:
break;
}
console.log("success");
this.ButDisplay = true;
},
这里我使用了一个switch检查函数,遍历其他按钮,恢复原样
- 点击按钮后,仅仅后台数据修改,前端没有刷新
this.MapShow();
这里我在按钮响应方法里每点击一次重新加载可视化地图,进行前端页面刷新