<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>ECharts 地圖展現</title>
</head>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
</style>
<body>
<!--定義頁面圖表容器-->
<!-- 必須制定容器的大小(height、width) -->
<div id="maps" style="width: 33%; height: 500px; border: 1px solid #ccc; display: inline-block;float: left;"></div>
<div id="mapsProvice" style="width: 33%; height: 500px; border: 1px solid #ccc; display: inline-block;float: left;"></div>
<div id="mapsCity" style="width: 33%; height: 500px; border: 1px solid #ccc; display: inline-block;float: left;"></div>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript" src="echarts.min.js"></script>
<script type="text/javascript" src="mapjs/china.js"></script>
<script type="text/javascript">
function loadScript(url, callback) {
var script = document.createElement("script")
script.type = "text/javascript";
if (script.readyState) {
script.onreadystatechange = function() {
if (script.readyState == "loaded" || script.readyState == "complete") {
script.onreadystatechange = null;
callback();
}
};
} else {
script.onload = function() {
callback();
};
}
script.src = url;
document.getElementsByTagName("head")[0].appendChild(script);
}
var myChartMap = echarts.init(document.getElementById('maps'));
var option = {
title: {
text: '全國態勢圖',
subtext: '進入省市可詳細查看',
x: 'center',
textStyle: {
color: '#404a59'
}
},
legend: {
orient: 'vertical',
x: 'left',
data: ['安全狀態']
},
dataRange: {
orient: 'vertical',
x: 'left',
min: 0,
max: 1000,
color: ['orange', 'yellow'],
text: ['高', '低'], // 文本,默認爲數值文本
},
series: [{
tooltip: {
trigger: 'item',
formatter: '{b}'
},
name: '安全態勢',
type: 'map',
mapType: 'china',
x: '5%',
y: 'center',
roam: false,
selectedMode: 'single',
itemStyle: {
normal: {
label: {
show: true
}
},
emphasis: {
label: {
show: true
}
}
},
data: [{
name: '北京',
selected: true,
value: 100
}, {
name: '天津',
selected: false
}, {
name: '上海',
selected: false
}, {
name: '重慶',
selected: true,
value: 100
}, {
name: '河北',
selected: false
}, {
name: '河南',
selected: false
}, {
name: '雲南',
selected: false
}, {
name: '遼寧',
selected: true,
value: 100
}, {
name: '黑龍江',
selected: false
}, {
name: '湖南',
selected: true,
value: 1000
}, {
name: '安徽',
selected: false
},
]
}],
animation: false
};
myChartMap.setOption(option, true); //顯示國家地圖
var myChartProvice = echarts.init(document.getElementById('mapsProvice'));
var myChartCity = echarts.init(document.getElementById('mapsCity'));
if (selectProvice = "undefined") {
selectProvice = "四川";
selectCity = "成都市";
var url = "mapjs/" + selectProvice + "/" + selectProvice + ".js";
loadScript(url, setOpt);
var cityurl = "mapjs/" + selectProvice + "/city/" + selectCity + ".js";
loadScript(cityurl, setOptCity);
setOpt();
setOptCity();
}
myChartMap.on('mapselectchanged', function(param) { //單擊省份事件
selectProvice = param.batch[0].name;
var url = "mapjs/" + selectProvice + "/" + selectProvice + ".js";
loadScript(url, setOpt);
setOpt();
})
myChartProvice.on('mapselectchanged', function(param) {
selectCity = param.batch[0].name;
var cityurl = "mapjs/" + selectProvice + "/city/" + selectCity + ".js";
loadScript(cityurl, setOptCity);
setOptCity();
});
function setOpt() {
var option = {
title: {
text: selectProvice + '地區詳情',
x: 'center',
textStyle: {
color: 'black'
}
},
dataRange: {
x: '-3000px',
y: 'bottom',
splitList: [{
start: 41,
color: 'rgba(182,132,220,0.8)'
}, {
start: 21,
end: 40,
color: 'rgba(255,108,96,0.8)'
}, {
start: 11,
end: 20,
color: 'rgba(252,179,34,0.8)'
}, {
start: 5,
end: 10,
color: 'rgba(89,172,226,0.8)'
}, {
start: 0,
end: 4,
label: '<4',
color: "rgba(120,205,81,0.8)"
},
],
//
textStyle: {
color: 'white',
},
},
roam: true,
series: [{
tooltip: {
trigger: 'item',
formatter: '{b}'
},
name: '安全態勢',
type: 'map',
mapType: selectProvice,
x: 'center',
y: 'center',
// roam: true,
selectedMode: 'single',
itemStyle: {
normal: {
label: {
show: true
}
},
emphasis: {
label: {
show: true
}
}
},
data: ""
}],
animation: false,
}
myChartProvice.setOption(option, true);
}
function setOptCity() {
var option = {
title: {
text: selectCity + '地區詳情',
x: 'center',
textStyle: {
color: 'black'
}
},
roam: true,
dataRange: {
x: '-3000px',
y: 'bottom',
splitList: [{
start: 41,
color: 'rgba(182,132,220,0.8)'
}, {
start: 21,
end: 40,
color: 'rgba(255,108,96,0.8)'
}, {
start: 11,
end: 20,
color: 'rgba(252,179,34,0.8)'
}, {
start: 5,
end: 10,
color: 'rgba(89,172,226,0.8)'
}, {
start: 0,
end: 4,
label: '<4',
color: "rgba(120,205,81,0.8)"
}, ],
textStyle: {
color: 'white',
},
},
series: [{
tooltip: {
trigger: 'item',
formatter: '{b}'
},
name: '安全態勢',
type: 'map',
mapType: selectCity,
x: 'center',
y: 'center',
// roam: true,
itemStyle: {
normal: {
label: {
show: true
},
},
emphasis: {
label: {
show: true
}
}
},
selectedMode: 'single',
data: ""
}],
animation: false,
}
myChartCity.setOption(option, true);
}
</script>
</body>
</html>