|
@@ -0,0 +1,187 @@
|
|
1
|
+<template>
|
|
2
|
+ <div class="chart" :id="id"></div>
|
|
3
|
+</template>
|
|
4
|
+
|
|
5
|
+<script>
|
|
6
|
+import * as echarts from 'echarts'
|
|
7
|
+const colors = [
|
|
8
|
+ new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
|
|
9
|
+ offset: 0,
|
|
10
|
+ color: '#1EBFFF'
|
|
11
|
+ },
|
|
12
|
+ {
|
|
13
|
+ offset: 1,
|
|
14
|
+ color: 'rgb(31, 96, 223)'
|
|
15
|
+ }
|
|
16
|
+ ]),
|
|
17
|
+ new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
|
|
18
|
+ offset: 0,
|
|
19
|
+ color: '#FFCA02'
|
|
20
|
+ },
|
|
21
|
+ {
|
|
22
|
+ offset: 1,
|
|
23
|
+ color: '#FF6E2F'
|
|
24
|
+ }
|
|
25
|
+ ])
|
|
26
|
+]
|
|
27
|
+export default {
|
|
28
|
+ props: {
|
|
29
|
+ id: {type: String, required: true},
|
|
30
|
+ dataset: {type: Array, required: true},
|
|
31
|
+ encode: {type: Array, required: true},
|
|
32
|
+ yAxisName: {type: Array},
|
|
33
|
+ xlabelFormat: {type: Function},
|
|
34
|
+ showLegend: {type: Boolean, default: true},
|
|
35
|
+ top: {type: Number, default: 50},
|
|
36
|
+ colorIdx: {type: Number}, // 颜色idx
|
|
37
|
+ colorArr: {type: Array, default: ()=>colors},
|
|
38
|
+ bottom: {type: Number},
|
|
39
|
+ legendIcon: {type: String, default: 'circle'},
|
|
40
|
+ tooltip: {type: Function}, // tooltip format
|
|
41
|
+ stack: {type: String},
|
|
42
|
+ isBig: {type: Boolean, default: false},
|
|
43
|
+ labelSize: {type: Number, default: 12},
|
|
44
|
+ autoStop: {type: Boolean, default: true} // tooltip循环会否循环一轮后自动停止
|
|
45
|
+ },
|
|
46
|
+ data() {
|
|
47
|
+ return {
|
|
48
|
+ option: {
|
|
49
|
+ grid: {
|
|
50
|
+ top: this.top,
|
|
51
|
+ bottom: this.xlabelFormat ? 50 : 30,
|
|
52
|
+ left: 45,
|
|
53
|
+ right: 45,
|
|
54
|
+ },
|
|
55
|
+ legend: {right: 0, top: 0, icon: this.legendIcon, textStyle: {color: '#000', fontSize: 12}},
|
|
56
|
+ tooltip: {
|
|
57
|
+ trigger: 'axis',
|
|
58
|
+ // confine: true,
|
|
59
|
+ // backgroundColor: 't',
|
|
60
|
+ // axisPointer: {
|
|
61
|
+ // lineStyle: { color: "#0081AC", type: "dashed" },
|
|
62
|
+ // },
|
|
63
|
+ // show: this.tooltip,
|
|
64
|
+ // formatter: this.tooltip,
|
|
65
|
+ // position: function(point, params, dom,rect,size) {
|
|
66
|
+ // let boxWidth = size.contentSize[0];
|
|
67
|
+ // let boxHeight = size.contentSize[1];
|
|
68
|
+ // return [point[0]-boxWidth-20, point[1]-boxHeight-40];
|
|
69
|
+ // }
|
|
70
|
+ },
|
|
71
|
+ xAxis: {
|
|
72
|
+ type: 'category',
|
|
73
|
+ axisLabel: {
|
|
74
|
+ interval: 0,
|
|
75
|
+ },
|
|
76
|
+ axisLine: {
|
|
77
|
+ lineStyle: {
|
|
78
|
+ // color: '#1977C6'
|
|
79
|
+ }
|
|
80
|
+ },
|
|
81
|
+ splitLine: {
|
|
82
|
+ lineStyle: {
|
|
83
|
+ // color: '#082E81',
|
|
84
|
+ type: 'dashed'
|
|
85
|
+ }
|
|
86
|
+ },
|
|
87
|
+ axisTick: false
|
|
88
|
+ },
|
|
89
|
+ yAxis: [{
|
|
90
|
+ axisLine: {
|
|
91
|
+ show: false
|
|
92
|
+ },
|
|
93
|
+ nameTextStyle: {
|
|
94
|
+ fontSize: this.labelSize
|
|
95
|
+ },
|
|
96
|
+ splitLine: {
|
|
97
|
+ lineStyle: {
|
|
98
|
+ // color: '#082E81',
|
|
99
|
+ type: 'dashed'
|
|
100
|
+ }
|
|
101
|
+ },
|
|
102
|
+ axisTick: false,
|
|
103
|
+ },{
|
|
104
|
+ axisLine: {
|
|
105
|
+ show: false
|
|
106
|
+ },
|
|
107
|
+ nameTextStyle: {
|
|
108
|
+ fontSize: this.labelSize
|
|
109
|
+ },
|
|
110
|
+ splitLine: {
|
|
111
|
+ lineStyle: {
|
|
112
|
+ // color: '#082E81',
|
|
113
|
+ type: 'dashed'
|
|
114
|
+ }
|
|
115
|
+ },
|
|
116
|
+ axisTick: false,
|
|
117
|
+ }],
|
|
118
|
+ dataset: {},
|
|
119
|
+ series: []
|
|
120
|
+ }
|
|
121
|
+ }
|
|
122
|
+ },
|
|
123
|
+ watch: {
|
|
124
|
+ dataset: function(newV) {
|
|
125
|
+ const option = this.option
|
|
126
|
+ option.dataset.source = newV
|
|
127
|
+ this.chart.setOption(option)
|
|
128
|
+ }
|
|
129
|
+ },
|
|
130
|
+ mounted() {
|
|
131
|
+ this.chart = echarts.init($(`#${this.id}`)[0])
|
|
132
|
+ const option = this.option
|
|
133
|
+ option.yAxis[0].name = this.yAxisName[0]
|
|
134
|
+ option.yAxis[1].name = this.yAxisName[1]
|
|
135
|
+ if (this.xlabelFormat) {
|
|
136
|
+ option.xAxis.axisLabel.formatter = this.xlabelFormat
|
|
137
|
+ }
|
|
138
|
+ this.encode.map((encode, idx) => {
|
|
139
|
+ option.series.push({
|
|
140
|
+ name: encode.seriesName,
|
|
141
|
+ type: 'bar',
|
|
142
|
+ stack: this.stack,
|
|
143
|
+ barWidth: 10,
|
|
144
|
+ yAxisIndex: idx,
|
|
145
|
+ encode: encode,
|
|
146
|
+ itemStyle: {
|
|
147
|
+ barBorderRadius: this.legendIcon === 'circle' ? [5, 5, 0, 0] : 0,
|
|
148
|
+ color: this.colorArr[idx]
|
|
149
|
+ }
|
|
150
|
+ })
|
|
151
|
+ })
|
|
152
|
+ option.dataset.source = this.dataset
|
|
153
|
+ // if (this.tooltipFormat) {
|
|
154
|
+ // option.tooltip.formatter = this.tooltipFormat
|
|
155
|
+ // }
|
|
156
|
+ // option.tooltip= {
|
|
157
|
+ // trigger: 'axis',
|
|
158
|
+ // axisPointer: {
|
|
159
|
+ // // type: 'shadow'
|
|
160
|
+ // }
|
|
161
|
+ // }
|
|
162
|
+ this.chart.setOption(option)
|
|
163
|
+ },
|
|
164
|
+ methods: {
|
|
165
|
+ xLabelFormat(name) {
|
|
166
|
+ const nameArr = []
|
|
167
|
+ for(let i=0; i<name.length; i+=6) {
|
|
168
|
+ nameArr.push(name.substring(i, i+6))
|
|
169
|
+ }
|
|
170
|
+ return nameArr.join('\n');
|
|
171
|
+ },
|
|
172
|
+ // tooltipFormat(params) {
|
|
173
|
+ // const type = params[0].seriesType;
|
|
174
|
+ // const data = params[0].data;
|
|
175
|
+ // console.log(params[0].data, '333')
|
|
176
|
+ // return `<div style="background: black">${data.hs || "--"}户</div>`;
|
|
177
|
+ // },
|
|
178
|
+ }
|
|
179
|
+}
|
|
180
|
+</script>
|
|
181
|
+
|
|
182
|
+<style lang="less">
|
|
183
|
+ .chart {
|
|
184
|
+ width: 100%;
|
|
185
|
+ height: 100%;
|
|
186
|
+ }
|
|
187
|
+</style>
|