|
@@ -0,0 +1,168 @@
|
|
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/bar'
|
|
15
|
+// 其他组件
|
|
16
|
+import 'echarts/lib/component/title'
|
|
17
|
+import 'echarts/lib/component/tooltip'
|
|
18
|
+import 'echarts/lib/component/legend'
|
|
19
|
+//
|
|
20
|
+import { LinearGradient } from './util'
|
|
21
|
+
|
|
22
|
+// 默认颜色集合
|
|
23
|
+const colors = [
|
|
24
|
+ LinearGradient([{ offset: 0, color: 'rgba(69, 141, 240, 1)' }, { offset: 1, color: 'rgba(69, 141, 240, 0.4)' }]),
|
|
25
|
+ LinearGradient([{ offset: 0, color: 'rgba(162, 69, 240, 1)' }, { offset: 1, color: 'rgba(162, 69, 240, 0.4)' }]),
|
|
26
|
+ LinearGradient([{ offset: 0, color: 'rgba(161, 240, 75, 1)' }, { offset: 1, color: 'rgba(161, 240, 75, 0.4)' }]),
|
|
27
|
+ LinearGradient([{ offset: 0, color: 'rgba(241, 186, 70, 1)' }, { offset: 1, color: 'rgba(241, 186, 70, 0.4)' }]),
|
|
28
|
+ LinearGradient([{ offset: 0, color: 'rgba(246, 151, 151, 1)' }, { offset: 1, color: 'rgba(246, 151, 151, 0.4)' }]),
|
|
29
|
+]
|
|
30
|
+
|
|
31
|
+export default {
|
|
32
|
+ name: 'StatisticEnterprise',
|
|
33
|
+ components: {
|
|
34
|
+ Title: () => import('@/components/Typography/Title.vue'),
|
|
35
|
+ },
|
|
36
|
+ props: {
|
|
37
|
+ dimensions: {
|
|
38
|
+ type: Array,
|
|
39
|
+ default: () => (['label', 'value'])
|
|
40
|
+ },
|
|
41
|
+ dataSource: {
|
|
42
|
+ type: Array,
|
|
43
|
+ default: () => ([])
|
|
44
|
+ }
|
|
45
|
+ },
|
|
46
|
+ watch: {
|
|
47
|
+ dataSource: {
|
|
48
|
+ handler(val) {
|
|
49
|
+ this.renderChart(val)
|
|
50
|
+ },
|
|
51
|
+ immediate: true
|
|
52
|
+ }
|
|
53
|
+ },
|
|
54
|
+ data() {
|
|
55
|
+ return {
|
|
56
|
+ options: {
|
|
57
|
+ title: {
|
|
58
|
+ subtext: '单位: 个',
|
|
59
|
+ },
|
|
60
|
+ legend: {
|
|
61
|
+ right: '5%',
|
|
62
|
+ },
|
|
63
|
+ tooltip: {
|
|
64
|
+ trigger: 'axis',
|
|
65
|
+ backgroundColor: '#ffffff',
|
|
66
|
+ formatter: (params) => {
|
|
67
|
+ if (!params || !params.length) return '';
|
|
68
|
+
|
|
69
|
+ let htmlStr = `<div style="text-align: left">${params[0].name}</div>`
|
|
70
|
+ htmlStr += params.map((item) => {
|
|
71
|
+ const color = item.color.colorStops[0].color
|
|
72
|
+ const seriesName = item.seriesName
|
|
73
|
+ const value = item.value[seriesName]
|
|
74
|
+
|
|
75
|
+ return `
|
|
76
|
+ <div style="display: flex; justify-content: space-between; align-items: center">
|
|
77
|
+ <div style="flex: 1;">
|
|
78
|
+ <span style="display: inline-block; width: 0.8em; height: 0.8em; background-color: ${color};"></span>
|
|
79
|
+ <span style="display: inline-block; margin-left: 0.8em">${seriesName}</span>
|
|
80
|
+ </div>
|
|
81
|
+ <div style="flex: 1; margin-left: 1em">${value}个</div>
|
|
82
|
+ </div>
|
|
83
|
+ `
|
|
84
|
+ }).join('')
|
|
85
|
+
|
|
86
|
+ return `<div style="box-sizing: border-box; padding: 0.6em 1em">${htmlStr}</div>`;
|
|
87
|
+ },
|
|
88
|
+ textStyle: {
|
|
89
|
+ color: '#333',
|
|
90
|
+ lineHeight: 28,
|
|
91
|
+ },
|
|
92
|
+ extraCssText: 'box-shadow: 0 0 3px rgba(0, 0, 0, 0.3);',
|
|
93
|
+ axisPointer: {
|
|
94
|
+ type: 'shadow',
|
|
95
|
+ shadowStyle: {
|
|
96
|
+ color: LinearGradient([{ offset: 0, color: 'rgba(153, 205, 255, 0.2)' }, { offset: 1, color: 'rgba(153, 205, 255, 0.05)' }]),
|
|
97
|
+ }
|
|
98
|
+ }
|
|
99
|
+ },
|
|
100
|
+ xAxis: {
|
|
101
|
+ type: 'category',
|
|
102
|
+ },
|
|
103
|
+ yAxis: {
|
|
104
|
+ type: 'value'
|
|
105
|
+ },
|
|
106
|
+ series: [
|
|
107
|
+ {
|
|
108
|
+ type: 'bar',
|
|
109
|
+ barMaxWidth: 40,
|
|
110
|
+ itemStyle: {
|
|
111
|
+ color: LinearGradient([{ offset: 0, color: 'rgba(69, 141, 240, 1)' }, { offset: 1, color: 'rgba(69, 141, 240, 0.4)' }])
|
|
112
|
+ },
|
|
113
|
+ }
|
|
114
|
+ ],
|
|
115
|
+ },
|
|
116
|
+ myChart: null,
|
|
117
|
+ }
|
|
118
|
+ },
|
|
119
|
+ mounted() {
|
|
120
|
+ this.$nextTick(() => {
|
|
121
|
+ this.renderChart(this.dataSource)
|
|
122
|
+ })
|
|
123
|
+ },
|
|
124
|
+ beforeDestroy() {
|
|
125
|
+ if (this.myChart) {
|
|
126
|
+ this.myChart.dispose()
|
|
127
|
+ this.myChart = null
|
|
128
|
+ }
|
|
129
|
+ },
|
|
130
|
+ methods: {
|
|
131
|
+ renderChart(source) {
|
|
132
|
+ if (!this.$refs.chart) return;
|
|
133
|
+
|
|
134
|
+ if (!this.myChart) {
|
|
135
|
+ this.myChart = echarts.init(this.$refs.chart, null, {renderer: 'svg'})
|
|
136
|
+ }
|
|
137
|
+
|
|
138
|
+ // const keyProp = this.dimensions[0]
|
|
139
|
+ // const valProp = this.dimensions[1]
|
|
140
|
+
|
|
141
|
+ const option = {
|
|
142
|
+ ...this.options,
|
|
143
|
+ // tooltip: {
|
|
144
|
+ // ...this.options.tooltip,
|
|
145
|
+ // formatter: (params) => {
|
|
146
|
+ // return `${params.name}: ${params.data[valProp]}个`
|
|
147
|
+ // }
|
|
148
|
+ // },
|
|
149
|
+ dataset: {
|
|
150
|
+ source,
|
|
151
|
+ dimensions: this.dimensions,
|
|
152
|
+ },
|
|
153
|
+ series: source.map((item, index) => ({
|
|
154
|
+ type: 'bar',
|
|
155
|
+ barMaxWidth: 25,
|
|
156
|
+ itemStyle: {
|
|
157
|
+ color: colors[index % colors.length]
|
|
158
|
+ },
|
|
159
|
+ }))
|
|
160
|
+ }
|
|
161
|
+
|
|
162
|
+ console.log(option)
|
|
163
|
+
|
|
164
|
+ this.myChart.setOption(option)
|
|
165
|
+ },
|
|
166
|
+ }
|
|
167
|
+}
|
|
168
|
+</script>
|