//安装echarts:
npm install echarts --save
方法一:
main.js中引入 :
//引入echarts
import echarts from 'echarts'
//将echarts设置全局变量,$echarts自定义
Vue.prototype.$echarts = echarts
然后在需要用到的地方:
<template>
<div id="main" ref="main" class="text item echart" style="height:800px;width:800px;"/>
</template>
<script>
// import echarts from 'echarts'
export default {
data() {
return {
}
},
created() {
},
mounted() {
this.$nextTick(() => {
this.myEcharts()
})
},
methods: {
myEcharts() {
// 通过ref获取
var mychart = this.$refs.main
// 通过id获取
// var mychat=document.getElementById('main')
// 初始化echarts实例
// const mychats = echarts.init(mychart)
const mychats = this.$echarts.init(mychart)
// 准备数据
const option = {
tooltip: {
trigger: 'item',
formatter: '{a} <br/>{b}: {c} ({d}%)'
},
legend: {
orient: 'vertical',
left: 10,
data: ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎']
},
series: [
{
name: '访问来源',
type: 'pie',
radius: ['50%', '70%'],
avoidLabelOverlap: false,
label: {
show: false,
position: 'center'
},
emphasis: {
label: {
show: true,
fontSize: '30',
fontWeight: 'bold'
}
},
labelLine: {
show: false
},
data: [
{ value: 335, name: '直接访问' },
{ value: 310, name: '邮件营销' },
{ value: 234, name: '联盟广告' },
{ value: 135, name: '视频广告' },
{ value: 1548, name: '搜索引擎' }
]
}
]
}
// 开始制图
mychats.setOption(option)
}
}
}
</script>
方法二:
在需要用到图表的地方上直接引用
//引入echarts
import echarts from 'echarts'
代码:
<template>
<div id="main" ref="main" class="text item echart" style="height:800px;width:800px;"/>
</template>
<script>
import echarts from 'echarts'
export default {
data() {
return {
}
},
created() {
},
mounted() {
this.$nextTick(() => {
this.myEcharts()
})
},
methods: {
myEcharts() {
//通过ref获取
var mychart = this.$refs.main
//通过id获取
//var mychat=document.getElementById('main')
// 初始化echarts实例
const mychats = echarts.init(mychart)
//准备数据
const option = {
tooltip: {
trigger: 'item',
formatter: '{a} <br/>{b}: {c} ({d}%)'
},
legend: {
orient: 'vertical',
left: 10,
data: ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎']
},
series: [
{
name: '访问来源',
type: 'pie',
radius: ['50%', '70%'],
avoidLabelOverlap: false,
label: {
show: false,
position: 'center'
},
emphasis: {
label: {
show: true,
fontSize: '30',
fontWeight: 'bold'
}
},
labelLine: {
show: false
},
data: [
{ value: 335, name: '直接访问' },
{ value: 310, name: '邮件营销' },
{ value: 234, name: '联盟广告' },
{ value: 135, name: '视频广告' },
{ value: 1548, name: '搜索引擎' }
]
}
]
}
//开始制图
mychats.setOption(option)
}
}
}
</script>
如第一张图为效果
在这里调用绘图的方法