1.安裝依賴
npm install echarts -S
2.全局引入main.js
// 引入echarts
import echarts from 'echarts'
Vue.prototype.$echarts = echarts
3.模版引入柱形圖(此處爲模版演示)圓環在下面介紹怎麼更改
export default {
name: 'hello',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
},
mounted(){
this.drawLine();
},
methods: {
drawLine(){
// 基於準備好的dom,初始化echarts實例
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圖表如圓環將紅框裏的代碼粘貼到vue裏的myChart.setOption 方法裏即可 同時可配置顏色 color 更改表格中的字體顏色
export default {
name: 'hello',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
},
mounted(){
this.drawLine();
},
methods: {
drawLine(){
// 基於準備好的dom,初始化echarts實例
let myChart = this.$echarts.init(document.getElementById('myChart'))
// 繪製圖表
myChart.setOption({
color:['#4472C5','#ED7C30','#80FF80','#FF8096','#800080'],//配置顏色
tooltip: {
trigger: 'item',
formatter: "{a} <br/>{b}: {c} ({d}%)"
},
legend: {
orient: 'vertical',
x: 'left',
data:['直接訪問','郵件營銷','聯盟廣告','視頻廣告','搜索引擎'],
textStyle: {//------------此處更改表格中的字體顏色
fontSize: '12',
color:'#fff'
}
},
series: [
{
name:'訪問來源',
type:'pie',
radius: ['50%', '70%'],
avoidLabelOverlap: false,
label: {
normal: {
show: false,
position: 'center'
},
emphasis: {
show: true,
textStyle: {
fontSize: '30',
fontWeight: 'bold'
}
}
},
labelLine: {
normal: {
show: false
}
},
data:[
{value:335, name:'直接訪問'},
{value:310, name:'郵件營銷'},
{value:234, name:'聯盟廣告'},
{value:135, name:'視頻廣告'},
{value:1548, name:'搜索引擎'}
]
}
]
});
}
}
}
更多技巧請查看vue專欄 https://blog.csdn.net/qq_42221334/column/info/27230/1