<div class="chart">div>template><script>import { merge } from 'lodash'; import echart from 'echarts'; import { BASIC_OPTION } from './default_option'; import { COLOR_ARRAY } from '../color'; import ResizeListener from 'element-resize-detector'; export default { name: 'ChartPie', props: { seriesData: { type: Array, required: true, default: () => [] }, extraOption: { type: Object, default: () => ({}) } }, data() { return { chart: null }; }, watch: { seriesData: { deep: true, handler() { this.updateChartView(); } } }, mounted() { this.chart = echart.init(this.$el); this.updateChartView(); window.addEventListener('resize', this.handleWindowResize); this.addChartResizeListener(); }, beforeDestroy() { window.removeEventListener('resize', this.handleWindowResize); }, methods: { /** * 將業務數據加入到基礎樣式配置中 * @returns {Object} 完整的echart配置 */ assembleDataToOption() { const formatter = name => { const total = this.seriesData.reduce((acc, cur) => acc + cur.value, 0); const data = this.seriesData.find(item => item.name === name) || {}; const percent = data.value ? `${Math.round((data.value / total) * 100)}%` : '0%'; return `{a|${name}}{b|${percent}}`; }; return merge( {}, BASIC_OPTION, { color: COLOR_ARRAY }, { legend: { formatter }, series: [{ data: this.seriesData }] }, this.extraOption ); }, /** * 對chart元素尺寸進行監聽,當發生變化時同步更新echart視圖 */ addChartResizeListener() { const instance = ResizeListener({ strategy: 'scroll', callOnAdd: true }); instance.listenTo(this.$el, () => { if (!this.chart) return; this.chart.resize(); }); }, /** * 更新echart視圖 */ updateChartView() { if (!this.chart) return; const fullOption = this.assembleDataToOption(); this.chart.setOption(fullOption, true); }, /** * 當窗口縮放時,echart動態調整自身大小 */ handleWindowResize() { if (!this.chart) return; this.chart.resize(); } } };script><style lang="less" scoped>.chart { width: 100%; height: 100%; }style>
<van-empty v-if="isSeriesEmpty" /> <chart-pie v-else v-bind="$props" />template><script>import { isEmpty } from 'lodash';import ChartPie from './echart_pie.vue';export default { name: 'EchartPie', components: { ChartPie }, props: ChartPie.props, computed: { isSeriesEmpty() { return ( isEmpty(this.seriesData) || this.seriesData.every(item => !item.value) ); } } };script>
這是一個用於監聽DOM元素尺寸變化的插件。咱們已經對窗口縮放作了監聽,可是有時候其父級容器的大小也會動態改變的。vue
咱們對父級容器的寬度進行監聽,當父級容器的尺寸發生變化時,echart能調用自身的resize方法,保持視圖正常。數組
固然,這個不適用於tab選項卡的狀況,在tab選項卡中,父級容器從display:none到有實際的clientWidth,可能會比註冊一個resizeDetector先完成,因此等開始監聽父級容器resize的時候,可能爲時已晚。安全
在tab選項卡中很容易出現這種場景:app
看起來很是的逗,只有一點點大。解決這個問題,最有效的方法仍是在切換選項卡時手動去經過ref獲取echart實例,並手動調用resize方法,這是最安全的,也是最有效的。echarts
<div class="echart-pie-wrap"> <echart-pie :series-data="dataList" :extra-option="extraOption" />div>template><script>import EchartPie from '@/components/echarts/echart_pie';export default{ components: { EchartPie }, data() { return { dataList: [ { name: "西瓜", value: 20 }, { name: "橘子", value: 13 }, { name: "楊桃", value: 33 } ], extraOption: { color: [ '#fe883a', '#2d90d1','#f75981', '#90e2a9'] } } } }script><style lang="less" scoped>.echart-wrapper { width: 300px; height: 300px; margin: 10px auto; }style>
當數據正常時,效果以下 less
當無數據時,效果以下dom
這裏面提供的封裝只是一種大概的思路,實際項目中,可能會與這裏出現誤差。軟件開發沒有銀彈可言,很是適合我這個項目的,可能在細節上不太適合看這篇文章的你。可是思路是相似的,能夠對照着作一些調整。ide