1.安装
npm install echarts --save-dev
2.在main.js文件中引入
import * as echarts from 'echarts'
Vue.prototype.$echarts = echarts
3. 新建一个.vue文件,我是在views文件夹里建了一个Two.vue
<template>
<div class="two">
<div id="main" style="width: 600px;height:400px;"></div>
</div>
</template>
<script>
import * as echarts from 'echarts';
export default {
name: 'two',
data () {
return {
charts: '',
opinion:['直接访问','邮件营销','联盟广告','视频广告','搜索引擎'],
opinionData:[
{value:335, name:'直接访问'},
{value:310, name:'邮件营销'},
{value:234, name:'联盟广告'},
{value:135, name:'视频广告'},
{value:1548, name:'搜索引擎'}
]
}
},
methods:{
drawPie(id){
this.charts = echarts.init(document.getElementById(id))
this.charts.setOption({
tooltip: {
trigger: 'item',
formatter: '{a}<br/>{b}:{c} ({d}%)'
},
legend: {
orient: 'vertical',
x: 'left',
data:this.opinion
},
series: [
{
name:'访问来源',
type:'pie',
radius:['50%','70%'],
avoidLabelOverlap: false,
label: {
normal: {
show: false,
position: 'center'
},
emphasis: {
show: true,
textStyle: {
fontSize: '30',
fontWeight: 'blod'
}
}
},
labelLine: {
normal: {
show: false
}
},
data:this.opinionData
}
]
})
}
},
//调用
mounted(){
this.$nextTick(function() {
this.drawPie('main')
})
}
}
</script>
<style scoped>
* {
margin: 0;
padding: 0;
list-style: none;
}
</style>
4.在router文件下的index.js中写two.vue的路径
,{
path: '/two',
name: 'Two',
// route level code-splitting
// this generates a separate chunk (about.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () => import(/* webpackChunkName: "about" */ '../views/Two.vue')
}
5.在app.vue中 写那个什么来着---(忘了怎么称呼)
<router-link to="/two">Two</router-link>
然后运行npm run serve/dev就行
它的搭建基础我没有写就是vue+router+iview没写
参考文章:https://segmentfault.com/a/1190000011275318
我在运行成功之前报了特别多的错,不知道你们用时,会不会也这样,反正最后成功了