1. 程式人生 > 其它 >echart繪製中國地圖及各省份地圖

echart繪製中國地圖及各省份地圖

示例:

 地圖檔案下載地址:https://gitcode.net/mirrors/fuhang-lm/echarts?utm_source=csdn_github_accelerator&from_codechina=yes

這裡以北京市地圖為例,如果是其他省份或者全國,下載對應的js檔案並引入系統,需要替換一下兩處,換成對應的省份:

var mapName = '北京',
geo: { map: '北京',}

 名字命名在對應的js檔案裡,需要寫對,地理座標js檔案也有,複製出來就可以了

html

<div id="chart_bjmap" style="width:100%;height:610px;"
></div> ...省略... <script src="/home/js/echarts.min.js"></script> <script src="/home/js/beijing.js"></script>

js,放射狀和擴散圈

//   北京地圖
  function echart_bjmap() {
        var rs = [[{"name":"朝陽區"},{"name": '朝陽區', "value": 32}],[{"name": "海淀區"},{"name": '海淀區', "value": 222}]]
        console.log(rs)
// 基於準備好的dom,初始化echarts例項 var myChart = echarts.init(document.getElementById('chart_bjmap')); var mapName = '北京' var data = [] var toolTipData = []; /*獲取地圖資料*/ myChart.showLoading(); myChart.hideLoading(); var geoCoordMap = { 東城區: [
116.418757,39.917544], 西城區: [116.366794,39.915309], 朝陽區: [116.486409,39.921489], 豐臺區: [116.286968,39.863642], 石景山區: [116.195445,39.914601], 海淀區: [116.310316,39.956074], 門頭溝區: [116.105381,39.937183], 通州區: [116.658603,39.902486], 順義區: [116.653525,40.128936], 昌平區: [116.235906,40.218085], 大興區: [116.338033,39.728908], 懷柔區: [116.637122,40.324272], 平谷區: [117.112335,40.144783] }; var GZData = rs //資料 var convertData = function (data) { var res = []; for (var i = 0; i < data.length; i++) { var dataItem = data[i]; var fromCoord = geoCoordMap[dataItem[0].name]; var toCoord = geoCoordMap[dataItem[1].name]; if (fromCoord && toCoord) { res.push({ fromName: dataItem[0].name, toName: dataItem[1].name, coords: [fromCoord, toCoord] }); } } return res; }; var color = ['#c5f80e']; var series = []; [ ['', GZData] ].forEach(function (item, i) { series.push({ name: item[0], type: 'lines', zlevel: 2, symbol: ['none', 'arrow'], symbolSize: 10, effect: { show: true, period: 6, trailLength: 0, symbol: 'arrow', symbolSize: 5 }, lineStyle: { normal: { color: color[i], width: 1, opacity: 0.6, curveness: 0.2 } }, data: convertData(item[1]) }, { name: item[0], type: 'effectScatter', coordinateSystem: 'geo', zlevel: 2, rippleEffect: { brushType: 'stroke' }, label: { normal: { show: true, position: 'right', formatter: '{b}' } }, symbolSize: function (val) { return val[2] / 8; //擴散圈的大小 }, itemStyle: { normal: { color: color[i] } }, data: item[1].map(function (dataItem) { return { name: dataItem[1].name, value: geoCoordMap[dataItem[1].name].concat([dataItem[1].value]) }; }) }); }); option = { tooltip: { trigger: 'item', backgroundColor: 'rgba(166, 200, 76, 0.82)', borderColor: '#FFFFCC', showDelay: 0, hideDelay: 0, enterable: true, transitionDuration: 0, extraCssText: 'z-index:100', formatter: function (params, ticket, callback) { //根據業務自己拓展要顯示的內容 if (params.seriesType == "effectScatter") { var res = ""; var name = params.name; var value = params.value[params.seriesIndex + 1]; res = "<span style='color:#fff;'>" + name + "</span><br/>醫院數:" + value; return res; } else if (params.seriesType == "scatter") { var res = ""; var name = params.name; var value = params.data.value[2]; res = "<span style='color:#fff;'>" + name + "</span><br/>醫院數:" + value; return res; } else { return name; } } }, geo: { map: '北京', label: { emphasis: { show: false } }, roam: true, itemStyle: { normal: { borderColor: 'rgba(147, 235, 248, 1)', borderWidth: 1, areaColor: { type: 'radial', x: 0.5, y: 0.5, r: 0.8, colorStops: [{ offset: 0, color: 'rgba(175,238,238, 0)' // 0% 處的顏色 }, { offset: 1, color: 'rgba(47,79,79, .1)' // 100% 處的顏色 }], globalCoord: false // 預設為 false }, shadowColor: 'rgba(128, 217, 248, 1)', // shadowColor: 'rgba(255, 255, 255, 1)', shadowOffsetX: -2, shadowOffsetY: 2, shadowBlur: 10 }, emphasis: { areaColor: '#389BB7', borderWidth: 0 } } }, series: series }; // 使用剛指定的配置項和資料顯示圖表。 myChart.setOption(option); window.addEventListener("resize", function () { myChart.resize(); }); }