提交 b4e493cc authored 作者: lidongxu's avatar lidongxu

fix(competitor): 竞品分析-tab 切换导致 ECharts 的大小不对

初始化的时候获取外层容器的宽度设置,高度固定
上级 c9a2a993
...@@ -179,12 +179,23 @@ watchEffect(() => { ...@@ -179,12 +179,23 @@ watchEffect(() => {
onMounted(() => { onMounted(() => {
nextTick(() => { nextTick(() => {
chart.value = echarts.init(echartsRef.value) chart.value = echarts.init(echartsRef.value, null, {
width: document.querySelector('.el-tabs__content').offsetWidth + 'px',
height: props.height
})
}) })
}) })
useWindowResize(() => {
chart.value.resize() const resize = () => {
}) chart.value.resize({
width: document.querySelector('.el-tabs__content').offsetWidth + 'px',
height: props.height
})
}
const injectMessage = inject('activeName')
watch(injectMessage, resize)
useWindowResize(resize)
onBeforeUnmount(() => { onBeforeUnmount(() => {
if (!chart.value) { if (!chart.value) {
......
...@@ -42,7 +42,7 @@ const setOptions = () => { ...@@ -42,7 +42,7 @@ const setOptions = () => {
// 面积图是否切换 // 面积图是否切换
const seriesResult = props.chartData.series.map(o => { const seriesResult = props.chartData.series.map(o => {
if (!isArea.value) { if (!isArea.value) {
const obj = {...o} const obj = { ...o }
delete obj.areaStyle delete obj.areaStyle
delete obj.lineStyle delete obj.lineStyle
return obj return obj
...@@ -93,14 +93,6 @@ const setOptions = () => { ...@@ -93,14 +93,6 @@ const setOptions = () => {
magicType: { magicType: {
type: ['stack', 'tiled'] // 切换图表类型 type: ['stack', 'tiled'] // 切换图表类型
}, },
myTool1: {
show: true,
title: '切换表格',
icon: 'path://M5,5 L5,15 L15,15 L15,5 Z M20,5 L20,15 L30,15 L30,5 Z M5,20 L5,30 L15,30 L15,20 Z M20,20 L20,30 L30,30 L30,20 Z',
onclick: () => {
emits('changeType', 'table')
}
},
myTool2: { myTool2: {
show: true, show: true,
title: '面积/折线图', title: '面积/折线图',
...@@ -108,6 +100,14 @@ const setOptions = () => { ...@@ -108,6 +100,14 @@ const setOptions = () => {
onclick: () => { onclick: () => {
isArea.value = !isArea.value isArea.value = !isArea.value
} }
},
myTool1: {
show: true,
title: '切换表格',
icon: 'path://M5,5 L5,15 L15,15 L15,5 Z M20,5 L20,15 L30,15 L30,5 Z M5,20 L5,30 L15,30 L15,20 Z M20,20 L20,30 L30,30 L30,20 Z',
onclick: () => {
emits('changeType', 'table')
}
} }
} }
}, },
...@@ -141,21 +141,28 @@ watchEffect(() => { ...@@ -141,21 +141,28 @@ watchEffect(() => {
setOptions() setOptions()
}) })
const initChart = () => {
chart.value = echarts.init(echartsRef.value)
}
const resize = () => {
chart.value.resize()
}
useWindowResize(resize)
onMounted(() => { onMounted(() => {
nextTick(() => { nextTick(() => {
initChart() chart.value = echarts.init(echartsRef.value, null, {
width: document.querySelector('.el-tabs__content').offsetWidth + 'px',
height: props.height
})
}) })
}) })
const resize = () => {
chart.value.resize({
width: document.querySelector('.el-tabs__content').offsetWidth + 'px',
height: props.height
})
}
const injectMessage = inject('activeName')
watch(injectMessage, resize)
useWindowResize(resize)
onBeforeUnmount(() => { onBeforeUnmount(() => {
if (!chart.value) { if (!chart.value) {
return return
......
...@@ -4,9 +4,10 @@ ...@@ -4,9 +4,10 @@
class="tabs"> class="tabs">
<el-tab-pane v-for="item in list" <el-tab-pane v-for="item in list"
:label="item.name" :label="item.name"
:name="item.name" :name="item.name">
lazy> <keep-alive>
<component :is="item.component"></component> <component :is="item.component"></component>
</keep-alive>
</el-tab-pane> </el-tab-pane>
</el-tabs> </el-tabs>
</div> </div>
...@@ -23,6 +24,7 @@ const list = ref([ ...@@ -23,6 +24,7 @@ const list = ref([
{ name: '生意参谋-竞品', component: shallowRef(sycmPrd) } { name: '生意参谋-竞品', component: shallowRef(sycmPrd) }
]) ])
const activeName = ref(list.value[0].name) const activeName = ref(list.value[0].name)
provide('activeName', activeName);
</script> </script>
<style scoped <style scoped
...@@ -34,6 +36,7 @@ const activeName = ref(list.value[0].name) ...@@ -34,6 +36,7 @@ const activeName = ref(list.value[0].name)
padding: 20px; padding: 20px;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
width: 100%;
::v-deep(.el-tabs__content) { ::v-deep(.el-tabs__content) {
flex: 1; flex: 1;
...@@ -51,12 +54,18 @@ const activeName = ref(list.value[0].name) ...@@ -51,12 +54,18 @@ const activeName = ref(list.value[0].name)
flex: 1; flex: 1;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
width: 100%;
.chart_wrap { .chart_wrap {
flex: 1; flex: 1;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
justify-content: space-around; justify-content: space-around;
width: 100%;
.chart{
flex: 1;
}
} }
} }
} }
......
import { debounce } from '@/utils'
export default {
data() {
return {
$_sidebarElm: null,
$_resizeHandler: null
}
},
mounted() {
this.initListener()
},
activated() {
if (!this.$_resizeHandler) {
// avoid duplication init
this.initListener()
}
// when keep-alive chart activated, auto resize
this.resize()
},
beforeDestroy() {
this.destroyListener()
},
deactivated() {
this.destroyListener()
},
methods: {
// use $_ for mixins properties
// https://vuejs.org/v2/style-guide/index.html#Private-property-names-essential
$_sidebarResizeHandler(e) {
if (e.propertyName === 'width') {
this.$_resizeHandler()
}
},
initListener() {
this.$_resizeHandler = debounce(() => {
this.resize()
}, 100)
window.addEventListener('resize', this.$_resizeHandler)
this.$_sidebarElm = document.getElementsByClassName('sidebar-container')[0]
this.$_sidebarElm && this.$_sidebarElm.addEventListener('transitionend', this.$_sidebarResizeHandler)
},
destroyListener() {
window.removeEventListener('resize', this.$_resizeHandler)
this.$_resizeHandler = null
this.$_sidebarElm && this.$_sidebarElm.removeEventListener('transitionend', this.$_sidebarResizeHandler)
},
resize() {
const { chart } = this
chart && chart.resize()
}
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论