在vue-cli創建的項目中 使用echarts
修改部分配置
<template> <div class="line-container"> <ul class="attribute-list clearfix"> <li class="item"> <span class="title">titile標題設置</span> <ul class="clearfix"> <li class="subItem"> <span>show: </span> <el-switch v-model="title.show" @change="onChange1" /> </li> </ul> </li> </ul> <div> <div id="lineChart"></div> </div> </div> </template> export default { name: 'lineChart', data(){ return { option: { title: { text: '折線圖屬性集合' }, tooltip: { trigger: 'axis' }, legend: { data: ['郵件營銷', '聯盟廣告', '視頻廣告', '直接訪問', '搜索引擎'] }, grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true }, toolbox: { feature: { saveAsImage: {} } }, xAxis: { type: 'category', boundaryGap: false, data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'] }, yAxis: { type: 'value' }, series: [ { name: '郵件營銷', type: 'line', stack: '總量', data: [120, 132, 101, 134, 90, 230, 210] }, { name: '聯盟廣告', type: 'line', stack: '總量', data: [220, 182, 191, 234, 290, 330, 310] }, { name: '視頻廣告', type: 'line', stack: '總量', data: [150, 232, 201, 154, 190, 330, 410] }, { name: '直接訪問', type: 'line', stack: '總量', data: [320, 332, 301, 334, 390, 330, 320] }, { name: '搜索引擎', type: 'line', stack: '總量', data: [820, 932, 901, 934, 1290, 1330, 1320] } ] }, lineChart: '', title: { text: '', left: 0, show: true, textStyle: { color: '#333', fontSize: '18' } } } }, created() {}, mounted() { this.getLineChart() }, methods: { // 設置折線圖 getLineChart() { this.lineChart = this.$echart.init(document.getElementById('lineChart')) this.lineChart.setOption(this.option) }, /** --------------------title相關設置開始------------------ */ // 標題展示 onChange1() { this.option.title = this.title console.log(this.option) this.lineChart.setOption(this.option) }, } } </script>
這樣設置后,只有第一次操作有效果,后面就沒有效果了
解決方法
修改echarts的option不能賦值data里定義的對象,因為對象的=賦值會改變option的數組存儲,可以賦值對象里的屬性值
可以直接修改option的屬性, 遇到其他數據型修改,可以把setOption第二參數,傳true,第二個參數是 notMerge: 是否不跟之前設置的 option
進行合並,默認為 false
,即合並
// 標題展示 onChange1(checked) { this.option.title.show = checked // 或者賦值對象里具體的屬性值 this.option.title.show = this.title.show console.log(this.option) this.lineChart.setOption(this.option) // this.lineChart.setOption(this.option, true)
},