vue中echarts封装组件
echart封装组件echart封装组件echart组件代码调用组件页面调用echart封装组件最近有个需求页面,有很多echart图表一个一个写太麻烦,自己琢磨+借鉴大佬的代码封装了一个echart组件代码<template><div :id=id :data=data></div></template><script&...
·
echarts封装组件
最近有个需求页面,有很多echart图表一个一个写太麻烦,自己琢磨+借鉴大佬的代码封装了一个
安装echarts
npm install echarts -S
man.js中引入
import echarts from 'echarts'
Vue.prototype.$echarts = echarts
echarts组件代码
<template>
<div :id=id :data=data></div>
</template>
<script>
export default {
props: ['id', 'data'],
data () {
return {
ChartLineGraph: null
}
},
watch: {
data: {
handler (newValue, oldValue) {
this.drawLineGraph(this.id, newValue)
},
deep: true
}
},
mounted () {
this.drawLineGraph(this.id, this.data)
},
methods: {
drawLineGraph (id, data) {
// eslint-disable-next-line no-unused-vars
let _this = this
let muChart = document.getElementById(id)
this.ChartLineGraph = this.$echarts.init(muChart)
this.ChartLineGraph.setOption(data)
window.addEventListener('resize', function () {
_this.ChartLineGraph.resize()
})
}
},
beforeDestroy () {
if (this.ChartLineGraph) {
this.ChartLineGraph.clear()
}
},
components: {
}
}
</script>
<style scoped>
</style>
调用组件
import linegraph from '../components/methods'
data () {
return{
// 这里就是你传的echart数据
option: {
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [{
data: [820, 932, 901, 934, 1290, 1330, 1320],
type: 'line'
}],
tooltip: {}
}
}
}
components: {
linegraph
}
页面调用
<linegraph :id="'ss'" :data='option' style="width:500px;height:300px"</linegraph>
更多推荐
已为社区贡献1条内容
所有评论(0)