barChart.vue
4.62 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<template>
<div :class="className" :style="{height:height,width:width}"></div>
</template>
<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import {debounce} from '../../utils/debounce'
export default {
name: 'barChart',
props: {
className: {
type: String,
default: 'chart'
},
width: {
type: String,
default: '100%'
},
height: {
type: String,
default: '100%'
},
autoResize: {
type: Boolean,
default: true
},
chartData: {
type: Object,
required: true
}
},
data() {
return {
chart: null
}
},
watch: {
chartData: function () {
this.$nextTick(()=>{
this.initChart()
})
}
},
mounted() {
this.initChart()
if (this.autoResize) {
this.__resizeHandler = debounce(() => {
if (this.chart) {
this.chart.resize()
}
}, 100)
window.addEventListener('resize', this.__resizeHandler)
}
},
beforeDestroy() {
if (!this.chart) {
return
}
if (this.autoResize) {
window.removeEventListener('resize', this.__resizeHandler)
}
this.chart.dispose()
this.chart = null
},
methods: {
setOptions({yData} = {}) {
this.chart.setOption(
{
grid: {
top: 0,
left: 0,
right: '20',
bottom: -10,
containLabel: true
},
title:{show:false},
xAxis: {
show: false,//尽管显示false,但仍然占用空间containLabel为true的话[echarts bug]
nameGap:0
},
yAxis: [{
show: true,
nameLocation:'start',
//data: ['支付宝', '微信', '其它'],
data: [
{
value:'支付宝',
textStyle:{
color:'#01AEFE'
}
},
{
value:'微信',
textStyle:{
color:'#06C406'
}
},
{
value:'其它',
textStyle:{
color:'#FFAB00'
}
}
],//右侧Y轴值
inverse: true,
axisLine: {show: false},
splitLine: {show: false},
axisTick: {show: false },
axisLabel: {
//margin:-20,
// nameTextStyle:{
// align:'left'
// }
}
}, {
show: true,
inverse: true,
data: [
{
value:1000,
textStyle:{
color:'#01AEFE'
}
},
{
value:200,
textStyle:{
color:'#06C406'
}
},
{
value:300,
textStyle:{
color:'#FFAB00'
}
}
],//右侧Y轴值
axisLabel: {
textStyle: {fontSize: 12,},
},
axisLine: { show: false },
splitLine: { show: false},
axisTick: {show: false},
}],
series: [{
name: '条',
type: 'bar',
yAxisIndex: 0,
data: [1000,200,300],//百分比
barWidth: '40%',
itemStyle: {
normal: {
barBorderRadius: 16,
color: {
type: 'linear',
x: 0,
x1: 1,
colorStops: [{
offset: 0,
color: '#02ddff'
}, {
offset: 1,
color: '#00feff'
}]
}
},
},
label: {
normal: {show: false}
},
}, {
name: '框',
type: 'bar',
yAxisIndex: 1,
barGap: '-100%',
data: [1500,1500,1500],//百分比
barWidth: '40%',
itemStyle: {
normal: {
color: 'none',
borderColor: '#157ADB',
borderWidth: 0.5,
//barBorderRadius: 15,
}
}
}, ]
}
)
},
initChart() {
this.chart = echarts.init(this.$el)
this.setOptions(this.chartData)
},
}
}
</script>