在vue2中使用echarts (Vue-ECharts插件)

Vue-ECharts

ECharts 的 Vue.js 組件。

基於 ECharts v4.1.0+ 開發,依賴 Vue.js v2.2.6+。html

安裝

npm(推薦方式)

$ npm install echarts vue-echarts

CDN

在 HTML 文件按以下方式依次引入 echartsvue-echartsvue

<script src="https://cdn.jsdelivr.net/npm/echarts@4.1.0/dist/echarts.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-echarts@4.0.2"></script>

使用方法

用 npm 與 Vue Loader 基於 ES Module 引入(推薦用法)

import Vue from 'vue'
import ECharts from 'vue-echarts' // 在 webpack 環境下指向 components/ECharts.vue

// 手動引入 ECharts 各模塊來減少打包體積
import 'echarts/lib/chart/bar'
import 'echarts/lib/component/tooltip'

// 若是須要配合 ECharts 擴展使用,只須要直接引入擴展包便可
// 以 ECharts-GL 爲例:
// 須要安裝依賴:npm install --save echarts-gl,並添加以下引用
import 'echarts-gl'

// 註冊組件後便可使用
Vue.component('v-chart', ECharts)

⚠️ 注意事項

引入源碼版本

Vue-ECharts 默認在 webpack 環境下會引入未編譯的源碼版本,若是你正在使用官方的 Vue CLI 來建立項目,可能會遇到默認配置把 node_modules 中的文件排除在 Babel 轉譯範圍之外的問題。請按以下方法修改配置:node

當使用 Vue CLI 3+ 時,須要在 vue.config.js 中的 transpileDependencies 增長 vue-echartsresize-detector,以下:webpack

// vue.config.js
module.exports = {
  transpileDependencies: [
    'vue-echarts',
    'resize-detector'
  ]
}

當使用 Vue CLI 2webpack 模板時,須要按下述的方式修改 build/webpack.base.conf.jsgit

{
        test: /\.js$/,
        loader: 'babel-loader',
-       include: [resolve('src'), resolve('test')]
+       include: [
+         resolve('src'),
+         resolve('test'),
+         resolve('node_modules/vue-echarts'),
+         resolve('node_modules/resize-detector')
+       ]
      }

若是你正直接配置使用 webpack,那麼也請作相似的修改使其可以正常工做。es6

在 Nuxt.js 中使用

在 Nuxt.js 的服務端中使用 Vue-ECharts 時,可能沒有正常轉譯。這是由於 Nuxt.js 默認會將 node_modules 目錄下的絕大多數文件被排除在服務端打包代碼之外。須要手動將 vue-echarts 加入白名單。github

對於 Nuxt.js v2 項目,按以下方式修改 nuxt.config.jsweb

module.exports = {
  build: {
    transpile: ['vue-echarts', 'resize-detector']
  }
}

對於 Nuxt.js v1 項目,按以下方式修改 nuxt.config.jsnpm

// 別忘了運行
// npm i --save-dev webpack-node-externals
const nodeExternals = require('webpack-node-externals')

module.exports = {
  // ...
  build: {
    extend (config, { isServer }) {
      // ...
      if (isServer) {
        config.externals = [
          nodeExternals({
            // `whitelist` 選項的默認值是
            // [/es6-promise|\.(?!(?:js|json)$).{1,5}$/i]
            whitelist: [/es6-promise|\.(?!(?:js|json)$).{1,5}$/i, /^vue-echarts/]
          })
        ]
      }
    }
  }
}

AMD

require.config({
  paths: {
    'vue': 'path/to/vue',
    'echarts': 'path/to/echarts',
    'vue-echarts': 'path/to/vue-ehcarts'
  }
})

require(['vue', 'vue-echarts'], function (Vue, ECharts) {
  // 註冊組件後便可使用
  Vue.component('v-chart', ECharts)
})

全局變量

在沒有使用任何模塊系統的狀況下,組件將經過 window.VueECharts 變量暴露接口:json

// 註冊組件後便可使用
Vue.component('v-chart', VueECharts)

調用組件

<template>
<v-chart :options="polar"/>
</template>

<style>
/**
 * 默認尺寸爲 600px×400px,若是想讓圖表響應尺寸變化,能夠像下面這樣
 * 把尺寸設爲百分比值(同時請記得爲容器設置尺寸)。
 */
