安裝echarts依賴
npm 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(){
// 基于準(zhǔn)備好的dom芭析,初始化echarts實(shí)例
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]
}]
});
}
}
}
注意: 這里echarts初始化應(yīng)在鉤子函數(shù)mounted()中,這個鉤子函數(shù)是在el 被新創(chuàng)建的 vm.$el 替換,并掛載到實(shí)例上去之后調(diào)用
按需引入
上面全局引入會將所有的echarts圖表打包佑女,導(dǎo)致體積過大,所以我覺得最好還是按需引入割笙。
// 引入基本模板
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: 'hello',
data() {
return {
msg: 'Welcome to Your Vue.js App'
}
},
mounted() {
this.drawLine();
},
methods: {
drawLine() {
// 基于準(zhǔn)備好的dom昼捍,初始化echarts實(shí)例
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]
}]
});
}
}
}
這里之所以使用 require 而不是 import,是因?yàn)?require 可以直接從 node_modules 中查找琴庵,而 import 必須把路徑寫全误算。
本文轉(zhuǎn)自 https://www.cnblogs.com/Smiled/p/7686316.html