欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue-echarts如何實現(xiàn)圖表組件封裝詳解

 更新時間:2022年05月14日 16:14:49   作者:王李香  
由于在項目中需要對數(shù)據(jù)進行可視化處理,也就是用圖表展示,所以下面這篇文章主要給大家介紹了關(guān)于vue-echarts如何實現(xiàn)圖表組件封裝的相關(guān)資料,需要的朋友可以參考下

背景:

需要大量使用圖表的項目,為了提升開發(fā)效率,可以對圖表類進行封裝成組件,方便頁面的搭建,也能進行統(tǒng)一管理維護,即使后面系統(tǒng)風格需要調(diào)整,調(diào)整起來也比較方便、快速。

有哪些工具?

常用的有Echarts、AntV,使用起來都大同小異,具體圖表如何實現(xiàn),看官網(wǎng)參數(shù)進行配置調(diào)整即可。

怎么封裝?

以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對應(yīng)參數(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ù)調(diào)整;height控制echart的高度。option的title、tooltip、grid、legend、xAxis、yAxis、series幾個重要參數(shù),使用assign進行整合,如果chartOption有傳自定義的配置參數(shù),則生效自定義配置參數(shù),沒有,則使用默認配置。設(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ù)配置,可通過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…

總結(jié)

到此這篇關(guān)于vue-echarts如何實現(xiàn)圖表組件封裝的文章就介紹到這了,更多相關(guān)vue-echarts圖表組件封裝內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue?日期獲取的示例代碼

    Vue?日期獲取的示例代碼

    moment.js是一款現(xiàn)在對時間處理的強大的函數(shù),這篇文章主要介紹了Vue?日期獲取的示例代碼,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-01-01
  • 你了解vue3.0響應(yīng)式數(shù)據(jù)怎么實現(xiàn)嗎

    你了解vue3.0響應(yīng)式數(shù)據(jù)怎么實現(xiàn)嗎

    這篇文章主要介紹了你了解vue3.0響應(yīng)式數(shù)據(jù)怎么實現(xiàn)嗎,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-06-06
  • vue3中的響應(yīng)式原理-effect

    vue3中的響應(yīng)式原理-effect

    這篇文章主要介紹了vue3中的響應(yīng)式原理-effect,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • Vue創(chuàng)建淺層響應(yīng)式數(shù)據(jù)的實例詳解

    Vue創(chuàng)建淺層響應(yīng)式數(shù)據(jù)的實例詳解

    這篇文章主要介紹了Vue創(chuàng)建淺層響應(yīng)式數(shù)據(jù)的實例,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2023-11-11
  • vue實現(xiàn)選擇商品規(guī)格功能

    vue實現(xiàn)選擇商品規(guī)格功能

    這篇文章主要為大家詳細介紹了vue實現(xiàn)選擇商品規(guī)格功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Vue?keep-alive的實現(xiàn)原理分析

    Vue?keep-alive的實現(xiàn)原理分析

    這篇文章主要介紹了Vue?keep-alive的實現(xiàn)原理分析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • element-ui tooltip修改背景顏色和箭頭顏色的實現(xiàn)

    element-ui tooltip修改背景顏色和箭頭顏色的實現(xiàn)

    這篇文章主要介紹了element-ui tooltip修改背景顏色和箭頭顏色的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-12-12
  • vue單頁應(yīng)用在頁面刷新時保留狀態(tài)數(shù)據(jù)的方法

    vue單頁應(yīng)用在頁面刷新時保留狀態(tài)數(shù)據(jù)的方法

    今天小編就為大家分享一篇vue單頁應(yīng)用在頁面刷新時保留狀態(tài)數(shù)據(jù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • Vue.js圖片滑動驗證的實現(xiàn)示例

    Vue.js圖片滑動驗證的實現(xiàn)示例

    為了防止有人惡意使用腳本進行批量操作,會設(shè)置圖片滑動驗證,本文就介紹了Vue.js圖片滑動驗證的實現(xiàn)示例,感興趣的可以了解一下
    2023-05-05
  • vue3通過ref獲取子組件defineExpose的數(shù)據(jù)和方法

    vue3通過ref獲取子組件defineExpose的數(shù)據(jù)和方法

    defineExpose是Vue3中新增的選項,用于向父組件暴露子組件內(nèi)部的屬性和方法,通過defineExpose,子組件可以主動控制哪些屬性和方法可以被父組件訪問,本文主要介紹了vue3通過ref獲取子組件defineExpose的數(shù)據(jù)和方法,需要的朋友可以參考下
    2023-10-10

最新評論