mirror of
https://github.com/vbenjs/vue-vben-admin.git
synced 2025-08-27 15:41:32 +08:00
fix(charts): fix useCharts resize not work
This commit is contained in:
@@ -42,7 +42,8 @@
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
// background: rgba(255, 255, 255, 0.3);
|
||||
background: rgba(241, 241, 246, 0.7);
|
||||
// background: #f0f2f5;
|
||||
background: rgba(240, 242, 245, 0.5);
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
}
|
||||
|
@@ -1,23 +1,19 @@
|
||||
import { useTimeout } from '/@/hooks/core/useTimeout';
|
||||
import { tryOnUnmounted } from '/@/utils/helper/vueHelper';
|
||||
import { ref, unref, Ref, nextTick } from 'vue';
|
||||
import { unref, Ref, nextTick } from 'vue';
|
||||
|
||||
import ApexCharts from 'apexcharts';
|
||||
|
||||
export function useApexCharts(elRef: Ref<HTMLDivElement>) {
|
||||
const chartInstanceRef = ref<Nullable<ApexCharts>>(null);
|
||||
let chartInstance: Nullable<ApexCharts> = null;
|
||||
|
||||
function setOptions(options: any) {
|
||||
nextTick(() => {
|
||||
useTimeout(() => {
|
||||
const el = unref(elRef);
|
||||
|
||||
if (!el || !unref(el)) {
|
||||
return;
|
||||
}
|
||||
chartInstanceRef.value = new ApexCharts(el, options);
|
||||
|
||||
const chartInstance = unref(chartInstanceRef);
|
||||
if (!el || !unref(el)) return;
|
||||
chartInstance = new ApexCharts(el, options);
|
||||
|
||||
chartInstance && chartInstance.render();
|
||||
}, 30);
|
||||
@@ -25,15 +21,11 @@ export function useApexCharts(elRef: Ref<HTMLDivElement>) {
|
||||
}
|
||||
|
||||
tryOnUnmounted(() => {
|
||||
let chartInstance = unref(chartInstanceRef);
|
||||
if (!chartInstance) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!chartInstance) return;
|
||||
chartInstance.destroy();
|
||||
chartInstanceRef.value = null;
|
||||
chartInstance = null;
|
||||
});
|
||||
|
||||
return {
|
||||
setOptions,
|
||||
};
|
||||
|
@@ -1,6 +1,6 @@
|
||||
import { useTimeout } from '/@/hooks/core/useTimeout';
|
||||
import { tryOnUnmounted } from '/@/utils/helper/vueHelper';
|
||||
import { ref, unref, Ref, nextTick } from 'vue';
|
||||
import { unref, Ref, nextTick } from 'vue';
|
||||
import type { EChartOption, ECharts } from 'echarts';
|
||||
import echarts from 'echarts';
|
||||
import { useDebounce } from '/@/hooks/core/useDebounce';
|
||||
@@ -12,7 +12,7 @@ export function useECharts(
|
||||
elRef: Ref<HTMLDivElement>,
|
||||
theme: 'light' | 'dark' | 'default' = 'light'
|
||||
) {
|
||||
const chartInstanceRef = ref<Nullable<ECharts>>(null);
|
||||
let chartInstance: Nullable<ECharts> = null;
|
||||
let resizeFn: Fn = resize;
|
||||
let removeResizeFn: Fn = () => {};
|
||||
|
||||
@@ -25,7 +25,7 @@ export function useECharts(
|
||||
if (!el || !unref(el)) {
|
||||
return;
|
||||
}
|
||||
chartInstanceRef.value = echarts.init(el, theme);
|
||||
chartInstance = echarts.init(el, theme);
|
||||
const { removeEvent } = useEvent({
|
||||
el: window,
|
||||
name: 'resize',
|
||||
@@ -39,21 +39,14 @@ export function useECharts(
|
||||
}, 30);
|
||||
}
|
||||
}
|
||||
tryOnUnmounted(() => {
|
||||
removeResizeFn();
|
||||
});
|
||||
|
||||
function setOptions(options: any, clear = true) {
|
||||
nextTick(() => {
|
||||
useTimeout(() => {
|
||||
let chartInstance = unref(chartInstanceRef);
|
||||
|
||||
if (!chartInstance) {
|
||||
init();
|
||||
chartInstance = chartInstance = unref(chartInstanceRef);
|
||||
if (!chartInstance) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!chartInstance) return;
|
||||
}
|
||||
clear && chartInstance.clear();
|
||||
|
||||
@@ -63,20 +56,20 @@ export function useECharts(
|
||||
}
|
||||
|
||||
function resize() {
|
||||
const chartInstance = unref(chartInstanceRef);
|
||||
if (!chartInstance) return;
|
||||
chartInstance.resize();
|
||||
}
|
||||
|
||||
tryOnUnmounted(() => {
|
||||
const chartInstance = unref(chartInstanceRef);
|
||||
if (!chartInstance) return;
|
||||
removeResizeFn();
|
||||
chartInstance.dispose();
|
||||
chartInstanceRef.value = null;
|
||||
chartInstance = null;
|
||||
});
|
||||
|
||||
return {
|
||||
setOptions,
|
||||
echarts,
|
||||
resize,
|
||||
};
|
||||
}
|
||||
|
@@ -11,7 +11,7 @@ export function useWatermark(appendEl: Ref<HTMLElement | null> = ref(document.bo
|
||||
const el = unref(appendEl);
|
||||
el && el.removeChild(domId);
|
||||
}
|
||||
window.addEventListener('resize', func);
|
||||
window.removeEventListener('resize', func);
|
||||
};
|
||||
const createWatermark = (str: string) => {
|
||||
clear();
|
||||
|
96
src/views/dashboard/welcome/DemoChart.vue
Normal file
96
src/views/dashboard/welcome/DemoChart.vue
Normal file
@@ -0,0 +1,96 @@
|
||||
<template>
|
||||
<div class="container">
|
||||
<div id="main"></div>
|
||||
<div id="main1" ref="elRef"></div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script lang="ts">
|
||||
// https://vega.github.io/vega/usage/
|
||||
import { defineComponent, onMounted, ref, unref } from 'vue';
|
||||
import { useECharts } from '/@/hooks/web/useECharts';
|
||||
import echarts from 'echarts';
|
||||
|
||||
export default defineComponent({
|
||||
name: 'DemoChart',
|
||||
setup() {
|
||||
const elRef = ref<any>(null);
|
||||
const { setOptions } = useECharts(elRef);
|
||||
|
||||
// onMounted(() => {
|
||||
// const el = unref(elRef);
|
||||
// if (!el || !unref(el)) return;
|
||||
// const chart = echarts.init(el);
|
||||
|
||||
// window.addEventListener('resize', () => {
|
||||
// chart!.resize();
|
||||
// });
|
||||
// // removeResizeFn = removeEvent;
|
||||
// var option = {
|
||||
// title: {
|
||||
// text: 'ECharts entry example',
|
||||
// },
|
||||
// tooltip: {},
|
||||
// legend: {
|
||||
// data: ['Sales'],
|
||||
// },
|
||||
// xAxis: {
|
||||
// data: ['shirt', 'cardign', 'chiffon shirt', 'pants', 'heels', 'socks'],
|
||||
// },
|
||||
// yAxis: {},
|
||||
// series: [
|
||||
// {
|
||||
// name: 'Sales',
|
||||
// type: 'bar',
|
||||
// data: [5, 20, 36, 10, 10, 20],
|
||||
// },
|
||||
// ],
|
||||
// };
|
||||
// chart && chart.setOption(option as any);
|
||||
// });
|
||||
onMounted(() => {
|
||||
var myChart = echarts.init(elRef.value);
|
||||
// specify chart configuration item and data
|
||||
var option = {
|
||||
title: {
|
||||
text: 'ECharts entry example',
|
||||
},
|
||||
tooltip: {},
|
||||
legend: {
|
||||
data: ['Sales'],
|
||||
},
|
||||
xAxis: {
|
||||
data: ['shirt', 'cardign', 'chiffon shirt', 'pants', 'heels', 'socks'],
|
||||
},
|
||||
yAxis: {},
|
||||
series: [
|
||||
{
|
||||
name: 'Sales',
|
||||
type: 'bar',
|
||||
data: [5, 20, 36, 10, 10, 20],
|
||||
},
|
||||
],
|
||||
};
|
||||
setOptions(option);
|
||||
// use configuration item and data specified to show chart
|
||||
// myChart.setOption(option);
|
||||
// window.addEventListener('resize', () => {
|
||||
// myChart.resize();
|
||||
// });
|
||||
});
|
||||
|
||||
return { elRef };
|
||||
},
|
||||
});
|
||||
</script>
|
||||
<style>
|
||||
.container {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
#main,
|
||||
#main1 {
|
||||
width: 40%;
|
||||
height: 300px;
|
||||
}
|
||||
</style>
|
Reference in New Issue
Block a user