在Vue中使用echarts的实例代码(3种图)

2020-06-13 10:44:09易采站长站整理

<div class="bottom" id="echart" ref="mychart">

</div>
</div>
</template>

这是组件的html部分,可以看见top以及des是我自己添加的,bottom才是核心,也是整个echarts展示的部分,注意这里添加了ref,在script的代码中,我们将通过这个钩子,将DOM挂载到echarts中


<script>
// echarts相关
let echarts = require('echarts/lib/echarts');
require('echarts/lib/chart/bar');
require('echarts/lib/component/tooltip');
require('echarts/lib/component/toolbox');
require('echarts/lib/component/legend');
require('echarts/lib/component/markLine');

export default {
name: 'r-echarts-line',
data () {
return {
typePick: '数值',
typeList: [
{
name: '数值'
},
{
name: '百分比'
}
],
pagePick: 0,
// myChart实例
myChart: {},
percent: {
label: {
normal: {
show: true,
position: 'inside',
formatter: '{c}%'
}
}
},
numeric: {
label: {
normal: {
show: true,
position: 'inside',
formatter: '{c}'
}
}
}
}
},
props: {
index: {
required: true,
type: Number
},
data: {
required: true,
type: Object
}
},
mounted () {
this.setEchart();
},
updated () {
if (!this.myChart) {
this.setEchart();
}
this.chartChange();
},
computed: {
origin () {
return this.data;
},
opt() {
let that = this;
let obj = {
color: ['#606c94'],
tooltip: {
},
toolbox: {
show: true,
feature: {
saveAsImage: {show: true}
}
},
label: {
normal: {
show: true,
position: 'inside',
formatter: '{c}'
},
emphasis: {
show: true
}
},
xAxis: {
type: 'value',
},
yAxis: {
data: that.origin[that.type][that.pagePick].key,
axisLabel: {
interval: 0,
rotate: -30
}
},
series: [{
name: that.origin.title,
type: 'bar',
data: that.origin[that.type][that.pagePick].val,
barMaxWidth: '30',
markLine: {
data: [
{type: 'average', name: '平均值'}
] }
}] }
return obj;
},
type () {
if (this.typePick == '数值') {
return 'numeric';
} else if (this.typePick == '百分比') {
return 'percent';
} else {
return 'numeric';
}
}
},
methods: {
setEchart () {