微信小程序中使用ECharts 并且动态设置数据

项目下载地址 GitHub 地址 https://github.com/ecomfe/echarts-for-weixin

将当前文件夹里的内容拷贝到项目中

目录:

json:

复制代码
{
  "usingComponents": {
    "ec-canvas": "../components/ec-canvas/ec-canvas"
  }
}

wxml:

复制代码
<ec-canvas id="eChart" canvas-id="eChart" ec="{{ ec }}"></ec-canvas>

js:

复制代码
import * as echarts from '../components/ec-canvas/echarts.js';
Page({
  data: {
    ec: {
      lazyLoad: true,
    }
  },
  onReady: function () {
    const num = 77
    this.initchart(num)
  },
  initchart(data) {
    let ec_canvas = this.selectComponent('#eChart');
    ec_canvas.init((canvas, width, height, dpr) => {
      const chart = echarts.init(canvas, null, {
        width: width,
        height: height,
        devicePixelRatio: dpr // new
      });
      canvas.setChart(chart);

      const option = {
        title: {
          text: ''
        },
        tooltip: {
          formatter: '{a} <br/>{b} : {c}%'
        },
        series: [
          {
            name: '评估',  // 设置环形进度条的名称
            type: 'gauge',
            radius: '80%',  // 设置环形的半径
            startAngle: -135,  // 设置起始角度为左下角
            endAngle: -45,  // 设置结束角度为右下角
            progress: {
              show: true,  // 显示进度条
              width: 10,  // 设置环形的宽度
              itemStyle: {
                // 使用 LinearGradient 设置渐变色
                color: new echarts.graphic.LinearGradient(0, 0, 1, 1, [
                  { offset: 0, color: '#61E5AB' },  // 渐变起始颜色
                  { offset: 1, color: '#228D7D' }   // 渐变结束颜色
                ])
              }
            },
            detail: {
              valueAnimation: true,  // 动画效果
              formatter: '{value}',  // 只显示值
              fontSize: 30,  // 设置字体大小
              color: '#333',  // 设置字体颜色
              offsetCenter: [0, '40%'],  // 设置文本位置
            },
            data: [
              {
                value: data
              }
            ],
            axisLine: {

            },
            axisTick: {
              show: false
            },
            axisLabel: {
              show: false,
              distance: 50
            },
            splitLine: {
              show: false  // 去掉分割线
            },
          }
        ]
      };

      chart.setOption(option);
      return chart;
    })
  },
}

结果:

相关推荐
chaosama5 小时前
微信小程序带参分享、链接功能
微信小程序·小程序
胡西风_foxww6 小时前
微信小程序动态组件加载的应用场景与实现方式
微信小程序·应用·加载·动态组件
ALLSectorSorft8 小时前
上门服务小程序会员系统框架设计
小程序·apache
甜甜的资料库10 小时前
基于小程序老人监护管理系统源码数据库文档
微信小程序
说私域16 小时前
基于定制开发开源AI智能名片S2B2C商城小程序的首屏组件优化策略研究
人工智能·小程序·开源·零售
Uyker1 天前
微信小程序动态效果实战指南:从悬浮云朵到丝滑列表加载
前端·微信小程序·小程序
happyCoder1 天前
uniapp 微信小程序实现定时消息订阅提醒(前后端)
微信小程序
Uyker2 天前
从零开始制作小程序简单概述
前端·微信小程序·小程序
打小就很皮...2 天前
HBuilder 发行Android(apk包)全流程指南
前端·javascript·微信小程序
说私域2 天前
定制开发开源AI智能名片驱动下的海报工厂S2B2C商城小程序运营策略——基于社群口碑传播与子市场细分的实证研究
人工智能·小程序·开源·零售