微信小程序图表的引入并解决van-tab切换时不显示图表的问题

1、在GitHub上下载ec-canvas,地址如下:

GitHub - ecomfe/echarts-for-weixin: Apache ECharts 的微信小程序版本

2、找到文件夹下面的ec-canvas文件夹,把它放在与pages同级的目录。

3、在需要用到页面里面引入,如index.json

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

4、在index.js里面echarts.js

import * as echarts from '../../ec-canvas/echarts.js'

5、在index.wxml里面添加元素

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

6、在index.wxss里面定义容器的大小

.box {
    width:100%;
    height:600rpx;
    margin-top: 30;
    font-size: 42rpx;
    font-weight: bold;
} 

7、在index.js里面添加代码

// 1、引入依赖脚本
import * as echarts from '../../../ec-canvas/echarts.js'

///报表数据
var chartData = {
    dataList:[
        {value: 1548,name: 'CityE'},
        { value: 735, name: 'CityC' },
        { value: 510, name: 'CityD' },
        { value: 434, name: 'CityB' },
        { value: 335, name: 'CityA' }
    ],
    dataLegend:['CityA', 'CityB', 'CityD', 'CityC', 'CityE']
  };
function setOption(dataList,dataLegend){
    var option = {
        title: {
          text: '本月未巡检区域占比',
          top:10,
          left: 'center',
          textStyle: {
            fontSize: 14,
            fontWeight: 'normal',
          },
        },
        tooltip: {
          trigger: 'item',
        },
        legend: {
          bottom: 10,
          left: 'center',
          data:dataLegend,
         itemWidth:8,//图例标记的图形宽度
         itemHeight:8,
         icon:"circle",
        },
        series: [
          {
            type: 'pie',
            radius: '45%',
            center: ['50%', '50%'],
            selectedMode: 'single',
            data:dataList,
            label: {
                formatter: '{b}: ({d}%)'
              },
          }
        ]
      };
      return option
}
// 2、进行初始化数据
function initChart(canvas, width, height, dpr) {
    var echart = echarts.init(canvas, null, {
      width: width,
      height: height,
      devicePixelRatio: dpr // new
    });
    canvas.setChart(echart);
    let option = setOption(chartData.dataList,chartData.dataLegend)
    echart.setOption(option)
    chart = echart
    return echart
  };
  const app = getApp()
  ///定义图表
  var chart = null;

Page({

  data: {
    ec1: {
      onInit: initChart // 3、将数据放入到里面
    },
  },

})

8、运行时可能会提示echarts文件比较大,如果不想要这个提示,可以去官网定制echarts类型,地址如下:ECharts 在线构建 https://echarts.apache.org/zh/builder.html   

下载后,是min.js文件,可以重命名为echarts.js并替换掉项目里面的js文件

9、当放置在van-tabs的第二个tab选项时,需要在index.wxml里面加上wx:if条件

<van-tabs sticky offset-top="{{naviBarHeight}}" color="#1989f7"  active="{{activeName}}" bind:change="onChange">
  <van-tab title="选项卡1"></van-tab>
 <van-tab title="选项卡2">
      <view class="box" wx:if="{{activeName=='1'}}">
        <ec-canvas
          id="mychart-dom-bar"
          canvas-id="mychart-bar"
          ec="{{ ec1 }}"
        ></ec-canvas>
  </view>
 </van-tab>
</van-tabs>

10、js代码

data:{
   activeName: 0, 
},
onChange(event){
    this.setData({
      activeName: event.detail.name,
    });

  },

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

~犇犇~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值