echarts大製作:絢麗地圖
阿新 • • 發佈:2019-02-04
話不多說,先上圖
實現:
前提:
- 引入最新版的echarts(npm install echarts --save)
- 引入中國地圖Geo資料ChinaJson,這個檔案沒法上傳做附件,可自行在網上找一個類似於下圖格式的json即可
程式碼:
import echarts from 'echarts'; import {CommonEchartsConfig} from '../../util/echarts/CommonEchartsConfig'; import ChinaJson from './china.json'; let arealDistributionCharts; /** * componentId echarts dom Id * data 圖表資料 data:{min:0,nax:647,mapData:[{name: "湖北", value: 647},{name: "湖南", value: 349}]} 必須包含min,max,mapData這三個屬性 * */ renderArealDistributionEcharts:function (componentId,data) { //載入資料 let option= CommonEchartsConfig.initDefaultMap('',['成交金額'],[data.mapData],null,data.max); if(document.getElementById(componentId)!= null){ //配置rank1,2,3名的markpoint let markPointData = []; for (let p of ChinaJson.features){ if (data.mapData && data.mapData[0] && data.mapData[0].name.indexOf(p.properties.name) > -1){ markPointData.push({ coord: p.properties.cp, symbol:'circle', symbolSize:20, lebel:{ show:false, }, itemStyle:{ color:'#FFF', borderWidth:8, borderColor:'#FB4444', }, }); } if (data.mapData && data.mapData[1] && data.mapData[1].name.indexOf(p.properties.name) > -1){ markPointData.push({ coord: p.properties.cp, symbol:'circle', symbolSize:15, lebel:{ show:false, }, itemStyle:{ color:'#FFF', borderWidth:6, borderColor:'#FF7A3A', }, }); } if (data.mapData && data.mapData[2] && data.mapData[2].name.indexOf(p.properties.name) > -1){ markPointData.push({ coord: p.properties.cp, symbol:'circle', symbolSize:10, lebel:{ show:false, }, itemStyle:{ color:'#FFF', borderWidth:4, borderColor:'#FBB02F', }, }); } } //載入地圖geoJson echarts.registerMap('china', ChinaJson); //初始化地圖 arealDistributionCharts = echarts.init(document.getElementById(componentId)); //自定義地圖配置 CommonEchartsConfig.setMapRoam(option); CommonEchartsConfig.setToolboxIsShow(option,false); let toolTipAttr = { backgroundColor:'#FFF', borderColor:'#EAEAEA', borderWidth:1, padding:10, formatter:function (params) { let _html; if(params && params.name){ _html= "<span class='tooltip-province'>"+params.name+"</span><br>"+ "<span class='tooltip-data'>成交金額:"+formatData.formatAmount(params.value,2)+"</span><br>"; } return _html; } }; CommonEchartsConfig.setTooltipAttr(option,toolTipAttr); let mapAttr = { left:'10%', top:'12%', label:{ show:false, }, itemStyle:{ color:'transparent', borderWidth:1, borderColor:'#FFF', }, emphasis:{ label:{ show:false, }, itemStyle:{ areaColor:'#ffc1ae', borderWidth:1, borderColor:'#FFF', }, }, markPoint:{ show:true, symbolSize: 20, itemStyle:{ color:'blue', }, data:markPointData, } }; CommonEchartsConfig.setMapAttr(option,mapAttr); let visualMap = [{ type: 'piecewise', // 定義為連續型 viusalMap itemWidth:22, itemHeight:7, itemGap:0, splitNumber:6, textGap:10, top:12, left:15, text:[' 低','高 '], textStyle:{ color:'#3B49BD', }, inRange:{ symbol:'rect', color: ['#3B49BD','#5562CB','#7381D6','#8393F4','#B9C2FB','#EAEDFC'], }, orient:'horizontal', min:parseInt(data.min), max:parseInt(data.max), calculable : true, }]; CommonEchartsConfig.setVisualMap(option,visualMap); arealDistributionCharts.setOption(option,true); this.resizeEcharts(); } }, //自動渲染地圖 resizeEcharts: function () { window.onresize = function () { arealDistributionCharts.resize(); }; }