vue項目中引入echarts及動態(tài)數(shù)據(jù)自動更新及圖表尺寸重新加載(resize)三點

一:首先,我們需要在項目中引入echars

npm install echarts -S

二:在項目的mian.js文件中引入
1)全局引入

import echarts from 'echarts'
Vue.prototype.$echarts = echarts

2)局部引入

<template>
  <div style="width:500px;height:200px;"></div>
</template>
<script>
// 引入基本模板
let echarts = require('echarts/lib/echarts')
// 引入柱狀圖組件
require('echarts/lib/chart/bar')
// 引入提示框和title組件
require('echarts/lib/component/tooltip')
require('echarts/lib/component/title')

export default {
  name: 'verticalBar',
  props: {
    color: {
      type: Array,
      default: () => []
    },
    columnList: {
      type: Array,
      default: () => []
    },
    advanceData: {
      type: Array,
      default: () => []
    },
    depositData: {
      type: Array,
      default: () => []
    },
  },
  data() {
    return {

    }
  },
  watch: {
    //這里選擇一個我們要監(jiān)聽的傳遞到組件中的一個值,從而去執(zhí)行this.drawPie()去達到更新數(shù)據(jù)
    columnList: {
      handler(newVal) {
        this.drawPie()
      },
      deep: true
    }
  },
  computed: {
    //我們將options作為一個對象,是為了在初始化和數(shù)據(jù)變化時,直接使用this.myChart.setOption(this.options)即可實現(xiàn)圖表重新更新
    options() {
      return {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
            label: {
              show: true
            }
          }
        },
        grid: {
          left: "8%",
          top: "22%",
          right: "6%",
          bottom: "16%"
        },
        legend: {
          data: ["預收", "押金"],
          top: "2%",
          icon: 'circle',
          itemWidth: 12,
          itemHeight: 12,
          textStyle: {
            color: "#1FC3CE",
            fontSize: 14
          }
        },
        xAxis: {
          data: this.columnList,
          axisLine: {
            show: true, //隱藏X軸軸線
            lineStyle: {
              color: "rgba(51,181,194,.4)",
              width: 1
            }
          },
          axisTick: {
            show: false, //隱藏X軸刻度
            alignWithLabel: true
          },
          axisLabel: {
            show: true,
            textStyle: {
              color: "#33b5c2", //X軸文字顏色
              fontSize: 14
            },
            interval: 0,
            rotate: 0
          }
        },
        yAxis: [{
          type: "value",
          name: "(單位:萬)",
          nameTextStyle: {
            color: "#33b5c2",
            fontSize: 14
          },
          splitLine: {
            show: true,
            lineStyle: {
              width: 1,
              color: "rgba(51,181,194,.4)"
            }
          },
          axisTick: {
            show: false
          },
          axisLine: {
            show: false
          },
          axisLabel: {
            show: true,
            textStyle: {
              color: "#33b5c2",
              fontSize: 14
            }
          }
        }
        ],
        series: [{
          name: "預收",
          type: "bar",
          barWidth: 14,
          itemStyle: {
            normal: {
              barBorderRadius: [7, 7, 0, 0],
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                offset: 0,
                color: "#00ebd6"
              },
              {
                offset: 1,
                color: "#01a3f8"
              }
              ]
              )
            }
          },
          data: this.advanceData
        },
        {
          name: "押金",
          type: "bar",
          barWidth: 14,
          itemStyle: {
            normal: {
              barBorderRadius: [7, 7, 0, 0],
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                offset: 0,
                color: "#f8d278"
              },
              {
                offset: 1,
                color: "#ef7c1f"
              }
              ])
            }
          },
          data: this.depositData
        }]
      }
    }
  },
  mounted() {
    //這里增加一個nextTick,防止圖表初始化異常
    this.$nextTick(() => {
      this.drawPie()
    })
  },
  methods: {
    chartResize() {
      this.myChart.resize()
    },
    drawPie() {
      //注意:因為echarts不能自動雙向綁定,所以需要重新調用drawPie方法來重新向options賦值及重執(zhí)行繪制
      //調用setOption時,echarts不會刪除重繪,而是以動畫效果方式重加載
      this.options.xAxis.data = this.columnList;
      this.options.series[0].data = this.advanceData;
      this.options.series[1].data = this.depositData;
      //我們在過去使用echarts都是用id來標識容器,這里需要直接將當前組件作為標識,避免使用id造成id重復,否則會出現(xiàn)同一個頁面不能多次使用的問題
      this.myChart = echarts.init(this.$el, 'macarons')
      this.myChart.setOption(this.options);
    }
  }
}
</script>

<style>
</style>

其中color、columnList、advanceData、depositData為我們傳遞的值。

代碼為自己當前項目的源碼,也優(yōu)化不少,在使用中目前沒有任何問題,在這里分享給大家。
如果有什么優(yōu)化的地方,歡迎留言。

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容