React 使用recharts實(shí)現(xiàn)散點(diǎn)地圖的示例代碼
一、前端框架react+ant design UI
二、首先安裝recharts
npm install recharts
或者
yarn add recharts
三、引入插件及chinaJSON.js(里面有經(jīng)緯度信息)
由于項(xiàng)目需要我是全部存數(shù)據(jù)庫的,因?yàn)橐残枰约禾砑?,下面附一份兒全國省市縣經(jīng)緯度數(shù)據(jù)
import React,{Component} from 'react'; import {message} from 'antd'; import echarts from "echarts"; import {mapJson,geoCoordMap} from './chinaJSON.js';
三、具體實(shí)現(xiàn)代碼如下
import React,{Component} from 'react'; import {message} from 'antd'; import echarts from "echarts"; import {mapJson,geoCoordMap} from './chinaJSON.js'; const convertData = (data) => { var res = []; for (var i = 0; i < data.length; i++) { var geoCoord = geoCoordMap[data[i].name]; if (geoCoord) { res.push({ value: geoCoord.concat(data[i].value), name: data[i].name, }); } } return res; }; class LayoutImg extends Component { constructor() { super(); this.state = { loading: true, loaded:false, vmData:[ {name: '云浮', value: 24,num:10}, {name: '煙臺', value: 28,num:5}, {name: '昆山', value: 33,num:8}, {name: '泰州', value: 36,num:10}, {name: '廣州', value: 38,num:8}, {name: '深圳', value: 41,num:10}, {name: '三亞', value: 54,num:12}, {name: '成都', value: 58,num:20}, {name: '重慶', value: 66,num:5}, {name: '大慶', value: 279,num:10} ] } } componentDidMount() { this.initMapDidMount(); } initMapDidMount(){ echarts.registerMap('china', mapJson); // 注冊地圖 var mapChart = echarts.init(document.getElementById('map')); var option = { backgroundColor: '#404a59', title: { text: '分布', // subtext: '點(diǎn)擊進(jìn)入', // sublink: 'http://www.baidu.com/', left: 'center', textStyle: { color: '#fff' } }, tooltip : { trigger: 'item', formatter: function (params) { //格式化鼠標(biāo)指到點(diǎn)上的彈窗返回的數(shù)據(jù)格式 return params.name + ' : ' + params.value[2]; } }, geo: { //地里坐標(biāo)系組件(相當(dāng)于每個省塊) map: 'china', roam:true, //是否開啟縮放 label: { emphasis: { //鼠標(biāo)劃到后彈出的文字 顯示省份 color: '#FF0000', //高亮背景色 show: true, //是否高亮顯示 fontSize:12 //字體大小 } }, itemStyle: { //坐標(biāo)塊本身 normal: { //坐標(biāo)塊默認(rèn)樣式控制 areaColor: '#323c48', //坐標(biāo)塊兒顏色 borderColor: '#111' }, emphasis: { areaColor: '#79FF79' //放坐標(biāo)塊兒上,塊兒顏色 } } }, series: [ { name: '信息', // series名稱 type: 'effectScatter', // series圖表類型 effectType: 'ripple', // 圓點(diǎn)閃爍樣式,目前只支持ripple波紋式 coordinateSystem: 'geo', // series坐標(biāo)系類型 data:convertData(this.state.vmData),// series數(shù)據(jù)內(nèi)容 showEffectOn: 'emphasis', //配置何時顯示特效 render 一直顯示,emphasis放上去顯示 symbolSize: function (val) { return val[2] / 10; }, rippleEffect: { // ripple的樣式控制 brushType: 'stroke', color: '#28FF28', }, label: { normal: { formatter: '', position: 'right', show: true //顯示位置信息, } }, itemStyle: { //散點(diǎn)本身顯示控制 normal: { color: '#28FF28', shadowBlur: 10, shadowColor: '#28FF28' } }, zlevel: 1 } ], symbolSize: 12, } if (option && typeof option === "object") { mapChart.setOption(option); } } render() { return ( <div className="cloudhost-box"> <div id="map" style={{width: '1100px',height: '550px',mergeLeft:"0px"}} /> </div> ); } } export default LayoutImg;
效果圖如下:
附:有什么其它的相關(guān)配置可以看官網(wǎng)再做具體修改recharts地址
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
React如何實(shí)現(xiàn)全屏監(jiān)聽Esc鍵
這篇文章主要介紹了React如何實(shí)現(xiàn)全屏監(jiān)聽Esc鍵,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08react?card?slider實(shí)現(xiàn)滑動卡片教程示例
這篇文章主要為大家介紹了react?card?slider實(shí)現(xiàn)滑動卡片教程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09react的ui庫antd中form表單使用SelectTree反顯問題及解決
這篇文章主要介紹了react的ui庫antd中form表單使用SelectTree反顯問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01react實(shí)現(xiàn)復(fù)選框全選和反選組件效果
這篇文章主要為大家詳細(xì)介紹了react實(shí)現(xiàn)復(fù)選框全選和反選組件效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-08-08ReactiveCocoa代碼實(shí)踐之-UI組件的RAC信號操作
這篇文章主要介紹了ReactiveCocoa代碼實(shí)踐之-UI組件的RAC信號操作 的相關(guān)資料,需要的朋友可以參考下2016-04-04