該示例使用?vue-cli
?腳手架搭建
安裝echarts依賴
npm install echarts -S
或者使用國內的淘寶鏡像:
npm install -g cnpm --registry=https://registry.npm.taobao.org
cnpm install echarts -S
創建圖表
全局引入
import echarts from 'echarts'Vue.prototype.$echarts = echarts
<div id="myChart" :style="{width: '300px', height: '300px'}"></div>
export default {name: 'hello',data () {return {msg: 'Welcome to Your Vue.js App'}},mounted(){this.drawLine();},methods: {drawLine(){let myChart = this.$echarts.init(document.getElementById('myChart'))myChart.setOption({title: { text: '在Vue中使用echarts' },tooltip: {},xAxis: {data: ["襯衫","羊毛衫","雪紡衫","褲子","高跟鞋","襪子"]},yAxis: {},series: [{name: '銷量',type: 'bar',data: [5, 20, 36, 10, 10, 20]}]});}}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
注意:?這里echarts初始化應在鉤子函數mounted()中,這個鉤子函數是在el 被新創建的 vm.$el 替換,并掛載到實例上去之后調用
按需引入
上面全局引入會將所有的echarts圖表打包,導致體積過大,所以我覺得最好還是按需引入。
let echarts = require('echarts/lib/echarts')
require('echarts/lib/chart/bar')
require('echarts/lib/component/tooltip')
require('echarts/lib/component/title')
export default {name: 'hello',data() {return {msg: 'Welcome to Your Vue.js App'}},mounted() {this.drawLine();},methods: {drawLine() {let myChart = echarts.init(document.getElementById('myChart'))myChart.setOption({title: { text: 'ECharts 入門示例' },tooltip: {},xAxis: {data: ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]},yAxis: {},series: [{name: '銷量',type: 'bar',data: [5, 20, 36, 10, 10, 20]}]});}}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
這里之所以使用 require 而不是 import,是因為 require 可以直接從 node_modules 中查找,而 import 必須把路徑寫全。
頁面展示

參考文章
- 使用 Echarts 創建圖表
- Vue 2.x 實戰之后臺管理系統開發
- echarts圖表自適應問題