diff --git a/package.json b/package.json index 3275810..5769800 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vue-echarts", - "version": "4.0.1", + "version": "4.0.2", "description": "ECharts component for Vue.js.", "main": "dist/vue-echarts.js", "module": "components/ECharts.vue", diff --git a/src/components/ECharts.vue b/src/components/ECharts.vue index c9142fb..b8a0b79 100644 --- a/src/components/ECharts.vue +++ b/src/components/ECharts.vue @@ -54,6 +54,9 @@ const EVENTS = [ 'contextmenu' ] +const INIT_TRIGGERS = ['theme', 'initOptions', 'autoresize'] +const REWATCH_TRIGGERS = ['manualUpdate', 'watchShallow'] + export default { props: { options: Object, @@ -131,11 +134,11 @@ export default { } return this.chart[name](...args) }, - delegateGet (name, method) { + delegateGet (methodName) { if (!this.chart) { this.init() } - return this.chart[method]() + return this.chart[methodName]() }, getArea () { return this.$el.offsetWidth * this.$el.offsetHeight @@ -183,31 +186,54 @@ export default { width: { configurable: true, get: () => { - return this.delegateGet('width', 'getWidth') + return this.delegateGet('getWidth') } }, height: { configurable: true, get: () => { - return this.delegateGet('height', 'getHeight') + return this.delegateGet('getHeight') } }, isDisposed: { configurable: true, get: () => { - return !!this.delegateGet('isDisposed', 'isDisposed') + return !!this.delegateGet('isDisposed') } }, computedOptions: { configurable: true, get: () => { - return this.delegateGet('computedOptions', 'getOption') + return this.delegateGet('getOption') } } }) this.chart = chart }, + initOptionsWatcher () { + if (this.__unwatchOptions) { + this.__unwatchOptions() + this.__unwatchOptions = null + } + + if (!this.manualUpdate) { + this.__unwatchOptions = this.$watch('options', (val, oldVal) => { + if (!this.chart && val) { + this.init() + } else { + // mutating `options` will lead to merging + // replacing it with new reference will lead to not merging + // eg. + // `this.options = Object.assign({}, this.options, { ... })` + // will trigger `this.chart.setOption(val, true) + // `this.options.title.text = 'Trends'` + // will trigger `this.chart.setOption(val, false)` + this.chart.setOption(val, val !== oldVal) + } + }, { deep: !this.watchShallow }) + } + }, destroy () { if (this.autoresize) { removeListener(this.$el, this.__resizeHandler) @@ -223,29 +249,20 @@ export default { } }, created () { - if (!this.manualUpdate) { - this.$watch('options', (val, oldVal) => { - if (!this.chart && val) { - this.init() - } else { - // mutating `options` will lead to merging - // replacing it with new reference will lead to not merging - // eg. - // `this.options = Object.assign({}, this.options, { ... })` - // will trigger `this.chart.setOption(val, true) - // `this.options.title.text = 'Trends'` - // will trigger `this.chart.setOption(val, false)` - this.chart.setOption(val, val !== oldVal) - } - }, { deep: !this.watchShallow }) - } + this.initOptionsWatcher() - let watched = ['theme', 'initOptions', 'autoresize', 'manualUpdate', 'watchShallow'] - watched.forEach(prop => { + INIT_TRIGGERS.forEach(prop => { this.$watch(prop, () => { this.refresh() }, { deep: true }) }) + + REWATCH_TRIGGERS.forEach(prop => { + this.$watch(prop, () => { + this.initOptionsWatcher() + this.refresh() + }) + }) }, mounted () { // auto init if `options` is already provided