refactor: ts+echarts+resize方式简化

Former-commit-id: 0a85c26f3c27843cd4e20b367e62d1f9b40370d9
This commit is contained in:
haoxr
2023-01-25 18:53:14 +08:00
parent 235fea1070
commit 8c5537c49e
6 changed files with 283 additions and 456 deletions

View File

@@ -1,73 +0,0 @@
import { ref } from 'vue';
export default function () {
const chart = ref<any>();
const sidebarElm = ref<Element>();
const chartResizeHandler = () => {
if (chart.value) {
chart.value.resize();
}
};
const sidebarResizeHandler = (e: TransitionEvent) => {
if (e.propertyName === 'width') {
chartResizeHandler();
}
};
const initResizeEvent = () => {
window.addEventListener('resize', chartResizeHandler, {passive:true});
};
const destroyResizeEvent = () => {
window.removeEventListener('resize', chartResizeHandler);
};
const initSidebarResizeEvent = () => {
sidebarElm.value = document.getElementsByClassName('sidebar-container')[0];
if (sidebarElm.value) {
sidebarElm.value.addEventListener(
'transitionend',
sidebarResizeHandler as EventListener,
{passive:true}
);
}
};
const destroySidebarResizeEvent = () => {
if (sidebarElm.value) {
sidebarElm.value.removeEventListener(
'transitionend',
sidebarResizeHandler as EventListener
);
}
};
const mounted = () => {
initResizeEvent();
initSidebarResizeEvent();
};
const beforeDestroy = () => {
destroyResizeEvent();
destroySidebarResizeEvent();
};
const activated = () => {
initResizeEvent();
initSidebarResizeEvent();
};
const deactivated = () => {
destroyResizeEvent();
destroySidebarResizeEvent();
};
return {
chart,
mounted,
beforeDestroy,
activated,
deactivated
};
}

View File

@@ -7,16 +7,7 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import {
nextTick,
onActivated,
onBeforeUnmount,
onDeactivated,
onMounted
} from 'vue';
import { init, EChartsOption } from 'echarts';
import * as echarts from 'echarts'; import * as echarts from 'echarts';
import resize from '@/utils/resize';
const props = defineProps({ const props = defineProps({
id: { id: {
@@ -39,12 +30,7 @@ const props = defineProps({
} }
}); });
const { mounted, chart, beforeDestroy, activated, deactivated } = resize(); const options = {
function initChart() {
const barChart = init(document.getElementById(props.id) as HTMLDivElement);
barChart.setOption({
grid: { grid: {
left: '2%', left: '2%',
right: '2%', right: '2%',
@@ -143,26 +129,16 @@ function initChart() {
} }
} }
] ]
} as EChartsOption); };
chart.value = barChart;
}
onBeforeUnmount(() => {
beforeDestroy();
});
onActivated(() => {
activated();
});
onDeactivated(() => {
deactivated();
});
onMounted(() => { onMounted(() => {
mounted(); const chart = echarts.init(
nextTick(() => { document.getElementById(props.id) as HTMLDivElement
initChart(); );
chart.setOption(options);
window.addEventListener('resize', () => {
chart.resize();
}); });
}); });
</script> </script>

View File

@@ -4,15 +4,7 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { import * as echarts from 'echarts';
nextTick,
onActivated,
onBeforeUnmount,
onDeactivated,
onMounted
} from 'vue';
import { init, EChartsOption } from 'echarts';
import resize from '@/utils/resize';
const props = defineProps({ const props = defineProps({
id: { id: {
@@ -35,12 +27,7 @@ const props = defineProps({
} }
}); });
const { mounted, chart, beforeDestroy, activated, deactivated } = resize(); const options = {
function initChart() {
const funnelChart = init(document.getElementById(props.id) as HTMLDivElement);
funnelChart.setOption({
title: { title: {
show: true, show: true,
text: '订单线索转化漏斗图', text: '订单线索转化漏斗图',
@@ -104,28 +91,16 @@ function initChart() {
] ]
} }
] ]
} as EChartsOption); };
chart.value = funnelChart;
}
onBeforeUnmount(() => {
beforeDestroy();
});
onActivated(() => {
activated();
});
onDeactivated(() => {
deactivated();
});
onMounted(() => { onMounted(() => {
mounted(); const chart = echarts.init(
nextTick(() => { document.getElementById(props.id) as HTMLDivElement
initChart(); );
chart.setOption(options);
window.addEventListener('resize', () => {
chart.resize();
}); });
}); });
</script> </script>
<style lang="scss" scoped></style>

