index.vue
1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<template>
<VCharts ref="myChartRef" :option="{ ...options, backgroundColor: 'rgba(128, 128, 128, 0)' }" :autoresize="autoResize"
:style="{ width, height }" :theme="theme" />
</template>
<script lang="ts" setup>
import { computed, nextTick, provide, ref } from 'vue'
import VCharts, { THEME_KEY } from 'vue-echarts'
import { useAppStore } from '@/store'
import {useEventListener} from "@vueuse/core";
defineProps({
options: {
type: Object,
default() {
return {}
},
},
autoResize: {
type: Boolean,
default: true,
},
width: {
type: String,
default: '100%',
},
height: {
type: String,
default: '100%',
},
})
// 支持深色模式
const appStore = useAppStore();
const theme = computed(() => {
if (appStore.theme === 'dark') return 'dark';
return '';
});
provide(THEME_KEY, theme)
const renderChart = ref(false)
const myChartRef = ref<any>(null);
/**
* echarts图表-保存图片
*/
const downloadEchartsImg = (name:string) => {
const $a = document.createElement('a');
$a.download = name + '.png';
$a.target = '_blank';
$a.href = myChartRef.value.getConnectedDataURL(myChartRef.value.getDom());
// Chrome and Firefox
if (typeof MouseEvent === 'function') {
const evt = new MouseEvent('click', {
view: window,
bubbles: true,
cancelable: false
});
$a.dispatchEvent(evt);
}
}
// wait container expand
nextTick(() => {
renderChart.value = true
})
defineExpose({
downloadEchartsImg
})
</script>
<style scoped lang="less">
</style>