Vue?echarts實(shí)例項(xiàng)目商家銷量統(tǒng)計(jì)圖實(shí)現(xiàn)詳解

總體效果如圖
組件結(jié)構(gòu)設(shè)計(jì)
SellerPage.vue
<!--針對(duì)于/sellerpage 這條路徑顯示 測試顯示組件-->
<template>
<div class="comP1">
<Seller></Seller>
</div>
</template>
<script>
import Seller from "@/components/Seller";
export default {
name: "SellerPage",
components:{Seller}
}
</script>
<style scoped>
</style>Seller.vue
<!-- 顯示商家銷量統(tǒng)計(jì)的圖表 -->
<template>
<div class="comP2" ref="seller_1"></div>
</template>
<script>
export default {
data () {
return {}
},
methods: {}
}
</script>
<style lang="less" scoped>
</style>接下來就在 Seller.vue 搞事情了
在mounted生命周期中初始化 echartsInstance 對(duì)象
因?yàn)樵诮M件掛載到頁面上 echarts 才能找到具體的DOM渲染
methods 里定義 initChart 方法this.$refs.seller_1 找到具體盒子
this.theme 主題 來自于 Vuex 使用映射快捷引入
import {mapState} from "vuex";
computed:{
...mapState(['theme'])
},然后就是設(shè)置配置項(xiàng) 在之前的基礎(chǔ)都有講到過
新增了柱狀圖漸變?cè)O(shè)置 可以詳看注釋
鼠標(biāo)移入和移出時(shí)間 用來停止和開啟定時(shí)器 后面會(huì)用到
methods:{
// 初始化echarts 實(shí)例對(duì)象
initChart(){
this.chartsInstance = this.$echarts.init(this.$refs.seller_1,this.theme)
const initOption = {
title:{
text:'▎銷售業(yè)績統(tǒng)計(jì)',
left:20,
top:15
},
grid:{
top: '24%',
left: '3%',
right:'6%',
bottom:'3%',
containLabel: true // 距離是包含坐標(biāo)軸上的文字
},
xAxis:{
type:'value',
},
yAxis:{
type: 'category',
},
tooltip:{
trigger:'axis',
axisPointer:{
type:'line',
z:0,
lineStyle:{
color:'#2d3443'
}
}
},
series:[
{
type:'bar', // 柱狀圖
label:{
show:true,// 顯示柱內(nèi)數(shù)值
position:'right',// 數(shù)值顯示位置
textStyle: {
color:'#fff'// 數(shù)值顯示顏色
}
},
itemStyle:{
// 設(shè)置漸變 x1,y1,x2,y2(指明漸變的方向) [{指明不同百分比下顏色的值}]
color:new this.$echarts.graphic.LinearGradient(0,0,1,0,[
{
offset:0,
color:'#5052ee'
},
{
offset: 1,
color: '#ab6ee5'
}
])
}
},
]
}
this.chartsInstance.setOption(initOption)
// 對(duì)圖表進(jìn)行 鼠標(biāo)移入移出 事件的監(jiān)聽
this.chartsInstance.on('mouseover',()=>{
clearInterval(this.timerID)
})
this.chartsInstance.on('mouseout',()=>{
this.startInterval()
})
},
}發(fā)送請(qǐng)求獲取對(duì)應(yīng)的數(shù)據(jù)并進(jìn)行相應(yīng)操作
使用到的data:
data(){
return{
chartsInstance:null, 圖表的實(shí)例對(duì)象 初始化后賦值給它
allData:null, 請(qǐng)求過來的數(shù)據(jù)
currentPage:1, 當(dāng)前頁碼 定時(shí)器進(jìn)行改變 來截取哪些數(shù)據(jù)展示
totalPage:0, 總頁數(shù)
timerID:null 定時(shí)器的ID 用于啟停
}
},直接使用 注冊(cè)的 axios =>$http.get 來獲取 并通過 async await 簡化 解構(gòu)出來
進(jìn)行 sort 排序操作
計(jì)算出 每頁顯示5條信息的情況下 總頁數(shù)是多少 能被5整除就直接用 整除不了就再加一頁
async getData(){
const {data:res} = await this.$http.get('seller')
this.allData = res
// 對(duì)數(shù)據(jù)進(jìn)行排序
this.allData.sort((a,b) =>{
return a.value - b.value // 從小到大排序
})
// 每五個(gè)數(shù)據(jù) 顯示一頁
this.totalPage = this.allData.length % 5 === 0 ? this.allData.length / 5 : Math.floor(this.allData.length / 5) + 1
this.updateChart()
this.startInterval()
}數(shù)據(jù)和頁碼轉(zhuǎn)存到 data 里了 可以更新設(shè)置 把圖表渲染出來
當(dāng)期頁碼默認(rèn)是1 就截取 0-5的索引
在使用 map 生成新的數(shù)組 用于 x軸 和 y軸
最后更新配置項(xiàng) 配置項(xiàng)會(huì)自動(dòng)整合
// 更新圖表
updateChart(){
const start = (this.currentPage - 1) * 5
const end = this. currentPage * 5
const showData = this.allData.slice(start,end) // slice 截取 不包括 end
const sellerNames = showData.map((item) =>{
return item.name
})
const sellerValues = showData.map((item) =>{
return item.value
})
const dataOption = {
yAxis:{data:sellerNames},
series:[{data:sellerValues}]
}
this.chartsInstance.setOption(dataOption)
},當(dāng)?shù)谝豁摰臄?shù)據(jù)展示出來時(shí)就可以開啟定時(shí)器了
開始之前先清除之前的定時(shí)器(來回切換頁面后 回到最初的數(shù)據(jù))
頁碼累計(jì)相加 到最大值再返回到1
改變 當(dāng)前頁的同時(shí) 調(diào)用更新圖表數(shù)據(jù)的方法
鼠標(biāo)移入移出 啟停定時(shí)器的方法 在注冊(cè)實(shí)例的時(shí)候已經(jīng)添加
// 開啟定時(shí)器
startInterval(){
if (this.timerID){
clearInterval(this.timerID)
}
this.timerID = setInterval(()=>{
this.currentPage++
if (this.currentPage > this.totalPage){
this.currentPage = 1
}
this.updateChart()
},3600)
},小細(xì)節(jié)
xAxis:{
type:'value',
// 細(xì)節(jié)處理 固定x軸的最大值
max:this.allData[this.allData.length -1].value
},當(dāng)窗口尺寸發(fā)生變化時(shí)的操作
自己定義一個(gè) 合適 簡易的 rem :當(dāng)前窗口柵格化100份 * 3.6
根據(jù)這個(gè)數(shù)據(jù) 設(shè)定 標(biāo)題大小 提示文字大小 柱狀圖的寬度和 圓角尺寸
初始化頁面時(shí) 調(diào)用一次 之后 跟隨窗口事件調(diào)用
mounted() {
window.addEventListener('resize',this.screenAdapter)
this.screenAdapter()
}, // 當(dāng)瀏覽器寬度發(fā)生變化時(shí)
screenAdapter(){
const titleFontSize = this.$refs.seller_1.offsetWidth / 100 * 3.6
// 分辨率改變時(shí)更新的配置
const adapterOption = {
title:{
textStyle:{
fontSize: titleFontSize
}
},
tooltip:{
axisPointer:{
lineStyle:{
width:titleFontSize,
}
}
},
series:[
{
type:'bar', // 柱狀圖
barWidth:titleFontSize,// 柱狀寬度
itemStyle:{
barBorderRadius:[0,titleFontSize/2,titleFontSize/2,0],// 柱狀的圓角
}
},
]
}
this.chartsInstance.setOption(adapterOption)
this.chartsInstance.resize()
}到此這篇關(guān)于Vue echarts實(shí)例項(xiàng)目商家銷量統(tǒng)計(jì)圖實(shí)現(xiàn)詳解的文章就介紹到這了,更多相關(guān)Vue 銷量統(tǒng)計(jì)圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue中echarts自動(dòng)輪播tooltip問題
- Vue?Echarts實(shí)現(xiàn)實(shí)時(shí)大屏動(dòng)態(tài)數(shù)據(jù)顯示
- Vue中使用 Echarts5.0 遇到的一些問題(vue-cli 下開發(fā))
- Vue echarts 實(shí)現(xiàn)離線中國地圖的示例代碼(細(xì)化到省份)
- Vue?echarts實(shí)例項(xiàng)目地區(qū)銷量趨勢(shì)堆疊折線圖實(shí)現(xiàn)詳解
- Vue echarts模擬后端數(shù)據(jù)流程詳解
- Vue echarts封裝組件需求分析與實(shí)現(xiàn)
相關(guān)文章
vue-resource post數(shù)據(jù)時(shí)碰到Django csrf問題的解決
這篇文章主要介紹了vue-resource post數(shù)據(jù)時(shí)碰到Django csrf問題的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Vue.js 2.0 移動(dòng)端拍照壓縮圖片預(yù)覽及上傳實(shí)例
這篇文章主要介紹了Vue.js 2.0 移動(dòng)端拍照壓縮圖片預(yù)覽及上傳實(shí)例,本來移動(dòng)端開發(fā)H5應(yīng)用,準(zhǔn)備將mui框架和Vue.js+vue-router+vuex 全家桶結(jié)合起來使用2017-04-04
element ui 表格動(dòng)態(tài)列顯示空白bug 修復(fù)方法
今天小編就為大家分享一篇element ui 表格動(dòng)態(tài)列顯示空白bug 修復(fù)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09
詳解vue-router的導(dǎo)航鉤子(導(dǎo)航守衛(wèi))
這篇文章主要介紹了詳解vue-router的導(dǎo)航鉤子(導(dǎo)航守衛(wèi)),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
vue cli3 實(shí)現(xiàn)分環(huán)境打包的步驟
這篇文章主要介紹了vue cli3 實(shí)現(xiàn)分環(huán)境打包的步驟,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03

