当前位置: 首页 > news >正文

小程序使用echarts

参考文档:echarts官网、echarts-for-weixin

  • 第一步引入组件库,可直接从echarts-for-weixin下载,也可以从echarts官网自定义生成,这里我们就不贴了
  • 组件库引入好后,就是页面引用啦,废话不多说,直接上代码

index.js:

import * as echarts from '../../components/ec-canvas/echarts'; // 这样引用可避免报init is undefined
let chart = {};
Page({data: {ec: {lazyLoad: true}},onLoad(options) {this.initChart(1)},// 初始化组件initChart(status) {const data = this.selectComponent("#mychart-dom-bar" + status)data.init((canvas, width, height) => {chart[status] = echarts.init(canvas, null, {width: width,height: height,devicePixelRatio: 2});canvas.setChart(chart[status]);this.setOption(status);return chart[status];})},setOption(i) {chart[i].clear(); // 清除chart[i].setOption(this['getOption' + i](), true); //获取新数据},getOption1() {return {tooltip: {trigger: 'axis'},grid: {left: 0,bottom: 0,top: 25,containLabel: true},xAxis: {type: 'category',data: ['洗美', '贴膜', '改装', '改色'],axisTick: {show: false},axisLabel: {color: 'rgba(0,0,0,0.45)',fontWeight: 'bold',lineHeight: 14,padding: [10, 0, 19, 0]},axisLine: {lineStyle: {color: 'rgba(0,0,0,0.15)'}}},yAxis: {type: 'value',axisLabel: {color: 'rgba(0,0,0,0.45)',fontWeight: 'bold',lineHeight: 14,padding: [0, 8, 0, 0]},splitLine: {lineStyle: {color: 'rgba(0,0,0,0.15)'}}},series: [{data: [120, 200, 150, 80],type: 'bar',color: '#0E71B2',label: {show: true,position: 'top',color: '#0E71B2',fontSize: 16}}]}},
})

index.wxml:

<view class="ec-container"><ec-canvas id="mychart-dom-bar1" canvas-id="mychart-bar1" ec="{{ec}}"></ec-canvas>
</view>

index.wxss:

.ec-container {display: flex;flex-direction: column;align-items: center;justify-content: center;width: 100vw;height: 600rpx;padding: 0 20rpx;
}ec-canvas {width: 100%;height: 100%;
}

此方法也可封装为一个页面生成多个图表
欢迎大家留言指正!

http://www.lryc.cn/news/101767.html

相关文章:

  • 面向对象——封装
  • 【LeetCode】160.相交链表
  • 【JWT的使用】
  • Python获取音视频时长
  • TCP四次握手为什么客户端等待的时间是2MSL
  • Android Studio 启用设备远程调试配置完整步聚
  • 玩转LaTeX(三)【数学公式(基础)、​矩阵、多行公式】
  • jenkins 配置git
  • 单机部署MinIo并设置开机自启
  • Latex | 使用MATLAB生成.eps矢量图并导入Latex中的方法
  • 宝塔面板定时任务重启各种服务
  • Ansible playbook编写
  • 个人博客系统 -- 登录页面添加图片验证码
  • 剑指offer10-I.斐波那契数列
  • 13年测试经验,性能测试-压力测试指标分析总结,看这篇就够了...
  • 大数据课程D3——hadoop的Source
  • F5 LTM 知识点和实验 4-持久化
  • SpringBoot之WebMvcConfigurer详解
  • WPF实战学习笔记22-添加自定义询问窗口
  • Spring Boot项目的创建
  • Python加载数据的5种方法
  • QPoint、QLine、QSize、QRect
  • vue+leaflet笔记之地图量测
  • “深入理解SpringBoot:从入门到精通的几个关键要点“
  • 数值线性代数: 共轭梯度法
  • 【JVM】详解对象的创建过程
  • 华纳云:ubuntu下如何搭建nfs服务
  • HCIA实验二
  • stm32 舵机 cubemx
  • 无涯教程-jQuery - Spinner组件函数