1. 程式人生 > 其它 >VUE中解決echarts資料二次渲染不成功的問題xAxis

VUE中解決echarts資料二次渲染不成功的問題xAxis

應該設定:mychart.setOption({},true);

原因:chart.setOption(option,notMerge,lazyUpdate);

option:圖表的配置項和資料

notMerge:可選,是否不跟之前設定的option進行合併,預設為false,即合併。(這裡是導致二次渲染不成功的根本)

lazyUpdate:可選,在設定完option後是否不立即更新圖表,預設為false,即立即更新。

methods:  

// 商品 getGoods () { this.goodsName = this.goodsName this.charts = echarts.init(document.getElementById("
goods")); var emphasisStyle = { itemStyle: { shadowBlur: 10, shadowColor: 'rgba(0,0,0,0.3)' } }; this.charts.setOption({ legend: { icon: "circle", left: '88%', top: '0%', align: 'left', //文字位置 data: ['
增幅', '數量'], itemWidth: 8, // 設定寬度 itemHeight: 8, // 設定高度 bottom: 10, left: 'center', }, tooltip: { trigger: 'item', //axis 單個柱形圖 axisPointer: { // 座標軸指示器,座標軸觸發有效 type: 'shadow'// 預設為直線,可選為:'line' | 'shadow' }, formatter: (
params) => { var per = params.seriesName == '增幅' ? '%' : '' return params.name + '<br/>' + params.seriesName + ' : ' + params.value + per; } }, toolbox: {}, xAxis: { data: this.xAxisData, //x軸 name: '', axisLine: { onZero: true }, splitLine: { show: false }, splitArea: { show: false } }, yAxis: [ { type: 'value', name: '數量', axisLabel: { } }, { type: 'value', name: '環比增長率', min: 0, max: this.maxProportion, interval: this.maxProportion / 5, axisLabel: { formatter: '{value}' } } ], grid: { bottom: 140 }, series: [ { name: '增幅', type: 'bar', stack: 'one', emphasis: emphasisStyle, data: this.data2, color: '#73C0DE', barWidth: '30px', itemStyle: { normal: { label: { position: 'top' } } } }, { name: '數量', type: 'bar', stack: 'one', emphasis: emphasisStyle, data: this.data1, color: '#91CC75', barWidth: '30px', itemStyle: { normal: { label: { position: 'top' } } } }, ], // 可為負值 data1 和 data2為資料 data: (this.data1 || this.data2 || []).map((item) => { return { value: item, itemStyle: { normal: { label: { show: true, position: item > 0 ? 'top' : 'bottom', textStyle: { color: '#C23531', fontSize: 12 } } } } } }), }, true); },