View File

@@ -7,15 +7,7 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { import * as echarts from 'echarts';
nextTick,
onActivated,
onBeforeUnmount,
onDeactivated,
onMounted
} from 'vue';
import { init, EChartsOption } from 'echarts';
import resize from '@/utils/resize';
const props = defineProps({ const props = defineProps({
id: { id: {
@@ -37,13 +29,7 @@ const props = defineProps({
required: true required: true
} }
}); });
const options = {
const { mounted, chart, beforeDestroy, activated, deactivated } = resize();
function initChart() {
const pieChart = init(document.getElementById(props.id) as HTMLDivElement);
pieChart.setOption({
grid: { grid: {
left: '2%', left: '2%',
right: '2%', right: '2%',
@@ -79,29 +65,15 @@ function initChart() {
] ]
} }
] ]
} as EChartsOption); };
chart.value = pieChart;
}
onBeforeUnmount(() => {
beforeDestroy();
});
onActivated(() => {
activated();
});
onDeactivated(() => {
deactivated();
});
onMounted(() => { onMounted(() => {
mounted(); const chart = echarts.init(
nextTick(() => { document.getElementById(props.id) as HTMLDivElement
initChart(); );
chart.setOption(options);
window.addEventListener('resize', () => {
chart.resize();
}); });
}); });
</script> </script>
<style lang="scss" scoped></style>

View File

@@ -7,15 +7,7 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { import * as echarts from 'echarts';
nextTick,
onActivated,
onBeforeUnmount,
onDeactivated,
onMounted
} from 'vue';
import { init, EChartsOption } from 'echarts';
import resize from '@/utils/resize';
const props = defineProps({ const props = defineProps({
id: { id: {
@@ -38,12 +30,7 @@ const props = defineProps({
} }
}); });
const { mounted, chart, beforeDestroy, activated, deactivated } = resize(); const options = {
function initChart() {
const radarChart = init(document.getElementById(props.id) as HTMLDivElement);
radarChart.setOption({
grid: { grid: {
left: '2%', left: '2%',
right: '2%', right: '2%',
@@ -98,29 +85,16 @@ function initChart() {
] ]
} }
] ]
} as EChartsOption); };
chart.value = radarChart;
}
onBeforeUnmount(() => {
beforeDestroy();
});
onActivated(() => {
activated();
});
onDeactivated(() => {
deactivated();
});
onMounted(() => { onMounted(() => {
mounted(); const chart = echarts.init(
nextTick(() => { document.getElementById(props.id) as HTMLDivElement
initChart(); );
chart.setOption(options);
window.addEventListener('resize', () => {
chart.resize();
}); });
}); });
</script> </script>
<style lang="scss" scoped></style>

View File

@@ -20,7 +20,7 @@ const date: Date = new Date();
const greetings = computed(() => { const greetings = computed(() => {
if (date.getHours() >= 6 && date.getHours() < 8) { if (date.getHours() >= 6 && date.getHours() < 8) {
return '晨起披衣出草堂,轩窗已自喜微凉🌅!'; return '晨起披衣出草堂,轩窗已自喜微凉🌅!';
} else if (date.getHours() >= 8 && date.getHours() < 18) { } else if (date.getHours() >= 8 && date.getHours() < 12) {
return '上午好🌞!'; return '上午好🌞!';
} else if (date.getHours() >= 12 && date.getHours() < 18) { } else if (date.getHours() >= 12 && date.getHours() < 18) {
return '下午好☕!'; return '下午好☕!';
@@ -209,5 +209,8 @@ const greetings = computed(() => {
display: flex; display: flex;
justify-content: space-between; justify-content: space-between;
} }
.svg-icon {
fill: currentColor !important;
}
} }
</style> </style>