2021-11-17 19:20:07 +08:00
|
|
|
import { ref, Ref, onUnmounted, onMounted } from 'vue';
|
2021-11-16 19:01:03 +08:00
|
|
|
import * as echarts from 'echarts/core';
|
|
|
|
|
2021-11-17 19:20:07 +08:00
|
|
|
/**
|
|
|
|
* eChart hook
|
2021-11-26 10:45:04 +08:00
|
|
|
* @param domId
|
|
|
|
* @param chart
|
2021-11-17 19:20:07 +08:00
|
|
|
*/
|
2021-11-26 10:45:04 +08:00
|
|
|
export const useChart = (domId: string): Ref<echarts.ECharts> => {
|
2021-11-16 19:01:03 +08:00
|
|
|
let chartContainer: HTMLCanvasElement;
|
2021-11-26 10:45:04 +08:00
|
|
|
const selfChart = ref<echarts.ECharts | any>();
|
2021-11-16 19:01:03 +08:00
|
|
|
const updateContainer = () => {
|
|
|
|
selfChart.value.resize({
|
|
|
|
width: chartContainer.clientWidth,
|
|
|
|
height: chartContainer.clientHeight,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
if (!chartContainer) {
|
|
|
|
chartContainer = document.getElementById(domId) as HTMLCanvasElement;
|
|
|
|
}
|
|
|
|
selfChart.value = echarts.init(chartContainer);
|
|
|
|
});
|
|
|
|
|
|
|
|
window.addEventListener('resize', updateContainer, false);
|
|
|
|
|
|
|
|
onUnmounted(() => {
|
|
|
|
window.removeEventListener('resize', updateContainer);
|
|
|
|
});
|
2021-11-17 19:20:07 +08:00
|
|
|
|
2021-11-26 10:45:04 +08:00
|
|
|
return selfChart;
|
2021-11-17 19:20:07 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* counter utils
|
2021-11-26 10:45:04 +08:00
|
|
|
* @param duration
|
|
|
|
* @returns
|
2021-11-17 19:20:07 +08:00
|
|
|
*/
|
2021-11-26 10:45:04 +08:00
|
|
|
export const useCounter = (duration = 60): [Ref<number>, () => void] => {
|
2021-12-06 22:53:37 +08:00
|
|
|
let intervalTimer;
|
2021-11-27 18:50:07 +08:00
|
|
|
onUnmounted(() => {
|
2021-11-26 10:45:04 +08:00
|
|
|
clearInterval(intervalTimer);
|
|
|
|
});
|
2021-11-17 19:20:07 +08:00
|
|
|
const countDown = ref(0);
|
|
|
|
|
2021-11-26 10:45:04 +08:00
|
|
|
return [
|
|
|
|
countDown,
|
|
|
|
() => {
|
|
|
|
countDown.value = duration;
|
|
|
|
intervalTimer = setInterval(() => {
|
|
|
|
if (countDown.value > 0) {
|
|
|
|
countDown.value -= 1;
|
|
|
|
} else {
|
|
|
|
clearInterval(intervalTimer);
|
|
|
|
countDown.value = 0;
|
|
|
|
}
|
|
|
|
}, 1000);
|
|
|
|
},
|
|
|
|
];
|
|
|
|
};
|