.echarts {
  width: 100%;
  height: 100%;
}
</style>

<script>
import ECharts from 'vue-echarts'
import 'echarts/lib/chart/line'
import 'echarts/lib/component/polar'

export default {
  components: {
    'v-chart': ECharts
  },
  data () {
    let data = []

    for (let i = 0; i <= 360; i++) {
        let t = i / 180 * Math.PI
        let r = Math.sin(2 * t) * Math.cos(2 * t)
        data.push([r, i])
    }

    return {
      polar: {
        title: {
          text: '極座標雙數值軸'
        },
        legend: {
          data: ['line']
        },
        polar: {
          center: ['50%', '54%']
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          }
        },
        angleAxis: {
          type: 'value',
          startAngle: 0
        },
        radiusAxis: {
          min: 0
        },
        series: [
          {
            coordinateSystem: 'polar',
            name: 'line',
            type: 'line',
            showSymbol: false,
            data: data
          }
        ],
        animationDuration: 2000
      }
    }
  }
}
</script>

查看這裏瞭解更多例子。

Props (均爲響應式)

  • initOptions

    用來初始化 ECharts 實例。

  • theme

    當前 ECharts 實例使用的主題。

  • options

    ECharts 實例的數據。修改這個 prop 會觸發 ECharts 實例的 setOption 方法。

    若是直接修改 options 綁定的數據而對象引用保持不變,setOption 方法調用時將帶有參數 notMerge: false。不然,若是爲 options 綁定一個新的對象,setOption 方法調用時則將帶有參數 notMerge: true

    例如,若是有以下模板:

    <v-chart :options="data"/>

    那麼:

    this.data = newObject // setOption(this.options, true)
    this.data.title.text = 'Trends' // setOption(this.options, false)
  • group

    實例的分組,會自動綁定到 ECharts 組件的同名屬性上。

  • autoresize (默認值:false

    這個 prop 用來指定 ECharts 實例在組件根元素尺寸變化時是否須要自動進行重繪。

  • manual-update (默認值:false

    在性能敏感(數據量很大)的場景下,咱們最好對於 options prop 繞過 Vue 的響應式系統。當將 manual-update prop 指定爲 true 且不傳入 options prop 時,數據將不會被監聽。而後,你須要用 ref 獲取組件實例之後手動調用 mergeOptions 方法來更新圖表。

計算屬性

  • width [只讀]

    用來獲取 ECharts 實例的當前寬度。

  • height [只讀]

    用來獲取 ECharts 實例的當前高度。

  • computedOptions [只讀]

    用來讀取 ECharts 更新內部 options 後的實際數據。

方法

  • mergeOptions(底層調用了 ECharts 實例的 setOption 方法)

    提供了一個更貼切的名稱來描述 setOption 方法的實際行爲。

  • appendData
  • resize
  • dispatchAction
  • showLoading
  • hideLoading
  • convertToPixel
  • convertFromPixel
  • containPixel
  • getDataURL
  • getConnectedDataURL
  • clear
  • dispose

靜態方法

  • connect
  • disconnect
  • registerMap
  • registerTheme
  • graphic.clipPointsByRect
  • graphic.clipRectByRect

事件

Vue-ECharts 支持以下事件:

  • legendselectchanged
  • legendselected
  • legendunselected
  • legendscroll
  • datazoom
  • datarangeselected
  • timelinechanged
  • timelineplaychanged
  • restore
  • dataviewchanged
  • magictypechanged
  • geoselectchanged
  • geoselected
  • geounselected
  • pieselectchanged
  • pieselected
  • pieunselected
  • mapselectchanged
  • mapselected
  • mapunselected
  • axisareaselected
  • focusnodeadjacency
  • unfocusnodeadjacency
  • brush
  • brushselected
  • rendered
  • finished
  • 鼠標事件

    • click
    • dblclick
    • mouseover
    • mouseout
    • mousemove
    • mousedown
    • mouseup
    • globalout
    • contextmenu

更多詳細信息請參考 ECharts 的 API 文檔

本地開發

$ npm i
$ npm run dev

打開 http://localhost:8080/demo 來查看 demo。

相關文章
相關標籤/搜索