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