Skip to content

Commit

Permalink
fix(charts): fix useCharts resize not work
Browse files Browse the repository at this point in the history
  • Loading branch information
anncwb committed Nov 4, 2020
1 parent 6936adb commit 6d9585b
Show file tree
Hide file tree
Showing 9 changed files with 289 additions and 279 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
- 修复分割菜单且左侧菜单没有数据时候,继续展示上一次子菜单的问题
- 修复`useMessage`类型问题
- 修复表单项设置`disabled`不生效问题
- 修复`useECharts``resize`时不能自适应,报错
- 修复`useWatermark`在清空后`resize`未删除

## 2.0.0-rc.8 (2020-11-2)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"dependencies": {
"@iconify/iconify": "^2.0.0-rc.1",
"ant-design-vue": "^2.0.0-beta.13",
"apexcharts": "^3.22.1",
"apexcharts": "3.22.0",
"axios": "^0.21.0",
"echarts": "^4.9.0",
"lodash-es": "^4.17.15",
Expand Down
3 changes: 2 additions & 1 deletion src/components/Loading/FullLoading.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
20 changes: 6 additions & 14 deletions src/hooks/web/useApexCharts.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,31 @@
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);
});
}

tryOnUnmounted(() => {
let chartInstance = unref(chartInstanceRef);
if (!chartInstance) {
return;
}

if (!chartInstance) return;
chartInstance.destroy();
chartInstanceRef.value = null;
chartInstance = null;
});

return {
setOptions,
};
Expand Down
23 changes: 8 additions & 15 deletions src/hooks/web/useECharts.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 = () => {};

Expand All @@ -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',
Expand All @@ -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();

Expand All @@ -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,
};
}
2 changes: 1 addition & 1 deletion src/hooks/web/useWatermark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
96 changes: 96 additions & 0 deletions src/views/dashboard/welcome/DemoChart.vue
Original file line number Diff line number Diff line change
@@ -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>
1 change: 0 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ const viteConfig: UserConfig = {
// The package will be recompiled using rollup, and the new package compiled into the esm module specification will be put into node_modules/.vite_opt_cache
optimizeDeps: {
include: [
'echarts',
'echarts/map/js/china',
'ant-design-vue/es/locale/zh_CN',
'@ant-design/icons-vue',
Expand Down
Loading

0 comments on commit 6d9585b

Please sign in to comment.