Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit ab1922e
Author: 花裤衩 <panfree23@gmail.com>
Date:   Fri May 24 10:02:27 2019 +0800

    Create FUNDING.yml

commit 13c1ecf
Author: 花裤衩 <panfree23@gmail.com>
Date:   Thu May 23 11:27:10 2019 +0800

    fix[Chart]: fixed chart bug in keep-alive (PanJiaChen#2119)
  • Loading branch information
Edwin Hui committed May 24, 2019
1 parent 43e8e8f commit 684092f
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 54 deletions.
4 changes: 4 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# These are supported funding model platforms

patreon: panjiachen
custom: https://panjiachen.github.io/vue-element-admin-site/donate
10 changes: 2 additions & 8 deletions src/views/dashboard/admin/components/BarChart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'
import resize from './mixins/resize'
const animationDuration = 6000
export default {
mixins: [resize],
props: {
className: {
type: String,
Expand All @@ -33,18 +34,11 @@ export default {
this.$nextTick(() => {
this.initChart()
})
this.__resizeHandler = debounce(() => {
if (this.chart) {
this.chart.resize()
}
}, 100)
window.addEventListener('resize', this.__resizeHandler)
},
beforeDestroy() {
if (!this.chart) {
return
}
window.removeEventListener('resize', this.__resizeHandler)
this.chart.dispose()
this.chart = null
},
Expand Down
36 changes: 6 additions & 30 deletions src/views/dashboard/admin/components/LineChart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'
import resize from './mixins/resize'
export default {
mixins: [resize],
props: {
className: {
type: String,
Expand All @@ -32,8 +33,7 @@ export default {
},
data() {
return {
chart: null,
sidebarElm: null
chart: null
}
},
watch: {
Expand All @@ -48,38 +48,18 @@ export default {
this.$nextTick(() => {
this.initChart()
})
if (this.autoResize) {
this.__resizeHandler = debounce(() => {
if (this.chart) {
this.chart.resize()
}
}, 100)
window.addEventListener('resize', this.__resizeHandler)
}
// 监听侧边栏的变化
this.sidebarElm = document.getElementsByClassName('sidebar-container')[0]
this.sidebarElm && this.sidebarElm.addEventListener('transitionend', this.sidebarResizeHandler)
},
beforeDestroy() {
if (!this.chart) {
return
}
if (this.autoResize) {
window.removeEventListener('resize', this.__resizeHandler)
}
this.sidebarElm && this.sidebarElm.removeEventListener('transitionend', this.sidebarResizeHandler)
this.chart.dispose()
this.chart = null
},
methods: {
sidebarResizeHandler(e) {
if (e.propertyName === 'width') {
this.__resizeHandler()
}
initChart() {
this.chart = echarts.init(this.$el, 'macarons')
this.setOptions(this.chartData)
},
setOptions({ expectedData, actualData } = {}) {
this.chart.setOption({
Expand Down Expand Up @@ -149,10 +129,6 @@ export default {
animationEasing: 'quadraticOut'
}]
})
},
initChart() {
this.chart = echarts.init(this.$el, 'macarons')
this.setOptions(this.chartData)
}
}
}
Expand Down
10 changes: 2 additions & 8 deletions src/views/dashboard/admin/components/PieChart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'
import resize from './mixins/resize'
export default {
mixins: [resize],
props: {
className: {
type: String,
Expand All @@ -31,18 +32,11 @@ export default {
this.$nextTick(() => {
this.initChart()
})
this.__resizeHandler = debounce(() => {
if (this.chart) {
this.chart.resize()
}
}, 100)
window.addEventListener('resize', this.__resizeHandler)
},
beforeDestroy() {
if (!this.chart) {
return
}
window.removeEventListener('resize', this.__resizeHandler)
this.chart.dispose()
this.chart = null
},
Expand Down
10 changes: 2 additions & 8 deletions src/views/dashboard/admin/components/RaddarChart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'
import resize from './mixins/resize'
const animationDuration = 3000
export default {
mixins: [resize],
props: {
className: {
type: String,
Expand All @@ -33,18 +34,11 @@ export default {
this.$nextTick(() => {
this.initChart()
})
this.__resizeHandler = debounce(() => {
if (this.chart) {
this.chart.resize()
}
}, 100)
window.addEventListener('resize', this.__resizeHandler)
},
beforeDestroy() {
if (!this.chart) {
return
}
window.removeEventListener('resize', this.__resizeHandler)
this.chart.dispose()
this.chart = null
},
Expand Down
56 changes: 56 additions & 0 deletions src/views/dashboard/admin/components/mixins/resize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { debounce } from '@/utils'

export default {
data() {
return {
$_sidebarElm: null
}
},
mounted() {
this.$_initResizeEvent()
this.$_initSidebarResizeEvent()
},
beforeDestroy() {
this.$_destroyResizeEvent()
this.$_destroySidebarResizeEvent()
},
// to fixed bug when cached by keep-alive
// https://github.com/PanJiaChen/vue-element-admin/issues/2116
activated() {
this.$_initResizeEvent()
this.$_initSidebarResizeEvent()
},
deactivated() {
this.$_destroyResizeEvent()
this.$_destroySidebarResizeEvent()
},
methods: {
// use $_ for mixins properties
// https://vuejs.org/v2/style-guide/index.html#Private-property-names-essential
$_resizeHandler() {
return debounce(() => {
if (this.chart) {
this.chart.resize()
}
}, 100)()
},
$_initResizeEvent() {
window.addEventListener('resize', this.$_resizeHandler)
},
$_destroyResizeEvent() {
window.removeEventListener('resize', this.$_resizeHandler)
},
$_sidebarResizeHandler(e) {
if (e.propertyName === 'width') {
this.$_resizeHandler()
}
},
$_initSidebarResizeEvent() {
this.$_sidebarElm = document.getElementsByClassName('sidebar-container')[0]
this.$_sidebarElm && this.$_sidebarElm.addEventListener('transitionend', this.$_sidebarResizeHandler)
},
$_destroySidebarResizeEvent() {
this.$_sidebarElm && this.$_sidebarElm.removeEventListener('transitionend', this.$_sidebarResizeHandler)
}
}
}

0 comments on commit 684092f

Please sign in to comment.