Vue+ECharts+高德地圖API實(shí)現(xiàn)天氣預(yù)報(bào)數(shù)據(jù)可視化的教程
前言
所謂數(shù)據(jù)可視化,我們可以理解為從宏觀角度來(lái)看一眼就能看出來(lái)整個(gè)數(shù)據(jù)的占比,走向。對(duì)于數(shù)據(jù)可視化,很多互聯(lián)網(wǎng)公司是很看重這一塊的,包括大廠;就比如阿里的淘寶,雙十一的時(shí)候往往就需要將消費(fèi)者的一些數(shù)據(jù)通過(guò)圖的形式展現(xiàn)出來(lái)。接下來(lái)我們就來(lái)實(shí)現(xiàn)一個(gè)天氣的數(shù)據(jù)可視化(移動(dòng)端開發(fā)),看如下效果圖(iPhone6/7/8)。
準(zhǔn)備工作
- 注冊(cè)一個(gè)高德地圖
API
賬號(hào),選擇開發(fā)支持,地圖 JS API
。
- 登錄控制臺(tái)成為開發(fā)者并創(chuàng)建
key
。
- 進(jìn)入安全密鑰使用說(shuō)明,找到方式二。
- 創(chuàng)建一個(gè)
vue
項(xiàng)目,將vue
的一些默認(rèn)組件和樣式刪除,在views
下新建一個(gè)Index.vue
,并且在index.js
下配置路由。目錄結(jié)構(gòu)如下所示:
- 通過(guò)
npm install echarts --save
安裝一個(gè)依賴,這樣就可以使用echarts
了。
開始(細(xì)分11步)
將準(zhǔn)備工作第三步找到的方式二的兩個(gè)
<script>
引入到index.html
中,將你自己申請(qǐng)的key
值和安全密鑰粘貼到里面去。這樣就可以使用高德地圖 JS API
開發(fā)地圖應(yīng)用。設(shè)置頭部樣式和背景色,時(shí)間和切換城市用到了彈性布局。
//html <div class="container"> <div class="nav"> <div class="time">7:41</div> <div class="city">切換城市</div> </div> <div>
//css .container { min-height: 100vh; background: #000; opacity: 0.7; color: #fff; } .nav { display: flex; justify-content: space-between; padding: 10px; }
- 設(shè)置我們需要的天氣數(shù)據(jù)展示的
html+css
結(jié)構(gòu),這主要考查的是切頁(yè)面能力。
//html <div class="city-info"> <p class="city">{{}}</p> <p class="weather">{{}}</p> <h2 class="temp"> <em></em>℃ </h2> <div class="detail"> <span>風(fēng)力:{{ }}</span>| <span>風(fēng)向:{{ }}</span>| <span>空氣濕度:{{ }}</span> </div> </div> <div class="future"> <div class="group" v-if="futureData.length > 0"> 明天: <span class="tm">白天:{{ }}℃ {{ }} {{ }}風(fēng) {{ }} </span> <span class="tm"> 夜間:{{ }}℃ {{ }} {{ }}風(fēng) {{ }} </span> </div> <div class="group" v-if="futureData.length > 0"> 后天: <span class="tm">白天:{{ }}℃ {{ }} {{ }}風(fēng) {{ }} </span> <span class="tm"> 夜間:{{ }}℃ {{ }} {{ }}風(fēng) {{ }} </span> </div> </div>
//css .city-info { text-align: center; .temp { font-size: 26px; em { font-size: 34px; font-style: normal; } } } .future { padding: 0 10px; margin-top: 30px; .group { height: 44px; line-height: 44px; background: rgba(255, 255, 255, 0.3); margin-bottom: 10px; padding: 0 10px; font-size: 13px; border-radius: 5px; } }
- 再放一個(gè)
div
用于存放折線圖。
//html <div class="echart-container"> </div>
//css .echart-container { width: 100%; height: 50vh; }
用
watchEffect
或onMounted
來(lái)獲取天氣數(shù)據(jù)。想要獲取天氣情況我們先要獲得定位,這是需要用到高德地圖
API
,我們來(lái)到這個(gè)位置:開發(fā) > 地圖 JS API v2.0 > 教程 > 服務(wù) > 定位
,找到IP
定位獲取當(dāng)前城市信息。
將這段代碼復(fù)制到onMounted
的回調(diào)函數(shù)中,這樣我們就能獲取到定位信息。
- 接下來(lái)就可以來(lái)獲取天氣了,我們把獲取天氣封裝成一個(gè)函數(shù)
getWeather
。同樣的我們來(lái)到:開發(fā) > 地圖 JS API v2.0 > 教程 > 服務(wù) > 天氣
,找到實(shí)時(shí)天氣查詢。
把上圖中的代碼復(fù)制到獲取天氣的函數(shù)中,并將它放在獲取定位成功后執(zhí)行,傳入定位的城市,這樣就可以獲得定位的城市的天氣情況了。
- 同樣的,我們來(lái)獲取未來(lái)幾天的天氣情況,通過(guò)下面的代碼就可以獲取到。
weather.getForecast('cityName', function(err, data) { console.log(err, data); });
注意:此時(shí)輸出的未來(lái)天氣是一個(gè)數(shù)組。
- 我們已經(jīng)獲取到了天氣數(shù)據(jù)了,接下來(lái)就要把這些數(shù)據(jù)存起來(lái),把它變成響應(yīng)式的,然后把它放到頁(yè)面上展示出來(lái)。
const state = reactive({ today: {}, futureData: [], }) state.today = data state.futureData = data.forecasts return { ...toRefs(state), }
把數(shù)據(jù)放到頁(yè)面上我理解的是挖坑然后埋數(shù)據(jù),就像下面這樣:
<p class="city">{{ today.city }}</p> <p class="weather">{{ today.weather }}</p>
注意:由于futureData
是一個(gè)數(shù)組,我們要在它放數(shù)據(jù)的div
上加一個(gè)v-if="futureData.length > 0"
,要不然會(huì)報(bào)錯(cuò)。
<div class="group" v-if="futureData.length > 0"> 明天: <span class="tm">白天:{{ futureData[1].dayTemp }}℃ {{ futureData[1].dayWeather}} {{ futureData[1].dayWindDir }}風(fēng) {{ futureData[1].dayWindPower }} </span> <span class="tm"> 夜間:{{ futureData[1].nightTemp }}℃ {{ futureData[1].nightWeather }} {{ futureData[1].nightWindDir }}風(fēng) {{ futureData[1].nightWindPower }} </span> </div>
- 接下來(lái)我們就來(lái)做一個(gè)折線圖了,打開
ECharts
官網(wǎng),選一個(gè)折線圖Examples - Apache ECharts
定義一個(gè)方法initEchart
來(lái)完成圖的繪制(這里定義了一個(gè)空數(shù)組來(lái)獲取未來(lái)幾天的溫度)
const tempArr = ref([]) data.forecasts.forEach(item => { tempArr.value.push(item.dayTemp) }) const echartContainer = ref(null) const initEchart = () => { const myChat = echarts.init(echartContainer.value); let option = { xAxis: { type: 'category', data: ['今天', '明天', '后天', '大后天'], lineStyle: { color: '#fff' }, axisTick: { show: false }, }, yAxis: { type: 'value', show: false }, series: [ { data: tempArr.value, type: 'line' } ] }; myChat.setOption(option) } return { echartContainer }
別忘了在裝這幅圖的div
上掛一個(gè)ref="echartContainer"
喲。
這樣就能幫我們初始化一個(gè)折線圖了。
- 最后直接在獲取未來(lái)天氣中調(diào)用
initEchart
就可以了。
部分代碼
<script> import { toRefs, watchEffect, reactive, ref, onMounted } from 'vue'; import * as echarts from 'echarts'; export default { setup() { const echartContainer = ref(null) const state = reactive({ today: {}, futureData: [], }) const tempArr = ref([]) onMounted(() => { //1.獲取定位 AMap.plugin('AMap.CitySearch', function () { var citySearch = new AMap.CitySearch() citySearch.getLocalCity(function (status, result) { // console.log(status); if (status === 'complete' && result.info === 'OK') { // 查詢成功,result即為當(dāng)前所在城市信息 //console.log(result.city); getWeather(result.city) } }) }) }) const getWeather = (cityName) => { //加載天氣查詢插件 AMap.plugin('AMap.Weather', function () { //創(chuàng)建天氣查詢實(shí)例 var weather = new AMap.Weather(); //執(zhí)行實(shí)時(shí)天氣信息查詢 weather.getLive(cityName, function (err, data) { console.log(err, data); state.today = data }); //未來(lái)的天氣 weather.getForecast(cityName, function (err, data) { console.log(err, data); state.futureData = data.forecasts data.forecasts.forEach(item => { tempArr.value.push(item.dayTemp) }) initEchart() }); }); } const initEchart = () => { const myChat = echarts.init(echartContainer.value); let option = { xAxis: { type: 'category', data: ['今天', '明天', '后天', '大后天'], lineStyle: { color: '#fff' }, axisTick: { show: false }, }, yAxis: { type: 'value', show: false }, series: [ { data: tempArr.value, type: 'line' } ] }; myChat.setOption(option) } return { ...toRefs(state), echartContainer } } } </script>
結(jié)語(yǔ)
ECharts中的一些圖表是很好用的,我們可以自己動(dòng)手多去嘗試嘗試。未來(lái)的學(xué)習(xí)之旅還很長(zhǎng),對(duì)于數(shù)據(jù)的可視化我們還可以做成3D的效果。本文如有錯(cuò)失,歡迎大家指正,最后感謝大家的觀看,點(diǎn)個(gè)免費(fèi)的贊吧??。
以上就是Vue+ECharts+高德地圖API實(shí)現(xiàn)天氣預(yù)報(bào)數(shù)據(jù)可視化的教程的詳細(xì)內(nèi)容,更多關(guān)于Vue+ECharts+高德地圖API 數(shù)據(jù)可視化的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue之帶參數(shù)跳轉(zhuǎn)打開新頁(yè)面、新窗口
這篇文章主要介紹了vue之帶參數(shù)跳轉(zhuǎn)打開新頁(yè)面、新窗口方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04解決vue自定義指令導(dǎo)致的內(nèi)存泄漏問(wèn)題
這篇文章主要介紹了解決vue自定義指令導(dǎo)致的內(nèi)存泄漏問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08vue+echarts實(shí)現(xiàn)中國(guó)地圖流動(dòng)效果(步驟詳解)
這篇文章主要介紹了vue+echarts實(shí)現(xiàn)中國(guó)地圖流動(dòng)效果(步驟詳解),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01vue+axios實(shí)現(xiàn)圖片上傳識(shí)別人臉的示例代碼
本文主要介紹了vue+axios實(shí)現(xiàn)圖片上傳識(shí)別人臉,這里采用的是vant的文件上傳組件,通過(guò)上傳圖片后端識(shí)別圖片里的人臉,感興趣的可以了解一下2021-11-11el-tree樹組件懶加載(后端上千條數(shù)據(jù)前端進(jìn)行處理)
本文主要介紹了el-tree樹組件懶加載(后端上千條數(shù)據(jù)前端進(jìn)行處理),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03Vue路由組件的緩存keep-alive和include屬性的具體使用
:瀏覽器頁(yè)面在進(jìn)行切換時(shí),原有的路由組件會(huì)被銷毀,通過(guò)緩存可以保存被切換的路由組件,本文主要介紹了Vue路由組件的緩存keep-alive和include屬性的具體使用,感興趣的可以了解一下2023-11-11Vue3中使用mock.js模擬數(shù)據(jù)的示例詳解
前后端同時(shí)開發(fā)的時(shí)候,后端接口數(shù)據(jù)沒(méi)有出來(lái),前端可以使用mock模擬假數(shù)據(jù),所以下面小編就來(lái)為大家詳細(xì)介紹一下如何在Vue3中使用mock.js模擬數(shù)據(jù)吧2025-03-03Electron采集桌面共享和系統(tǒng)音頻(桌面捕獲)實(shí)例
這篇文章主要為大家介紹了Electron采集桌面共享和系統(tǒng)音頻(桌面捕獲)實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10