微信小程序中使用echarts图表

微信小程序中使用echarts

  1. 首先在componens文件夹中引入组件ec-canvas
    ec-canvas组件下载
  2. 在wxml中引入:
<ec-canvas force-use-old-canvas="true" id="one-line" canvas-id="mychart-bar" ec="{{ ec }}"></ec-canvas>
  1. 在json中引入组件
"usingComponents": {
    "ec-canvas":"../../../../components/ec-canvas/ec-canvas"
  },
  1. 在js中设置
 data: {
    ec: {},
    lineOptions: {
      xAxis: {
        type: 'category',
        boundaryGap: false,
        data: [],
      },
      yAxis: {
        type: 'value',
      },
      label: {
        show: true,
      },
      series: [{
        data: [],
        type: 'line',
        itemStyle: {
          color: '#1AAD19'
        },
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
              offset: 0,
              color: 'rgba(26,173,25,0.8)'
            },
            {
              offset: 1,
              color: 'rgba(26,173,25,0.3)'
            }
          ])
        }
      }]
    }
  }onLoad:function (options) {
  	// 渲染图表
  	this.initChart('#one-line');
  }
  // 初始化echarts
  initChart(idName) {
    const ecComponent = this.selectComponent(idName);
    ecComponent.init((canvas, width, height, dpr) => {
      // 2.这个不用管,固定写法
      const chart = echarts.init(canvas, null, {
        width,
        height: height,
        devicePixelRatio: dpr // new
      });
      canvas.setChart(chart);
      // 3.注册地图
      // echarts.registerMap('china', geoJson)
      // 4.设置option
      this.setTimeLineOption(chart, idName);
      // 5.最后一定要返回chart
      return chart;
    })
  },
  // 设置options
  setTimeLineOption(chart, idName) {
  	 let _currOptions = this.data.lineOptions;
  	// 对xAxis.data设置数据
  	// 对series.data设置数据
 	 chart.setOption(_currOptions);
  }

注意:如果微信开发者工具中报vendor.js超过500k,可以查看gulp压缩文件

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值