-
Notifications
You must be signed in to change notification settings - Fork 701
Expand file tree
/
Copy pathChartComponent.vue
More file actions
132 lines (117 loc) · 2.58 KB
/
ChartComponent.vue
File metadata and controls
132 lines (117 loc) · 2.58 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<script setup lang="ts">
import { computed, nextTick, onMounted, onUnmounted, watch } from 'vue'
import { getChartInstance } from '@/views/chat/component/index.ts'
import type { BaseChart, ChartAxis, ChartData } from '@/views/chat/component/BaseChart.ts'
import { useEmitt } from '@/utils/useEmitt.ts'
const params = withDefaults(
defineProps<{
id: string | number
type: string
data?: Array<ChartData>
columns?: Array<ChartAxis>
x?: Array<ChartAxis>
y?: Array<ChartAxis>
series?: Array<ChartAxis>
multiQuotaName?: string | undefined
showLabel?: boolean
}>(),
{
data: () => [],
columns: () => [],
x: () => [],
y: () => [],
series: () => [],
multiQuotaName: undefined,
showLabel: false,
}
)
const chartId = computed(() => {
return 'chart-component-' + params.id
})
const axis = computed(() => {
const _list: Array<ChartAxis> = []
params.columns.forEach((column) => {
_list.push({ name: column.name, value: column.value })
})
params.x.forEach((column) => {
_list.push({ name: column.name, value: column.value, type: 'x' })
})
params.y.forEach((column) => {
_list.push({
name: column.name,
value: column.value,
type: 'y',
'multi-quota': column['multi-quota'],
})
})
params.series.forEach((column) => {
_list.push({ name: column.name, value: column.value, type: 'series' })
})
if (params.multiQuotaName) {
_list.push({
name: params.multiQuotaName,
value: params.multiQuotaName,
type: 'other-info',
hidden: true,
})
}
return _list
})
let chartInstance: BaseChart | undefined
function renderChart() {
chartInstance = getChartInstance(params.type, chartId.value)
if (chartInstance) {
chartInstance.showLabel = params.showLabel
chartInstance.init(axis.value, params.data)
chartInstance.render()
}
}
watch(
() => params.showLabel,
() => {
renderChart()
}
)
function destroyChart() {
if (chartInstance) {
chartInstance.destroy()
chartInstance = undefined
}
}
function getExcelData() {
return {
axis: axis.value,
data: params.data,
}
}
useEmitt({
name: 'view-render-all',
callback: renderChart,
})
useEmitt({
name: `view-render-${params.id}`,
callback: renderChart,
})
defineExpose({
renderChart,
destroyChart,
getExcelData,
})
onMounted(() => {
nextTick(() => {
renderChart()
})
})
onUnmounted(() => {
destroyChart()
})
</script>
<template>
<div :id="chartId" class="chart-container"></div>
</template>
<style scoped lang="less">
.chart-container {
height: 100%;
width: 100%;
}
</style>