在vue中通過axios異步使用echarts的方法
現(xiàn)實(shí)的工作中, 數(shù)據(jù)不可能是像之前的demo演示的那樣把數(shù)據(jù)寫死的. 所有的數(shù)據(jù)都應(yīng)該通過發(fā)送請(qǐng)求進(jìn)行獲取, 所以, 這篇文章, 我將在Vue項(xiàng)目中使用Echarts: 在Vue中引入Echarts中的數(shù)據(jù)提取出來, 放入到static/data.json文件中,請(qǐng)求該文件獲取數(shù)據(jù)。
一、 實(shí)現(xiàn)異步加載數(shù)據(jù)
(一)引入vue-resource
通過npm下載axios
//命令行中輸入 npm install axios --save
在main.js中引入axios并注冊(cè)
// main.js import http from './http' Vue.prototype.$http = http //掛載到原型上
(二)設(shè)置data.json
將該柱狀圖的沒有數(shù)據(jù)的option抽取到data.json中, 代碼如下:
{ "title": { "text": "簡單餅狀圖" }, "tooltip": {}, "xAxis": { "data": ["襯衫","羊毛衫","雪紡衫","褲子","高跟鞋","襪子"], "name": "產(chǎn)品" }, "yAxis": {}, "series": [{ "name": "銷量", "type": "bar", "data": [5, 20, 36, 10, 10, 20], "itemStyle": { "normal": { "color": "hotpink" } } }] }
(三)在async-bar-chart.vue中請(qǐng)求數(shù)據(jù)
- 從aysnc-barChart-option.js中引入option
- 在methods中添加drawBarChart()方法
- 在mounted()鉤子函數(shù)中調(diào)用drawBarChart()
代碼如下:
<template> <div id="myChart" :style="{width: '800px', height: '400px'}"></div> </template> <script> export default { name: 'echarts', data() { return { msg: 'Welcome to Your Vue.js App', goods: {} } }, mounted() { this.drawLine(); }, created() { this.$http.get('./static/dat.json').then(res => { const data = res.data; this.goods = data console.log(this.goods); console.log(Array.from(this.goods.xAxis.data)); }) }, methods: { drawLine() { // 基于準(zhǔn)備好的dom,初始化echarts實(shí)例 let myChart = this.$echarts.init(document.getElementById('myChart')) // 繪制圖表 myChart.setOption({ title: {}, //{text: '異步數(shù)據(jù)加載示例'}, tooltip: {}, xAxis: { data: [] //["襯衫","羊毛衫","雪紡衫","褲子","高跟鞋","襪子"] }, yAxis: {}, series: [{ name: '銷量', type: 'bar', data: [] //[5, 20, 36, 10, 10, 20] }] }); this.$http.get("./static/dat.json") .then((res) => { const data = res.data; const list = data.series.map(good=>{ let list = good.data; return [...list] }) console.log(list); console.log(Array.from(...list)); myChart.setOption({ title: data.title, xAxis: [{ data: data.xAxis.data }], series: [{ name: '銷量', type: 'bar', data: Array.from(...list) //[5, 20, 36, 10, 10, 20] }] }); }) } } } </script>
二. 添加加載動(dòng)畫
如果數(shù)據(jù)加載時(shí)間較長,一個(gè)空的坐標(biāo)軸放在畫布上也會(huì)讓用戶覺得是不是產(chǎn)生 bug 了,因此需要一個(gè) loading 的動(dòng)畫來提示用戶數(shù)據(jù)正在加載。
ECharts 默認(rèn)有提供了一個(gè)簡單的加載動(dòng)畫。只需要調(diào)用 showLoading 方法顯示。數(shù)據(jù)加載完成后再調(diào)用 hideLoading 方法隱藏加載動(dòng)畫。
在drawLine()方法中添加showLoading()和hideLoading(), 代碼如下:
methods: { drawLine() { // 基于準(zhǔn)備好的dom,初始化echarts實(shí)例 let myChart = this.$echarts.init(document.getElementById('myChart')) // 繪制圖表 myChart.setOption({ title: {}, //{text: '異步數(shù)據(jù)加載示例'}, tooltip: {}, xAxis: { data: [] //["襯衫","羊毛衫","雪紡衫","褲子","高跟鞋","襪子"] }, yAxis: {}, series: [{ name: '銷量', type: 'bar', data: [] //[5, 20, 36, 10, 10, 20] }] }); //顯示加載動(dòng)畫 myChart.showLoading(); this.$http.get("./static/dat.json").then((res) => { setTimeout(() => { //未來讓加載動(dòng)畫效果明顯,這里加入了setTimeout,實(shí)現(xiàn)3s延時(shí) const data = res.data; const list = data.series.map(good => { let list = good.data; return [...list] }) console.log(list); console.log(Array.from(...list)); myChart.hideLoading(); //隱藏加載動(dòng)畫 myChart.setOption({ title: data.title, xAxis: [{ data: data.xAxis.data }], series: [{ name: '銷量', type: 'bar', data: Array.from(...list) //[5, 20, 36, 10, 10, 20] }] }); }, 3000) }) } }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
組件中多個(gè)el-upload存在導(dǎo)致上傳圖片失效的問題及解決
這篇文章主要介紹了組件中多個(gè)el-upload存在導(dǎo)致上傳圖片失效的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03詳解如何將 Vue-cli 改造成支持多頁面的 history 模式
本篇文章主要介紹了詳解如何將 Vue-cli 改造成支持多頁面的 history 模式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11vue input輸入框關(guān)鍵字篩選檢索列表數(shù)據(jù)展示
這篇文章主要為大家詳細(xì)介紹了vue input輸入框關(guān)鍵字篩選檢索列表數(shù)據(jù)展示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05vue項(xiàng)目中如何實(shí)現(xiàn)網(wǎng)頁的截圖功能?(html2canvas)
這篇文章主要介紹了vue項(xiàng)目中如何實(shí)現(xiàn)網(wǎng)頁的截圖功能?(html2canvas),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02深入學(xué)習(xí)Vue nextTick的用法及原理
這篇文章主要介紹了深入學(xué)習(xí)Vue nextTick的用法及原理,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10Electron-vue開發(fā)的客戶端支付收款工具的實(shí)現(xiàn)
這篇文章主要介紹了Electron-vue開發(fā)的客戶端支付收款工具的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05vue中使用cookies和crypto-js實(shí)現(xiàn)記住密碼和加密的方法
這篇文章給大家介紹一下關(guān)于vue中使用cookies和crypto-js如何實(shí)現(xiàn)密碼的加密與記住密碼,有一定的參考價(jià)值,有需要的朋友可以參考一下,希望對(duì)你們有所幫助。2018-10-10解決vue項(xiàng)目打包上服務(wù)器顯示404錯(cuò)誤,本地沒出錯(cuò)的問題
這篇文章主要介紹了解決vue項(xiàng)目打包上服務(wù)器顯示404錯(cuò)誤,本地沒出錯(cuò)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11Vue實(shí)現(xiàn)點(diǎn)擊顯示不同圖片的效果
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)點(diǎn)擊顯示不同圖片的效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08