|
@@ -0,0 +1,151 @@
|
|
1
|
+<template>
|
|
2
|
+ <el-card>
|
|
3
|
+ <Title>分级统计</Title>
|
|
4
|
+ <div ref="chart" class="chart-div"></div>
|
|
5
|
+ </el-card>
|
|
6
|
+</template>
|
|
7
|
+
|
|
8
|
+<script>
|
|
9
|
+// 主模块
|
|
10
|
+import echarts from 'echarts/lib/echarts'
|
|
11
|
+// 引入 svg 主要解决文字锯齿问题, zrender 为 echart 底层
|
|
12
|
+import 'zrender/lib/svg/svg'
|
|
13
|
+// 饼图
|
|
14
|
+import 'echarts/lib/chart/pie'
|
|
15
|
+// 其他组件
|
|
16
|
+import 'echarts/lib/component/title'
|
|
17
|
+import 'echarts/lib/component/tooltip'
|
|
18
|
+import 'echarts/lib/component/legend'
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+export default {
|
|
22
|
+ name: 'StatisticClassification',
|
|
23
|
+ components: {
|
|
24
|
+ Title: () => import('@/components/Typography/Title.vue'),
|
|
25
|
+ },
|
|
26
|
+ props: {
|
|
27
|
+ dimensions: {
|
|
28
|
+ type: Array,
|
|
29
|
+ default: () => (['label', 'value'])
|
|
30
|
+ },
|
|
31
|
+ dataSource: {
|
|
32
|
+ type: Array,
|
|
33
|
+ default: () => ([])
|
|
34
|
+ }
|
|
35
|
+ },
|
|
36
|
+ watch: {
|
|
37
|
+ dataSource: {
|
|
38
|
+ handler(val) {
|
|
39
|
+ this.renderChart(val)
|
|
40
|
+ },
|
|
41
|
+ immediate: true
|
|
42
|
+ }
|
|
43
|
+ },
|
|
44
|
+ data() {
|
|
45
|
+ return {
|
|
46
|
+ options: {
|
|
47
|
+ title: {
|
|
48
|
+ subtext: '单位: 个',
|
|
49
|
+ },
|
|
50
|
+ tooltip: {
|
|
51
|
+ trigger: 'item',
|
|
52
|
+ },
|
|
53
|
+ legend: {
|
|
54
|
+ orient: 'vertical',
|
|
55
|
+ bottom: '5%',
|
|
56
|
+ right: '5%'
|
|
57
|
+ },
|
|
58
|
+ series: [
|
|
59
|
+ {
|
|
60
|
+ type: 'pie',
|
|
61
|
+ radius: ['40%', '70%'],
|
|
62
|
+ label: {
|
|
63
|
+ normal: {
|
|
64
|
+ show: true,
|
|
65
|
+ position: 'center',
|
|
66
|
+ color:'#454c5c',
|
|
67
|
+ padding: [0, 0, 20, 0], //设置字angular的边距
|
|
68
|
+ fontSize: 24,
|
|
69
|
+ },
|
|
70
|
+ },
|
|
71
|
+ labelLine: {
|
|
72
|
+ show: false
|
|
73
|
+ },
|
|
74
|
+ }
|
|
75
|
+ ],
|
|
76
|
+ },
|
|
77
|
+ myChart: null,
|
|
78
|
+ }
|
|
79
|
+ },
|
|
80
|
+ mounted() {
|
|
81
|
+ this.$nextTick(() => {
|
|
82
|
+ this.renderChart(this.dataSource)
|
|
83
|
+ })
|
|
84
|
+ },
|
|
85
|
+ beforeDestroy() {
|
|
86
|
+ if (this.myChart) {
|
|
87
|
+ this.myChart.dispose()
|
|
88
|
+ this.myChart = null
|
|
89
|
+ }
|
|
90
|
+ },
|
|
91
|
+ methods: {
|
|
92
|
+ renderChart(source) {
|
|
93
|
+ if (!this.$refs.chart) return;
|
|
94
|
+
|
|
95
|
+ if (!this.myChart) {
|
|
96
|
+ this.myChart = echarts.init(this.$refs.chart, null, {renderer: 'svg'})
|
|
97
|
+ }
|
|
98
|
+
|
|
99
|
+ const keyProp = this.dimensions[0]
|
|
100
|
+ const valProp = this.dimensions[1]
|
|
101
|
+
|
|
102
|
+ const total = source.reduce((acc, item) => acc + item[valProp], 0)
|
|
103
|
+ const legendData = source.reduce((acc, item) => {
|
|
104
|
+ const name = item[keyProp]
|
|
105
|
+ const value = item[valProp]
|
|
106
|
+ const percent = total > 0 ? value * 100 / total : 0
|
|
107
|
+
|
|
108
|
+ return {
|
|
109
|
+ ...acc,
|
|
110
|
+ [name]: ` ${name} ${value}个 ${percent}%`
|
|
111
|
+ }
|
|
112
|
+ }, {})
|
|
113
|
+
|
|
114
|
+ const first = this.options.series[0]
|
|
115
|
+ const option = {
|
|
116
|
+ ...this.options,
|
|
117
|
+ tooltip: {
|
|
118
|
+ ...this.options.tooltip,
|
|
119
|
+ formatter: (params) => {
|
|
120
|
+ return `${params.name}: ${params.data[valProp]}个`
|
|
121
|
+ }
|
|
122
|
+ },
|
|
123
|
+ series: [
|
|
124
|
+ {
|
|
125
|
+ ...first,
|
|
126
|
+ label: {
|
|
127
|
+ ...first.label,
|
|
128
|
+ normal: {
|
|
129
|
+ ...first.label.normal,
|
|
130
|
+ formatter: `${total} 个`
|
|
131
|
+ }
|
|
132
|
+ }
|
|
133
|
+ }
|
|
134
|
+ ],
|
|
135
|
+ legend: {
|
|
136
|
+ ...this.options.legend,
|
|
137
|
+ formatter: name => legendData[name],
|
|
138
|
+ },
|
|
139
|
+ dataset: {
|
|
140
|
+ source,
|
|
141
|
+ dimensions: this.dimensions,
|
|
142
|
+ }
|
|
143
|
+ }
|
|
144
|
+
|
|
145
|
+ console.log(option)
|
|
146
|
+
|
|
147
|
+ this.myChart.setOption(option)
|
|
148
|
+ },
|
|
149
|
+ }
|
|
150
|
+}
|
|
151
|
+</script>
|