vue-echarts如何實(shí)現(xiàn)圖表組件封裝詳解
背景:
需要大量使用圖表的項(xiàng)目,為了提升開(kāi)發(fā)效率,可以對(duì)圖表類(lèi)進(jìn)行封裝成組件,方便頁(yè)面的搭建,也能進(jìn)行統(tǒng)一管理維護(hù),即使后面系統(tǒng)風(fēng)格需要調(diào)整,調(diào)整起來(lái)也比較方便、快速。
有哪些工具?
常用的有Echarts、AntV,使用起來(lái)都大同小異,具體圖表如何實(shí)現(xiàn),看官網(wǎng)參數(shù)進(jìn)行配置調(diào)整即可。
怎么封裝?
以Echarts為例,效果截圖
- 安裝依賴(lài),安裝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對(duì)圖表進(jìn)行封裝,這里只演示line折線圖,常用的柱狀圖bar、餅圖pie等與折線圖line的實(shí)現(xiàn)方法相似,更改defaultOption和option對(duì)應(yīng)參數(shù)即可。vue-echarts只需要傳入option配置即可進(jìn)行圖表更新,配合vue的computed,我們可以根據(jù)props進(jìn)來(lái)的數(shù)據(jù)進(jìn)行實(shí)時(shí)計(jì)算,從而實(shí)現(xiàn)echarts的繪制和數(shù)據(jù)更新。dataSource為傳進(jìn)來(lái)echarts的數(shù)據(jù),包括橫縱坐標(biāo)的數(shù)值和配置參數(shù);chartOption是一些用戶(hù)自定義的line參數(shù),可以根據(jù)當(dāng)前圖表的風(fēng)格對(duì)組件做一些參數(shù)調(diào)整;height控制echart的高度。option的title、tooltip、grid、legend、xAxis、yAxis、series幾個(gè)重要參數(shù),使用assign進(jìn)行整合,如果chartOption有傳自定義的配置參數(shù),則生效自定義配置參數(shù),沒(méi)有,則使用默認(rèn)配置。設(shè)置autoresize讓圖表自適應(yīng)。
<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ù)配置,可通過(guò)chartOption配置實(shí)現(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: '語(yǔ)文', color: '#FF6F00', data: [45, 56, 24, 87, 45] }, { name: '數(shù)學(xué)', color: '#FFB903', data: [34, 86, 67, 34, 89] }, { name: '英語(yǔ)', 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…
總結(jié)
到此這篇關(guān)于vue-echarts如何實(shí)現(xiàn)圖表組件封裝的文章就介紹到這了,更多相關(guān)vue-echarts圖表組件封裝內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
你了解vue3.0響應(yīng)式數(shù)據(jù)怎么實(shí)現(xiàn)嗎
這篇文章主要介紹了你了解vue3.0響應(yīng)式數(shù)據(jù)怎么實(shí)現(xiàn)嗎,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-06-06Vue創(chuàng)建淺層響應(yīng)式數(shù)據(jù)的實(shí)例詳解
這篇文章主要介紹了Vue創(chuàng)建淺層響應(yīng)式數(shù)據(jù)的實(shí)例,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-11-11Vue?keep-alive的實(shí)現(xiàn)原理分析
這篇文章主要介紹了Vue?keep-alive的實(shí)現(xiàn)原理分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04element-ui tooltip修改背景顏色和箭頭顏色的實(shí)現(xiàn)
這篇文章主要介紹了element-ui tooltip修改背景顏色和箭頭顏色的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12vue單頁(yè)應(yīng)用在頁(yè)面刷新時(shí)保留狀態(tài)數(shù)據(jù)的方法
今天小編就為大家分享一篇vue單頁(yè)應(yīng)用在頁(yè)面刷新時(shí)保留狀態(tài)數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09Vue.js圖片滑動(dòng)驗(yàn)證的實(shí)現(xiàn)示例
為了防止有人惡意使用腳本進(jìn)行批量操作,會(huì)設(shè)置圖片滑動(dòng)驗(yàn)證,本文就介紹了Vue.js圖片滑動(dòng)驗(yàn)證的實(shí)現(xiàn)示例,感興趣的可以了解一下2023-05-05vue3通過(guò)ref獲取子組件defineExpose的數(shù)據(jù)和方法
defineExpose是Vue3中新增的選項(xiàng),用于向父組件暴露子組件內(nèi)部的屬性和方法,通過(guò)defineExpose,子組件可以主動(dòng)控制哪些屬性和方法可以被父組件訪問(wèn),本文主要介紹了vue3通過(guò)ref獲取子組件defineExpose的數(shù)據(jù)和方法,需要的朋友可以參考下2023-10-10