wx-charts 微信小程序图表应用

wx-charts 微信小程序图表应用

在这里插入图片描述

  • .json文件引用
{
  "usingComponents": {
  	   "ec-canvas":"../../component/ec-canvas/ec-canvas"
  },
  "navigationBarTitleText": ""
}
  • .wxml定义
<view class="container">
    <ec-canvas id="mychart" canvas-id="mychart-bar" ec="{{ ec }}" ></ec-canvas>
  </view>
  • .js文件
import * as echarts from '../../component/ec-canvas/echarts';

data: {
    //折现属性
    series:[{
        data: ([34, 26, 25, 19, 37, 35, 10]).reverse(),
        name:'温度A',
        smooth:false,
        type:'line'
       }, {
        data: ([15, 12, 7, 23, 3, 14, 22]).reverse(),
        name: '温度B',
        smooth: false,
        type: 'line'
      }],
    // 默认7天
    ascissaData:(['周日','周一','周二','周三','周四','周五','周六']).reverse(),
    ec: {
      lazyLoad: true//延迟加载
    }
  },
/* 初始化图表*/
/* 界面初始化 */
 onLoad: function () {    
    this.echartsComponnet = this.selectComponent('#mychart');
     this.init_echarts() 
  },
//初始化图表
  init_echarts: function () {
    this.echartsComponnet.init((canvas, width, height) => {
      // 初始化图表
      const Chart = echarts.init(canvas, null, {
        width: width,
        height: height
      });
      Chart.setOption(this.getOption());
      // 注意这里一定要返回 chart 实例,否则会影响事件处理等
      return Chart;
    });
  },

  // 获取数据
  getOption: function () {
    var that = this
    console.log(that.data.series)
    console.log(that.data.ascissaData)
    var legendList = []
    for (var i in that.data.series) {
      var obj = {
        name: that.data.series[i].name,
        icon: 'circle',
        textStyle: {
          color: '#000000',
        }
      }
      legendList.push(obj)

      that.data.series[i].data.reverse()
    }
    var option = {
      // 折线图线条的颜色
      color: ["#37A2DA", "#67E0E3", "#9FE6B8"],
      // 折线图的线条代表意义
      legend: {
        itemWidth: 5, //小圆点的宽度
        itemGap: 25,
        selectedModel: 'single', //折线可多选
        inactiveColor: '#87CEEB',
        data: legendList,
        bottom: 0,
        left: 30,
        z: 100
      },
      // 刻度
      grid: {
        containLabel: true
      },
      // 悬浮图标
      tooltip: {
        show: true,
        trigger: 'axis',
        position: function (pos, params, dom, rect, size) {
          var obj = {
            top: 60
          };
          obj[['left', 'right'][+(pos[0] < size.viewSize[0] / 2)]] = 5;
          return obj;
        }
      },
      xAxis: {
        type: 'category',
        boundaryGap: false,
        data: that.data.ascissaData.reverse(),
        // show: false
      },
      yAxis: {
        x: 'center',
        type: 'value',
        splitLine: {
          lineStyle: {
            type: 'dashed'
          }
        },
        axisLine: { //y轴坐标是否显示
          show: false
        },
        axisTick: { //y轴刻度小标是否显示
          show: false
        }
      },
      series: that.data.series
    }
    return option
  },

效果图:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值