vue中使用echarts實(shí)現(xiàn)動態(tài)數(shù)據(jù)綁定以及獲取后端接口數(shù)據(jù)
前言
之前幾篇echarts的文章是實(shí)現(xiàn)了靜態(tài)的柱狀圖、折線圖、餅狀圖、地圖,在項(xiàng)目中我們肯定是需要獲取后端接口,將后端返回的數(shù)據(jù)顯示在圖表上,所以這次就記錄一下如何實(shí)現(xiàn)echarts的動態(tài)數(shù)據(jù)綁定。
簡單來講,就是從接口獲取到的數(shù)據(jù),需要在圖表的方法里再次定義一下,然后用setOption方法就可以獲取到了。
1.柱狀圖
首先看接口傳過來的數(shù)據(jù),傳過來一個數(shù)組,第一條年度2021,數(shù)量1,第二條年度2022,數(shù)量3
因?yàn)橹鶢顖D的數(shù)據(jù)有兩個,橫坐標(biāo)和縱坐標(biāo),所以我們將傳來的數(shù)據(jù),橫坐標(biāo)做一個數(shù)組,縱坐標(biāo)做一個數(shù)組。
首先在data中定義
lwData: {}, // 論文數(shù)據(jù) lwndArr: [], // 年度數(shù)組 lwtsArr: [], // 論文發(fā)表天數(shù)數(shù)組
接著獲取接口數(shù)據(jù),把接口數(shù)據(jù)處理一下放進(jìn)兩個數(shù)組里。年度為橫坐標(biāo),將data中傳來的年度循環(huán)放入年度數(shù)組。天數(shù)為縱坐標(biāo),將data中傳來的天數(shù)循環(huán)放入天數(shù)數(shù)組。
this.axios.post(this.counturl, { type:'paper' }).then(res => { if (res.result === '00000') { this.lwData = res.data for(let i=0;i<this.lwData.length; i++) { this.lwndArr[i] = this.lwData[i].nd } for(let i=0;i<this.lwData.length; i++) { this.lwtsArr[i] = this.lwData[i].count } lwndArr = this.lwndArr lwtsArr = this.lwtsArr } else { this.$Message.error(res.data.information) } })
echarts和別的獲取接口數(shù)據(jù)不一樣的地方,在于echarts中需要再次定義一下數(shù)組,然后把接口獲取到的數(shù)據(jù)放進(jìn)去,不能直接引用this里的數(shù)據(jù)。
在獲取echarts圖表的方法里,定義橫縱坐標(biāo)的兩個data,然后使用setOption方法,引用定義的data,就可以顯示出接口里的數(shù)據(jù)了。(不再需要const option)
// 論文發(fā)表天數(shù)柱狀圖 getLwBar () { let lwndArr = [] let lwtsArr = [] const lwBar = echarts.init(document.getElementById('lwBar'))// 圖標(biāo)初始化 this.axios.post(this.counturl, { type:'paper' }).then(res => { if (res.result === '00000') { this.lwData = res.data for(let i=0;i<this.lwData.length; i++) { this.lwndArr[i] = this.lwData[i].nd } for(let i=0;i<this.lwData.length; i++) { this.lwtsArr[i] = this.lwData[i].count } lwndArr = this.lwndArr lwtsArr = this.lwtsArr lwBar.setOption({ title: { text: '論文發(fā)表天數(shù)柱狀圖' }, tooltip: { }, legend: { data: ['論文發(fā)表天數(shù)'] }, xAxis:{ name: '年份', data:lwndArr }, yAxis:{ name:'論文發(fā)表天數(shù)', type:'value' }, series:[ { name: '論文發(fā)表天數(shù)', type: 'bar', // 類型為柱狀圖 data: lwtsArr, barWidth: '20%', // 柱條寬度 每個柱條的寬度就是類目寬度的 20% // 柱子的樣式 itemStyle: { color: '#5574c2' } } ] }) } else { this.$Message.error(res.data.information) } }) // 隨著屏幕大小調(diào)節(jié)圖表 window.addEventListener('resize', () => { lwBar.resize() }) },
效果:
2.折線圖
折線圖和柱狀圖一樣,需要把橫坐標(biāo)和縱坐標(biāo)分開。
首先在data中定義
zzData: {}, // 著作數(shù)據(jù) zzndArr: [], // 著作年度數(shù)組 zzslArr: [], // 著作出版數(shù)量數(shù)組
接著獲取接口數(shù)據(jù),setOption
// 著作折線圖 getZzLine () { let zzndArr = [] let zzslArr = [] const zzLine = echarts.init(document.getElementById('zzLine'))// 圖標(biāo)初始化 this.axios.post(this.counturl, { type:'book' }).then(res => { if (res.result === '00000') { this.zzData = res.data for(let i=0;i<this.zzData.length; i++) { this.zzndArr[i] = this.zzData[i].nd } for(let i=0;i<this.zzData.length; i++) { this.zzslArr[i] = this.zzData[i].count } zzndArr = this.zzndArr zzslArr = this.zzslArr zzLine.setOption({ title: { text: '著作出版數(shù)量折線圖' }, tooltip: { trigger: 'axis' // axis item none三個值 }, legend: { data: ['著作'] }, xAxis:{ name: '年份', data:zzndArr }, yAxis:{ name:'數(shù)量', type:'value' }, series:[ { name: '著作出版數(shù)量', type: 'line', // 類型為z折線圖 data: zzslArr, type: 'line', stack: 'x', itemStyle: { color: '#ef6567', width: 4 } } ] }) } else { this.$Message.error(res.data.information) } }) // 隨著屏幕大小調(diào)節(jié)圖表 window.addEventListener('resize', () => { zzLine.resize() }) },
效果:
3.餅狀圖
餅狀圖和柱狀、折線圖的區(qū)別在于,餅狀圖只需要獲取一個數(shù)據(jù),數(shù)據(jù)格式如下:
data: [ { value: 335, name: '初級會計(jì)師' }, { value: 200, name: '中級會計(jì)師' }, ]
所以我們只需要后端傳過來的數(shù)據(jù)也是這樣的就可以了,要注意在圖表方法中再定義一次 。
接口數(shù)據(jù):
除此之外,餅狀圖還有一個表頭數(shù)據(jù)很重要,因?yàn)樗泻芏鄠€表頭數(shù)據(jù),所以不能和柱狀、折線
一樣直接定義,也需要從接口獲取一下,所以我們先在data中定義這兩個數(shù)據(jù)。
scaleData: [], // 餅狀圖數(shù)據(jù) scaleLegend: [], // 餅狀圖標(biāo)注
接著獲取接口,把對應(yīng)的數(shù)據(jù)獲取到,使用setOption
// 畢業(yè)人數(shù) getPieEcharts () { let scaleData= [] let scaleLegend = [] const kjjgPie = echarts.init(document.getElementById('kjjgPie'))// 圖標(biāo)初始化 this.axios.post(this.scaleurl, { type:this.selectedScale }).then(res => { if (res.result === '00000') { this.scaleData = res.data scaleData = this.scaleData for(let i = 0; i<res.data.length; i++) { this.scaleLegend[i] = res.data[i].name } scaleLegend = this.scaleLegend kjjgPie.setOption({ legend: { data: scaleLegend }, tooltip: {}, color:['#5470c6', '#91cc75', '#fac858', '#ee6666', '#73c0de', '#3ba272', '#fc8452', '#9a60b4', '#ea7ccc'], series: [ { radius: '50%', // name: '畢業(yè)人數(shù)', type: 'pie', // 類型為柱狀圖 label: { //echarts餅圖內(nèi)部顯示百分比設(shè)置 show: true, position: "outside", //outside 外部顯示 inside 內(nèi)部顯示 formatter: '(vvxyksv9kd%)', }, data: scaleData } ] }) } else { this.$Message.error(res.data.information) } }) // 隨著屏幕大小調(diào)節(jié)圖表 window.addEventListener('resize', () => { kjjgPie.resize() }) },
效果:
這里右上角有一個選擇框,可以根據(jù)選擇的數(shù)據(jù),顯示對應(yīng)的餅狀圖。
在這里可以簡單提一下,首先是select選擇框:
<Select v-model="selectedScale" style="display:inline-block;width:200px;float:right;margin-right:10px;" @on-change="scaleChange"> <Option v-for="item in selectList.scale" :value="item.code" :key="item.code" placeholder="請選擇"> {{ item.name }} </Option> </Select>
在data中定義默認(rèn)的數(shù)據(jù):
selectedScale: 'zyzg', // 被選擇的餅狀圖類別
用select選擇框的on-change事件,當(dāng)選項(xiàng)改變時,將改變的value傳給定義的selectedScale,接口會根據(jù)selectedScale的內(nèi)容,返回不一樣的數(shù)據(jù)。
scaleChange(value) { this.selectedScale = value this.getPieEcharts() },
4.地圖
地圖的具體內(nèi)容可以看之前兩篇地圖的文章。需求是鼠標(biāo)放在某一個地區(qū),要顯示對應(yīng)的內(nèi)容,新增的需求是要提供多個散點(diǎn),還有個全省的數(shù)據(jù)。
地圖和餅狀圖一樣,可以要求后端按照規(guī)定的格式傳過來,會方便很多,散點(diǎn)圖的數(shù)據(jù)就獲取對應(yīng)的幾條就可以了。
傳過來的接口數(shù)據(jù):
data中定義:
profileData: [], // 地圖數(shù)據(jù) sdData: [], // 散點(diǎn)數(shù)據(jù) qsljnumber: '', // 全省領(lǐng)軍人數(shù) qslwnumber: '', // 全省論文數(shù)量 qszznumber: '', // 全省著作數(shù)量
接口數(shù)據(jù):
initCharts () { const charts = echarts.init(this.$refs['charts']) let airData = [] let currentSdData = [] this.axios.post(this.profileurl, { }).then(res => { if (res.result === '00000') { this.profileData = res.data airData=this.profileData this.sdData[0] = res.data[0] this.sdData[1] = res.data[14] this.sdData[2] = res.data[15] this.sdData[3] = res.data[16] currentSdData = this.sdData this.qsljnumber = res.data[17].text.ljnumber this.qslwnumber = res.data[17].text.lwnumber this.qszznumber = res.data[17].text.zznumber charts.setOption({ series:[ { type: 'map', data:airData }, { type: 'effectScatter', data:currentSdData } ] }) } else { this.$Message.error(res.data.information) } }) const option = { // 背景顏色 backgroundColor: 'white', // 提示浮窗樣式 tooltip: { show: true, trigger: 'item', alwaysShowContent: false, backgroundColor: '#0C121C', borderColor: 'rgba(0, 0, 0, 0.16);', hideDelay: 100, triggerOn: 'mousemove', enterable: true, textStyle: { color: '#DADADA', fontSize: '12', width: 20, height: 30, overflow: 'break' }, formatter (params) { console.log(params) return `地區(qū):${params.data.name}</br>領(lǐng)軍人數(shù):${params.data.text.ljnumber}</br>論文數(shù)量:${params.data.text.lwnumber}</br>著作數(shù)量:${params.data.text.zznumber}` }, showDelay: 100 }, // 地圖配置 geo: { map: 'jiangsu', // 地圖文字 label: { // 通常狀態(tài)下的樣式 normal: { // 默認(rèn)是否顯示地區(qū)名稱 show: true, textStyle: { color: 'black' } }, // 鼠標(biāo)放上去的樣式 emphasis: { textStyle: { color: 'black' } } }, // 地圖區(qū)域的樣式設(shè)置 itemStyle: { normal: { // 地圖邊界顏色 borderColor: '#fff', // 地圖區(qū)域背景顏色 areaColor: '#AAD5FF', }, // 鼠標(biāo)放上去高亮的樣式 emphasis: { // 鼠標(biāo)放上去地圖區(qū)域背景顏色 areaColor: '#0057da', borderWidth: 0 } } }, series: [ { data: airData, geoIndex: 0, type:'map' }, { type: 'effectScatter', coordinateSystem: 'geo', effectType: 'ripple', showEffectOn: 'render', rippleEffect: { period: 1, scale: 2, brushType: 'fill' }, symbolSize: [15, 15], // 這里渲染標(biāo)志里的內(nèi)容以及樣式 tooltip: { show: true, formatter (value) { return `地區(qū):${value.data.name}</br>領(lǐng)軍人數(shù):${value.data.text.ljnumber}</br>論文數(shù)量:${value.data.text.lwnumber}</br>著作數(shù)量:${value.data.text.zznumber}` }, color: '#fff' }, hoverAnimation: true, // 標(biāo)志的樣式 itemStyle: { normal: { color: 'rgba(255, 235, 59, .7)', shadowBlur: 10, shadowColor: '#333' } }, zlevel: 1, data: currentSdData } ], // 視覺映射組件 visualMap:{ min:1, max:300, inRange:{ color:['#e0ffff', '#006edd'] }, calculable: true //出現(xiàn)滑塊 } } // 地圖注冊,第一個參數(shù)的名字必須和option.geo.map一致 echarts.registerMap('jiangsu', zhongguo) charts.setOption(option) },
效果:
總結(jié)
到此這篇關(guān)于vue中使用echarts實(shí)現(xiàn)動態(tài)數(shù)據(jù)綁定以及獲取后端接口數(shù)據(jù)的文章就介紹到這了,更多相關(guān)vue使用echarts動態(tài)數(shù)據(jù)綁定內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue計(jì)算屬性無法監(jiān)聽到數(shù)組內(nèi)部變化的解決方案
今天小編就為大家分享一篇vue計(jì)算屬性無法監(jiān)聽到數(shù)組內(nèi)部變化的解決方案,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11vue基于element-ui的三級CheckBox復(fù)選框功能的實(shí)現(xiàn)代碼
最近vue項(xiàng)目需要用到三級CheckBox復(fù)選框,需要實(shí)現(xiàn)全選反選不確定三種狀態(tài)。這篇文章主要介紹了vue基于element-ui的三級CheckBox復(fù)選框功能的實(shí)現(xiàn)方法,需要的朋友可以參考下2018-10-10vue+drf+第三方滑動驗(yàn)證碼接入的實(shí)現(xiàn)
這篇文章要給大家介紹的是vue和drf以及第三方滑動驗(yàn)證碼接入的實(shí)現(xiàn),下文小編講詳細(xì)講解該內(nèi)容,感興趣的小伙伴可以和小編一起來學(xué)習(xí)奧2021-10-10Vue多布局模式實(shí)現(xiàn)方法詳細(xì)講解
這篇文章主要介紹了Vue多布局模式實(shí)現(xiàn)方法,多布局模式可以根據(jù)用戶角色所在場景切換頁面布局,是非常常見的基礎(chǔ)功能,感興趣的同學(xué)可以參考下文2023-05-05Vue?el-table實(shí)現(xiàn)右鍵菜單功能
這篇文章主要為大家詳細(xì)介紹了Vue?el-table實(shí)現(xiàn)右鍵菜單功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04