Skip to content

Commit d2ab913

Browse files
committed
fix[Chart]: fixed chart bug in keep-alive
1 parent 2f5c2ee commit d2ab913

File tree

5 files changed

+62
-49
lines changed

5 files changed

+62
-49
lines changed

src/views/dashboard/admin/components/BarChart.vue

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
<script>
66
import echarts from 'echarts'
77
require('echarts/theme/macarons') // echarts theme
8-
import { debounce } from '@/utils'
8+
import resize from './mixins/resize'
99
1010
const animationDuration = 6000
1111
1212
export default {
13+
mixins: [resize],
1314
props: {
1415
className: {
1516
type: String,
@@ -33,18 +34,11 @@ export default {
3334
this.$nextTick(() => {
3435
this.initChart()
3536
})
36-
this.__resizeHandler = debounce(() => {
37-
if (this.chart) {
38-
this.chart.resize()
39-
}
40-
}, 100)
41-
window.addEventListener('resize', this.__resizeHandler)
4237
},
4338
beforeDestroy() {
4439
if (!this.chart) {
4540
return
4641
}
47-
window.removeEventListener('resize', this.__resizeHandler)
4842
this.chart.dispose()
4943
this.chart = null
5044
},

src/views/dashboard/admin/components/LineChart.vue

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
<script>
66
import echarts from 'echarts'
77
require('echarts/theme/macarons') // echarts theme
8-
import { debounce } from '@/utils'
8+
import resize from './mixins/resize'
99
1010
export default {
11+
mixins: [resize],
1112
props: {
1213
className: {
1314
type: String,
@@ -48,39 +49,15 @@ export default {
4849
this.$nextTick(() => {
4950
this.initChart()
5051
})
51-
52-
if (this.autoResize) {
53-
this.__resizeHandler = debounce(() => {
54-
if (this.chart) {
55-
this.chart.resize()
56-
}
57-
}, 100)
58-
window.addEventListener('resize', this.__resizeHandler)
59-
}
60-
61-
// 监听侧边栏的变化
62-
this.sidebarElm = document.getElementsByClassName('sidebar-container')[0]
63-
this.sidebarElm && this.sidebarElm.addEventListener('transitionend', this.sidebarResizeHandler)
6452
},
6553
beforeDestroy() {
6654
if (!this.chart) {
6755
return
6856
}
69-
if (this.autoResize) {
70-
window.removeEventListener('resize', this.__resizeHandler)
71-
}
72-
73-
this.sidebarElm && this.sidebarElm.removeEventListener('transitionend', this.sidebarResizeHandler)
74-
7557
this.chart.dispose()
7658
this.chart = null
7759
},
7860
methods: {
79-
sidebarResizeHandler(e) {
80-
if (e.propertyName === 'width') {
81-
this.__resizeHandler()
82-
}
83-
},
8461
setOptions({ expectedData, actualData } = {}) {
8562
this.chart.setOption({
8663
xAxis: {

src/views/dashboard/admin/components/PieChart.vue

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
<script>
66
import echarts from 'echarts'
77
require('echarts/theme/macarons') // echarts theme
8-
import { debounce } from '@/utils'
8+
import resize from './mixins/resize'
99
1010
export default {
11+
mixins: [resize],
1112
props: {
1213
className: {
1314
type: String,
@@ -31,18 +32,11 @@ export default {
3132
this.$nextTick(() => {
3233
this.initChart()
3334
})
34-
this.__resizeHandler = debounce(() => {
35-
if (this.chart) {
36-
this.chart.resize()
37-
}
38-
}, 100)
39-
window.addEventListener('resize', this.__resizeHandler)
4035
},
4136
beforeDestroy() {
4237
if (!this.chart) {
4338
return
4439
}
45-
window.removeEventListener('resize', this.__resizeHandler)
4640
this.chart.dispose()
4741
this.chart = null
4842
},

src/views/dashboard/admin/components/RaddarChart.vue

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
<script>
66
import echarts from 'echarts'
77
require('echarts/theme/macarons') // echarts theme
8-
import { debounce } from '@/utils'
8+
import resize from './mixins/resize'
99
1010
const animationDuration = 3000
1111
1212
export default {
13+
mixins: [resize],
1314
props: {
1415
className: {
1516
type: String,
@@ -33,18 +34,11 @@ export default {
3334
this.$nextTick(() => {
3435
this.initChart()
3536
})
36-
this.__resizeHandler = debounce(() => {
37-
if (this.chart) {
38-
this.chart.resize()
39-
}
40-
}, 100)
41-
window.addEventListener('resize', this.__resizeHandler)
4237
},
4338
beforeDestroy() {
4439
if (!this.chart) {
4540
return
4641
}
47-
window.removeEventListener('resize', this.__resizeHandler)
4842
this.chart.dispose()
4943
this.chart = null
5044
},
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { debounce } from '@/utils'
2+
3+
export default {
4+
data() {
5+
return {
6+
$_sidebarElm: null
7+
}
8+
},
9+
mounted() {
10+
this.$_initResizeEvent()
11+
this.$_initSidebarResizeEvent()
12+
},
13+
beforeDestroy() {
14+
this.$_destroyResizeEvent()
15+
this.$_destroySidebarResizeEvent()
16+
},
17+
activated() {
18+
this.$_initResizeEvent()
19+
this.$_initSidebarResizeEvent()
20+
},
21+
deactivated() {
22+
this.$_destroyResizeEvent()
23+
this.$_destroySidebarResizeEvent()
24+
},
25+
methods: {
26+
// use $_ for mixins properties
27+
// https://vuejs.org/v2/style-guide/index.html#Private-property-names-essential
28+
$_sidebarResizeHandler(e) {
29+
if (e.propertyName === 'width') {
30+
this.$_resizeHandler()
31+
}
32+
},
33+
$_resizeHandler() {
34+
return debounce(() => {
35+
if (this.chart) {
36+
this.chart.resize()
37+
}
38+
}, 100)()
39+
},
40+
$_initResizeEvent() {
41+
window.addEventListener('resize', this.$_resizeHandler)
42+
},
43+
$_destroyResizeEvent() {
44+
window.removeEventListener('resize', this.$_resizeHandler)
45+
},
46+
$_initSidebarResizeEvent() {
47+
this.$_sidebarElm = document.getElementsByClassName('sidebar-container')[0]
48+
this.$_sidebarElm && this.$_sidebarElm.addEventListener('transitionend', this.$_sidebarResizeHandler)
49+
},
50+
$_destroySidebarResizeEvent() {
51+
this.$_sidebarElm && this.$_sidebarElm.removeEventListener('transitionend', this.$_sidebarResizeHandler)
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)