微信小程序使用echarts(ec-canvas)获取接口数据后,手机端页面视图未更新bug

我这里一个页面用了两个echarts,第一个是折线图与柱状图结合,第二个是横向柱状图;

我在js代码中的重要部分加了注释;wxml代码很简单,放在最后;

javascript 复制代码
import * as echarts from '../../components/ec-canvas/echarts';
import { getINCOMETJList } from "../../api/statistics/index";

// 接口拿到数据,页面没更新,需要把chart定义和option定义放到page的外边,代码的前边;
let chart = null 
let chart2 = null 
const option = {
  grid: {
    left: '3%',
    right: '4%',
    bottom: '3%',
    containLabel: true
  },
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      type: 'cross',
      crossStyle: {
        color: '#999'
      }
    }
  },
  toolbox: {
    show: false,
    feature: {
      dataView: { show: true, readOnly: false },
      magicType: { show: true, type: ['line', 'bar'] },
      restore: { show: true },
      saveAsImage: { show: true }
    }
  },
  legend: {
    data: ['就诊人次', '就诊金额']
  },
  xAxis: [
    {
      type: 'category',
      data: [], // 在需要接口数据的值这里初始给一个空数组,然后获取接口数据后,重新赋值;
      axisPointer: {
        type: 'shadow'
      }
    }
  ],
  yAxis: [
    {
      type: 'value',
      name: '(人)',
      axisLabel: {
        formatter: '{value} 人',
      }
    },
    {
      type: 'value',
      name: '(元)',
      axisLabel: {
        formatter: '{value} 元'
      }
    }
  ],
  series: [
    {
      name: '就诊人次',
      type: 'bar',
      tooltip: {
        valueFormatter: function (value) {
          return value + ' 人';
        }
      },
      data: [] // 在需要接口数据的值这里初始给一个空数组,然后获取接口数据后,重新赋值;
    },
    {
      name: '就诊金额',
      type: 'line',
      yAxisIndex: 1,
      smooth: true,
      tooltip: {
        valueFormatter: function (value) {
          return value + ' 元';
        }
      },
      data: [] // 在需要接口数据的值这里初始给一个空数组,然后获取接口数据后,重新赋值;
    }
  ]
};

const option2 = {
  title: {
    text: ''
  },
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      type: 'shadow'
    }
  },
  legend: {},
  grid: {
    left: '3%',
    right: '4%',
    bottom: '3%',
    containLabel: true
  },
  xAxis: {
    type: 'value',
    boundaryGap: [0, 0.01]
  },
  yAxis: {
    type: 'category',
    data: [1, 2, 3, 4, 5]
  },
  series: [
    {
      name: '药品',
      type: 'bar',
      data: [] // 在需要接口数据的值这里初始给一个空数组,然后获取接口数据后,重新赋值;
    }
  ]
};

// echart需要初始化,不用赋值;
function initChart(canvas, width, height, dpr) {
  chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr
  });
  return chart;
}

function initChart2(canvas, width, height, dpr) {
  chart2 = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr
  });
  return chart2;
}

Page({
  data: {
    ec: {
      onInit: initChart
    },
    ec2: {
      onInit: initChart2
    }
  },
  onLoad: function () {
    this.updateData();
  },
  updateData: function() {
   	// 使用接口拿到echarts所需数据
    getINCOMETJList({ ParameterData: ParameterData }).then((res) => {
      console.log(res)
      if(res.Code == 200) {
        const data = res.Data.ReplyData;
        this.setData({,
          Month: data.OPMONTHTJDATA.map(item => item.Month),
          MONTHVISITNUM: data.OPMONTHTJDATA.map(item => item.MONTHVISITNUM),
          MONTHAMT: data.OPMONTHTJDATA.map(item => item.MONTHAMT),
          CHARGEAMT: data.OPMEDICINETOPDATA.map(item => item.CHARGEAMT),
        })
		// 重新赋值,option中初始定义了空数组,在这里给数组赋值;
        option.xAxis.data = this.data.Month
        option.series[0].data = this.data.MONTHVISITNUM
        option.series[1].data = this.data.MONTHAMT
        option2.series[0].data = this.data.CHARGEAMT
		// 给chart图表添加接口返回的数据
        setTimeout(() => {
          chart.clear()
          chart2.clear()
          chart.setOption(option);
          chart2.setOption(option2);
        }, 500);
      }
    })
  }
});
xml 复制代码
<view class="card">
   <view class="card-content">
     <ec-canvas ec="{{ ec }}" force-use-old-canvas="true"></ec-canvas>
   </view>
 </view>

 <view class="card">
   <view class="card-content">
     <ec-canvas ec="{{ ec2 }}" force-use-old-canvas="true"></ec-canvas>
   </view>
 </view>
相关推荐
小羊Yveesss4 小时前
2026年微信小程序搭建一般需要多长时间?不同方案周期和延期点
微信小程序·小程序
蒙塔基的钢蛋儿4 小时前
记一次 STM32H723 硬件 Bug 排查:写个 ETH 寄存器,SDRAM 数据飞了
stm32·嵌入式硬件·bug
小羊Yveesss7 小时前
2026年微信小程序搭建算什么费用?年费、设计费、开发费和维护费
微信小程序·小程序
小羊Yveesss1 天前
2026年微信小程序后端怎么搭建?后台、数据、接口和运维怎么选
运维·微信小程序·小程序
小皮虾1 天前
小程序首页性能优化实战:从 4 秒到 1.8 秒
前端·微信小程序
paopaokaka_luck2 天前
英语单词学习系统的设计与实现(基于艾宾浩斯遗忘曲线的智能复习机制、语音朗读、多题型测试与错题自动收录、Echarts图形化分析)
vue.js·spring boot·后端·echarts
wok1573 天前
Git Bash 执行中文命令报错 127:MSYS 参数编码 bug 排查与修复
git·bug·bash
衍生星球3 天前
SpringBoot3 + Vue3 + 微信小程序如何学习,以及学哪些技术,组件
sql·微信小程序·uni-app·vue·springboot
李永奉3 天前
杰理可视化SDK开发-【BUG】AC7006F8更换AC7006F6芯片添加双备份OTA升级功能报代码端空间不足问题
bug
结实的洋葱3 天前
为啥程序会有bug?
bug