美文网首页
微信小程序引入echarts(清晰版)

微信小程序引入echarts(清晰版)

作者: HS_d119 | 来源:发表于2020-12-17 14:57 被阅读0次

1、下载

2、引进到项目中

3、.json配置组件引入

  "usingComponents": {
    "ec-canvas": "/assets/ec-canvas/ec-canvas"
  }

4、.wxml使用组件

<view class="box">
    <ec-canvas id="mychart-dom-bar" canvas-id="mychart-bar" ec="{{ ec }}"></ec-canvas>
</view>

5、.wxss样式(box样式就是限制charts图表的大小)

.box {
  display: block;
  width: 100%;
  height: 468rpx;
}

6、进入主题,.js开始脚本

import * as echarts from '../../../../assets/ec-canvas/echarts'; // TODO: 找对应的位置

let chart = null;

function initChart(canvas, width, height, dpr) {
  chart = echarts.init(canvas, null, {
    width,
    height,
    devicePixelRatio: dpr, // 重要,不加会模糊
  });
  canvas.setChart(chart);

 // TODO: 自己根据官网配置
  const option = {
    color: ['#3398DB'],
    tooltip: {
      trigger: 'axis',
      axisPointer: { // 坐标轴指示器,坐标轴触发有效
        type: 'shadow' // 默认为直线,可选为:'line' | 'shadow'
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: [{
      type: 'category',
      data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
      axisTick: {
        alignWithLabel: true
      }
    }],
    yAxis: [{
      type: 'value'
    }],
    series: [{
      name: '直接访问',
      type: 'bar',
      barWidth: '60%',
      data: [10, 52, 200, 334, 390, 330, 220]
    }]
  };
  chart.setOption(option);
  return chart;
}

Component({
  properties: {},
  data: {
    ec: {
      onInit: initChart // 3、将数据放入到里面
    }
  },
  methods: { }
})

相关文章

网友评论

      本文标题:微信小程序引入echarts(清晰版)

      本文链接:https://www.haomeiwen.com/subject/eudzgktx.html