(echarts)控制双图表显示隐藏There is a chart instance already initialized on the dom

(echarts)控制双图表显示隐藏There is a chart instance already initialized on the dom


需求背景:
返回数据中若zAxis、zAxisData字段有值,则显示柱状图,否则显示折线图


<a-card :title="echartTitle" style="width: 100%;" v-show="showLine">
  <div class="echartDiv">
    <div id="echart" ref="myLineChart" style="height:300px;width:1200px;"></div>
  </div>
</a-card>
<a-card :title="echartTitle" style="width: 100%;" v-show="showBar">
  <div class="echartDiv">
    <div id="echartBar" ref="myLineChartBar" style="height:300px;width:1200px;"></div>
  </div>
</a-card>

js:

data(){
	return{
	  show: true,
      cardDiv: true,
      myChart: null,      
      myChartBar: null,
      showBar: true,
      showLine: true,
      ...
	}
}      

//查询
handleSearch() {
  this.spinning = true
  GetfactoryCompletions({
    prompt: this.value,
  })
    .then((res) => {
      this.tableTitle = this.value + ' 如下表所示:'
      this.echartTitle = this.value + ' 如下图所示:'
      this.spinning = false
      if (res.code == 0) {
        this.show = false
        this.cardDiv = true 
        ...
        if (res.resMap.zAxis.length > 0 && res.resMap.zAxisData.length > 0) {
          this.showBar = true
          this.showLine = false
          this.zAxis = res.resMap.zAxis
          this.zAxisData = res.resMap.zAxisData
          this.$nextTick(() => {
            this.graphBar()
          })
        } else {
          this.showLine = true
          this.showBar = false
          this.xAxis = res.resMap.xAxis
          this.xAxisData = res.resMap.xAxisData
          this.$nextTick(() => {
            this.graph()
          })
        }
      } else {
         
      }
    })
    .catch((res) => {
       
    })
},

echarts图


    //折线图
    graph() {
      if (this.myChart != null) {
        this.myChart.dispose()
      }
      // 初始化echarts实例
      this.myChart = echarts.init(document.getElementById('echart'))
      // 绘制图表
      let option = {
        tooltip: {
          trigger: 'axis',
        },
        xAxis: {
          type: 'category',
          data: this.xAxisData,
        },
        yAxis: {
          type: 'value',
          axisLine: {
            show: true,
          },
          axisTick: {
            show: true,
          },
        },
        series: [
          {
            type: 'line',
            data: this.xAxis,
          },
        ],
      }
      this.myChart.setOption(option)
    },
    //柱状图
    graphBar() {
      if (this.myChartBar != null) {
        this.myChartBar.dispose()
      }
      // 初始化echarts实例
      this.myChartBar = echarts.init(document.getElementById('echartBar'))
      // 绘制图表
      let option = {
        tooltip: {
          trigger: 'axis',
        },
        title: { text: '' },
        legend: {
          show: false,
        },
        xAxis: {
          type: 'category',
          //   data: ['2020', '2021', '2023'],
          data: this.zAxisData,
        },
        yAxis: {
          type: 'value',
          axisLine: { show: true },
          name: '单位(亿方)',
          nameTextStyle: {
            fontSize: 14,
          },
        },
        series: [
          {
            name: '平均价格',
            type: 'bar',
            barWidth: 50, //柱子的粗细
            // data: [5000, 10000, 15000],
            data: this.zAxis,
            label: {
              show: true,
              position: 'top',
              valueAnimation: true,
            },
          },
        ],
      }
      this.myChartBar.setOption(option)
    },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值