主要思路
- 定义一个echarts图标,数据为空,image采用base64编码
- 图标宽高根据父宽高自适应
- 渲染echarts函数,切换清除图例
- 定义暂无数据指令
定义option
function emptyChartOptionFn(width = 150, height = 120) {
return {
title: {
text: `{a|}\n{b|暂无数据}`,
x: 'center',
y: 'center',
itemGap: 0,
textStyle: {
rich: {
a: {
height,
width,
backgroundColor: {
image:
'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTU1IiBoZWlnaHQ9IjEyMCIgdmlld0JveD0iMCAwIDE1NSAxMjAiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CjxnIG9wYWNpdHk9IjAuNiI+CjxyZWN0IHg9IjgyLjU0MyIgeT0iMTIiIHdpZHRoPSI3MCIgaGVpZ2h0PSI5MCIgcng9IjYiIHRyYW5zZm9ybT0icm90YXRlKDYuMDg2NjYgODIuNTQzIDEyKSIgZmlsbD0iI0Y1RjZGQSIgc3Ryb2tlPSIjOEY5QUIyIi8+CjxyZWN0IG9wYWNpdHk9IjAuMiIgeD0iOTMuMzQ5IiB5PSIzMy4yNjU2IiB3aWR0aD0iMTMiIGhlaWdodD0iMiIgcng9IjEiIHRyYW5zZm9ybT0icm90YXRlKDYuMDg2NjYgOTMuMzQ5IDMzLjI2NTYpIiBmaWxsPSIjOEY5QUIyIi8+CjxyZWN0IHg9IjExMC4yNTMiIHk9IjM1LjA2ODQiIHdpZHRoPSIyNyIgaGVpZ2h0PSIyIiByeD0iMSIgdHJhbnNmb3JtPSJyb3RhdGUoNi4wODY2NiAxMTAuMjUzIDM1LjA2ODQpIiBmaWxsPSIjOEY5QUIyIi8+CjxyZWN0IHg9IjkxLjg2NDYiIHk9IjQ3LjE4NjUiIHdpZHRoPSIzMiIgaGVpZ2h0PSIyIiByeD0iMSIgdHJhbnNmb3JtPSJyb3RhdGUoNi4wODY2NiA5MS44NjQ2IDQ3LjE4NjUpIiBmaWxsPSIjOEY5QUIyIi8+CjxyZ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yPSJlZmZlY3QxX2Ryb3BTaGFkb3dfMjA0MF82NjU5OCIgcmVzdWx0PSJzaGFwZSIvPgo8L2ZpbHRlcj4KPC9kZWZzPgo8L3N2Zz4K',
},
},
b: {
verticalAlign: 'top',
fontSize: 12,
lineHeight: 18,
color: '#686E7C',
},
},
},
subtextStyle: {
fontSize: 12,
},
},
}
}
自适配宽高
const getEmptyWH = el => {
let width = 150
let height = 120
if (el.clientWidth < 150) {
let radio = 120 / 150
width = el.clientWidth
height = width * radio
}
if (el.clientHeight < 120) {
let radio = 150 / 120
height = el.clientHeight - 20
width = height * radio
}
return {
width,
height,
}
}
渲染echarts图标至el
const emptyChartFn = el => {
let myChart = Vue.prototype.$echarts.init(el, 'light', {
height: 'auto',
width: 'auto',
})
const { width, height } = getEmptyWH(el)
myChart.setOption(optionFn(width, height))
el.myChart = myChart
el.showLoading = myChart.showLoading
el.hideLoading = myChart.hideLoading
el.debounceResize = debounce(() => {
const { width, height } = getEmptyWH(el)
el.myChart.clear()
el.myChart.setOption(optionFn(width, height))
el.myChart.resize({
animation: {
duration: 200,
},
})
}, 500)
const observer = new ResizeObserver(el.debounceResize)
observer.observe(el)
el._observer = observer
}
定义暂无数据指令
const emptyChart = {
inserted(el, binding, vnode) {
const isEmpty = binding.value
if (!isEmpty) {
return
}
Vue.nextTick(() => {
emptyChartFn(el)
})
},
update(el, binding, vnode) {
const isEmpty = binding.value
if (isEmpty) {
let myChart = el?.myChart
if (!myChart) {
emptyChartFn(el)
} else {
myChart.clear()
emptyChartFn(el)
}
}
},
unbind(el, binding, vnode) {
const observer = el._observer
if (observer) {
observer.unobserve(el)
delete el._observer
}
},
}
注册指令
Vue.directive('emptyChart', emptyChart)
使用
<div v-empty-chart="isEmpty" class="h-full pt-6"></div>
效果