百度地图和echarts的结合使用,是数据可视化大屏开发过程中最亮眼的搭配组合,而连接两者之间关系的就是bmap.js。本项目主要涉及到:
//api.map.baidu.com/
,采用自适应http和https协议头的写法,因此在预览时必须通过web服务器方式浏览; <script type="text/javascript" src="js/echarts.min.js">script>
<script type="text/javascript"
src="//api.map.baidu.com/api?v=3.0&ak=3HGqGo1RHf8zsLZ***">script>
<script type="text/javascript" src="js/bmap.min.js">script>
<script type="text/javascript" src="js/skins/lock_blue.js">script>
<div id="container" style="height: 100%;">div>
在项目应用,可以按照该格式生成API,通过封装函数进行调用和定时刷新。
//散点图标准数据格式;
var scrData = [
{"name": "杭州市", "value": [120.203437, 30.253831], data: 40},
{"name": "成都市", "value": [104.098202,30.595856], data: 20},
{"name": "上海市", "value": [121.473641, 31.223825], data: 50},
{"name": "长沙市", "value": [112.89211,28.27699], data: 40},
{"name": "西安市", "value": [108.955088,34.429473], data: 78},
{"name": "北京市", "value": [116.418642,39.906744], data: 18},
{"name": "兰州市", "value": [103.857887,36.146224], data: 38}
]
具体涵义见注释
bmap: {
center: [121.554586, 29.813444],//默认中心点;
zoom: 5,//国家级:5,省级:8,市级:10,街道级:12
roam: true, //是否开启平游或缩放
mapStyle: mapStyle// {style: 'midnight'}//grayscale,googlelite
},
series: [
{
type: 'lines',
coordinateSystem: 'bmap',
zlevel: 5,
effect: {
symbolSize: 2 // 图标大小
},
lineStyle: {
width: 10, // 尾迹线条宽度
color: function (params) {
//console.log(params);
if (params.name == "上海市" || params.name == "北京市") {
return '#F7AF21';
} else {
return 'rgb(22,255,255, 1)';
}
},
opacity: 0.7, // 尾迹线条透明度
curveness: 0 // 尾迹线条曲直度
},
label: {
show: false,
position: 'end',
},
silent: true,
data: getBodyData(scrData, getMax(scrData))
},
// 柱状体的顶部
{
type: 'scatter',
coordinateSystem: 'bmap',
zlevel: 5,
label: {
show: true,
formatter: function (params) {
//console.log(params)
return "产值:" + params.data.value[2];
},
position: "top"
},
symbol: 'circle',
symbolSize: [10, 5],
itemStyle: {
color: function (params) {
// console.log(params);
if (params.name == "上海市" || params.name == "北京市") {
return '#F7AF21';
} else {
return 'rgb(22,255,255, 1)';
}
},
opacity: 1
},
silent: true,
data: getTopData(scrData, getMax(scrData))
},
// 柱状体的底部
{
type: 'scatter',
coordinateSystem: 'bmap',
zlevel: 4,
label: {
formatter: '{b}',
position: 'bottom',
color: '#fff',
fontSize: 12,
distance: 10,
show: false
},
symbol: 'circle',
symbolSize: [10, 5],
itemStyle: {
color: function (params) {
if (params.name == "上海市") {
return '#F7AF21';
} else {
return 'rgb(22,255,255, 1)';
}
},
opacity: 1
},
silent: true,
data: scrData
},
// 底部外框
{
type: 'effectScatter',
coordinateSystem: 'bmap',
zlevel: 4,
label: {
show: false
},
rippleEffect: {
scale: 2,
brushType: "fill"
},
symbol: 'circle',
symbolSize: [20, 10],
itemStyle: {
color: function (params) {
if (params.name == "上海市" || params.name == "北京市") {
return '#F7AF21';
} else {
return 'rgb(22,255,255, 1)';
}
},
opacity: 1
},
silent: true,
data: scrData
}
]
通过 color: function (params){}回调函数进行判断和设置,在实际开发过程中,可以封装为一个函数,进行便捷调用。
color: function (params) {
//console.log(params);
if (params.name == "上海市" || params.name == "北京市") {
return '#F7AF21';
} else {
return 'rgb(22,255,255, 1)';
}
},
本案例在开发过程中,散点有明显的滑动动画;且在全屏的时候,散点无法自动紧贴地图,经过多次尝试,设置了定时 myChart.resize()效果。
myChart.setOption(option);
window.addEventListener("resize", function () {
setTimeout(function () {
myChart.resize();
}, 200)
});
// 百度地图接口对接设置;
var map = myChart.getModel().getComponent('bmap').getBMap();
//map.enableScrollWheelZoom(true);
//卫星地图
//map.setMapType(BMAP_SATELLITE_MAP);
//地图控件;
map.addControl(new BMap.MapTypeControl({mapTypes: [BMAP_NORMAL_MAP, BMAP_SATELLITE_MAP, BMAP_HYBRID_MAP]}));
var mapStyle = {
styleJson: [
{
featureType: 'water',
elementType: 'all',
stylers: {
color: '#044161'
}
},
{
featureType: 'land',
elementType: 'all',
stylers: {
color: '#004981'
}
},
{
featureType: 'boundary',
elementType: 'geometry',
stylers: {
color: '#064f85'
}
},
{
featureType: 'railway',
elementType: 'all',
stylers: {
visibility: 'off'
}
},
{
featureType: 'highway',
elementType: 'geometry',
stylers: {
color: '#004981'
}
},
{
featureType: 'highway',
elementType: 'geometry.fill',
stylers: {
color: '#005b96',
lightness: 1
}
},
{
featureType: 'highway',
elementType: 'labels',
stylers: {
visibility: 'off'
}
},
{
featureType: 'arterial',
elementType: 'geometry',
stylers: {
color: '#004981'
}
},
{
featureType: 'arterial',
elementType: 'geometry.fill',
stylers: {
color: '#00508b'
}
},
{
featureType: 'poi',
elementType: 'all',
stylers: {
visibility: 'off'
}
},
{
featureType: 'green',
elementType: 'all',
stylers: {
color: '#056197',
visibility: 'off'
}
},
{
featureType: 'subway',
elementType: 'all',
stylers: {
visibility: 'off'
}
},
{
featureType: 'manmade',
elementType: 'all',
stylers: {
visibility: 'off'
}
},
{
featureType: 'local',
elementType: 'all',
stylers: {
visibility: 'off'
}
},
{
featureType: 'arterial',
elementType: 'labels',
stylers: {
visibility: 'off'
}
},
{
featureType: 'boundary',
elementType: 'geometry.fill',
stylers: {
color: '#029fd4'
}
},
{
featureType: 'building',
elementType: 'all',
stylers: {
color: '#1a5787'
}
},
{
featureType: 'label',
elementType: 'all',
stylers: {
visibility: 'off'
}
}
]
}