plugin-component
分三步 1.书写 2.注册 3.使用
1. 在util文件夹下创建一个util.components.js
const components = {}
var echarts = require('echarts')
components.echartComponent = {
install: (Vue, options) => {
Vue.component('echarts', {
props: {
width: {
type: Number,
default: -1
},
height: {
type: Number,
default: -1
},
animation: {
type: Number,
default: 0
},
options: {
type: Object,
default: () => ({})
},
config: {
type: Object,
default: () => ({})
},
},
data() {
return {
echarts_timer: null,
echarts_index: 0
}
},
render(createElement) {
return createElement('div', {
attrs: {
id: this.randomId
},
style: this.canvasStyle,
directives: [
{
name: 'echarts'
}
]
})
},
mounted() {
this.draw()
this.methodShowLoading()
this.$watch('options', options => {
this.draw()
this.echartsContext.hideLoading()
})
},
beforeDestroy() {
clearInterval(this.echarts_timer)
this.echarts_timer = null
},
computed: {
randomId() {
return (
'echarts-' + Math.floor(Math.random() * 1000) + String(new Date().getTime()).slice(-2)
)
},
canvasStyle() {
return {
height: this.height === -1 ? '100%' : this.height + 'px',
width: this.width === -1 ? '100%' : this.width + 'px'
}
}
},
methods: {
recieveEchartsContext(context) {
this.echartsContext = context
},
draw() {
this.methodChartClear()
const options = this.options
this.echartsContext.setOption(options)
this.echartsContext.resize()
this.$nextTick(() => {
this.animation && this.methodAnimation()
this.config.handle && this.methodChartOnHandle(this.config.handle.event, this.config.handle.fn)
})
window.addEventListener('resize', () => {
this.echartsContext.resize()
})
},
methodShowLoading() {
this.echartsContext.showLoading({
text: '图表数据正在努力加载...',
x: 'center',
y: 'center',
textStyle: {
color: 'red',
fontSize: 14
},
effect: 'spin'
})
},
methodChartOnHandle(event, fn) {
this.echartsContext.on(event, fn)
},
methodChartUnHandle(event, fn) {
this.echartsContext.on(event, fn)
},
methodChartImgUrl() {
return this.echartsContext.getDataURL('png')
},
methodChartImgObj() {
return this.echartsContext.getImage('jpeg')
},
methodChartRefresh() {
this.echartsContext.refresh()
},
methodChartRestore() {
this.echartsContext.restore()
},
methodChartClear() {
this.echartsContext.clear()
},
methodChartDispose() {
this.echartsContext.dispose()
},
methodAnimation() {
if (this.echarts_timer) clearInterval(this.echarts_timer)
this.echarts_timer = setInterval(() => {
this.echartsContext.dispatchAction({
type: 'hideTip',
seriesIndex: 0,
dataIndex: this.echarts_index
})
this.echartsContext.dispatchAction({
type: 'showTip',
seriesIndex: 0,
dataIndex: this.echarts_index
})
this.echartsContext.dispatchAction({
type: 'downplay',
seriesIndex: 0,
dataIndex: this.echarts_index == 0 ? this.animation - 1 : this.echarts_index - 1
})
this.echartsContext.dispatchAction({
type: 'highlight',
seriesIndex: 0,
dataIndex: this.echarts_index
})
this.echarts_index++
if (this.echarts_index > this.animation - 1) {
this.echarts_index = 0
}
}, 2000)
}
}
})
Vue.directive('echarts', {
inserted: (el, binding, vnode) => {
const charts = echarts.init(el)
vnode.context.recieveEchartsContext && vnode.context.recieveEchartsContext(charts)
}
})
}
}
export default components
2. 在入口的文件中(比如main.js) 引入 util.components.js 文件
import {echartComponent} from '@/util/util.components.js'
Vue.use(echartComponent)
3. 在需要使用的页面调用
<echarts
:config="echartsConfig"
ref="echartsRef"
:options="echartsOptions"
:animation="echartsAnimation"
></echarts>
{
echartsConfig: {
handle: {
event: 'click',
fn: data => {
console.log('data', data)
}
}
}
echartsOptions {
}
echartsAnimation:number
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