项目中经常会用到echarts图标,用来对数据进行展示,实现动态化,实时更新等特点。一般比如在首页的话echarts图标太多,会让页面代码看的比较臃肿,封装组件显得比较重要了。下面是我封装的一个简单echarts组件,在项目里面直接引入,注册即可使用:
<template>
<div ref="chart" style="width: 100%; height: 400px;"></div>
</template>
<script>
// import echarts from 'echarts'
import * as echarts from 'echarts'
export default {
name: 'MyChart',
mounted() {
this.initChart()
},
methods: {
initChart() {
const chart = echarts.init(this.$refs.chart)
chart.setOption({
title: {
text: 'ECharts 示例'
},
tooltip: {},
legend: {
data: ['销量']
},
xAxis: {
data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
},
yAxis: {},
series: [{
name: '销量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
})
}
}
}
</script>
只需要把里面的配置项改变一下就OK了。