vue-echarts如何實現(xiàn)圖表組件封裝詳解
背景:
需要大量使用圖表的項目,為了提升開發(fā)效率,可以對圖表類進行封裝成組件,方便頁面的搭建,也能進行統(tǒng)一管理維護,即使后面系統(tǒng)風格需要調整,調整起來也比較方便、快速。
有哪些工具?
常用的有Echarts、AntV,使用起來都大同小異,具體圖表如何實現(xiàn),看官網(wǎng)參數(shù)進行配置調整即可。
怎么封裝?
以Echarts為例,效果截圖

- 安裝依賴,安裝vue-echarts需要安裝echarts,vue2下使用vue-echarts還需要安裝@vue/composition-api。
npm install echarts --save npm install vue-echarts --save npm install --save @vue/composition-api // 最終安裝版本 // "echarts": "^5.3.2", // "vue-echarts": "^6.0.3", // "@vue/composition-api": "^1.6.1",
- 使用vue-echarts對圖表進行封裝,這里只演示line折線圖,常用的柱狀圖bar、餅圖pie等與折線圖line的實現(xiàn)方法相似,更改defaultOption和option對應參數(shù)即可。vue-echarts只需要傳入option配置即可進行圖表更新,配合vue的computed,我們可以根據(jù)props進來的數(shù)據(jù)進行實時計算,從而實現(xiàn)echarts的繪制和數(shù)據(jù)更新。dataSource為傳進來echarts的數(shù)據(jù),包括橫縱坐標的數(shù)值和配置參數(shù);chartOption是一些用戶自定義的line參數(shù),可以根據(jù)當前圖表的風格對組件做一些參數(shù)調整;height控制echart的高度。option的title、tooltip、grid、legend、xAxis、yAxis、series幾個重要參數(shù),使用assign進行整合,如果chartOption有傳自定義的配置參數(shù),則生效自定義配置參數(shù),沒有,則使用默認配置。設置autoresize讓圖表自適應。
<template>
<v-chart
ref="myChart"
class="echarts"
:style="{height: height}"
:option="option"
autoresize
@click="click"
/>
</template>
<script>
// eslint-disable-next-line no-unused-vars
import echarts from 'echarts'
import VChart from 'vue-echarts'
export default {
name: 'EchartLine',
components: {
VChart
},
props: {
dataSource: {
type: Object,
default: () => {
return {}
}
},
chartOption: {
type: Object,
default: () => {
return {}
}
},
height: {
type: String,
default: '340px'
},
unit: {
type: String,
default: ''
}
},
computed: {
option () {
const option = {
title: _.assign({}, this.defaultOption.title, this.chartOption.title || {}),
tooltip: _.assign({}, this.defaultOption.tooltip, this.chartOption.tooltip || {}),
grid: _.assign({}, this.defaultOption.grid, this.chartOption.grid || {}),
legend: _.assign({}, this.defaultOption.legend, this.chartOption.legend || {}),
xAxis: _.assign({}, this.defaultOption.xAxis, this.chartOption.xAxis, {data: this.dataSource.xAxis}),
yAxis: _.assign({}, this.defaultOption.yAxis, this.chartOption.yAxis || {}),
series: _.map(this.dataSource.yAxis, dataItem => {
return {
type: 'line',
symbol: dataItem.symbol || 'circle',
smooth: dataItem.smooth !== false,
symbolSize: dataItem.symbolSize || 8,
showSymbol: dataItem.showSymbol || (dataItem.data.length === 1),
name: dataItem.name,
data: dataItem.data,
itemStyle: {
color: dataItem.color,
borderColor: 'rgba(255,255,255,0.8)',
borderWidth: 2
}
}
})
}
return option
}
},
data () {
return {
defaultOption: {
title: {
x: '5%'
},
tooltip: {
trigger: 'axis',
textStyle: {
color: '#fff'
},
backgroundColor: 'rgba(51,51,51,0.80)',
padding: [14, 20]
},
grid: {
top: '15%',
left: '24',
right: '24',
bottom: '60',
containLabel: true
},
legend: {
left: 'center',
bottom: '26',
itemGap: 25,
itemWidth: 8,
itemHeight: 8,
show: true,
icon: 'circle',
textStyle: {
color: () => {
return _.map(this.dataSource.yAxis, item => {
return item.color
})
}
}
},
xAxis: {
axisLabel: {
margin: 12,
textStyle: {
color: '#666'
}
},
axisTick: {
show: false
},
axisLine: {
lineStyle: {
color: '#E8E8E8'
}
},
splitLine: {
show: false
}
},
yAxis: {
minInterval: 1,
splitNumber: 5,
axisLine: {
show: true,
lineStyle: {
color: '#E8E8E8'
}
},
splitLine: {
show: true,
lineStyle: {
color: '#E8E8E8',
opacity: 0.5,
type: 'dotted'
}
},
axisTick: {
show: false
},
axisLabel: {
textStyle: {
color: '#666'
}
}
}
}
}
},
methods: {
click (e) {
this.$emit('click', e)
}
}
}
</script>
<style lang="scss" scoped>
.echarts{
width: 100%;
height: 100%;
}
</style>- 組件使用,配置dataSource即可展示數(shù)值,如果想自行定義一些參數(shù)配置,可通過chartOption配置實現(xiàn)。
<template>
<EchartLine :dataSource="dataSource"></EchartLine>
</template>
<script>
import EchartLine from '@/components/EchartLine'
export default {
name: 'EchartsDemo',
components: {
EchartLine
},
data () {
return {
dataSource: {
xAxis: ['星期一', '星期二', '星期三', '星期四', '星期五'],
yAxis: [
{
name: '語文',
color: '#FF6F00',
data: [45, 56, 24, 87, 45]
},
{
name: '數(shù)學',
color: '#FFB903',
data: [34, 86, 67, 34, 89]
},
{
name: '英語',
color: '#3D8BFF',
data: [66, 83, 45, 77, 73]
}
]
}
}
},
methods: {
}
}
</script>Tips:
vue-echarts資料:github.com/ecomfe/vue-…
echarts v5各參數(shù)配置:echarts.apache.org/zh/option.h…
總結
到此這篇關于vue-echarts如何實現(xiàn)圖表組件封裝的文章就介紹到這了,更多相關vue-echarts圖表組件封裝內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
你了解vue3.0響應式數(shù)據(jù)怎么實現(xiàn)嗎
這篇文章主要介紹了你了解vue3.0響應式數(shù)據(jù)怎么實現(xiàn)嗎,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-06-06
Vue創(chuàng)建淺層響應式數(shù)據(jù)的實例詳解
這篇文章主要介紹了Vue創(chuàng)建淺層響應式數(shù)據(jù)的實例,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-11-11
element-ui tooltip修改背景顏色和箭頭顏色的實現(xiàn)
這篇文章主要介紹了element-ui tooltip修改背景顏色和箭頭顏色的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-12-12
vue單頁應用在頁面刷新時保留狀態(tài)數(shù)據(jù)的方法
今天小編就為大家分享一篇vue單頁應用在頁面刷新時保留狀態(tài)數(shù)據(jù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
vue3通過ref獲取子組件defineExpose的數(shù)據(jù)和方法
defineExpose是Vue3中新增的選項,用于向父組件暴露子組件內部的屬性和方法,通過defineExpose,子組件可以主動控制哪些屬性和方法可以被父組件訪問,本文主要介紹了vue3通過ref獲取子組件defineExpose的數(shù)據(jù)和方法,需要的朋友可以參考下2023-10-10

