<template>
<div style="width:100%;" v-resize="bodyResize">
<div ref='echartsSankeyRef' style="width:100%;height:500px"></div>
</div>
</template>
<script>
import echarts from 'echarts'
import { reactive, ref, toRefs, computed, watch, onMounted } from '@vue/composition-api'
let option = {
tooltip: {
trigger: 'item',
triggerOn: 'mousemove'
},
series: {
type: 'sankey',
layout: 'none',
orient: 'horizontal',
left: 0,
bottom: 0,
right: 0,
emphasis: {
focus: 'adjacency'
},
label: {
show: true,
position: 'top'
},
lineStyle: {
color: 'red',
curveness: 0.5
},
data: [
{
name: 'a',
itemStyle: {
color: 'rgba(201, 78, 78, 1)'
}
},
{
name: 'b'
},
{
name: 'a1'
},
{
name: 'a2'
},
{
name: 'b1'
},
{
name: 'c'
}
],
links: [
{
source: 'a',
target: 'a1',
value: 5,
lineStyle: {
color: '#000000',
curveness: 0.5
}
},
{
source: 'a',
target: 'a2',
value: 3
},
{
source: 'b',
target: 'b1',
value: 8
},
{
source: 'a',
target: 'b1',
value: 3
},
{
source: 'b1',
target: 'a1',
value: 1
},
{
source: 'b1',
target: 'c',
value: 2
}
]
}
}
export default {
directives:{
resize:{
inserted: (el, binding, vNode) => {
const callback = binding.value
const debounce = binding.arg || 200
const options = binding.modifiers || { passive: true }
let debounceTimeout = null
const onResize = () => {
clearTimeout(debounceTimeout)
debounceTimeout = setTimeout(callback, debounce, options)
}
window.addEventListener('customResize', onResize, options)
window.addEventListener('resize', onResize, options)
el._onResize = {
onResize,
options
}
},
unbind (el, binding) {
const { onResize, options } = el._onResize;
window.removeEventListener('customResize', onResize, options)
window.removeEventListener('resize', onResize, options)
delete el._onResize
}
}
},
setup() {
let echartsSankeyRef = ref(null)
let echartsSankey = ref(null)
onMounted(() => {
console.log(echartsSankeyRef)
echartsSankey.value = echarts.init(echartsSankeyRef.value)
echartsSankey.value.setOption(option, true)
})
const bodyResize =()=> {
if (echartsSankey.value) {
echartsSankey.value.resize()
}
}
return {
echartsSankeyRef,
bodyResize
}
}
}
</script>