在vue中使用echarts實現(xiàn)飛機(jī)航線水滴圖詞云圖效果
vue中引入echarts
npm install echarts
在main.js中引用并掛載到vue上便于使用
import * as echarts from 'echarts' Vue.prototype.$echarts =echarts
飛機(jī)航線
html
<template> <div class="com-container"> <div class="com-chart" ref="chart" ></div> </div> </template>
css
.com-page { width: 100%; height: 100%; overflow: hidden; } .com-container { width: 100%; height: 100%; overflow: hidden; } .com-chart { width: 100%; height: 100%; overflow: hidden; } canvas { border-radius: 20px; } .com-container { position: relative; }
準(zhǔn)備數(shù)據(jù)
引入中國地圖JS文件,會自動注冊地圖;
// 中國地圖JS文件 require('echarts/map/js/china')
也可以通過axios方式引入json文件,需要手動注冊echarts.registerMap('china', chinaJson.data)
。
const ret = await axios.get('http://localhost:8999/static/map/china.json') this.echarts.registerMap('china', ret.data)
地圖數(shù)據(jù) 可以使用各個城市的數(shù)據(jù) 我這里沒去找
chinaGeoCoordMap: { 黑龍江: [127.9688, 45.368], 內(nèi)蒙古: [110.3467, 41.4899], 吉林: [125.8154, 44.2584], 北京市: [116.4551, 40.2539], 遼寧: [123.1238, 42.1216], 河北: [114.4995, 38.1006], 天津: [117.4219, 39.4189], 山西: [112.3352, 37.9413], 陜西: [109.1162, 34.2004], 甘肅: [103.5901, 36.3043], 寧夏: [106.3586, 38.1775], 青海: [101.4038, 36.8207], 新疆: [87.9236, 43.5883], 西藏: [91.11, 29.97], 四川: [103.9526, 30.7617], 重慶: [108.384366, 30.439702], 山東: [117.1582, 36.8701], 河南: [113.4668, 34.6234], 江蘇: [118.8062, 31.9208], 安徽: [117.29, 32.0581], 湖北: [114.3896, 30.6628], 浙江: [119.5313, 29.8773], 福建: [119.4543, 25.9222], 江西: [116.0046, 28.6633], 湖南: [113.0823, 28.2568], 貴州: [106.6992, 26.7682], 云南: [102.9199, 25.4663], 廣東: [113.12244, 23.009505], 廣西: [108.479, 23.1152], 海南: [110.3893, 19.8516], 上海: [121.4648, 31.2891] },
飛線數(shù)據(jù) 配置航線起點和終點數(shù)據(jù)
HxDatas: [ { start: '甘肅', end: '寧夏' }, { start: '青海', end: '新疆' }, { start: '西藏', end: '四川' }, { start: '山東', end: '河南' }, { start: '江蘇', end: '安徽' }, { start: '湖北', end: '福建' }, { start: '江西', end: '黑龍江' }, { start: '內(nèi)蒙古', end: '吉林' }, { start: '遼寧', end: '河北' }, { start: '天津', end: '山西' }, { start: '陜西', end: '海南' }, { start: '上海', end: '湖南' }, { start: '貴州', end: '廣西' } ]
處理飛行數(shù)據(jù)獲得起點和終點坐標(biāo)起點和終點
我這里統(tǒng)一設(shè)置了以北京為起點
methods: { convertData (data) { const res = [] const fromCoord = this.chinaGeoCoordMap[data.start]// 起點坐標(biāo) const toCoord = this.chinaGeoCoordMap[data.end]// 終點坐標(biāo) if (fromCoord && toCoord) { res.push([ // fromCoord, [116.4551, 40.2539], // 北京坐標(biāo) fromCoord // 航班數(shù)量 // value: ]) } // console.log(res) return res } }
配置地圖
this.chartInstance = this.$echarts.init(this.$refs.charts) const initOption = { geo: { type: 'map', map: 'china', top: '5%', bottom: '5%', itemStyle: { areaColor: 'rgba(3, 22, 32, 1)', borderColor: '#b2def2' } } } this.chartInstance.setOption(initOption)
配置折線line和散點
planePath: 'path://M1705.06,1318.313v-89.254l-319.9-221.799l0.073-208.063c0.521-84.662-26.629-121.796-63.961-121.491c-37.332-0.305-64.482,36.829-63.961,121.491l0.073,208.063l-319.9,221.799v89.254l330.343-157.288l12.238,241.308l-134.449,92.931l0.531,42.034l175.125-42.917l175.125,42.917l0.531-42.034l-134.449-92.931l12.238-241.308L1705.06,1318.313z' this.HxDatas.forEach((item, i) => { // console.log(this.convertData(item)) seriesArr.push( { type: 'lines', zlevel: 2, coordinateSystem: 'geo', symbol: ['none', 'arrow'], // 線兩端的標(biāo)記類型,可以是一個數(shù)組分別指定兩端 blendMode: 'lighter', dimensions: ['value'], polyline: true, effect: { // 線特效的配置 飛機(jī)樣式 show: true, period: 8, // 特效動畫的時間 trailLength: 0.1, // 特效尾跡的長度。取從 0 到 1 的值,數(shù)值越大尾跡越長。 // width: 1, // 尾跡線條寬度 opacity: 0.7, // 尾跡線條透明度 color: '#fff', curveness: 0.1, symbolSize: 13, // 特效標(biāo)記的大小,可以設(shè)置成諸如 10 這樣單一的數(shù)字,也可以用數(shù)組分開表示高和寬,例如 [20, 10] 表示標(biāo)記寬為20,高為10。 symbol: this.planePath }, // 線條樣式 lineStyle: { normal: { show: true, curveness: 0.4, // 尾跡線條曲直度 color: '#007acc' // 飛線顏色 } }, data: this.convertData(item) }, // 配置起點和終點散點樣式 { type: 'effectScatter', data: this.convertData(item)[0], zlevel: 2, coordinateSystem: 'geo', rippleEffect: { // 漣漪特效 // period: 4, // 動畫時間,值越小速度越快 brushType: 'stroke', // 波紋繪制方式 stroke, fill scale: 10 // 波紋圓環(huán)最大限制,值越大波紋越大 // color: '#fcdd6e' }, itemStyle: { // 控制散點的樣式 show: true, color: function () { return 'rgb(' + [ Math.round(Math.random() * 255), Math.round(Math.random() * 255), Math.round(Math.random() * 255), 0.5].join(',') + ')' } }, symbol: 'circle', symbolSize: function (val) { return 5 // 圓環(huán)大小 } } ) }) const dataoption = { series: seriesArr } this.chartInstance.setOption(dataoption)
使用
給父盒子開啟相對定位
包含組件的子盒子絕對定位
便于控制組件在頁面中呈現(xiàn)的位置
父層position:relative;
子層position:absolute
;的話, 就是依照父層的邊界進(jìn)行定位的
<template> <div class="com-page"> <div class="left"> <Map></Map> </div> </div> </template> <style> .com-container { position: relative; ... } .left{ position:absolute ... } </style>
水滴圖
需要額外引入
npm i echarts-liquidfill
在需要的組件
我這里用了 import * as echarts from 'echarts' 沒效果 改了import * as echarts from 'echarts/core'才有數(shù)據(jù)
import * as echarts from 'echarts/core' import 'echarts-liquidfill'
配置
this.chartInstance = this.echarts.init(this.$refs.liquidchart, this.theme) const initOption = { series: [ { type: 'liquidFill', center: ['10%', '30%'], data: [0.43], // 水球的數(shù)據(jù) radius: '35%', // 水球的實際大小,如果不寫會比容器小很多 backgroundStyle: { color: '#031620'// 沒有水球的背景顏色 }, name: '1號倉庫', label: { normal: { formatter () { return '8000件'// 中間數(shù)據(jù) }, color: '#FFFFFF ', insideColor: '#fff', textStyle: { fontSize: 10, fontWeight: 'bold', fontFamily: 'SourceHanSansCN-Regular' } } }, color: [ { type: 'linear', x: 0, y: 1, x2: 0, y2: 0, colorStops: [ { offset: 1, color: ['#326872'] // 0% 處的顏色 }, { offset: 0, color: ['#3BE7EC'] // 100% 處的顏色 } ], global: false // 缺省為 false } ], outline: { show: true, radius: '80%', borderDistance: 5, itemStyle: { borderColor: '#4381DC', borderWidth: 2 } } }, ] } this.chartInstance.setOption(initOption)
詞云圖
也需要另外引入
import 'echarts-wordcloud'
配置
initchart () { this.myChart = this.echarts.init(this.$refs.wordcloud) this.myChart.setOption({ series: [ { type: 'wordCloud', // 用來調(diào)整詞之間的距離 gridSize: 1, // 用來調(diào)整字的大小范圍 // Text size range which the value in data will be mapped to. // Default to have minimum 12px and maximum 60px size. sizeRange: [14, 60], // Text rotation range and step in degree. Text will be rotated randomly in range [-90, 90] by rotationStep 45 // 用來調(diào)整詞的旋轉(zhuǎn)方向,,[0,0]--代表著沒有角度,也就是詞為水平方向,需要設(shè)置角度參考注釋內(nèi)容 rotationRange: [-45, 0, 45, 90], // rotationRange: [ 0,90], // rotationRange: [0, 0], // 隨機(jī)生成字體顏色 // maskImage: maskImage, textStyle: { color: function () { return 'rgb(' + [ Math.round(Math.random() * 255), Math.round(Math.random() * 255), Math.round(Math.random() * 255) ].join(',') + ')' }, fontFamily: 'sans-serif', fontWeight: 'normal' // emphasis: { // shadowBlur: 10, // shadowColor: '#333' // } }, // 位置相關(guān)設(shè)置 // Folllowing left/top/width/height/right/bottom are used for positioning the word cloud // Default to be put in the center and has 75% x 80% size. left: 'center', top: 'center', right: null, bottom: null, // 數(shù)據(jù) data: this.wordList } ] }) }
data
wordList: [ { name: '短袖', value: 15000 }, { name: '連衣裙', value: 10081 }, { name: '純天然', value: 9386 }, { name: '植物', value: 7500 }, { name: '輕薄', value: 7500 }, { name: '洗發(fā)水', value: 6500 }, { name: '防曬霜', value: 6500 }, { name: '抗老', value: 6000 }, { name: '國風(fēng)', value: 4500 }, { name: '輕復(fù)古', value: 3800 }, { name: '鞋柜', value: 3000 }, { name: '秋季', value: 2500 }, { name: '襯衫', value: 2300 }, { name: '鏤空', value: 2000 }, { name: '月餅', value: 1900 }, { name: '空調(diào)', value: 1800 }, { name: '零食', value: 1700 }, { name: '咖啡', value: 1600 }, { name: '盛夏套裝', value: 1500 }, { name: '情侶睡衣', value: 1200 } ]
由于篇幅原因我這里沒有對屏幕自適應(yīng)進(jìn)行介紹
到此這篇關(guān)于在vue中使用echarts實現(xiàn)飛機(jī)航線水滴圖詞云圖效果的文章就介紹到這了,更多相關(guān)vue echarts飛機(jī)航線 水滴圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue如何解決watch和methods值執(zhí)行順序問題
這篇文章主要介紹了vue如何解決watch和methods值執(zhí)行順序問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08Vue2中pinia刷新后數(shù)據(jù)丟失的問題解決
Pinia是一個Vue.js狀態(tài)管理庫,如果你在組件中修改了store中的數(shù)據(jù)并刷新了界面,Pinia會將store中的數(shù)據(jù)重置為初始值,從而導(dǎo)致數(shù)據(jù)丟失的問題,本文就來介紹一下問題解決,感興趣的可以了解一下2023-12-12vue 使用vant插件做tabs切換和無限加載功能的實現(xiàn)
這篇文章主要介紹了vue 使用vant插件做tabs切換和無限加載功能的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11